Repository navigation
fix(lab-ops-hub): fetch userinfo URLs with Basic auth in the container crawler - #634
Conversation
…r crawler (#622) _fetch_crawl handed validated URLs straight to urllib, which keeps 'user@host' as the hostname: getaddrinfo failed before any request was sent, so every userinfo target both validators accept 502'd in the container while the Worker fallback fetched it. Split the userinfo off before fetching and send its credentials the way WHATWG fetch does: Authorization: Basic built from the percent-decoded username/password (UTF-8). A userinfo with empty username and password ('@', ':@') sends no header. The redirect handler re-derives the header from each target's own userinfo, so a credentialed target fetches with its credentials and a plain target never inherits the previous hop's Authorization. Tests: 3 new Python tests (10-spelling fetcher battery end-to-end against a local server, redirect-to-userinfo integration, redirect handler unit pin) + 1 TS pin that the Worker fetches userinfo URLs in validated form. Python 50/50, TS 36/36; ablation with app.py reverted fails exactly the 3 new Python tests.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing: 3
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 22bdbe74e9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if req.has_header("Authorization"): | ||
| req.remove_header("Authorization") |
There was a problem hiding this comment.
Preserve credentials across relative redirects
When a credentialed URL responds with a relative Location such as /landing, the initial _apply_userinfo_auth has already removed the userinfo from req.full_url, so urllib resolves the target without credentials; this second call then removes the copied Authorization header because _split_userinfo finds no @. The authenticated first hop therefore becomes an unauthenticated second hop, breaking common canonical/login redirects and diverging from the Worker path in src/lib.ts, where new URL(location, current) inherits the credentials from current. Retain the URL credentials as redirect metadata or preserve the header for relative same-origin redirects while still clearing it when an explicit target omits credentials.
Useful? React with 👍 / 👎.
| username = unquote(username) | ||
| password = unquote(password) |
There was a problem hiding this comment.
Preserve non-UTF-8 credential bytes
For accepted userinfo containing percent-encoded bytes that are not valid UTF-8, unquote silently replaces those bytes with U+FFFD before the value is re-encoded. For example, http://%FF@example.com/ is accepted, but this produces credentials b'\xef\xbf\xbd:' (Basic 77+9Og==) rather than the percent-decoded bytes b'\xff:' (Basic /zo=), so such targets receive the wrong password material and distinct credential spellings can collapse to the same header. Decode the userinfo with unquote_to_bytes and construct the Basic payload from the username and password byte sequences directly.
Useful? React with 👍 / 👎.
Fixes #622
Problem
_fetch_crawlhanded validated URLs straight tourllib.request. urllib never strips userinfo:Request.hostkeepsuser@host/user:pass@hostwhole,http.clientis built with it as the hostname, andgetaddrinfofails — so every userinfo target both validators accept (plainhttp://user@example.com/, password-bearing, and the percent-encoded forms #616/#619 polished) 502'd in the container before any request was sent, while the Worker fallback fetched it and recorded the wrong source.Reproduced on main
00bb59d5with the app's own_fetch_crawlagainst a local server: control URL → 200;user@,user:pa%3Ass@,caf%C3%A9@,@,user:@→ allURLError: [Errno -2] Name or service not known, no request received.Solution
_split_userinfo(url)/_apply_userinfo_auth(req)incontainer/app.py: strip the userinfo from the URL handed to urllib and send its credentials the way WHATWG fetch does — anAuthorization: Basicheader built from the percent-decoded username/password (UTF-8). A URL only "includes credentials" when username or password is non-empty, so bare@/:@sends no header (which also makesuser:@fetch exactly asuser@)._fetch_crawlapplies it to the start request._ValidatingRedirectHandler.redirect_requestapplies it to every followed request, re-deriving the header from the target's own userinfo: urllib copies the previous request's headers onto redirects, so without this a credentialed start URL'sAuthorizationwould leak to a plain redirect target, and a credentialed target would be fetched with the previous hop's credentials (or none). Per-hop SSRF re-validation is unchanged.Tests
%3Apassword, non-ASCII credentials, empty-password,:pass, bare@,:@, no userinfo), a redirect integration test (userinfo target gets its own auth; credentialed start does not leak to a plain target), and a redirect-handler unit pin. 50/50 pass (was 47).app.pyreverted, exactly the 3 new Python tests fail.ruff formatclean; biome format (treefmt's TS formatter) reports no fixes.fetchis not the reference here (it silently drops password-bearing userinfo with noAuthorizationheader and fails username-only URLs); the implementation follows the WHATWG fetch rule the issue's acceptance criteria specify, which is what the Cloudflare Workers fallback implements.Single-repo: no
validate_crawl_url/_fetch_crawlcopy exists in the opencode, openchamber or nix clones (grep-verified).Need help on this PR? Tag
@codesmith-botwith what you need.