Repository navigation
Conversation
webfetch checked the URL scheme and asked for permission only for the URL the model requested, then let fetch follow redirects automatically. A page the user approved (or allow-listed by host) could redirect to localhost, 169.254.169.254 or a LAN address and the body was returned without the user or their deny rules ever seeing that target. webfetch now fetches with `redirect: "manual"` (per request, through FetchHttpClient.RequestInit, not by changing the shared client layer) and follows 301/302/303/307/308 itself: each Location is resolved against the current URL and goes through the same http(s) check and permission.assert before it is requested. Chains are capped at 10 redirects. Each hop gets its own timeout so a permission prompt for a redirect target does not count against it. Behaviour without redirects is unchanged; the output url is still the requested URL. Removed behaviour: automatic redirect following without a permission check; redirect chains longer than 10 now fail. Verified: bun test test/tool-webfetch.test.ts (90 pass) from packages/core; the updated 302 test, the new denied-target test and the scheme/cap test fail against the old implementation; bun run typecheck:tsc in packages/core; bun run check from the repo root.
There was a problem hiding this comment.
Checking every redirect hop works. On the base, the PR's redirect tests fail: the chain gets a single permission check, and a denied redirect target is still fetched and its body returned. With this change all 90 webfetch tests pass (including the live local-server redirect tests) and typecheck:tsc is clean.
Public API: unchanged. The only addition is an exported MAX_REDIRECTS constant in the tool module.
One thing to fix (inline): a malformed Location header now makes the tool run crash with an uncaught exception instead of returning a tool error.
| ) | ||
| const location = redirectLocation(response) | ||
| if (location !== undefined) | ||
| return { type: "redirect", url: new URL(location, url).toString() } as const |
There was a problem hiding this comment.
new URL(location, url) throws synchronously inside the generator, so a malformed Location (e.g. http://[) becomes a defect. The tool run dies with ERR_INVALID_URL instead of returning { status: "error" }. I checked this with a 302 + location: http://[: the base returns a normal tool error for the same response. Parsing it with Effect.try (like authorize does), or resolving it inside authorize, would keep it a normal failure.
What
redirect: "manual"(provided per request viaFetchHttpClient.RequestInit; the shared client layer is untouched) and follows 301/302/303/307/308 itself.Locationis resolved against the current URL and runs the same http(s) scheme check andpermission.assertas the requested URL before it is fetched. Chains are capped at 10 redirects.urlis still the requested URL).Why
webfetch approved only the URL the model asked for, then fetch followed redirects silently. A page the user approved or allow-listed by host could redirect to
http://127.0.0.1:<port>/,http://169.254.169.254/or a LAN address, and the body came back without the user, their deny rules or a Console tool policy ever seeing that target. Related advisory: GHSA-6cwg-8rrp-47rr.Verified
bun test test/tool-webfetch.test.tsfrompackages/core: 90 pass. The 302 test now expects one assertion per hop; new cases cover a redirect target denied by a rule (fails, target never requested, body not returned), afile://redirect target, and the redirect cap.webfetch.ts, those three tests fail.bun run typecheck:tscinpackages/core;bun run checkfrom the repo root.Open question
Redirect targets get their own permission request, so a user in
askmode who approveshttps://a.comis asked again forhttps://www.a.comafter a redirect. That is the safe default; if it is too noisy, an option is to auto-allow same-origin hops while still asking for any cross-origin target.