Skip to content

Commit 125655e

Browse files
authored
fix(core): reject URL credentials and ignore fragments in web permissions (anomalyco#61701)
1 parent 7aabf5e commit 125655e

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎packages/core/src/codemode/fetch.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const fetch = (input: string | URL, init: Init = {}) =>
2020
const parsed = URL.parse(String(input))
2121
if (parsed === null) return yield* Effect.fail(new TypeError(`fetch received an invalid URL: ${String(input)}`))
2222
if (parsed.protocol === "file:") return yield* Effect.fail(new Error("fetch cannot be used for filesystem access."))
23+
// Rules match the URL, so `https://[email protected]` would slip past a rule for `https://evil.com/*`.
24+
if (parsed.username || parsed.password)
25+
return yield* Effect.fail(new TypeError("fetch cannot be used with credentials in the URL."))
26+
// The fragment is never sent, and would keep a rule for the exact URL from matching.
27+
parsed.hash = ""
2328
const url = parsed.href
2429
const execution = yield* CodeModeExecution.Service
2530
const permission = yield* Permission.Service
@@ -70,6 +75,9 @@ const fetch = (input: string | URL, init: Init = {}) =>
7075
const next = new URL(location, target)
7176
if (next.protocol !== "http:" && next.protocol !== "https:")
7277
return Effect.fail(new Error(`fetch cannot follow a redirect to ${next.protocol} URLs.`))
78+
if (next.username || next.password)
79+
return Effect.fail(new Error("fetch cannot follow a redirect with credentials in the URL."))
80+
next.hash = ""
7381
return check(next.href).pipe(
7482
Effect.andThen(follow(next.href, redirected(request, response.status, target, next), redirects + 1)),
7583
)

‎packages/core/src/tool/plugin/webfetch.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const request = (url: string, format: Format, userAgent = openCodeUserAgent) =>
7070

7171
const assertHttpUrl = (url: URL) => {
7272
if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error("URL must use http:// or https://")
73+
// Rules match the URL, so `https://[email protected]` would slip past a rule for `https://evil.com/*`.
74+
if (url.username || url.password) throw new Error("URL must not include credentials")
7375
}
7476

7577
export const MAX_REDIRECTS = 10
@@ -95,6 +97,7 @@ const execute = (
9597
try: () => {
9698
const target = new URL(location, url)
9799
assertHttpUrl(target)
100+
target.hash = ""
98101
return target.href
99102
},
100103
catch: (error) => (error instanceof Error ? error : new Error(String(error))),
@@ -144,6 +147,8 @@ export const Plugin = {
144147
try: () => {
145148
const parsed = new URL(input.url)
146149
assertHttpUrl(parsed)
150+
// The fragment is never sent, and would keep a rule for the exact URL from matching.
151+
parsed.hash = ""
147152
return parsed.href
148153
},
149154
catch: (error) => error,

‎packages/core/test/tool-execute.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ describe("fetch permissions", () => {
107107
expect(hits).toEqual(["GET /redirect"])
108108
})
109109

110+
test("fetch matches rules without the fragment and rejects credentials in the URL", async () => {
111+
hits.length = 0
112+
const asked: Array<ReadonlyArray<string>> = []
113+
const result = await Effect.runPromise(
114+
createCodeMode(
115+
new Map(),
116+
permissionService({ assert: (input) => Effect.sync(() => void asked.push(input.resources)) }),
117+
).execute(
118+
{
119+
code: `await fetch("${server.url.origin}/allowed#top"); try { await fetch("${server.url.protocol}//user@${server.url.host}/") } catch (e) { return e.message }`,
120+
},
121+
context,
122+
),
123+
)
124+
125+
expect(result.output.output).toBe("fetch cannot be used with credentials in the URL.")
126+
expect(asked).toEqual([[`${server.url.origin}/allowed`]])
127+
expect(hits).toEqual(["GET /allowed"])
128+
})
129+
110130
test("a rejection with feedback reaches the program as the error message", async () => {
111131
hits.length = 0
112132
const url = `${server.url.origin}/corrected`

‎packages/core/test/tool-webfetch.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,23 @@ describe("WebFetchTool registration", () => {
537537
}),
538538
)
539539

540+
it.effect("matches rules without the fragment and rejects credentials in the URL", () =>
541+
Effect.gen(function* () {
542+
reset()
543+
const registry = yield* Tool.Service
544+
545+
yield* executeTool(registry, call({ url: "https://example.com/page#top", format: "text" }))
546+
expect(assertions.map((assertion) => assertion.resources)).toEqual([["https://example.com/page"]])
547+
548+
reset()
549+
expect(yield* executeTool(registry, call({ url: "https://[email protected]/", format: "text" }))).toMatchObject({
550+
status: "error",
551+
error: { message: "URL must not include credentials" },
552+
})
553+
expect(requests).toEqual([])
554+
}),
555+
)
556+
540557
it.effect("rejects redirects to non-HTTP schemes and endless redirect chains", () =>
541558
Effect.gen(function* () {
542559
reset()

0 commit comments

Comments
 (0)