fix(replay): keep __proto__ own properties and reject symbol-keyed results (#540)
This commit is contained in:
@@ -431,6 +431,11 @@ const projectComplexValue = (
|
||||
context: ProjectionContext,
|
||||
): unknown[] | Record<string, unknown> => {
|
||||
const descriptors = Object.getOwnPropertyDescriptors(candidate);
|
||||
// A symbol-keyed property cannot be represented in JSON, and dropping it
|
||||
// quietly would make the digest differ from a program that never had it.
|
||||
// Report it as unsupported instead, alongside accessors and prototypes.
|
||||
if (Object.getOwnPropertySymbols(candidate).length > 0)
|
||||
throw new TypeError("Symbol-keyed replay results are unavailable");
|
||||
const output: unknown[] | Record<string, unknown> = Array.isArray(candidate)
|
||||
? []
|
||||
: {};
|
||||
@@ -448,12 +453,24 @@ const projectComplexValue = (
|
||||
context,
|
||||
);
|
||||
} else {
|
||||
output[key] = projectValueRecursive(
|
||||
const value = projectValueRecursive(
|
||||
descriptor.value,
|
||||
depth + 1,
|
||||
ancestors,
|
||||
context,
|
||||
);
|
||||
// Plain assignment to `__proto__` mutates the output's prototype instead
|
||||
// of adding an own property, so the key would vanish from the digest.
|
||||
if (key === "__proto__") {
|
||||
Object.defineProperty(output, key, {
|
||||
value,
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
} else {
|
||||
output[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Array.isArray(candidate) && Array.isArray(output)) {
|
||||
|
||||
@@ -497,6 +497,53 @@ describe("deterministic replay Date compatibility", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects results the JSON projection cannot represent faithfully", async () => {
|
||||
const workerRequest = await request("parser.mjs", "esm", "default", []);
|
||||
workerRequest.left.modules[0] = {
|
||||
alias: "entry",
|
||||
format: "esm",
|
||||
dependencies: {},
|
||||
source: "export default function () { return { a: 1, [Symbol('s')]: 2 }; }",
|
||||
};
|
||||
const result = await runWorker(workerRequest);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
left: [
|
||||
{
|
||||
outcome: "serialization_error",
|
||||
exception: {
|
||||
message: "Symbol-keyed replay results are unavailable",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps an own __proto__ property instead of mutating the output", async () => {
|
||||
const workerRequest = await request("parser.mjs", "esm", "default", []);
|
||||
workerRequest.left.modules[0] = {
|
||||
alias: "entry",
|
||||
format: "esm",
|
||||
dependencies: {},
|
||||
source: `export default function () {
|
||||
const value = {};
|
||||
Object.defineProperty(value, "__proto__", {
|
||||
value: { injected: true },
|
||||
enumerable: true,
|
||||
});
|
||||
return value;
|
||||
}`,
|
||||
};
|
||||
const result = await runWorker(workerRequest);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
left: [
|
||||
{
|
||||
outcome: "return",
|
||||
value: { __proto__: { injected: true } },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
describe("replay determinism and builtin identity", () => {
|
||||
const evaluate = async (
|
||||
expression: string,
|
||||
|
||||
Reference in New Issue
Block a user