Skip to content

Emit field_end for trailing bare field names on finalize#230

Merged
Kludex merged 2 commits intoKludex:masterfrom
bysiber:fix/querystring-finalize-field-name
Apr 10, 2026
Merged

Emit field_end for trailing bare field names on finalize#230
Kludex merged 2 commits intoKludex:masterfrom
bysiber:fix/querystring-finalize-field-name

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

Summary

QuerystringParser.finalize() doesn't emit field_end when the parser ends in FIELD_NAME state, silently dropping trailing bare field names.

Problem

When a querystring ends with a bare field name (no = sign), e.g. a=1&foo, the parser emits field_start and field_name for "foo" but finishes in FIELD_NAME state. The finalize() method only checks for FIELD_DATA:

def finalize(self) -> None:
    if self.state == QuerystringState.FIELD_DATA:
        self.callback("field_end")
    self.callback("end")

Since the state is FIELD_NAME, not FIELD_DATA, the field_end callback is never emitted. This causes unbalanced callbacks — the FormParser's on_field_end closure (which creates and delivers the field to the user) is never called, so the field is silently dropped.

Note: bare field names within a chunk (e.g. foo&a=1) are handled correctly — only trailing bare names at end-of-input are affected.

Fix

Check for both FIELD_DATA and FIELD_NAME states:

if self.state in (QuerystringState.FIELD_DATA, QuerystringState.FIELD_NAME):
    self.callback("field_end")

bysiber and others added 2 commits February 20, 2026 17:17
…re field names

When a querystring ends with a bare field name (no = sign), e.g.
"a=1&foo", the parser emits field_start and field_name for "foo"
but ends in FIELD_NAME state. finalize() only checks for
FIELD_DATA state, so field_end is never emitted and the field
is silently dropped.

Fix by also checking for FIELD_NAME state in finalize().
@Kludex Kludex changed the title Fix finalize not emitting field_end for trailing bare field names Emit field_end for trailing bare field names on finalize Apr 10, 2026
@Kludex
Copy link
Copy Markdown
Owner

Kludex commented Apr 10, 2026

This is also the behavior Django and Flask have.

@Kludex Kludex merged commit 81dc112 into Kludex:master Apr 10, 2026
6 checks passed
@Kludex Kludex mentioned this pull request Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants