Skip to content

fix(core): check webfetch permission on every redirect hop - #59352

Closed
R44VC0RP wants to merge 1 commit into
devfrom
webfetch-redirect-permission
Closed

R44VC0RP wants to merge 1 commit into
devfrom
webfetch-redirect-permission

Conversation

@R44VC0RP

Copy link
Copy Markdown
Contributor

What

  • webfetch now fetches with redirect: "manual" (provided per request via FetchHttpClient.RequestInit; the shared client layer is untouched) and follows 301/302/303/307/308 itself.
  • Each Location is resolved against the current URL and runs the same http(s) scheme check and permission.assert as the requested URL before it is fetched. Chains are capped at 10 redirects.
  • Each hop has its own timeout, so a permission prompt for a redirect target does not eat the request timeout. With no redirect, behaviour is unchanged (the output url is 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.ts from packages/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), a file:// redirect target, and the redirect cap.
  • Go-red: with the old webfetch.ts, those three tests fail.
  • bun run typecheck:tsc in packages/core; bun run check from the repo root.

Open question

Redirect targets get their own permission request, so a user in ask mode who approves https://a.com is asked again for https://www.a.com after 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.

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.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants