Skip to content

Commit e803323

Browse files
rekram1-nodeexe.dev user
andauthored
fix(core): match webfetch rules against the URL as written (#62373)
Co-authored-by: exe.dev user <exedev@anton.exe.xyz>
1 parent b60984d commit e803323

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ type Init = {
1616

1717
const fetch = (input: string | URL, init: Init = {}) =>
1818
Effect.gen(function* () {
19-
// Rules are matched against the URL that is requested, so `HTTPS://EVIL.COM` meets a rule for `https://evil.com/*`.
20-
const parsed = URL.parse(String(input))
21-
if (parsed === null) return yield* Effect.fail(new TypeError(`fetch received an invalid URL: ${String(input)}`))
22-
if (parsed.protocol === "file:") return yield* Effect.fail(new Error("fetch cannot be used for filesystem access."))
19+
// Rules match the URL as written. The fragment is never sent, and would keep a rule for the exact URL from
20+
// matching.
21+
const url = String(input).split("#")[0]
22+
const parsed = URL.parse(url)
23+
if (parsed?.protocol === "file:") return yield* Effect.fail(new Error("fetch cannot be used for filesystem access."))
2324
// Rules match the URL, so `https://user@evil.com` would slip past a rule for `https://evil.com/*`.
24-
if (parsed.username || parsed.password)
25+
if (parsed?.username || parsed?.password)
2526
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 = ""
28-
const url = parsed.href
2927
const execution = yield* CodeModeExecution.Service
3028
const permission = yield* Permission.Service
3129
const source = { type: "tool" as const, messageID: execution.messageID, id: execution.id }
@@ -35,7 +33,7 @@ const fetch = (input: string | URL, init: Init = {}) =>
3533
action: "webfetch",
3634
resources: [url],
3735
save: ["*"],
38-
metadata: display([url, init]),
36+
metadata: display([input, init]),
3937
sessionID: execution.sessionID,
4038
agent: execution.agent,
4139
source,

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,11 @@ export const Plugin = {
141141
output: Output,
142142
execute: (input, context) =>
143143
Effect.gen(function* () {
144-
// Rules are matched against the URL that is requested, so `HTTPS://EVIL.COM` meets a rule for
145-
// `https://evil.com/*`.
146-
const url = yield* Effect.try({
147-
try: () => {
148-
const parsed = new URL(input.url)
149-
assertHttpUrl(parsed)
150-
// The fragment is never sent, and would keep a rule for the exact URL from matching.
151-
parsed.hash = ""
152-
return parsed.href
153-
},
144+
// Rules match the URL as written. The fragment is never sent, and would keep a rule for the exact
145+
// URL from matching.
146+
const url = input.url.split("#")[0]
147+
yield* Effect.try({
148+
try: () => assertHttpUrl(new URL(url)),
154149
catch: (error) => error,
155150
})
156151

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

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

540-
it.effect("matches rules without the fragment and rejects credentials in the URL", () =>
540+
it.effect("matches rules against the URL as written without the fragment and rejects credentials", () =>
541541
Effect.gen(function* () {
542542
reset()
543543
const registry = yield* Tool.Service
544544

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"]])
545+
yield* executeTool(registry, call({ url: "https://Example.com#top", format: "text" }))
546+
expect(assertions.map((assertion) => assertion.resources)).toEqual([["https://Example.com"]])
547547

548548
reset()
549549
expect(yield* executeTool(registry, call({ url: "https://user@example.com/", format: "text" }))).toMatchObject({

0 commit comments

Comments
 (0)