Skip to content

Bug: SchemaGetter.makeTreeRecord (used by Schema.fromURLSearchParams / Schema.fromFormData) turns one bracket-path key into an array of length up to 2³²−1 #9023

Closed

Description

@devin-ai-integration

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

  1. Check out main @ 95a3ab7, run pnpm install, and save the test below as packages/effect/test/repro.test.ts.
  2. From packages/effect, run: pnpm vitest run test/repro.test.ts --silent=false --disable-console-intercept
  3. The trigger is a URL query string or FormData key 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 whose length is 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 plain Schema.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: effect 4.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

  • bracketPathToTokens converts any segment that passes Arr.isCanonicalArrayIndex to a number. That check accepts any integer from 0 up to 2³²−2 (Array.ts:948-951).
  • makeTreeRecord then creates an array container and assigns cur[token] = value without 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 default errors: "first", decoding fails quickly at the first hole. __proto__ keys are already handled safely by InternalRecord.assignProperty.
  • Exposure: I didn't find any built-in effect/http or HttpApi code path that applies fromURLSearchParams/fromFormData to 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.

Written by Devin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions