From 76594882b4a9a6a14dd4f99b515d4aba2d5e6f16 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 14 Sep 2026 12:54:02 -0500 Subject: [PATCH] fix(codemode): bound what one built-in can build with string, array, and promise caps (#49019) --- packages/codemode/interpreter-support.md | 5 ++ packages/codemode/src/interpreter/limits.ts | 19 +++++ packages/codemode/src/interpreter/objects.ts | 8 ++- packages/codemode/src/interpreter/promises.ts | 8 ++- packages/codemode/src/interpreter/runtime.ts | 10 ++- packages/codemode/src/stdlib/array.ts | 30 ++++---- packages/codemode/src/stdlib/json.ts | 5 +- packages/codemode/src/stdlib/string.ts | 38 +++++++--- packages/codemode/test/callbacks.test.ts | 6 +- packages/codemode/test/limits.test.ts | 71 +++++++++++++++++++ 10 files changed, 169 insertions(+), 31 deletions(-) create mode 100644 packages/codemode/src/interpreter/limits.ts create mode 100644 packages/codemode/test/limits.test.ts diff --git a/packages/codemode/interpreter-support.md b/packages/codemode/interpreter-support.md index fc7842e089d..11e3e3f23cc 100644 --- a/packages/codemode/interpreter-support.md +++ b/packages/codemode/interpreter-support.md @@ -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 diff --git a/packages/codemode/src/interpreter/limits.ts b/packages/codemode/src/interpreter/limits.ts new file mode 100644 index 00000000000..e1c9f2faaf5 --- /dev/null +++ b/packages/codemode/src/interpreter/limits.ts @@ -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") +} diff --git a/packages/codemode/src/interpreter/objects.ts b/packages/codemode/src/interpreter/objects.ts index 379d61d3dde..13cd9f4e84a 100644 --- a/packages/codemode/src/interpreter/objects.ts +++ b/packages/codemode/src/interpreter/objects.ts @@ -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 } diff --git a/packages/codemode/src/interpreter/promises.ts b/packages/codemode/src/interpreter/promises.ts index d909eab5943..1d79d84c0c6 100644 --- a/packages/codemode/src/interpreter/promises.ts +++ b/packages/codemode/src/interpreter/promises.ts @@ -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 { create(effect: Effect.Effect): Effect.Effect { 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))) diff --git a/packages/codemode/src/interpreter/runtime.ts b/packages/codemode/src/interpreter/runtime.ts index e9ccd9c4fda..ba835295ca7 100644 --- a/packages/codemode/src/interpreter/runtime.ts +++ b/packages/codemode/src/interpreter/runtime.ts @@ -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 { 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 { 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) } } diff --git a/packages/codemode/src/stdlib/array.ts b/packages/codemode/src/stdlib/array.ts index 78d251d8411..468e05d566b 100644 --- a/packages/codemode/src/stdlib/array.ts +++ b/packages/codemode/src/stdlib/array.ts @@ -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 = (runner: Runner) => { 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 = (runner: Runner) => { 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 = (runner: Runner) => { [ "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 = (runner: Runner) => { (thisValue, args) => { const flatten = (items: Array, depth: number): Array => 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) }, ], [ diff --git a/packages/codemode/src/stdlib/json.ts b/packages/codemode/src/stdlib/json.ts index 16c4e647831..54cbdf465b7 100644 --- a/packages/codemode/src/stdlib/json.ts +++ b/packages/codemode/src/stdlib/json.ts @@ -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 = (runner: Runner, args: Array): Effect.Effect(runner: Runner) => { 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 = (runner: Runner) => { `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 = [] + 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 = (runner: Runner) => { 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 = (runner: Runner) => { 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 } diff --git a/packages/codemode/test/callbacks.test.ts b/packages/codemode/test/callbacks.test.ts index 00e4f5a9aca..60bb8dacef1 100644 --- a/packages/codemode/test/callbacks.test.ts +++ b/packages/codemode/test/callbacks.test.ts @@ -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") }) diff --git a/packages/codemode/test/limits.test.ts b/packages/codemode/test/limits.test.ts new file mode 100644 index 00000000000..d9ca1c6acaa --- /dev/null +++ b/packages/codemode/test/limits.test.ts @@ -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", + ) + }) +})