Skip to content

fix: match percent-encoded brackets case-insensitively in form mode - #178

Open
youdie006 wants to merge 1 commit into
samscott89:mainfrom
youdie006:fix/176-lowercase-bracket-escapes
Open

youdie006 wants to merge 1 commit into
samscott89:mainfrom
youdie006:fix/176-lowercase-bracket-escapes

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #176.

The bug

In form-encoding mode the parser eagerly rewrites percent-encoded brackets so that nested keys can still form structure, but it compares the two bytes after % against literal uppercase byte strings:

match &self.iter.as_slice()[..2] {
    b"5B" => { /* ... */ next = Some(b'['); }
    b"5D" => { /* ... */ next = Some(b']'); }
    _ => { /* unknown percent encoding, leave it as is */ }
}

Lowercase %5b/%5d falls into the catch-all arm, so the key never becomes structure:

abc%5Bdef%5D=ghi  ->  { abc: { def: ghi } }
abc%5bdef%5d=ghi  ->  { "abc[def]": ghi }     <- wrong shape, and no error

The silent part is what makes it awkward to debug: deserialization does not fail, it just produces a flat key, so the caller sees a missing-field error somewhere further away from the cause.

Oracle

Two things agree against the current behaviour.

RFC 3986 section 6.2.2.1 states that the hexadecimal digits within a percent-encoding triplet are case-insensitive. And this crate already implements that rule one module over: char_to_hexdigit in src/de/parse/decode.rs accepts b'a'..=b'f' alongside b'A'..=b'F'. So the decoder treats hex case-insensitively and the eager bracket rewrite does not — the two paths in the same crate disagree about the same input.

The fix

Match on the byte pattern instead of a fixed byte string, accepting either case for the second digit. The 5 is a digit and has no case.

Tests

Added to tests/test_regression.rs, following the convention of the existing entries there. It covers uppercase, lowercase, and mixed case in a single key, and then pins the boundary that matters for a change like this: a doubly-encoded bracket must not be rewritten into structure. abc%255Bdef%255D=ghi still yields the flat key abc%5Bdef%5D, in either case, because the rewrite inspects the two bytes after % (25) and correctly declines. Without that assertion, a sloppier fix that matched too eagerly would pass.

Verified red then green rather than only green — with the source change reverted, the new test fails as:

"abc%5bdef%5d=ghi" failed to deserialize: missing field `abc`

and passes with it applied. Full suite with the CI feature set: cargo test --all-targets --features "actix4,warp,axum" all 13 test targets ok, 0 failed. cargo fmt --all --check clean and cargo clippy --all-targets -F "actix4,warp,axum" -- -D warnings clean.

This is independent of #177 (issue #175) — different file, different cause — so the two can be merged in either order.

Credit to @hey-jj for the report.

Disclosure: I use AI assistance in my work, and I review and verify everything before it goes out.

Form-encoding mode eagerly rewrites percent-encoded brackets so that nested
keys can still form structure, but it compared the two bytes after `%` against
the literal uppercase `5B`/`5D`. Lowercase `%5b`/`%5d` fell through to the
catch-all arm, so `abc%5bdef%5d=ghi` stayed a flat `abc[def]` key instead of
nesting, and no error was raised.

The hex digits of a percent-encoding are case-insensitive (RFC 3986 section
6.2.2.1), and this crate's own `char_to_hexdigit` already accepts both cases,
so the two paths disagreed.

Fixes samscott89#176
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.

Form-encoding mode decodes only uppercase %5B/%5D, not lowercase

1 participant