Skip to content

fix(core): save the site, not every URL, when always allowing a web fetch - #66000

Merged
rekram1-node merged 3 commits into
devfrom
webfetch-host-scope
Nov 24, 2026
Merged

rekram1-node merged 3 commits into
devfrom
webfetch-host-scope

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #64429 for the common case: choosing Allow always on a webfetch prompt saved webfetch: * → allow, so one click allowed every URL on every site for the project. It now saves the URL's site.

 approve https://opencode.ai/docs/permissions → Allow always
- saves  *                                         every URL, every site
+ saves  https://opencode.ai, https://opencode.ai/*  that site only

This applies everywhere a webfetch approval is saved: the webfetch tool, Code Mode fetch, and the URLs the image and video tools download or hand to a provider. They all go through a new Permission.site(url).

  • Why the site and not the path (what the issue asks for): other coding tools scope saved web approvals by domain, and a path rule for /docs/permissions would prompt again for /docs/agents, which defeats "always". Path- or host-wide choices in the prompt itself are [FEATURE] Let the user choose the saved approval scope in the "Always allow" prompt (e.g. parent directories for external_directory) #64430, which this doesn't touch.

  • Two patterns, not one. Rules match the URL as written, and https://opencode.ai/* doesn't match a bare https://opencode.ai. Saving both avoids a second prompt for the same site, which is the trailing-slash question the issue raises.

  • As written. The site is taken from the URL as the agent wrote it, not normalized, for the same reason: https://OpenCode.ai/x saves https://OpenCode.ai/*, which matches the next fetch written the same way.

  • Redirects are unchanged: each hop is still only checked for deny rules, so a saved site doesn't widen what a redirect can reach.

  • Unusual URLs save only themselves. A URL with credentials in its site (https://opencode.ai@evil.com/x goes to evil.com) or that isn't http(s) saves just that exact URL, never *, so the saved rule can't name a misleading site. The site ends where URL parsers end it, including at \, so https://evil.com\@opencode.ai/ saves https://evil.com/*. Rules treat * and ? as wildcards, so a site containing one (https://*/x parses, and would otherwise save a pattern for every site) gets no pattern, and neither does an unusual URL containing one: those prompts offer no "Allow always". The regex is linear (no backtracking), and 200k characters take about 1 ms.

Existing saved webfetch: * rules are left alone.

The permissions docs now mention that web fetches propose the URL's site.

No flag: this narrows what a click saves and changes no other behavior.

Checks

  • packages/core: tool-webfetch.test.ts, tool-execute.test.ts, tool-image-generate.test.ts, tool-video-generate.test.ts, permission.test.ts, codemode.test.ts, generate.test.ts, session-generate.test.ts (the webfetch and Code Mode tests now expect the site patterns)
  • bun run check

Added by the opencode bot (AI-generated).

Related: #64430, #55232

@rekram1-node
rekram1-node enabled auto-merge (squash) November 24, 2026 06:29

@opencodejr opencodejr 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.

Looks right: Allow always on a web fetch now saves the URL's site instead of *, for webfetch, Code Mode fetch, and the image and video tools.

How I checked: I ran the updated tool-webfetch.test.ts against the base. It fails there with save: ["*"] and passes with this PR. A small script with Permission.site and Wildcard.match confirmed the saved patterns cover other pages on the same site and the bare origin, and not lookalike hosts or URLs that only mention the site in the query. The core tests nearest the change pass, and tsc is clean. I couldn't get the TUI prompt to appear with the simulated model, so there are no screenshots.

Public API: unchanged. Only the pattern proposed for webfetch saves changes, and existing saved rules keep working.

One gap worth closing: a * in the hostname (see inline).

Related: #64430, #55232

Comment thread packages/core/src/permission.ts Outdated
* as written, so the site is kept as written too, with and without a path.
*/
export function site(url: string) {
const origin = /^https?:\/\/[^/?#]+/i.exec(url)?.[0]

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("https://*/x") parses, and webfetch's assertHttpUrl accepts it, so site() returns https://* and https://*/*. Those match every https URL. Likewise https://*.com/x saves a pattern for all .com sites. A fetch target suggested by an injected prompt could still turn one click into an all-sites rule. The prompt does list the patterns, and it's no worse than today's *, but it undoes the narrowing. Could the host be rejected when the origin contains *, e.g. by returning [] so there's no Always option for that URL?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Scope the webfetch "Allow always" approval to the requested URL path instead of saving *

1 participant