lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

Std.Http: `decideRedirect` resolves `Location: /` to the current request path instead of `/`

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

Prerequisites

  • [x] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • [x] Test your test case against the latest nightly release (here: nightly-2026-10-27 through elan, see Versions)
  • [ ] When reporting a perceived soundness bug: Does it affect lake check --paranoid? (not a soundness bug)

Description

Std.Http.Protocol.H1.decideRedirect resolves an absolute-path reference / (and /?query) as if it were the empty reference. For GET /login?next=%2Fhome answered with 302 Found and Location: /, the planned target is /login?next=%2Fhome again instead of /. With Location: /?y the base path is kept: /b/c/d;p?y instead of /?y.

RFC 3986 §5.2.2 uses the base path only when R.path == ""; when R.path starts with /, T.path = remove_dot_segments(R.path) and T.query = R.query. The cause appears to be the authority := none branch of RedirectPlan.rewriteTarget in src/Std/Http/Protocol/H1/Redirect.lean, which tests refPath.isEmpty before refPath.absolute: URI.Path.isEmpty (src/Std/Http/Data/URI/Basic.lean) only looks at segments, and / parses to { segments := #[], absolute := true }.

Context

Location: / is a very common redirect (after logout, or to a site root). A client that follows the plan re-requests the same URL until it hits its redirect limit. The open HTTP client PRs #14546 and #14547 both touch Redirect.lean; at their current heads the branch still tests refPath.isEmpty first.

Steps to Reproduce

import Std.Http
open Std.Http Std.Http.Protocol.H1

def current : URI.Origin :=
  { scheme := URI.Scheme.ofString! "http", host := (URI.parse! "http://example.com/").host?.get!, port := 80 }

/-- Target that `decideRedirect` plans for `GET <base>` answered by `302 Found` + `Location: <loc>`. -/
def plannedTarget (base loc : String) : String :=
  let req : Request.Head := { method := .get, version := .v11, uri := RequestTarget.parse! base }
  let hs := Headers.empty.insert .location (Header.Value.ofString! loc)
  match decideRedirect current req false false .v11 .found hs with
  | .follow plan => toString plan.target
  | .done => "<not followed>"

#eval plannedTarget "/login?next=%2Fhome" "/"       -- expected "/"
#eval plannedTarget "/b/c/d;p?q" "/"                -- expected "/"
#eval plannedTarget "/b/c/d;p?q" "/?y"              -- expected "/?y"
#eval plannedTarget "/b/c/d;p?q" "/g"               -- control: "/g"
#eval plannedTarget "/b/c/d;p?q" ""                 -- control: the empty reference keeps "/b/c/d;p?q"
#eval (RequestTarget.parse! "/").path.isEmpty       -- `/` has no segments, so this is true

Expected behavior: "/", "/", "/?y", "/g", "/b/c/d;p?q", and / should not be treated as an empty path for reference resolution.

Actual behavior: "/login?next=%2Fhome", "/b/c/d;p?q", "/b/c/d;p?y", "/g", "/b/c/d;p?q", true.

Versions

Identical output on:

  • Lean (version 4.37.0-nightly-2026-10-27, x86_64-unknown-linux-gnu, commit 67bc4fe8130f9b5e1a073c73ed5cda09de6c63fe, Release)
  • Lean 4.36.0-rc1 (c9afcddf514a6772bd4d649f7cfc04eff0b9f15e)
  • Lean 4.35.0 (eb6cafb7290e8bd5f2937522d8d804f58f02bafc)

Ubuntu 24.04 on WSL2.

Additional Information

A possible fix is to take the empty-reference branch only for a relative empty path, e.g. if refPath.isEmpty && !refPath.absolute then basePath ... (and the same for the query condition), or to give Path a separate "no path at all" test.

This report was prepared with AI assistance (Claude Code, Claude Opus 5.5); the reproduction above was run on the three toolchains listed.

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