fix(core): reject URL credentials and ignore fragments in web permissions (#61701)

This commit is contained in:
Aiden Cline
2026-11-04 10:36:04 -06:00
committed by GitHub
Unverified
parent 7aabf5e9e3
commit 125655ec6f
4 changed files with 50 additions and 0 deletions
+8
View File
@@ -20,6 +20,11 @@ const fetch = (input: string | URL, init: Init = {}) =>
const parsed = URL.parse(String(input))
if (parsed === null) return yield* Effect.fail(new TypeError(`fetch received an invalid URL: ${String(input)}`))
if (parsed.protocol === "file:") return yield* Effect.fail(new Error("fetch cannot be used for filesystem access."))
// Rules match the URL, so `https://user@evil.com` would slip past a rule for `https://evil.com/*`.
if (parsed.username || parsed.password)
return yield* Effect.fail(new TypeError("fetch cannot be used with credentials in the URL."))
// The fragment is never sent, and would keep a rule for the exact URL from matching.
parsed.hash = ""
const url = parsed.href
const execution = yield* CodeModeExecution.Service
const permission = yield* Permission.Service
@@ -70,6 +75,9 @@ const fetch = (input: string | URL, init: Init = {}) =>
const next = new URL(location, target)
if (next.protocol !== "http:" && next.protocol !== "https:")
return Effect.fail(new Error(`fetch cannot follow a redirect to ${next.protocol} URLs.`))
if (next.username || next.password)
return Effect.fail(new Error("fetch cannot follow a redirect with credentials in the URL."))
next.hash = ""
return check(next.href).pipe(
Effect.andThen(follow(next.href, redirected(request, response.status, target, next), redirects + 1)),
)
@@ -70,6 +70,8 @@ const request = (url: string, format: Format, userAgent = openCodeUserAgent) =>
const assertHttpUrl = (url: URL) => {
if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error("URL must use http:// or https://")
// Rules match the URL, so `https://user@evil.com` would slip past a rule for `https://evil.com/*`.
if (url.username || url.password) throw new Error("URL must not include credentials")
}
export const MAX_REDIRECTS = 10
@@ -95,6 +97,7 @@ const execute = (
try: () => {
const target = new URL(location, url)
assertHttpUrl(target)
target.hash = ""
return target.href
},
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
@@ -144,6 +147,8 @@ export const Plugin = {
try: () => {
const parsed = new URL(input.url)
assertHttpUrl(parsed)
// The fragment is never sent, and would keep a rule for the exact URL from matching.
parsed.hash = ""
return parsed.href
},
catch: (error) => error,
+20
View File
@@ -107,6 +107,26 @@ describe("fetch permissions", () => {
expect(hits).toEqual(["GET /redirect"])
})
test("fetch matches rules without the fragment and rejects credentials in the URL", async () => {
hits.length = 0
const asked: Array<ReadonlyArray<string>> = []
const result = await Effect.runPromise(
createCodeMode(
new Map(),
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input.resources)) }),
).execute(
{
code: `await fetch("${server.url.origin}/allowed#top"); try { await fetch("${server.url.protocol}//user@${server.url.host}/") } catch (e) { return e.message }`,
},
context,
),
)
expect(result.output.output).toBe("fetch cannot be used with credentials in the URL.")
expect(asked).toEqual([[`${server.url.origin}/allowed`]])
expect(hits).toEqual(["GET /allowed"])
})
test("a rejection with feedback reaches the program as the error message", async () => {
hits.length = 0
const url = `${server.url.origin}/corrected`
+17
View File
@@ -537,6 +537,23 @@ describe("WebFetchTool registration", () => {
}),
)
it.effect("matches rules without the fragment and rejects credentials in the URL", () =>
Effect.gen(function* () {
reset()
const registry = yield* Tool.Service
yield* executeTool(registry, call({ url: "https://example.com/page#top", format: "text" }))
expect(assertions.map((assertion) => assertion.resources)).toEqual([["https://example.com/page"]])
reset()
expect(yield* executeTool(registry, call({ url: "https://user@example.com/", format: "text" }))).toMatchObject({
status: "error",
error: { message: "URL must not include credentials" },
})
expect(requests).toEqual([])
}),
)
it.effect("rejects redirects to non-HTTP schemes and endless redirect chains", () =>
Effect.gen(function* () {
reset()