fix(codemode): bound what one built-in can build with string, array, and promise caps (#49019)

This commit is contained in:
Aiden Cline
2026-09-14 12:54:02 -05:00
committed by GitHub
Unverified
parent 75f046212f
commit 76594882b4
10 changed files with 169 additions and 31 deletions
+5
View File
@@ -28,6 +28,11 @@ ultimate source of truth. Upstream test262 files run verbatim from `test/test262
- [x] The global `search(...)` built-in: synchronous tool discovery that counts as an admitted tool call and is
shadowable by program declarations like other globals.
- [x] Cooperative timeout, an optional total tool-call limit, output bounding, and unrestricted tool-call concurrency.
- [x] The timeout fires between interpreter steps, so one built-in is bounded in what it may build: strings up to
2^24 characters (`repeat`, `pad*`, `concat`, `join`, `+`, template literals, `JSON.stringify`), arrays up to
10,000,000 elements (`Array(n)`, `length =`, `Array.from`, `split`, `matchAll`, `concat`, `flat`; below the JS
maximum of 2^32 - 1), and 10,000 pending promises at once. Exceeding one throws a `RangeError`. A single regular
expression match can still run long on a pathological pattern; the host regex engine has no interrupt hook.
- [ ] Strict-mode early errors: duplicate parameter names, `yield` as an identifier, and a trailing comma after a
rest parameter are accepted unless the program itself begins with `"use strict"`.
- [ ] Valid JavaScript rejected by TypeScript transpilation before interpretation, such as `in` inside a destructuring
@@ -0,0 +1,19 @@
import { rangeError } from "./model.js"
// The timeout only fires between interpreter steps, so a single built-in must not be able to materialize an
// unbounded value. These bound what one call may build; programs cannot reach such sizes any other way.
/** Longest string a built-in or operator may produce. */
export const MAX_STRING_LENGTH = 1 << 24
/** Longest array a built-in may create or grow to. */
export const MAX_ARRAY_LENGTH = 10_000_000
/** Most promises that may be pending at once. */
export const MAX_PENDING_PROMISES = 10_000
export const checkStringLength = (length: number): void => {
if (length > MAX_STRING_LENGTH) throw rangeError("Invalid string length")
}
export const checkArrayLength = (length: number): void => {
if (length > MAX_ARRAY_LENGTH) throw rangeError("Invalid array length")
}
+5 -3
View File
@@ -1,5 +1,6 @@
import type { BlockStatement, Expression, Pattern } from "acorn"
import type { Effect, Fiber } from "effect"
import { checkArrayLength } from "./limits.js"
import {
AsyncIteratorSymbol,
type Binding,
@@ -189,13 +190,13 @@ export const isWrapper = (
value instanceof ProgramURL ||
value instanceof ProgramURLSearchParams
const MAX_ARRAY_LENGTH = 4_294_967_295
const MAX_ARRAY_INDEX = 4_294_967_295
export const parseArrayIndex = (key: string | number): number | undefined => {
const property = String(key)
if (!/^(0|[1-9]\d*)$/.test(property)) return undefined
const index = Number(property)
return index < MAX_ARRAY_LENGTH ? index : undefined
return index < MAX_ARRAY_INDEX ? index : undefined
}
const canonical = (key: PropertyKey): string | symbol => (typeof key === "symbol" ? key : String(key))
@@ -257,7 +258,8 @@ const writeArray = (target: ProgramArray, name: string | symbol, value: unknown)
}
if (name !== "length") return undefined
const length = typeof value === "number" ? value : Number(value)
if (!Number.isInteger(length) || length < 0 || length > MAX_ARRAY_LENGTH) return false
if (!Number.isInteger(length) || length < 0) return false
checkArrayLength(length)
target.items.length = length
return true
}
@@ -1,6 +1,7 @@
import { Cause, Deferred, Effect, Exit, Fiber, Scope } from "effect"
import type { Diagnostic } from "../codemode.js"
import { CallSite, ProgramThrow, typeError } from "./model.js"
import { MAX_PENDING_PROMISES } from "./limits.js"
import { CallSite, ProgramThrow, rangeError, typeError } from "./model.js"
import {
Callable,
define,
@@ -50,6 +51,11 @@ export class PromiseRuntime<R> {
create(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<ProgramPromise, never, R> {
return Effect.flatMap(CallSite, (site) => {
if (this.active.size >= MAX_PENDING_PROMISES) {
throw rangeError(
`Too many pending promises (limit ${MAX_PENDING_PROMISES}); await promises before creating more.`,
)
}
// Allocate before forking so reruns get distinct IDs and diagnostics retain creation order.
const id = this.nextID++
const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site.node)))
+8 -2
View File
@@ -60,6 +60,7 @@ import {
typeError,
unsupportedSyntax,
} from "./model.js"
import { checkStringLength } from "./limits.js"
import { locate, materialize } from "./errors.js"
import type { Prototypes } from "./intrinsics.js"
import { globals, type Host } from "./globals.js"
@@ -1319,8 +1320,11 @@ class Frame<R> {
const l = coerceOperand(lhs)
const r = coerceOperand(rhs)
switch (operator) {
case "+":
return (l as string) + (r as string)
case "+": {
const sum = (l as string) + (r as string)
if (typeof sum === "string") checkStringLength(sum.length)
return sum
}
case "-":
return (l as number) - (r as number)
case "*":
@@ -1990,10 +1994,12 @@ class Frame<R> {
throw typeError("Invalid template literal quasi.", quasi)
}
output += quasi.value.cooked
checkStringLength(output.length)
if (index < expressions.length) {
const raw = yield* self.evaluateExpression(expressions[index])
output += coerceToString(toProgram(self.runtime.prototypes, raw, "Template interpolation"))
checkStringLength(output.length)
}
}
+18 -12
View File
@@ -1,5 +1,6 @@
import { Effect } from "effect"
import { constructor, type Method, methods, prototypeFrom, receiver } from "../interpreter/native.js"
import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../interpreter/limits.js"
import { invalidData, rangeError, typeError } from "../interpreter/model.js"
import { get, ProgramArray, ProgramGenerator, ProgramObject } from "../interpreter/objects.js"
import { describeValue, rejectCircularInsertion } from "../interpreter/references.js"
@@ -7,13 +8,11 @@ import { applyCollectionCallback, preserveConsumerError, type Runner } from "../
import { compareText } from "../tool-runtime.js"
import { coerceToNumber, coerceToString } from "./value.js"
const MAX_LENGTH = 4_294_967_295
const arrayLikeSource = (source: unknown): { readonly length: number; readonly source: ProgramObject } => {
if (source instanceof ProgramObject && typeof get(source, "length") === "number") {
const length = get(source, "length") as number
const normalized = Number.isNaN(length) || length <= 0 ? 0 : Math.trunc(length)
if (normalized > MAX_LENGTH) throw new RangeError("Invalid array length")
checkArrayLength(normalized)
return { length: normalized, source }
}
throw invalidData(
@@ -93,7 +92,7 @@ export const arrayGlobal = <R>(runner: Runner<R>) => {
if (args.length !== 1) return new ProgramArray(into, [...args])
const first = args[0]
if (typeof first !== "number") return new ProgramArray(into, [first])
if (!Number.isInteger(first) || first < 0 || first > MAX_LENGTH) throw rangeError("Invalid array length.")
if (!Number.isInteger(first) || first < 0 || first > MAX_ARRAY_LENGTH) throw rangeError("Invalid array length.")
// Sparse like JS: Array(3) has holes, and combinator loops already skip them.
return new ProgramArray(into, new Array(first))
}
@@ -145,7 +144,11 @@ export const arrayGlobal = <R>(runner: Runner<R>) => {
if (args.length > 1 || (args.length === 1 && typeof args[0] !== "string")) {
throw typeError("Array.join expects zero arguments or one string separator.")
}
return target.map((item) => coerceToString(item ?? "")).join(args.length === 0 ? "," : (args[0] as string))
const joined = target
.map((item) => coerceToString(item ?? ""))
.join(args.length === 0 ? "," : (args[0] as string))
checkStringLength(joined.length)
return joined
},
],
[
@@ -198,12 +201,13 @@ export const arrayGlobal = <R>(runner: Runner<R>) => {
[
"concat",
1,
(thisValue, args) =>
wrap(
self(thisValue, "concat").items.concat(
...args.map((item) => (item instanceof ProgramArray ? item.items : item)),
),
),
(thisValue, args) => {
const joined = self(thisValue, "concat").items.concat(
...args.map((item) => (item instanceof ProgramArray ? item.items : item)),
)
checkArrayLength(joined.length)
return wrap(joined)
},
],
[
"flat",
@@ -211,7 +215,9 @@ export const arrayGlobal = <R>(runner: Runner<R>) => {
(thisValue, args) => {
const flatten = (items: Array<unknown>, depth: number): Array<unknown> =>
items.flatMap((item) => (item instanceof ProgramArray && depth > 0 ? flatten(item.items, depth - 1) : [item]))
return wrap(flatten(self(thisValue, "flat").items, optNumber("flat", args[0], "depth") ?? 1))
const flattened = flatten(self(thisValue, "flat").items, optNumber("flat", args[0], "depth") ?? 1)
checkArrayLength(flattened.length)
return wrap(flattened)
},
],
[
+4 -1
View File
@@ -1,6 +1,7 @@
import { Effect } from "effect"
import { methods } from "../interpreter/native.js"
import { applyCollectionCallback, type Runner } from "../interpreter/runner.js"
import { checkStringLength } from "../interpreter/limits.js"
import { syntaxError, typeError } from "../interpreter/model.js"
import { typeofValue } from "../interpreter/references.js"
import { fromData, toData, toProgram } from "../data.js"
@@ -57,7 +58,9 @@ const stringify = <R>(runner: Runner<R>, args: Array<unknown>): Effect.Effect<un
.map(String)
: null
// A string cannot pollute, so __proto__ stays: JSON.stringify includes own __proto__ keys, like JS.
return Effect.succeed(JSON.stringify(toData(args[0], "JSON.stringify value", "json", false), properties, indent))
const text = JSON.stringify(toData(args[0], "JSON.stringify value", "json", false), properties, indent)
if (text !== undefined) checkStringLength(text.length)
return Effect.succeed(text)
}
// Validate up front; the replacer walk below reads the original value.
+29 -9
View File
@@ -1,6 +1,7 @@
import { Effect } from "effect"
import { toProgram } from "../data.js"
import { constructor, type Method, methods } from "../interpreter/native.js"
import { checkArrayLength, checkStringLength } from "../interpreter/limits.js"
import { invalidData, rangeError, typeError } from "../interpreter/model.js"
import { ProgramArray, ProgramPromise, ProgramRegExp, record } from "../interpreter/objects.js"
import { containsOpaqueReference, typeofValue } from "../interpreter/references.js"
@@ -176,8 +177,12 @@ export const stringGlobal = <R>(runner: Runner<R>) => {
if (args[0] === undefined) {
return wrap(requestedLimit !== undefined && requestedLimit >>> 0 === 0 ? [] : [value])
}
if (args[0] instanceof ProgramRegExp) return wrap(value.split(args[0].regex, requestedLimit))
return wrap(value.split(str("split", args, 0), requestedLimit === undefined ? undefined : requestedLimit >>> 0))
const parts =
args[0] instanceof ProgramRegExp
? value.split(args[0].regex, requestedLimit)
: value.split(str("split", args, 0), requestedLimit === undefined ? undefined : requestedLimit >>> 0)
checkArrayLength(parts.length)
return wrap(parts)
}),
simple("slice", 2, (value, args) => value.slice(optNum("slice", args, 0), optNum("slice", args, 1))),
simple("includes", 1, (value, args) => {
@@ -213,10 +218,12 @@ export const stringGlobal = <R>(runner: Runner<R>) => {
`String.matchAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.match for a single match.`,
)
}
return new ProgramArray(
protos.Array,
Array.from(value.matchAll(pattern), (match) => matchToValue(protos, match)),
)
const matches: Array<unknown> = []
for (const match of value.matchAll(pattern)) {
checkArrayLength(matches.length + 1)
matches.push(matchToValue(protos, match))
}
return new ProgramArray(protos.Array, matches)
}),
simple("search", 1, (value, args) => value.search(toHostRegex(args[0], "search"))),
simple("repeat", 1, (value, args) => {
@@ -224,10 +231,19 @@ export const stringGlobal = <R>(runner: Runner<R>) => {
if (!Number.isFinite(count) || count < 0) {
throw rangeError("String.repeat expects a finite non-negative count.")
}
checkStringLength(value.length * count)
return value.repeat(count)
}),
simple("padStart", 1, (value, args) => value.padStart(num("padStart", args, 0), optStr("padStart", args, 1))),
simple("padEnd", 1, (value, args) => value.padEnd(num("padEnd", args, 0), optStr("padEnd", args, 1))),
simple("padStart", 1, (value, args) => {
const length = num("padStart", args, 0)
checkStringLength(length)
return value.padStart(length, optStr("padStart", args, 1))
}),
simple("padEnd", 1, (value, args) => {
const length = num("padEnd", args, 0)
checkStringLength(length)
return value.padEnd(length, optStr("padEnd", args, 1))
}),
simple("charAt", 1, (value, args) => value.charAt(optNum("charAt", args, 0) ?? 0)),
simple("at", 1, (value, args) => value.at(optNum("at", args, 0) ?? 0)),
simple("substring", 2, (value, args) =>
@@ -238,7 +254,11 @@ export const stringGlobal = <R>(runner: Runner<R>) => {
simple("toWellFormed", 0, (value) => value.toWellFormed()),
simple("charCodeAt", 1, (value, args) => value.charCodeAt(optNum("charCodeAt", args, 0) ?? 0)),
simple("codePointAt", 1, (value, args) => value.codePointAt(optNum("codePointAt", args, 0) ?? 0)),
simple("concat", 1, (value, args) => value.concat(...args.map((_, index) => str("concat", args, index)))),
simple("concat", 1, (value, args) => {
const joined = value.concat(...args.map((_, index) => str("concat", args, index)))
checkStringLength(joined.length)
return joined
}),
])
return string
}
+3 -3
View File
@@ -126,9 +126,9 @@ describe("constructors callable without new, like JS", () => {
expect(await value(`return [7].map(Array)`)).toEqual([[7, 0, [7]]])
})
test("array length boundaries match JS", async () => {
expect(await value(`return Array(4294967295).length`)).toBe(4294967295)
const diagnostic = await error(`return Array(4294967296)`)
test("array length is bounded below the JS maximum, so one call cannot materialize an unbounded array", async () => {
expect(await value(`return Array(10000000).length`)).toBe(10_000_000)
const diagnostic = await error(`return Array(10000001)`)
expect(diagnostic.message).toContain("Invalid array length")
expect((await error(`try { Array(-1) } catch (e) { throw Error(e.name) }`)).message).toContain("RangeError")
})
+71
View File
@@ -0,0 +1,71 @@
import { describe, expect, test } from "bun:test"
import { Effect } from "effect"
import { CodeMode } from "../src/index.js"
import { MAX_ARRAY_LENGTH, MAX_PENDING_PROMISES, MAX_STRING_LENGTH } from "../src/interpreter/limits.js"
const runtime = CodeMode.make({ tools: {} })
const value = async (code: string) => {
const result = await Effect.runPromise(runtime.execute(code))
if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
return result.value
}
const failure = async (code: string) => {
const result = await Effect.runPromise(runtime.execute(code))
if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
return result.error.message
}
const S = MAX_STRING_LENGTH
const A = MAX_ARRAY_LENGTH
describe("one built-in cannot build an unbounded value", () => {
test("strings: repeat, pad, concat, join, +, template, JSON.stringify", async () => {
expect(await value(`return "x".repeat(${S}).length`)).toBe(S)
for (const code of [
`"x".repeat(${S + 1})`,
`"x".padStart(${S + 1})`,
`"x".padEnd(${S + 1}, "y")`,
`"x".repeat(${S}).concat("y")`,
`["x".repeat(${S}), "y"].join("")`,
`"x".repeat(${S}) + "y"`,
`\`\${"x".repeat(${S})}y\``,
`JSON.stringify("x".repeat(${S}))`,
]) {
expect(await failure(`try { ${code} } catch (e) { throw Error(e.name + ": " + e.message) }`)).toContain(
"RangeError: Invalid string length",
)
}
})
test("arrays: constructor, length, Array.from, split, matchAll, concat, flat", async () => {
expect(await value(`return Array(${A}).length`)).toBe(A)
for (const code of [
`Array(${A + 1})`,
`const a = []; a.length = ${A + 1}`,
`Array.from({ length: ${A + 1} })`,
`"x".repeat(${S}).split("")`,
`"x".repeat(${A + 1}).matchAll(/x/g)`,
`Array(${A}).concat([1])`,
`[Array(${A}).fill(0), [1]].flat()`,
]) {
expect(await failure(`try { ${code} } catch (e) { throw Error(e.name + ": " + e.message) }`)).toContain(
"RangeError: Invalid array length",
)
}
})
test("promises: too many pending at once, while settled ones do not count", async () => {
const n = MAX_PENDING_PROMISES
expect(await value(`for (let i = 0; i < ${n * 2}; i++) Promise.resolve(i); return 1`)).toBe(1)
expect(
await failure(
`try { for (let i = 0; i <= ${n}; i++) new Promise(() => {}) } catch (e) { throw Error(e.name + ": " + e.message) }`,
),
).toContain("RangeError: Too many pending promises")
expect(await failure(`await Promise.all(Array(${n + 1}).fill(0).map(() => new Promise(() => {})))`)).toContain(
"Too many pending promises",
)
})
})