mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(core): match webfetch rules against the URL as written (#62373)
Co-authored-by: exe.dev user <exedev@anton.exe.xyz>
This commit is contained in:
committed by
GitHub
Unverified
parent
b60984d68a
commit
e803323c13
@@ -16,16 +16,14 @@ type Init = {
|
||||
|
||||
const fetch = (input: string | URL, init: Init = {}) =>
|
||||
Effect.gen(function* () {
|
||||
// Rules are matched against the URL that is requested, so `HTTPS://EVIL.COM` meets a rule for `https://evil.com/*`.
|
||||
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 as written. The fragment is never sent, and would keep a rule for the exact URL from
|
||||
// matching.
|
||||
const url = String(input).split("#")[0]
|
||||
const parsed = URL.parse(url)
|
||||
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)
|
||||
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
|
||||
const source = { type: "tool" as const, messageID: execution.messageID, id: execution.id }
|
||||
@@ -35,7 +33,7 @@ const fetch = (input: string | URL, init: Init = {}) =>
|
||||
action: "webfetch",
|
||||
resources: [url],
|
||||
save: ["*"],
|
||||
metadata: display([url, init]),
|
||||
metadata: display([input, init]),
|
||||
sessionID: execution.sessionID,
|
||||
agent: execution.agent,
|
||||
source,
|
||||
|
||||
@@ -141,16 +141,11 @@ export const Plugin = {
|
||||
output: Output,
|
||||
execute: (input, context) =>
|
||||
Effect.gen(function* () {
|
||||
// Rules are matched against the URL that is requested, so `HTTPS://EVIL.COM` meets a rule for
|
||||
// `https://evil.com/*`.
|
||||
const url = yield* Effect.try({
|
||||
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
|
||||
},
|
||||
// Rules match the URL as written. The fragment is never sent, and would keep a rule for the exact
|
||||
// URL from matching.
|
||||
const url = input.url.split("#")[0]
|
||||
yield* Effect.try({
|
||||
try: () => assertHttpUrl(new URL(url)),
|
||||
catch: (error) => error,
|
||||
})
|
||||
|
||||
|
||||
@@ -537,13 +537,13 @@ describe("WebFetchTool registration", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("matches rules without the fragment and rejects credentials in the URL", () =>
|
||||
it.effect("matches rules against the URL as written without the fragment and rejects credentials", () =>
|
||||
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"]])
|
||||
yield* executeTool(registry, call({ url: "https://Example.com#top", format: "text" }))
|
||||
expect(assertions.map((assertion) => assertion.resources)).toEqual([["https://Example.com"]])
|
||||
|
||||
reset()
|
||||
expect(yield* executeTool(registry, call({ url: "https://user@example.com/", format: "text" }))).toMatchObject({
|
||||
|
||||
Reference in New Issue
Block a user