Skip to content

Commit 0e4745a

Browse files
fix(tool): port upstream webfetch redirect rule checks (anomalyco/opencode#59748)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent eacb84a commit 0e4745a

2 files changed

Lines changed: 167 additions & 7 deletions

File tree

‎packages/opencode/src/tool/webfetch.ts‎

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Tool } from "./tool"
33
import TurndownService from "turndown"
44
import DESCRIPTION from "./webfetch.txt"
55
import { abortAfterAny } from "../util/abort"
6+
// altimate_change start — upstream_fix: check webfetch redirect targets against permission rules (anomalyco/opencode#59748, anomalyco/opencode#61701)
7+
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
8+
import { PermissionEvaluate } from "@/permission/evaluate"
9+
import { Wildcard } from "@opencode-ai/core/util/wildcard"
10+
// altimate_change end
611

712
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
813
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
@@ -15,6 +20,45 @@ const BROWSER_UA =
1520
// Status codes that warrant a retry with a different User-Agent
1621
const RETRYABLE_STATUSES = new Set([403, 406])
1722

23+
// altimate_change start — upstream_fix: check webfetch redirect targets against permission rules (anomalyco/opencode#59748, anomalyco/opencode#61701)
24+
export const MAX_REDIRECTS = 10
25+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308])
26+
27+
function assertFetchableUrl(url: URL) {
28+
if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error("URL must use http:// or https://")
29+
// Rules match the URL, so `https://[email protected]` would slip past a rule for `https://allowed.com*`.
30+
if (url.username || url.password) throw new Error("URL must not include credentials")
31+
}
32+
33+
// Follows redirects here instead of in fetch so each new URL is checked against the rules first.
34+
// A target only fails when the rules deny it; the user is not asked again.
35+
async function fetchCheckingRedirects(
36+
url: string,
37+
init: RequestInit,
38+
ruleset: PermissionV1.Ruleset,
39+
): Promise<Response> {
40+
let current = url
41+
for (let redirects = 0; ; redirects++) {
42+
const response = await fetch(current, { ...init, redirect: "manual" })
43+
const location = response.headers.get("location")
44+
if (!REDIRECT_STATUSES.has(response.status) || !location) return response
45+
await response.body?.cancel().catch(() => {})
46+
if (redirects >= MAX_REDIRECTS) throw new Error(`Too many redirects (more than ${MAX_REDIRECTS})`)
47+
const target = new URL(location, current)
48+
assertFetchableUrl(target)
49+
// The fragment is never sent, and would keep a rule for the exact URL from matching.
50+
target.hash = ""
51+
const rule = PermissionEvaluate.evaluate("webfetch", target.href, ruleset)
52+
if (rule.action === "deny") {
53+
throw new PermissionV1.DeniedError({
54+
ruleset: ruleset.filter((item) => Wildcard.match("webfetch", item.permission)),
55+
})
56+
}
57+
current = target.href
58+
}
59+
}
60+
// altimate_change end
61+
1862
// altimate_change start — process-level URL failure cache (#471)
1963
// Prevents repeated fetches to URLs that already returned 404/410 in this
2064
// process. Cache is module-scope so in daemon mode (`altimate serve`) and
@@ -308,11 +352,17 @@ export const WebFetchTool = Tool.define("webfetch", {
308352
if (!params.url.startsWith("http://") && !params.url.startsWith("https://")) {
309353
throw new Error("URL must start with http:// or https://")
310354
}
355+
// altimate_change start — upstream_fix: reject URL credentials and match rules on the requested URL (anomalyco/opencode#61701, anomalyco/opencode#60505)
356+
let parsed: URL
311357
try {
312-
new URL(params.url)
358+
parsed = new URL(params.url)
313359
} catch {
314360
throw new Error(`Invalid URL: "${params.url.slice(0, 200)}" is not a valid URL. Check the format and try again.`)
315361
}
362+
assertFetchableUrl(parsed)
363+
parsed.hash = ""
364+
const url = parsed.href
365+
// altimate_change end
316366

317367
// Check failure cache — avoid re-fetching URLs that already returned 404/410/451.
318368
// Prefix with "(cached failure, Nm ago)" so the agent can tell a cache-hit
@@ -328,7 +378,9 @@ export const WebFetchTool = Tool.define("webfetch", {
328378

329379
await ctx.ask({
330380
permission: "webfetch",
331-
patterns: [params.url],
381+
// altimate_change start — upstream_fix: match rules on the requested URL (anomalyco/opencode#60505)
382+
patterns: [url],
383+
// altimate_change end
332384
always: ["*"],
333385
metadata: {
334386
url: params.url,
@@ -370,13 +422,16 @@ export const WebFetchTool = Tool.define("webfetch", {
370422
let arrayBuffer: ArrayBuffer
371423
let response: Response
372424
try {
373-
response = await fetch(params.url, { signal, headers: honestHeaders })
425+
// altimate_change start — upstream_fix: check webfetch redirect targets against permission rules (anomalyco/opencode#59748)
426+
const ruleset = ctx.permissions ?? []
427+
response = await fetchCheckingRedirects(url, { signal, headers: honestHeaders }, ruleset)
374428

375429
// Retry with browser UA if the honest UA was rejected
376430
if (!response.ok && RETRYABLE_STATUSES.has(response.status)) {
377431
await response.body?.cancel().catch(() => {})
378-
response = await fetch(params.url, { signal, headers: browserHeaders })
432+
response = await fetchCheckingRedirects(url, { signal, headers: browserHeaders }, ruleset)
379433
}
434+
// altimate_change end
380435

381436
// altimate_change start — actionable error messages and failure caching (#471)
382437
if (!response.ok) {

‎packages/opencode/test/tool/webfetch.test.ts‎

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { describe, expect } from "bun:test"
2-
import { Effect, Layer } from "effect"
2+
import { Cause, Effect, Exit, Layer } from "effect"
33
import { FetchHttpClient } from "effect/unstable/http"
44
import { Agent } from "../../src/agent/agent"
55
import { Truncate } from "@/tool/truncate"
66
import { WebFetchTool } from "../../src/tool/webfetch"
77
import { SessionID, MessageID } from "../../src/session/schema"
88
import { Tool } from "@/tool/tool"
9+
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
910
import { testEffect } from "../lib/effect"
1011

1112
const it = testEffect(Layer.mergeAll(FetchHttpClient.layer, Truncate.defaultLayer, Agent.defaultLayer))
@@ -31,12 +32,27 @@ const withFetch = <A, E, R>(
3132
(server) => Effect.sync(() => server.stop(true)),
3233
)
3334

34-
const exec = Effect.fn("WebFetchToolTest.exec")(function* (args: Tool.InferParameters<typeof WebFetchTool>) {
35+
const exec = Effect.fn("WebFetchToolTest.exec")(function* (
36+
args: Tool.InferParameters<typeof WebFetchTool>,
37+
// altimate_change start — upstream_fix: redirect rule tests pass the effective ruleset (anomalyco/opencode#59748)
38+
permissions?: PermissionV1.Ruleset,
39+
// altimate_change end
40+
) {
3541
const info = yield* WebFetchTool
3642
const tool = yield* info.init()
37-
return yield* tool.execute(args, ctx)
43+
return yield* tool.execute(args, permissions ? { ...ctx, permissions } : ctx)
3844
})
3945

46+
// altimate_change start — upstream_fix: redirect rule tests (anomalyco/opencode#59748, anomalyco/opencode#61701)
47+
const failure = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
48+
Effect.gen(function* () {
49+
const exit = yield* Effect.exit(effect)
50+
expect(Exit.isFailure(exit)).toBe(true)
51+
if (Exit.isSuccess(exit)) return ""
52+
return String(Cause.squash(exit.cause))
53+
})
54+
// altimate_change end
55+
4056
describe("tool.webfetch", () => {
4157
it.instance("returns image responses as file attachments", () =>
4258
Effect.gen(function* () {
@@ -110,4 +126,93 @@ describe("tool.webfetch", () => {
110126
}),
111127
),
112128
)
129+
130+
// altimate_change start — upstream_fix: redirect rule tests (anomalyco/opencode#59748, anomalyco/opencode#61701)
131+
it.instance("follows redirects to targets the rules allow", () => {
132+
const requested: string[] = []
133+
return withFetch(
134+
(req) => {
135+
const path = new URL(req.url).pathname
136+
requested.push(path)
137+
if (path === "/start") return new Response(null, { status: 302, headers: { location: "/target#frag" } })
138+
return new Response("arrived", { status: 200, headers: { "content-type": "text/plain" } })
139+
},
140+
(url) =>
141+
Effect.gen(function* () {
142+
const result = yield* exec({ url: new URL("/start", url).toString(), format: "text" }, [
143+
{ permission: "webfetch", pattern: "*", action: "allow" },
144+
])
145+
expect(result.output).toBe("arrived")
146+
expect(requested).toEqual(["/start", "/target"])
147+
}),
148+
)
149+
})
150+
151+
it.instance("stops at a redirect target the rules deny", () => {
152+
const requested: string[] = []
153+
return withFetch(
154+
(req) => {
155+
const path = new URL(req.url).pathname
156+
requested.push(path)
157+
if (path === "/start") return new Response(null, { status: 302, headers: { location: "/secret" } })
158+
return new Response("secret", { status: 200, headers: { "content-type": "text/plain" } })
159+
},
160+
(url) =>
161+
Effect.gen(function* () {
162+
const message = yield* failure(
163+
exec({ url: new URL("/start", url).toString(), format: "text" }, [
164+
{ permission: "webfetch", pattern: "*", action: "allow" },
165+
{ permission: "webfetch", pattern: new URL("/secret", url).toString(), action: "deny" },
166+
]),
167+
)
168+
expect(message).toContain("prevents you from using this specific tool call")
169+
expect(requested).toEqual(["/start"])
170+
}),
171+
)
172+
})
173+
174+
it.instance("rejects redirects to non-HTTP schemes and endless redirect chains", () =>
175+
withFetch(
176+
(req) => {
177+
const path = new URL(req.url).pathname
178+
if (path === "/file") return new Response(null, { status: 301, headers: { location: "file:///etc/passwd" } })
179+
return new Response(null, { status: 307, headers: { location: `${path}x` } })
180+
},
181+
(url) =>
182+
Effect.gen(function* () {
183+
expect(yield* failure(exec({ url: new URL("/file", url).toString(), format: "text" }))).toContain(
184+
"URL must use http:// or https://",
185+
)
186+
expect(yield* failure(exec({ url: new URL("/loop", url).toString(), format: "text" }))).toContain(
187+
"Too many redirects",
188+
)
189+
}),
190+
),
191+
)
192+
193+
it.instance("rejects credentials in the requested URL and in redirect targets", () => {
194+
let requests = 0
195+
return withFetch(
196+
(req) => {
197+
requests++
198+
const target = new URL("/next", req.url)
199+
target.username = "user"
200+
return new Response(null, { status: 302, headers: { location: target.toString() } })
201+
},
202+
(url) =>
203+
Effect.gen(function* () {
204+
const withCredentials = new URL("/start", url)
205+
withCredentials.username = "user"
206+
expect(yield* failure(exec({ url: withCredentials.toString(), format: "text" }))).toContain(
207+
"URL must not include credentials",
208+
)
209+
expect(requests).toBe(0)
210+
expect(yield* failure(exec({ url: new URL("/start", url).toString(), format: "text" }))).toContain(
211+
"URL must not include credentials",
212+
)
213+
expect(requests).toBe(1)
214+
}),
215+
)
216+
})
217+
// altimate_change end
113218
})

0 commit comments

Comments
 (0)