Skip to content

feat: add BindFormAllowsArrayGaps to accept sparse form arrays - #205

Merged
mromaszewicz merged 1 commit into
mainfrom
feat/bindform-array-gaps
Dec 5, 2026
Merged

mromaszewicz merged 1 commit into
mainfrom
feat/bindform-array-gaps

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Follow-up to #204, for v1.10.0.

Problem

Since #204, BindForm refuses an indexed array whose indexes skip some:

topic[0]=a&topic[2]=c
form array 'topic' has no element at index 1: indexes must run from 0 without gaps

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.

func init() {
    runtime.BindFormAllowsArrayGaps = true
}

Setting it gives up the bound #204 added: a request decides how much memory the server allocates. The 20-byte body topic[4000000]=a allocates 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 reach BindForm.

Only an index as strconv.Itoa writes 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

TestBindFormArrayBounds gains a gaps allowed case 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

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>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner December 5, 2026 16:42
@mromaszewicz mromaszewicz added the enhancement New feature or request label Dec 5, 2026
@mromaszewicz
mromaszewicz merged commit 6c8b8c4 into main Dec 5, 2026
21 checks passed
@mromaszewicz
mromaszewicz deleted the feat/bindform-array-gaps branch December 5, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant