Skip to content

fix(urlencoded): keep incomplete percent-encoded bytes at field boundaries - #237

Merged
mcollina merged 1 commit into
fastify:mainfrom
RaphaelFakhri:fix/urlencoded-trailing-percent
Nov 4, 2026
Merged

mcollina merged 1 commit into
fastify:mainfrom
RaphaelFakhri:fix/urlencoded-trailing-percent

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

The urlencoded parser drops a % or %X escape that is cut off at the end of a name or value. For example, a=100% parses as a: "100" and a%=1 parses as a: "1". Malformed escapes in the middle of a value are already kept literally (Hello%world), and URLSearchParams keeps both kinds.

Decoder holds an incomplete escape in its buffer until the next chunk arrives. UrlEncoded resets the decoder at = and & and never reads the buffer at the end of the body, so the pending bytes are lost.

This change:

  • Adds Decoder.prototype.end(), which returns any pending escape literally and clears the buffer.
  • Calls it when a name or value ends (=, & and the end of the body).
  • Cuts the result back to fieldNameSize or fieldSize and sets the matching truncated flag when the literal bytes go past the limit. Without this, a name or value could end up one or two characters over the limit.

Fixes #236

Checklist

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina merged commit adbd231 into fastify:main Nov 4, 2026
48 of 51 checks passed
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.

urlencoded: a trailing % or %X is dropped from field names and values

3 participants