diff --git a/src/runtime/deserializer.ts b/src/runtime/deserializer.ts index f82444b5..26b04890 100644 --- a/src/runtime/deserializer.ts +++ b/src/runtime/deserializer.ts @@ -1,6 +1,3 @@ -// Run `deno run -A npm:esbuild --minify src/runtime/deserializer.ts` to minify -// this file. It is embedded into src/server/deserializer_code.ts. - export const KEY = "_f"; interface Signal { @@ -18,6 +15,20 @@ function b64decode(b64: string): Uint8Array { return bytes; } +const INVALID_REFERENCE_ERROR = "Invalid reference"; + +function getPropertyFromPath(o: object, path: string[]): object { + for (const key of path) { + if (key === null) continue; + if (key !== "value" && !Object.hasOwn(o, key)) { + throw new Error(INVALID_REFERENCE_ERROR); + } + // deno-lint-ignore no-explicit-any + o = (o as any)[key]; + } + return o; +} + export function deserialize( str: string, signal?: (a: T) => Signal, @@ -44,19 +55,20 @@ export function deserialize( } return value; } - const { v, r } = JSON.parse(str, reviver); const references = (r ?? []) as [string[], ...string[][]][]; for (const [targetPath, ...refPaths] of references) { - const target = targetPath.reduce((o, k) => k === null ? o : o[k], v); + const target = getPropertyFromPath(v, targetPath); for (const refPath of refPaths) { - if (refPath.length === 0) throw new Error("Invalid reference"); + if (refPath.length === 0) throw new Error(INVALID_REFERENCE_ERROR); // set the reference to the target object - const parent = refPath.slice(0, -1).reduce( - (o, k) => k === null ? o : o[k], - v, - ); - parent[refPath[refPath.length - 1]!] = target; + const parent = getPropertyFromPath(v, refPath.slice(0, -1)); + const key = refPath[refPath.length - 1]!; + if (key !== "value" && !Object.hasOwn(parent, key)) { + throw new Error(INVALID_REFERENCE_ERROR); + } + // deno-lint-ignore no-explicit-any + (parent as any)[key] = target; } } return v; diff --git a/src/server/serializer_test.ts b/src/server/serializer_test.ts index eca080a4..6469e036 100644 --- a/src/server/serializer_test.ts +++ b/src/server/serializer_test.ts @@ -1,7 +1,12 @@ // deno-lint-ignore-file no-explicit-any import { serialize } from "./serializer.ts"; -import { assert, assertEquals, assertSnapshot } from "../../tests/deps.ts"; +import { + assert, + assertEquals, + assertSnapshot, + assertThrows, +} from "../../tests/deps.ts"; import { deserialize, KEY } from "../runtime/deserializer.ts"; import { signal } from "@preact/signals-core"; import { signal as signal130 } from "@preact/signals-core@1.3.0"; @@ -213,3 +218,16 @@ Deno.test("serializer - multiple reference in signal", async (t) => { assertEquals(deserialized.s.peek(), inner); assertEquals(deserialized.inner, inner); }); + +Deno.test("deserializer - no prototype pollution with manual input", () => { + const serialized = String.raw`{ + "v": { + "*": ["onerror"] + }, + "r": [ + [["*"], ["constructor", "prototype", "*"]] + ] + }`; + assertThrows(() => deserialize(serialized, signal)); + assertEquals({}.constructor.prototype["*"], undefined); +}); diff --git a/tests/deps.ts b/tests/deps.ts index a7dce31d..9d719935 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -14,6 +14,7 @@ export { assertNotMatch, assertRejects, assertStringIncludes, + assertThrows, } from "https://deno.land/std@0.211.0/assert/mod.ts"; export { assertSnapshot } from "https://deno.land/std@0.211.0/testing/snapshot.ts"; export {