Repository navigation
Closed
Description
Summary
The bracket-path parser accepts any canonical array index below 2³²−1 and writes it directly into an array. A single key such as a[4294967294]=x therefore produces a sparse array with length === 4294967295. Decoding that tree costs time and memory proportional to the index. This affects schemas whose element type accepts undefined, and any schema decoded with errors: "all".
Reproduction
- Check out
main@ 95a3ab7, runpnpm install, and save the test below aspackages/effect/test/repro.test.ts. - From
packages/effect, run:pnpm vitest run test/repro.test.ts --silent=false --disable-console-intercept - The trigger is a URL query string or
FormDatakey whose bracket segment is a large numeric index, e.g.a[0]=x&a[10000000]=y.
import { it } from "vitest"
import { Schema, SchemaGetter } from "effect"
it("bracket-path index creates huge sparse arrays", () => {
const tree: any = SchemaGetter.makeTreeRecord([["a[4294967294]", "x"]])
console.log("makeTreeRecord a.length =", tree.a.length)
const lenient = Schema.fromURLSearchParams(Schema.Struct({ a: Schema.Array(Schema.UndefinedOr(Schema.String)) }))
for (const n of [1_000_000, 10_000_000]) {
const t0 = performance.now()
Schema.decodeUnknownExit(lenient)(new URLSearchParams(`a[0]=x&a[${n}]=y`))
console.log(`UndefinedOr element, default options, index ${n}: ${Math.round(performance.now() - t0)} ms`)
}
const strict = Schema.fromURLSearchParams(Schema.Struct({ a: Schema.Array(Schema.String) }))
const t0 = performance.now()
const exit = Schema.decodeUnknownExit(strict)(new URLSearchParams("a[0]=x&a[10000000]=y"), { errors: "all" })
console.log(`String element, errors: "all", index 10000000: ${Math.round(performance.now() - t0)} ms, error message ${String(exit).length} chars`)
}, 120_000)Actual behavior
makeTreeRecord a.length = 4294967295
UndefinedOr element, default options, index 1000000: 94 ms
UndefinedOr element, default options, index 10000000: 1106 ms
String element, errors: "all", index 10000000: 3405 ms, error message 358888896 chars
- A 22-byte key (
a[4294967294]=x) produces an array whoselengthis 4294967295. - With an element schema that accepts
undefined, decoding iterates every hole and succeeds. The time grows linearly with the index: about 0.1 s at 1M and about 1.1 s at 10M. - With
errors: "all", a plainSchema.Array(Schema.String)records one issue per hole. At index 10M that is about 3.4 s and a 359 MB error string.
Expected behavior
A key from untrusted input should not be able to allocate or iterate work proportional to an index it controls. fromURLSearchParams and fromFormData are meant for request query strings and form bodies. Comparable parsers bound this: for example, qs treats indices above arrayLimit (default 20) as object keys. Possible fixes:
- only treat a numeric segment as an array index when it is
<= current length(append or overwrite) or below a small limit, and keep it as an object key otherwise; or - compact holes, or reject sparse results.
Environment
- Version/commit:
effect4.0.3,main@ 95a3ab7 - OS: Ubuntu 22.04.5 LTS, Node v24.21.0
- Relevant configuration: default parse options, except
errors: "all"in the third case.
Evidence
bracketPathToTokensconverts any segment that passesArr.isCanonicalArrayIndexto a number. That check accepts any integer from 0 up to 2³²−2 (Array.ts:948-951).makeTreeRecordthen creates an array container and assignscur[token] = valuewithout comparing the index to the current length.- The reproduction output above is from the test in Reproduction.
Additional context
- Confirmed: the sparse array with
length === 4294967295, and the linear decode cost at 1M and 10M with both schema shapes shown above. - Not confirmed (extrapolated): that one request at the maximum index (about 4.3 billion) hangs the process or runs it out of memory. I extrapolated this from the linear timings and did not run it.
- Not affected: with a plain
Schema.Array(Schema.String)and the defaulterrors: "first", decoding fails quickly at the first hole.__proto__keys are already handled safely byInternalRecord.assignProperty. - Exposure: I didn't find any built-in
effect/httporHttpApicode path that appliesfromURLSearchParams/fromFormDatato requests automatically, so exposure depends on user code wiring these schemas to request input. - Related, but a different bug in the same function: SchemaGetter.makeTreeRecord mutates duplicate array-valued leaves #7640.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Add failing regression tests for sparse bracket-path array indices