lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

Std.Http: query values containing `=` are rejected (`?a=b=c`, base64 padding)

Open hiroki-abe-58 opened this issue 1 month ago • 2 comments

Prerequisites

  • [x] Check that your issue is not already filed. (Related, but different: #15246, #14934.)
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case.
  • [ ] Test your test case against the latest nightly release. Not run on nightly: tested on v4.35.0, and parseQuery and isQueryDataChar are unchanged on master at 627881d.

Description

Std.Http.RequestTarget.parse? and Std.Http.URI.parse? fail on any query whose value contains =. Examples:

  • ?a=b=c;
  • base64 padding, as in ?token=YWJjZA==;
  • a redirect target with its own query, as in ?next=/x?y=1.

RFC 3986 allows = anywhere in a query: it is a sub-delim inside pchar. Form-urlencoded parsing splits each pair at the first =.

parseQuery in src/Std/Http/Data/URI/Parser.lean looks like it means to allow this:

  1. It splits each pair on =.
  2. It joins the rest back together with String.intercalate "=" value.
  3. It checks the joined value with URI.EncodedQueryParam.fromString?.

EncodedQueryParam is EncodedQueryString isQueryDataChar, and isQueryDataChar in src/Std/Http/Internal/Char.lean excludes =. So the check rejects the value, the fold returns none, and the whole target fails as "invalid query string".

Percent-encoding the = (?a=b%3Dc) works. But clients, browsers and proxies are not required to encode it, so these requests fail to parse.

Steps to Reproduce

import Std.Http
open Std.Http

#eval (RequestTarget.parse? "/p?a=b").isSome            -- true
#eval (RequestTarget.parse? "/p?a=b%3Dc").isSome        -- true
#eval (RequestTarget.parse? "/p?a=b=c").isSome          -- false
#eval (RequestTarget.parse? "/p?token=YWJjZA==").isSome -- false
#eval (RequestTarget.parse? "/p?next=/x?y=1").isSome    -- false
#eval (URI.parse? "http://example.com/p?a=b=c").isSome  -- false

Expected behavior: all six print true, and the value of a in ?a=b=c is b=c.

Actual behavior: the last four print false.

Versions

"4.35.0", on Ubuntu 24.04.5 LTS (WSL2).

Additional Information

A fix needs a value check that accepts =. A key still cannot contain an unencoded =. One way is a separate predicate for values; another is to percent-encode = in the value before storing it. I don't know which fits the design of URI.Query best, so I'm opening an issue rather than a PR.

This was found and reduced with AI assistance (Claude Code). The reproduction above was run exactly as shown.

hiroki-abe-58 avatar Oct 27 '26 10:10 hiroki-abe-58

Some additions, from a check of Std.Http on 4.37.0-nightly-2026-10-20 (src/Std/Http/Data/URI/Parser.lean is unchanged on master since 2026-07-23):

  • The server rejects these requests: fed to Std.Http.Protocol.H1.Machine ({ config := {} }, then feed and step), GET /a?token=YWJj== HTTP/1.1 and GET /login?next=/home?tab=1 HTTP/1.1 (with Host: a) get 400 Bad Request, while GET /a?b=c HTTP/1.1 is read.
  • tests/elab/async_http_uri.lean currently expects the rejection, so a fix will need to change that expectation:
    -- A pair containing more than one unencoded `=` must be rejected because the
    -- three-element split falls into the error branch.
    #eval parseCheckFail "/path?key=a=b"
    
  • Tested patch for the second option above (keeping the types), in parseQuery:
    let value ← URI.EncodedQueryParam.fromString? (String.intercalate "%3D" value)
    
    Then ?a=b=c gives the value b%3Dc (decoded b=c), ?token=YWJj== gives YWJj%3D%3D (decoded YWJj==), and ?next=/x?y=1 gives /x?y%3D1 (decoded /x?y=1); key=a=b reads the same as key=a%3Db, which the test file already accepts. With this change the tests/elab/async_http_*.lean files pass except for the expectation above. One difference from the first option: the stored value is the re-encoded form, so serializing the query gives %3D where the request had =.

Found while proving, for a patched copy of Std.Http, that the URI and Host parsers accept every valid request-target and Host value (https://github.com/eumemic/stdhttp-verify, FINDINGS.md §9; the proofs there are about the parser with this patch).

Disclosure: the testing and patch were done with AI assistance (Claude).

eumemic avatar Oct 28 '26 19:10 eumemic

Thanks for checking the H1 server path and finding the existing rejection test. Your results make the compatibility trade-off clear: keep the types, accept the value, and serialize the extra = as %3D.

I haven't independently rerun your patch yet. If you plan to submit it, please link the PR here so I can avoid duplicating the work. I'll defer to the maintainers on the representation choice, and your note about updating async_http_uri.lean is particularly helpful.

hiroki-abe-58 avatar Oct 28 '26 19:10 hiroki-abe-58