diff --git a/.changeset/fix-schema-bracket-prototype-pollution.md b/.changeset/fix-schema-bracket-prototype-pollution.md new file mode 100644 index 0000000000..f570f34ca0 --- /dev/null +++ b/.changeset/fix-schema-bracket-prototype-pollution.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Fix a bug where decoding bracket paths from FormData or URLSearchParams could mutate inherited object prototypes. diff --git a/packages/effect/src/SchemaGetter.ts b/packages/effect/src/SchemaGetter.ts index 74359f8f97..cae60cab33 100644 --- a/packages/effect/src/SchemaGetter.ts +++ b/packages/effect/src/SchemaGetter.ts @@ -14,6 +14,7 @@ import * as DateTime from "./DateTime.ts" import * as Effect from "./Effect.ts" import * as Encoding from "./Encoding.ts" +import * as internalRecord from "./internal/record.ts" import * as Option from "./Option.ts" import * as Pipeable from "./Pipeable.ts" import * as Predicate from "./Predicate.ts" @@ -1703,6 +1704,20 @@ function bracketPathToTokens(bracketPath: string): Array { .map((part) => (INDEX_REGEXP.test(part) ? globalThis.Number(part) : part)) } +function getOrCreateContainer( + self: any, + key: PropertyKey, + shouldBeArray: boolean +): any { + const current = Object.hasOwn(self, key) ? self[key] : undefined + if (current !== undefined) { + return current + } + const container = shouldBeArray ? [] : {} + internalRecord.set(self, key, container) + return container +} + /** * Builds a nested tree object from a list of bracket-path entries. * @@ -1765,33 +1780,24 @@ export function makeTreeRecord( const next = tokens[i + 1] const shouldBeArray = typeof next === "number" || next === "" const index = cur.length - - if (cur[index] === undefined) { - cur[index] = shouldBeArray ? [] : {} - } - - cur = cur[index] + cur = getOrCreateContainer(cur, index, shouldBeArray) } } else if (isLast) { // If we're setting a value at a path that already exists // convert it to an array to support multiple values for the same key - if (Array.isArray(cur[token])) { + const hasOwn = Object.hasOwn(cur, token) + if (hasOwn && Array.isArray(cur[token])) { cur[token].push(value) - } else if (Object.prototype.hasOwnProperty.call(cur, token)) { - cur[token] = [cur[token], value] + } else if (hasOwn) { + internalRecord.set(cur, token, [cur[token], value]) } else { - cur[token] = value + internalRecord.set(cur, token, value) } } else { const next = tokens[i + 1] // if next is a number OR "" (from []), we are building an array const shouldBeArray = typeof next === "number" || next === "" - - if (cur[token] === undefined) { - cur[token] = shouldBeArray ? [] : {} - } - - cur = cur[token] + cur = getOrCreateContainer(cur, token, shouldBeArray) } }) }) diff --git a/packages/effect/test/schema/SchemaGetter.test.ts b/packages/effect/test/schema/SchemaGetter.test.ts index 66b75ab1b7..8b4633129e 100644 --- a/packages/effect/test/schema/SchemaGetter.test.ts +++ b/packages/effect/test/schema/SchemaGetter.test.ts @@ -1,3 +1,4 @@ +import { assert } from "@effect/vitest" import { DateTime, Effect, Option, Result, SchemaGetter } from "effect" import { describe, it } from "vitest" import { assertSome, deepStrictEqual } from "../utils/assert.ts" @@ -29,6 +30,28 @@ describe("SchemaGetter", () => { await decoding("Tue, 27 Jan 2026 17:14:06 GMT", DateTime.makeUnsafe("2026-01-27T17:14:06.000Z")) }) + describe("makeTreeRecord", () => { + it("reinitializes own undefined values before descending", () => { + deepStrictEqual( + SchemaGetter.makeTreeRecord([ + ["a", undefined], + ["a[b]", 1] + ]), + { a: { b: 1 } } + ) + }) + + it("reinitializes own undefined values at numeric indexes", () => { + deepStrictEqual( + SchemaGetter.makeTreeRecord([ + ["a[0]", undefined], + ["a[0][b]", 1] + ]), + { a: [{ b: 1 }] } + ) + }) + }) + describe("decodeFormData / encodeFormData", () => { const decoding = makeAsserts(SchemaGetter.decodeFormData()) const encoding = makeAsserts(SchemaGetter.encodeFormData()) @@ -223,6 +246,23 @@ describe("SchemaGetter", () => { } await decoding(formData, object) }) + + it("stores __proto__ paths as own properties", async () => { + const pollutedKey = "__effectSchemaPolluted" + Reflect.deleteProperty(Object.prototype, pollutedKey) + try { + const formData = new FormData() + formData.append(`__proto__[${pollutedKey}]`, "yes") + await decoding(formData, { + ["__proto__"]: { + [pollutedKey]: "yes" + } + }) + assert.isFalse(Object.hasOwn(Object.prototype, pollutedKey)) + } finally { + Reflect.deleteProperty(Object.prototype, pollutedKey) + } + }) }) describe("decodeURLSearchParams / encodeURLSearchParams", () => { @@ -379,5 +419,23 @@ describe("SchemaGetter", () => { } await decoding(urlSearchParams, object) }) + + it("does not traverse inherited constructor paths", async () => { + const pollutedKey = "__effectSchemaPolluted" + Reflect.deleteProperty(Object.prototype, pollutedKey) + try { + const urlSearchParams = new URLSearchParams(`constructor[prototype][${pollutedKey}]=yes`) + await decoding(urlSearchParams, { + constructor: { + prototype: { + [pollutedKey]: "yes" + } + } + }) + assert.isFalse(Object.hasOwn(Object.prototype, pollutedKey)) + } finally { + Reflect.deleteProperty(Object.prototype, pollutedKey) + } + }) }) })