Skip to content

fix: percent-encode % in default query-string mode - #173

Merged
samscott89 merged 1 commit into
mainfrom
fix/percent-encode-percent-sign
Aug 7, 2026
Merged

samscott89 merged 1 commit into
mainfrom
fix/percent-encode-percent-sign

Conversation

@samscott89

Copy link
Copy Markdown
Owner

Fixes #170. Thanks @hey-jj for the very precise report — the root cause was exactly where you pointed.

The bug

MINIMAL_QS_SET in src/ser/encode.rs never called .add(b'%'), so a literal % in a string value or map key was emitted raw in the default (non-form) mode. Anything that then looked like a percent escape got decoded on the way back in:

input round-tripped to
"%41" "A"
"100%25" "100%"
"a%3Db" "a=b"
"%AD0" Err(Utf8(..)) — on the crate's own output

It also broke the bracket escaping documented at src/config.rs:120: the distinct map keys "[x]" and "%5Bx%5D" both serialized to a[%5Bx%5D]=1, so a[%5Bx%5D]=1 was ambiguous.

The fix

One line — add % to the set. That makes %5B/%5D an unambiguous escape and restores the round-trip contract:

name=%2541          // "%41"
a[%5Bx%5D]=1        // { a: { "[x]": 1 } }
a[%255Bx%255D]=1    // { a: { "%5Bx%5D": 1 } }

Form encoding was already correct — FORM_URLENCODED_SET is built on NON_ALPHANUMERIC, which includes %.

Tests

  • New percent_signs_roundtrip regression test covering values, map keys, the non-UTF-8 cases, and the key collision.
  • edge_case_primitives gains a percent_escapes field. As the issue notes, the existing literal "!@#$%^&*()..." missed this because % is followed by ^, a non-hex byte — the new field uses % followed by hex digits. Confirmed it fails on the round-trip assertion without the one-line fix.
  • Two snapshots updated (% → %25); full CI feature matrix, doctests, clippy and fmt all pass locally.

Compatibility note

This changes serializer output for any value containing %. Anything that was previously round-tripping through serde_qs is unaffected (it was already corrupted); but a consumer that deliberately fed pre-encoded strings through the serializer and relied on the pass-through will now see them double-encoded. That pass-through was never a documented guarantee and was indistinguishable from the corruption above, so I think the patch bump is right — flagging it in case you disagree.

🤖 Generated with Claude Code

`MINIMAL_QS_SET` never added `b'%'`, so a literal `%` in a string value
or map key was emitted raw. Anything that looked like a percent escape
was then decoded on the way back in:

- `"%41"` round-tripped to `"A"`, `"100%25"` to `"100%"`
- `"%AD0"` made `from_str` fail with `Err(Utf8(..))` on the crate's own
  output
- the distinct map keys `"[x]"` and `"%5Bx%5D"` both serialized to
  `a[%5Bx%5D]=1`

Adding `%` to the set makes the bracket escaping unambiguous and
restores the round-trip contract. Form encoding was already correct --
`FORM_URLENCODED_SET` is built on `NON_ALPHANUMERIC`.

The `edge_case_primitives` round-trip test missed this because its
literal is `"!@#$%^&*()..."`, where `%` is followed by `^`, a non-hex
byte; it now also covers `%` followed by hex digits.

Fixes #170

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@samscott89
samscott89 merged commit 63010ed into main Aug 7, 2026
20 checks passed
@samscott89 samscott89 mentioned this pull request Aug 7, 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.

Default serializer never percent-encodes %, corrupting round-trips

1 participant