Skip to content

fix: panics, dropped values and unbounded cost in form bodies - #204

Merged
mromaszewicz merged 1 commit into
mainfrom
fix/issue-202
Dec 5, 2026
Merged

mromaszewicz merged 1 commit into
mainfrom
fix/issue-202

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Fixes #202, plus the related problems found while fixing it. All of it is in BindForm, MarshalForm and one helper they share with the parameter code.

What was wrong

The form code decided what a value is from its reflect.Kind alone, 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.Date and time.Time were walked as objects with no fields, so a format: date or format: date-time property of a form body was 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.Nullable as the value it holds, and a map as name[key]=value, which is what BindForm already 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

Input Before Now
BindForm with a key like topic[ (array, or object with additional properties) slice bounds panic key ignored
BindForm with a value for an any property reflect panic whatever BindStringToObject does for any, which today is an error
BindForm with a file under an additional property reflect panic file bound
MarshalForm on an object with additional properties reflect panic name[key]=value
MarshalForm on an any property holding nothing reflect panic not written
A byte slice styled in a form body, or nested in a styled parameter panic, or a UUID if it held 16 bytes array of integers in a form body, an error in a styled parameter

The last one is in marshalKnownTypes, so it also affected StyleParam for an object or array parameter with a []byte in it.

Cost of binding

Two ways a small form could be expensive:

  • Index-driven allocation. BindForm sized an array by the highest index a form named, so topic[4000000]=a allocated 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.
  • Quadratic key scan. Each nested array, map or set of additional properties was found by looking through every key of the form, so each element of an array of objects paid for the whole request. The keys are now sorted once per call and the ones under a name found by binary search.

Binding an array of objects which each hold an array:

Elements Body Before After
5,000 169 KB 0.5 s 10 ms
10,000 340 KB 1.9 s 16 ms
20,000 692 KB 7.8 s 24 ms
40,000 1.4 MB 31.8 s 41 ms

Behavior changes

  • Gaps in an indexed array are an error. topic[0]=a&topic[2]=c used to bind three elements with an empty one in the middle.
  • Only a canonical index names an element. topic[01] and topic[-1] are ignored. topic[01] used to be counted but never read.
  • Self-marshaling types are written as text. A net.IP was ip[0]=192&ip[1]=0&ip[2]=2&ip[3]=1 and is now ip=192.0.2.1. BindForm reads 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.
  • A self-parsing type with a plain key binds through BindStringToObject. That was an error for most such types before (a net.IP value was parsed as a byte), so in practice this turns errors into values.
  • Errors are deterministic. Keys under a name are visited in sorted order instead of map order, so a form with several bad values reports the same one each time.

Not changed

Tests

  • The encoding matrices from fix: bind styled form arrays and JSON fields #201 gain a row for a property with no encoding. The scalar matrix now writes each value with MarshalForm before reading it back, and adds date-time and self-serializing struct and slice columns, required and nullable.
  • Each panic and each dropped value has a test which fails on main for its own reason.
  • TestBindFormArrayBounds covers gaps and non-canonical indexes, and TestBindFormScalesWithRequest binds 200,000 keys of nested arrays, maps and additional properties with a deadline which the old scan misses by a wide margin.
  • TestMarshalFormIgnoresTextMarshaler covers both layouts and that BindForm reads either.

Tested on Go 1.26 and 1.27, with go vet and lint clean. oapi-codegen's internal/test module (273 packages) passes against this runtime.

🤖 Generated with Claude Code

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>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner December 5, 2026 16:19
@mromaszewicz mromaszewicz added bug Something isn't working ☢️ breaking change labels Dec 5, 2026
@mromaszewicz
mromaszewicz merged commit c9eee5d into main Dec 5, 2026
21 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-202 branch December 5, 2026 16:23
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ breaking change bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BindForm and MarshalForm: panics and silently dropped values in form bodies

1 participant