Prevent prototype pollution in Schema bracket-path decoding (#2602)

This commit is contained in:
Giulio Canti
2026-07-12 17:31:18 +02:00
committed by GitHub
parent 3c62b7b6dc
commit 01d00a3abf
3 changed files with 85 additions and 16 deletions
@@ -0,0 +1,5 @@
---
"effect": patch
---
Fix a bug where decoding bracket paths from FormData or URLSearchParams could mutate inherited object prototypes.
+22 -16
View File
@@ -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<string | number> {
.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<A>(
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)
}
})
})
@@ -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)
}
})
})
})