mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(core): normalize webfetch permission rules like requested URLs
This commit is contained in:
parent
2658ec02c8
commit
3bb8eb5e4f
3 changed files with 75 additions and 27 deletions
|
|
@ -17,6 +17,7 @@ import { Permission } from "../../permission.js"
|
|||
import type { FileAccess } from "../../file-access.js"
|
||||
import type { ReadTool } from "../../tool/plugin/read.js"
|
||||
import type { EditTool } from "../../tool/plugin/edit.js"
|
||||
import type { WebFetchTool } from "../../tool/plugin/webfetch.js"
|
||||
import { AbsolutePath } from "../../schema.js"
|
||||
|
||||
const legacySources = [
|
||||
|
|
@ -29,6 +30,7 @@ const decodeAgent = Schema.decodeUnknownOption(ConfigAgent.Info)
|
|||
const decodeLegacyAgent = Schema.decodeUnknownOption(ConfigAgentV1.Info)
|
||||
type PathAction = FileAccess.ExternalDirectoryAuthorization["action"] | typeof ReadTool.name | typeof EditTool.name
|
||||
const pathActions = ["external_directory", "read", "edit"] as const satisfies readonly PathAction[]
|
||||
const urlAction: typeof WebFetchTool.name = "webfetch"
|
||||
const agentKeys = new Set(["variant", ...Object.keys(ConfigAgent.Info.fields)])
|
||||
// Fields only V2 understands. A file that uses any of them is read as V2, whatever else it contains.
|
||||
const nativeKeys = ["permissions", "request", "system", "disabled"]
|
||||
|
|
@ -111,9 +113,22 @@ export const Plugin = define({
|
|||
function expandPermissions(rules: Permission.Ruleset, home: string): Permission.Ruleset {
|
||||
// Expand only resources tools resolve as filesystem paths. Bash resources are raw shell text:
|
||||
// rewriting `$HOME/private/**` would miss `$HOME/private/key`, and safe expansion needs shell-aware parsing.
|
||||
return rules.map((rule) =>
|
||||
isPathAction(rule.action) ? { ...rule, resource: expandHome(rule.resource, home) } : rule,
|
||||
)
|
||||
return rules.map((rule) => {
|
||||
if (isPathAction(rule.action)) return { ...rule, resource: expandHome(rule.resource, home) }
|
||||
if (rule.action === urlAction) return { ...rule, resource: normalizeUrlRule(rule.resource) }
|
||||
return rule
|
||||
})
|
||||
}
|
||||
|
||||
// Web requests are matched by their parsed URL (lowercase host, punycode, no default port, `/` path), so
|
||||
// rewrite the scheme and host of a rule the same way. The rest stays as written to keep its wildcards.
|
||||
function normalizeUrlRule(resource: string) {
|
||||
// `?` is also a one-character wildcard, so a host containing it is ambiguous and stays as written.
|
||||
const parts = /^([a-z][a-z0-9+.-]*:\/\/)([^/?#]*)(\/.*)?$/is.exec(resource)
|
||||
if (!parts) return resource
|
||||
const origin = URL.parse(`${parts[1]}${parts[2]}/`)
|
||||
if (!origin || origin.username || origin.password) return resource
|
||||
return `${origin.protocol}//${origin.host}${parts[3] ?? (resource.endsWith("*") ? "" : "/")}`
|
||||
}
|
||||
|
||||
function isPathAction(action: string): action is PathAction {
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ export const Plugin = {
|
|||
// Downloads follow the same rule as the webfetch tool, so denying web access also covers image URLs.
|
||||
yield* permission.assert({
|
||||
action: "webfetch",
|
||||
resources: [source],
|
||||
resources: [URL.parse(source)?.href ?? source],
|
||||
save: ["*"],
|
||||
metadata: { url: source },
|
||||
...Permission.invocation(context),
|
||||
|
|
|
|||
|
|
@ -155,6 +155,34 @@ permissions:
|
|||
}),
|
||||
)
|
||||
|
||||
it.effect("matches webfetch rules in the form requested URLs are checked in", () =>
|
||||
Effect.gen(function* () {
|
||||
const permissions = yield* loadPermissions({
|
||||
permission: {
|
||||
webfetch: {
|
||||
"*": "allow",
|
||||
"https://example.com": "deny",
|
||||
"HTTPS://Internal.Corp/*": "deny",
|
||||
"https://пример.срб/*": "deny",
|
||||
"https://port.test:443/*": "deny",
|
||||
"https://prefix.test*": "deny",
|
||||
"https://*.Wild.test/*": "deny",
|
||||
"https://query.test?q=*": "deny",
|
||||
},
|
||||
},
|
||||
})
|
||||
const check = (url: string) => Permission.evaluate("webfetch", new URL(url).href, permissions).effect
|
||||
expect(check("https://example.com")).toBe("deny")
|
||||
expect(check("https://example.com/other")).toBe("allow")
|
||||
expect(check("https://internal.corp/docs")).toBe("deny")
|
||||
expect(check("https://пример.срб/docs")).toBe("deny")
|
||||
expect(check("https://port.test/docs")).toBe("deny")
|
||||
expect(check("https://prefix.test/docs")).toBe("deny")
|
||||
expect(check("https://a.wild.test/docs")).toBe("deny")
|
||||
expect(permissions).toContainEqual({ action: "webfetch", resource: "https://query.test?q=*", effect: "deny" })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("matches Windows paths against home-relative permissions", () =>
|
||||
Effect.gen(function* () {
|
||||
const permissions = yield* loadHomePermissions("C:\\Users\\test")
|
||||
|
|
@ -813,6 +841,33 @@ function sourceCases() {
|
|||
}
|
||||
|
||||
function loadHomePermissions(home: string) {
|
||||
return loadPermissions(
|
||||
{
|
||||
permission: {
|
||||
external_directory: {
|
||||
"~/p/**": "allow",
|
||||
"/some/~/path": "deny",
|
||||
"$HOMELESS/**": "deny",
|
||||
},
|
||||
bash: {
|
||||
"$HOME/private/**": "deny",
|
||||
},
|
||||
},
|
||||
agent: {
|
||||
build: {
|
||||
permission: {
|
||||
external_directory: {
|
||||
"$HOME/cache/**": "deny",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
home,
|
||||
)
|
||||
}
|
||||
|
||||
function loadPermissions(config: unknown, home = Global.make().home) {
|
||||
return Effect.gen(function* () {
|
||||
const agents = yield* Agent.Service
|
||||
const build = Agent.ID.make("build")
|
||||
|
|
@ -820,29 +875,7 @@ function loadHomePermissions(home: string) {
|
|||
const entries = [
|
||||
new Document({
|
||||
type: "document",
|
||||
info: decode(
|
||||
migrateV1({
|
||||
permission: {
|
||||
external_directory: {
|
||||
"~/p/**": "allow",
|
||||
"/some/~/path": "deny",
|
||||
"$HOMELESS/**": "deny",
|
||||
},
|
||||
bash: {
|
||||
"$HOME/private/**": "deny",
|
||||
},
|
||||
},
|
||||
agent: {
|
||||
build: {
|
||||
permission: {
|
||||
external_directory: {
|
||||
"$HOME/cache/**": "deny",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
),
|
||||
info: decode(migrateV1(config)),
|
||||
}),
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue