Repository navigation
fix: panics, dropped values and unbounded cost in form bodies - #204
Merged
Merged
Conversation
BindForm and MarshalForm decided what a value is from its reflect.Kind alone, cut form keys at a bracket without checking that it was there, and looked through every key of the form for each nested property. Between them that dropped some properties without an error, panicked on others, and let a small form cost far more to bind than to send. Single values. A time, a date, or a type which parses or marshals itself as text is now read and written as one value, whatever it is made of. types.Date and time.Time were walked as objects with no fields, so they were neither sent nor received. BindForm reads such a type from its plain key, and still accepts the bracketed keys it was written with before. Nullable properties and maps. MarshalForm writes a nullable property as the value it holds and a map as name[key]=value, which is what BindForm reads. Both were written in Go's map syntax. A null property is not written, as a form can't express one. Panics which are gone: - A key with an unclosed bracket, such as topic[, for an array or an object with additional properties. It is ignored. - An `any` property with a value. It is bound as BindStringToObject binds an `any`, which today is an error. - A file sent under an additional property of an object. - MarshalForm on an object with additional properties, and on an `any` property which holds nothing. - A byte slice nested in a styled parameter, or styled in a form body. Every byte slice was taken for a UUID, which panicked unless it held 16 bytes and wrote those as a UUID when it did. Cost. BindForm sized an array by the highest index a form named, so a single key such as topic[4000000]=a allocated four million elements. Indexes now have to run from zero without a gap, and a form which skips one is an error, so an indexed array is never longer than the number of keys sent for it. Only an index as strconv.Itoa writes it names an element. A nested array, map or set of additional properties was also found by looking through every key of the form, which made binding quadratic in its size: 32 seconds for a 1.4 MB form of objects which each hold an array. The keys are now sorted once and the ones under a name found by binary search, and the same form binds in 41 ms. Compatibility. MarshalForm used to write a type which implements encoding.TextMarshaler as the array or object it is made of, so a net.IP was ip[0]=192&ip[1]=0 and so on, where it is now ip=192.0.2.1. BindForm reads both, but a server on an older version of this package reads only the first. MarshalFormIgnoresTextMarshaler, off by default, restores that layout. The matrix tests gain a row for a property with no encoding, and the scalar one now writes each value before reading it back. Fixes #202 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This was referenced Dec 5, 2026
mromaszewicz
added a commit
that referenced
this pull request
Dec 5, 2026
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.
Fixes #202, plus the related problems found while fixing it. All of it is in
BindForm,MarshalFormand one helper they share with the parameter code.What was wrong
The form code decided what a value is from its
reflect.Kindalone, cut form keys at a]without checking that one was there, and looked through every key of the form for each nested property. That dropped some properties without an error, panicked on others, and let a small form cost far more to bind than to send.Changes
Single values
A time, a date, or a type which parses or marshals itself as text is read and written as one value, whatever Go type it is made of.
types.Dateandtime.Timewere walked as objects with no fields, so aformat: dateorformat: date-timeproperty of a form body was neither sent nor received.BindFormreads such a type from its plain key, and still accepts the bracketed keys it was written with before.Nullable properties and maps
MarshalFormwrites anullable.Nullableas the value it holds, and a map asname[key]=value, which is whatBindFormalready reads. Both used to come out in Go's map syntax (name=map[true:x]). A null property is not written, since a form can't express one.Panics which are gone
BindFormwith a key liketopic[(array, or object with additional properties)BindFormwith a value for ananypropertyBindStringToObjectdoes forany, which today is an errorBindFormwith a file under an additional propertyMarshalFormon an object with additional propertiesname[key]=valueMarshalFormon ananyproperty holding nothingThe last one is in
marshalKnownTypes, so it also affectedStyleParamfor an object or array parameter with a[]bytein it.Cost of binding
Two ways a small form could be expensive:
BindFormsized an array by the highest index a form named, sotopic[4000000]=aallocated four million elements (369 ms and 213 MB for a 20-byte body, growing with the index). Indexes now have to run from zero without a gap. A form which skips one is an error, so an indexed array is never longer than the number of keys sent for it.Binding an array of objects which each hold an array:
Behavior changes
topic[0]=a&topic[2]=cused to bind three elements with an empty one in the middle.topic[01]andtopic[-1]are ignored.topic[01]used to be counted but never read.net.IPwasip[0]=192&ip[1]=0&ip[2]=2&ip[3]=1and is nowip=192.0.2.1.BindFormreads both, but a server on an older runtime reads only the first.runtime.MarshalFormIgnoresTextMarshaler, off by default, restores the old layout for clients which need it. Times, dates and UUIDs are single values either way.BindStringToObject. That was an error for most such types before (anet.IPvalue was parsed as a byte), so in practice this turns errors into values.Not changed
[]bytein a form body is still an array of integers, as it is without an encoding. The form code has noformatto say it should be base64.anyproperty with a value is an error rather than a panic. It will bind as a string ifBindStringToObjectlearns to do that forany.Tests
MarshalFormbefore reading it back, and adds date-time and self-serializing struct and slice columns, required and nullable.mainfor its own reason.TestBindFormArrayBoundscovers gaps and non-canonical indexes, andTestBindFormScalesWithRequestbinds 200,000 keys of nested arrays, maps and additional properties with a deadline which the old scan misses by a wide margin.TestMarshalFormIgnoresTextMarshalercovers both layouts and thatBindFormreads either.Tested on Go 1.26 and 1.27, with
go vetand lint clean. oapi-codegen'sinternal/testmodule (273 packages) passes against this runtime.🤖 Generated with Claude Code