Repository navigation
feat: add BindFormAllowsArrayGaps to accept sparse form arrays - #205
Merged
Merged
Conversation
Since #204 BindForm refuses an indexed array whose indexes skip some, such as topic[0]=a&topic[2]=c, because an array is as long as its highest index says and a form which may skip indexes can ask for one of any length with a single key. A client which sends such forms worked before that change. The package variable BindFormAllowsArrayGaps, off by default, restores the old reading: the array is as long as its highest index says, and the elements the form does not name are left at their zero value. Setting it gives the bound up again, so a request decides how much memory the server allocates. The documentation says so, and that it belongs only where every client is trusted or such forms are refused before they reach BindForm. Only an index as strconv.Itoa writes it names an element, with or without the setting. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #204, for v1.10.0.
Problem
Since #204,
BindFormrefuses an indexed array whose indexes skip some:That is the right default. An array is as long as its highest index says, so a form which may skip indexes can ask for an array of any length with a single key. But a form like this bound before v1.10.0, as
["a", "", "c"], and a server whose clients send such forms has no way to keep accepting them.Change
runtime.BindFormAllowsArrayGaps, a package variable which is off by default, restores the old reading. With it set, an array is as long as its highest index says, and the elements a form does not name are left at their zero value.Setting it gives up the bound #204 added: a request decides how much memory the server allocates. The 20-byte body
topic[4000000]=aallocates four million elements, and a larger index allocates more, until the process runs out of memory. The doc comment and the README both say so, and that the setting belongs only where every client is trusted, or where such forms are refused before they reachBindForm.Only an index as
strconv.Itoawrites it names an element, with or without the setting.topic[01]used to add an empty element before v1.10.0; that is not restored.Compatibility
Nothing changes unless the variable is set.
Testing
TestBindFormArrayBoundsgains agaps allowedcase which covers a plain array, an array of objects, a nullable array, a file array and a property with an encoding, and checks that a form without a gap binds as it does by default. The case for keys which are not indexes now runs with and without the setting.Tested on Go 1.26 and 1.27, with lint clean.
🤖 Generated with Claude Code