test:pack:ts denies payload readers by identity and behaviour, not by name
Finding pack-denial-by-name: the package-internal readers (readWire, resolveDocumentFormatter, @msgpack decode) are loaded from the packed dist by path and searched by identity through every packed entry, including default and namespace members; every other exported function is shown a MessagePack map and must not return it decoded. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c40cc555e1
commit
27b9e2b315
1 changed files with 44 additions and 7 deletions
|
|
@ -84,14 +84,51 @@ const mesh = require("cultmesh-ts");
|
|||
assert.equal(typeof cache.defineDocumentType, "function");
|
||||
// The document owner is also an entry of its own, which the browser client imports without the Node-only cache.
|
||||
assert.equal(documentEntry.decodeDocumentPayload, cache.decodeDocumentPayload);
|
||||
// No entry exports a decoder or formatter that reads a payload around its schema: by shape, every packed export whose name
|
||||
// says it decodes, reads a wire or formats is enumerated, and the only one allowed is the schema-checked owner.
|
||||
// No entry exports a function that reads a payload around its schema, whatever it is called, wherever it is mounted: the
|
||||
// package-internal readers are loaded from the packed dist files by path and looked for by identity in every packed
|
||||
// entry (own names, 'default', and the members of any namespace or function export), and every exported function is
|
||||
// shown a MessagePack map and must not hand it back decoded. The only reader allowed is the schema-checked owner.
|
||||
const path = require("node:path");
|
||||
const msgpack = require("@msgpack/msgpack");
|
||||
const dist = path.dirname(require.resolve("@gamecult/cultcache-ts"));
|
||||
const internalReaders = new Map([
|
||||
["readWire", require(path.join(dist, "wire.js")).readWire],
|
||||
["resolveDocumentFormatter", require(path.join(dist, "document-formatter.js")).resolveDocumentFormatter],
|
||||
["@msgpack/msgpack decode", msgpack.decode],
|
||||
["@msgpack/msgpack decodeMulti", msgpack.decodeMulti],
|
||||
]);
|
||||
for (const [name, reader] of internalReaders) assert.equal(typeof reader, "function", name + " was found in the packed package");
|
||||
const inspection = require("@gamecult/cultcache-ts/inspection");
|
||||
const decoderShaped = (entry) => Object.keys(entry).filter((name) => /decode|formatter|readwire/i.test(name)).sort();
|
||||
for (const [entryName, entry] of Object.entries({ root: cache, codecs: cache.codecs, document: documentEntry, inspection })) {
|
||||
const found = decoderShaped(entry);
|
||||
const allowed = entryName === "root" || entryName === "document" ? ["decodeDocumentPayload"] : [];
|
||||
assert.deepEqual(found, allowed, "decoder-shaped exports of the " + entryName + " entry");
|
||||
const entries = { root: cache, codecs: cache.codecs, document: documentEntry, inspection };
|
||||
const probePayload = msgpack.encode({ probe: 7 });
|
||||
const exportsOf = (entry) => {
|
||||
const found = [];
|
||||
const seen = new Set();
|
||||
const visit = (value, where, depth) => {
|
||||
if ((typeof value !== "function" && (typeof value !== "object" || value === null)) || seen.has(value) || depth > 4) return;
|
||||
seen.add(value);
|
||||
found.push([where, value]);
|
||||
for (const key of Object.getOwnPropertyNames(value)) {
|
||||
if (typeof value === "function" && ["length", "name", "prototype", "caller", "arguments"].includes(key)) continue;
|
||||
let member;
|
||||
try { member = value[key]; } catch { continue; }
|
||||
visit(member, where + "." + key, depth + 1);
|
||||
}
|
||||
};
|
||||
for (const key of Object.getOwnPropertyNames(entry)) visit(entry[key], key, 0);
|
||||
return found;
|
||||
};
|
||||
for (const [entryName, entry] of Object.entries(entries)) {
|
||||
for (const [where, value] of exportsOf(entry)) {
|
||||
for (const [name, reader] of internalReaders) {
|
||||
assert.notEqual(value, reader, entryName + " entry exports " + name + " as " + where);
|
||||
}
|
||||
if (typeof value !== "function" || value === cache.decodeDocumentPayload) continue;
|
||||
let returned;
|
||||
try { returned = value(probePayload); } catch { continue; }
|
||||
assert.ok(!JSON.stringify(returned, (_key, member) => (member instanceof Map ? Object.fromEntries(member) : member))?.includes('"probe":7'),
|
||||
entryName + " entry exports " + where + ", which reads a payload without its schema");
|
||||
}
|
||||
}
|
||||
assert.equal(typeof documentEntry.checkPayloadDepth, "undefined");
|
||||
assert.equal(documentEntry.MAX_PAYLOAD_DEPTH, 256);
|
||||
|
|
|
|||
Loading…
Reference in a new issue