fix: prototype pollution in deserializer (#2255)
This is not exploitable in practice unless a user maliciously crafts serialized values in __FRSH_STATE, because `serializer()` never outputs serialized representation that would be vulnerable to prototype pollution. But hey, defense in depth.
This commit is contained in:
+23
-11
@@ -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<T> {
|
||||
@@ -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?: <T>(a: T) => Signal<T>,
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user