mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
fix(codemode): copy extension results without trusting their own methods (#58031)
This commit is contained in:
committed by
GitHub
Unverified
parent
c81b2238c7
commit
d9be7138b7
@@ -23,6 +23,25 @@ import type { Eval } from "./runner.js"
|
||||
import { toPrimitiveString } from "./callback.js"
|
||||
import { formatValue } from "../stdlib/console.js"
|
||||
|
||||
/**
|
||||
* A host error's message as text. A host can store anything there; an object must not cross as itself, and turning it
|
||||
* into text would run its own methods.
|
||||
*/
|
||||
export const errorMessage = (error: Error): string => {
|
||||
const message: unknown = error.message
|
||||
if (typeof message === "string") return message
|
||||
if (message === undefined || typeof message === "function" || (typeof message === "object" && message !== null)) {
|
||||
return ""
|
||||
}
|
||||
return String(message)
|
||||
}
|
||||
|
||||
/** A host error's name as a program error type, read once so a getter cannot pass the check and then change. */
|
||||
export const errorType = (error: Error): ErrorType => {
|
||||
const name: unknown = error.name
|
||||
return typeof name === "string" && isErrorType(name) ? name : "Error"
|
||||
}
|
||||
|
||||
export const normalizeError = (error: unknown): Diagnostic => {
|
||||
if (error instanceof PendingThrow) {
|
||||
const location = error.node?.loc ? sourceLocation(error.node) : undefined
|
||||
@@ -63,7 +82,7 @@ export const normalizeError = (error: unknown): Diagnostic => {
|
||||
return { kind: "ExecutionFailure", message: `Uncaught: ${message}` }
|
||||
}
|
||||
|
||||
if (error instanceof RangeError && /call stack|recursion/i.test(error.message)) {
|
||||
if (error instanceof RangeError && /call stack|recursion/i.test(errorMessage(error))) {
|
||||
return {
|
||||
kind: "ExecutionFailure",
|
||||
message: "Execution exceeded the maximum nesting depth.",
|
||||
@@ -73,7 +92,7 @@ export const normalizeError = (error: unknown): Diagnostic => {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
kind: error.name === "SyntaxError" ? "ParseError" : "ExecutionFailure",
|
||||
message: error.message,
|
||||
message: errorMessage(error),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +112,7 @@ export const locate = (error: unknown, node?: AstNode): unknown => {
|
||||
return error
|
||||
}
|
||||
if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) {
|
||||
return new PendingThrow(isErrorType(error.name) ? error.name : "Error", error.message, node)
|
||||
return new PendingThrow(errorType(error), errorMessage(error), node)
|
||||
}
|
||||
return error
|
||||
}
|
||||
@@ -109,8 +128,10 @@ export const materialize = <R>(ctx: Interpreter<R>, thrown: unknown): Value => {
|
||||
}
|
||||
return thrown.value
|
||||
}
|
||||
const type = thrown instanceof Error && isErrorType(thrown.name) ? thrown.name : "Error"
|
||||
return createErrorValue(builtins[type], normalizeError(thrown).message)
|
||||
return createErrorValue(
|
||||
builtins[thrown instanceof Error ? errorType(thrown) : "Error"],
|
||||
normalizeError(thrown).message,
|
||||
)
|
||||
}
|
||||
|
||||
/** Error.prototype.toString: `name: message`, omitting whichever side is empty. */
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Effect } from "effect"
|
||||
import { type Extension, isAsync } from "../extension.js"
|
||||
import { type ExtensionInvocation, hooked } from "../tool-runtime.js"
|
||||
import { errorMessage, errorType } from "./errors.js"
|
||||
import type { Interpreter } from "./interpreter.js"
|
||||
import { createErrorValue, isErrorType } from "./intrinsics.js"
|
||||
import { MAX_VALUE_DEPTH } from "./limits.js"
|
||||
import { type AstNode, PendingThrow, Throw, typeError } from "./model.js"
|
||||
import { createErrorValue } from "./intrinsics.js"
|
||||
import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js"
|
||||
import { type AstNode, PendingThrow, rangeError, Throw, typeError } from "./model.js"
|
||||
import { fn } from "./native.js"
|
||||
import {
|
||||
define,
|
||||
@@ -79,18 +80,29 @@ export const extensionGlobals = <R>(
|
||||
if (typeof value === "function") return wrap(value, label)
|
||||
if (value !== null && typeof value === "object") {
|
||||
const next = (item: unknown, path: string) => fromHost(item, path, depth + 1, seen)
|
||||
if (value instanceof Date) return new DateObj(builtins.Date, value.getTime())
|
||||
// Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own
|
||||
// method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read.
|
||||
if (value instanceof Date) return new DateObj(builtins.Date, Date.prototype.getTime.call(value))
|
||||
if (value instanceof RegExp) return new RegExpObj(builtins.RegExp, value.source, value.flags)
|
||||
if (value instanceof Uint8Array) return new Bytes(builtins.Uint8Array, new Uint8Array(value))
|
||||
if (value instanceof ArrayBuffer) return new Bytes(builtins.Uint8Array, new Uint8Array(value.slice(0)))
|
||||
if (value instanceof ArrayBuffer) {
|
||||
const view = new Uint8Array(value)
|
||||
// Only a real buffer has bytes to view; anything else would be read as array-like and lose them silently.
|
||||
if (view.buffer !== value)
|
||||
throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`)
|
||||
return new Bytes(builtins.Uint8Array, new Uint8Array(view))
|
||||
}
|
||||
if (value instanceof Error) {
|
||||
if (seen.has(value)) throw typeError(`${label} produced a circular value.`)
|
||||
seen.add(value)
|
||||
const copied = createErrorValue(builtins[isErrorType(value.name) ? value.name : "Error"], value.message)
|
||||
const copied = createErrorValue(builtins[errorType(value)], errorMessage(value))
|
||||
const fields = value as unknown as Record<string, unknown>
|
||||
for (const key of new Set(["cause", ...Object.keys(value)])) {
|
||||
if (uncrossed.has(key) || !(key in value) || typeof fields[key] === "function") continue
|
||||
const item = crossing(() => next(fields[key], `${label}.${key}`))
|
||||
if (uncrossed.has(key) || !(key in value)) continue
|
||||
// Read once: a getter could pass the check and then hand back a function.
|
||||
const field = fields[key]
|
||||
if (typeof field === "function") continue
|
||||
const item = crossing(() => next(field, `${label}.${key}`))
|
||||
if (item === left) continue
|
||||
define(copied, key, item, key === "cause" ? hidden : undefined)
|
||||
}
|
||||
@@ -115,12 +127,18 @@ export const extensionGlobals = <R>(
|
||||
if (seen.has(value)) throw typeError(`${label} produced a circular value.`)
|
||||
seen.add(value)
|
||||
if (Array.isArray(value)) {
|
||||
const copied = new Arr(
|
||||
builtins.Array,
|
||||
value.map((item, index) => next(item, `${label}[${index}]`)),
|
||||
)
|
||||
const length = value.length
|
||||
if (length > MAX_ARRAY_LENGTH) {
|
||||
throw rangeError(`${label} produced an array of ${length} items; at most ${MAX_ARRAY_LENGTH} can cross.`)
|
||||
}
|
||||
// Filled in order so the copy stays a dense array; holes stay holes, as a copy made with `map` would leave them.
|
||||
const items: Array<Value> = []
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
if (index in value) items[index] = next(value[index], `${label}[${index}]`)
|
||||
}
|
||||
items.length = length
|
||||
seen.delete(value)
|
||||
return copied
|
||||
return new Arr(builtins.Array, items)
|
||||
}
|
||||
const prototype = Object.getPrototypeOf(value)
|
||||
if (prototype === Object.prototype || prototype === null) {
|
||||
@@ -137,29 +155,39 @@ export const extensionGlobals = <R>(
|
||||
// the host function is called, so a call it denies never reaches the host. The host's result, or what it threw,
|
||||
// crosses out so the program catches a copy, and `extension.after` sees how the call ended. Functions inside
|
||||
// results are part of a value's API and skip the hooks.
|
||||
const wrap = (value: Function, label: string, describe?: (args: ReadonlyArray<unknown>) => ExtensionInvocation) =>
|
||||
fn(builtins, value.name, value.length, function* (_, values, site) {
|
||||
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`))
|
||||
const cross = (reason: unknown) => new Throw(fromHost(reason, label))
|
||||
const call = describe?.(args)
|
||||
const hooks = ctx.tools.hooks
|
||||
const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross))
|
||||
const after = call && hooks["extension.after"]
|
||||
const settle = (effect: Effect.Effect<unknown, unknown, R>) =>
|
||||
(after ? hooked(call, undefined, after, effect) : effect).pipe(
|
||||
Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }),
|
||||
)
|
||||
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
||||
if (isAsync(value))
|
||||
return pending(
|
||||
Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => callHost<R>(value, args).effect))),
|
||||
site,
|
||||
)
|
||||
if (before) yield ctx.runner.blocking(before)
|
||||
const host = callHost<R>(value, args)
|
||||
if (host.async) return pending(settle(host.effect), site)
|
||||
return (yield ctx.runner.blocking(settle(host.effect))) as Value
|
||||
})
|
||||
const wrap = (value: Function, label: string, describe?: (args: ReadonlyArray<unknown>) => ExtensionInvocation) => {
|
||||
// The host function's own `name` and `length` can be anything, even getters: each is read once, and only text
|
||||
// and a count cross.
|
||||
const name: unknown = value.name
|
||||
const length: unknown = value.length
|
||||
return fn(
|
||||
builtins,
|
||||
typeof name === "string" ? name : "",
|
||||
typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0,
|
||||
function* (_, values, site) {
|
||||
const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`))
|
||||
const cross = (reason: unknown) => new Throw(fromHost(reason, label))
|
||||
const call = describe?.(args)
|
||||
const hooks = ctx.tools.hooks
|
||||
const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross))
|
||||
const after = call && hooks["extension.after"]
|
||||
const settle = (effect: Effect.Effect<unknown, unknown, R>) =>
|
||||
(after ? hooked(call, undefined, after, effect) : effect).pipe(
|
||||
Effect.mapBoth({ onFailure: cross, onSuccess: (result) => fromHost(result, label) }),
|
||||
)
|
||||
// A denied call fails the way the global would: an async global rejects its promise, a sync one throws.
|
||||
if (isAsync(value))
|
||||
return pending(
|
||||
Effect.andThen(before ?? Effect.void, settle(Effect.suspend(() => callHost<R>(value, args).effect))),
|
||||
site,
|
||||
)
|
||||
if (before) yield ctx.runner.blocking(before)
|
||||
const host = callHost<R>(value, args)
|
||||
if (host.async) return pending(settle(host.effect), site)
|
||||
return (yield ctx.runner.blocking(settle(host.effect))) as Value
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// An asynchronous host call: the program holds a promise that settles when the host's work does.
|
||||
const pending = (work: Effect.Effect<Value, Throw, R>, site: AstNode | undefined) => {
|
||||
@@ -199,10 +227,11 @@ const callHost = <R>(value: Function, args: ReadonlyArray<unknown>) => {
|
||||
|
||||
/**
|
||||
* An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`,
|
||||
* `syscall`, and `path`. `stack` stays on its own side, and no field may shadow an Error method. A field that cannot
|
||||
* cross (a socket, a handle, a function) is left behind so the error itself always arrives.
|
||||
* `syscall`, and `path`. `stack` stays on its own side, `message` crosses once as text, and no field may shadow an
|
||||
* Error method. A field that cannot cross (a socket, a handle, a function) is left behind so the error itself always
|
||||
* arrives.
|
||||
*/
|
||||
const uncrossed = new Set(["stack", "constructor", "toString", "__proto__"])
|
||||
const uncrossed = new Set(["stack", "message", "constructor", "toString", "__proto__"])
|
||||
const left = Symbol("left behind")
|
||||
const crossing = <T>(convert: () => T): T | typeof left => {
|
||||
try {
|
||||
@@ -233,5 +262,6 @@ const isPrimitive = (value: unknown): value is string | number | boolean | null
|
||||
const describeHost = (value: unknown): string => {
|
||||
if (typeof value !== "object" || value === null) return `a ${typeof value}`
|
||||
const name = (value as { constructor?: { name?: string } }).constructor?.name
|
||||
return name === undefined || name === "" ? "an object" : `a ${name}`
|
||||
if (name === undefined || name === "") return "an object"
|
||||
return `${/^[AEIO]/.test(name) ? "an" : "a"} ${name}`
|
||||
}
|
||||
|
||||
@@ -250,7 +250,164 @@ describe("values are converted at the boundary, never shared", () => {
|
||||
const target = CodeMode.make({
|
||||
extensions: [Extension.make({ name: "odd", globals: { detached: () => new Other() } })],
|
||||
})
|
||||
expect((await failure(`detached()`, target)).message).toContain("produced a Other, which the program cannot hold")
|
||||
expect((await failure(`detached()`, target)).message).toContain("produced an Other, which the program cannot hold")
|
||||
})
|
||||
})
|
||||
|
||||
describe("host values with overridden methods still cross as copies", () => {
|
||||
const returning = (sample: unknown) =>
|
||||
CodeMode.make({ extensions: [Extension.make({ name: "odd", globals: { sample: () => sample } })] })
|
||||
|
||||
// A host object whose own `toString` runs when converted to text, counting the conversions.
|
||||
const textual = () => {
|
||||
const counter = { conversions: 0 }
|
||||
const object = {
|
||||
toString() {
|
||||
counter.conversions += 1
|
||||
return "host text"
|
||||
},
|
||||
}
|
||||
return { counter, object }
|
||||
}
|
||||
|
||||
test("an array whose map returns itself is still copied by index, holes and all", async () => {
|
||||
const host = [1, , 3, ,]
|
||||
Object.defineProperty(host, "map", { value: () => host })
|
||||
expect(
|
||||
await value(`const x = sample(); x[0] = 99; return [x.length, 1 in x, x[2], 3 in x]`, returning(host)),
|
||||
).toEqual([4, false, 3, false])
|
||||
expect(host[0]).toBe(1)
|
||||
})
|
||||
|
||||
test("an array whose species hands back a kept array is still copied", async () => {
|
||||
const kept = [1, 2]
|
||||
const host = [1, 2]
|
||||
Object.defineProperty(host, "constructor", {
|
||||
value: {
|
||||
[Symbol.species]: function () {
|
||||
return kept
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(await value(`const x = sample(); x[0] = 99; return x`, returning(host))).toEqual([99, 2])
|
||||
expect(kept).toEqual([1, 2])
|
||||
expect(host).toEqual([1, 2])
|
||||
})
|
||||
|
||||
test("an array longer than a program may hold is refused with a clear error", async () => {
|
||||
const host: Array<number> = []
|
||||
host.length = 2 ** 32 - 1
|
||||
expect(
|
||||
await value(`try { sample(); return "crossed" } catch (e) { return [e.name, e.message] }`, returning(host)),
|
||||
).toEqual(["RangeError", "sample produced an array of 4294967295 items; at most 10000000 can cross."])
|
||||
})
|
||||
|
||||
test("an ArrayBuffer whose species hands back a kept buffer is still copied", async () => {
|
||||
const kept = new ArrayBuffer(4)
|
||||
const host = new ArrayBuffer(4)
|
||||
new Uint8Array(host).set([5, 6])
|
||||
Object.defineProperty(host, "constructor", { value: { [Symbol.species]: () => kept } })
|
||||
expect(await value(`const x = sample(); x[0] = 99; return [x[0], x[1]]`, returning(host))).toEqual([99, 6])
|
||||
expect([...new Uint8Array(kept)]).toEqual([0, 0, 0, 0])
|
||||
expect([...new Uint8Array(host)]).toEqual([5, 6, 0, 0])
|
||||
})
|
||||
|
||||
test("something that only claims to be an ArrayBuffer is refused rather than emptied", async () => {
|
||||
const buffer = new ArrayBuffer(4)
|
||||
expect(
|
||||
await value(
|
||||
`try { sample(); return "crossed" } catch (e) { return e.message.includes("cannot hold") }`,
|
||||
returning(new Proxy(buffer, {})),
|
||||
),
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
test("an ArrayBuffer whose slice returns itself is still copied", async () => {
|
||||
const host = new ArrayBuffer(2)
|
||||
new Uint8Array(host).set([1, 2])
|
||||
Object.defineProperty(host, "slice", { value: () => host })
|
||||
expect(await value(`const x = sample(); x[0] = 99; return x[0]`, returning(host))).toBe(99)
|
||||
expect([...new Uint8Array(host)]).toEqual([1, 2])
|
||||
})
|
||||
|
||||
test("a Date whose getTime is overridden crosses with its real time", async () => {
|
||||
const { counter, object } = textual()
|
||||
const host = new Date(0)
|
||||
Object.defineProperty(host, "getTime", { value: () => object })
|
||||
expect(await value(`return sample().getTime()`, returning(host))).toBe(0)
|
||||
expect(counter.conversions).toBe(0)
|
||||
})
|
||||
|
||||
test("an Error whose message is an object crosses without it", async () => {
|
||||
const { counter, object } = textual()
|
||||
const host = new Error("x")
|
||||
Object.defineProperty(host, "message", { value: object })
|
||||
expect(await value(`const e = sample(); return [typeof e.message, e.message]`, returning(host))).toEqual([
|
||||
"string",
|
||||
"",
|
||||
])
|
||||
expect(counter.conversions).toBe(0)
|
||||
})
|
||||
|
||||
test("an Error message that is undefined or own and enumerable crosses as text", async () => {
|
||||
const missing = new Error("x")
|
||||
Object.defineProperty(missing, "message", { value: undefined })
|
||||
expect(await value(`const e = sample(); return [e.message, String(e)]`, returning(missing))).toEqual(["", "Error"])
|
||||
const own = new Error("x")
|
||||
Object.defineProperty(own, "message", { value: { a: 1 }, enumerable: true })
|
||||
expect(await value(`return typeof sample().message`, returning(own))).toBe("string")
|
||||
})
|
||||
|
||||
test("an Error name that changes between reads cannot reach the host Object prototype", async () => {
|
||||
let reads = 0
|
||||
const host = new Error("x")
|
||||
Object.defineProperty(host, "name", { get: () => (reads++ === 0 ? "TypeError" : "constructor") })
|
||||
expect(
|
||||
await value(
|
||||
`const e = sample(); return [Object.getPrototypeOf(e) === Error.prototype || e instanceof TypeError, typeof Object.getPrototypeOf(e)]`,
|
||||
returning(host),
|
||||
),
|
||||
).toEqual([true, "object"])
|
||||
})
|
||||
|
||||
test("a host error with an object message thrown while copying is caught with text", async () => {
|
||||
const { counter, object } = textual()
|
||||
const thrown = new Error("x")
|
||||
Object.defineProperty(thrown, "message", { value: object })
|
||||
const host = {
|
||||
get field() {
|
||||
throw thrown
|
||||
},
|
||||
}
|
||||
expect(
|
||||
await value(
|
||||
`try { sample(); return "crossed" } catch (e) { return [typeof e.message, e.message] }`,
|
||||
returning(host),
|
||||
),
|
||||
).toEqual(["string", ""])
|
||||
expect(counter.conversions).toBe(0)
|
||||
})
|
||||
|
||||
test("a function whose name and length change between reads crosses with text and a count", async () => {
|
||||
const { counter, object } = textual()
|
||||
const host = () => null
|
||||
let names = 0
|
||||
let lengths = 0
|
||||
Object.defineProperty(host, "name", { get: () => (names++ === 0 ? "safe" : object) })
|
||||
Object.defineProperty(host, "length", { get: () => (lengths++ < 2 ? 1 : object) })
|
||||
expect(
|
||||
await value(`const f = sample(); return [typeof f.name, f.name, typeof f.length, f.length]`, returning(host)),
|
||||
).toEqual(["string", "safe", "number", 1])
|
||||
expect(counter.conversions).toBe(0)
|
||||
})
|
||||
|
||||
test("a function whose name and length are objects crosses with neither", async () => {
|
||||
const { counter, object } = textual()
|
||||
const host = () => null
|
||||
Object.defineProperty(host, "name", { value: object })
|
||||
Object.defineProperty(host, "length", { value: object })
|
||||
expect(await value(`const f = sample(); return [f.name, f.length]`, returning(host))).toEqual(["", 0])
|
||||
expect(counter.conversions).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user