@eyeoverthink/wasm-deflate-vault 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +174 -0
- package/README.md +43 -3
- package/package.json +48 -6
- package/src/wasm-deflate-vault.d.ts +28 -0
- package/src/wasm-deflate-vault.js +859 -0
- package/test/vault.test.js +284 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import {
|
|
7
|
+
VERSION,
|
|
8
|
+
VAULT_MAGIC,
|
|
9
|
+
buildZipArchive,
|
|
10
|
+
inspectZipArchive,
|
|
11
|
+
buildFileMerkleTree,
|
|
12
|
+
getFileMerkleProof,
|
|
13
|
+
verifyFileMerkleProof,
|
|
14
|
+
packDeflateWasmContainer,
|
|
15
|
+
unpackAndVerifyWasmContainer,
|
|
16
|
+
findWasmDataSegmentByteOffset,
|
|
17
|
+
encodeBridgeCarrier,
|
|
18
|
+
decodeBridgeCarrier,
|
|
19
|
+
isSmallOrderKey,
|
|
20
|
+
publicKeyToHex,
|
|
21
|
+
} from "../src/wasm-deflate-vault.js";
|
|
22
|
+
|
|
23
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
24
|
+
|
|
25
|
+
const SAMPLE_FILES = {
|
|
26
|
+
"Main.java": "public class Main { public static void main(String[] args) { System.out.println(42); } }",
|
|
27
|
+
"LinkedList.java": "public class LinkedList<T> { Node<T> head; public int size() { return 0; } }",
|
|
28
|
+
"MerkleBridge.java": "public class MerkleBridge { public static String tag() { return \"WDV1\"; } }",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
test("Package parity: src/wasm-deflate-vault.js and docs/wasm-deflate-vault.js are byte-identical", (t) => {
|
|
32
|
+
assert.equal(VERSION, "2.0.0");
|
|
33
|
+
assert.equal(VAULT_MAGIC, 0x57445631);
|
|
34
|
+
const docsPath = path.join(__dirname, "../docs/wasm-deflate-vault.js");
|
|
35
|
+
if (!fs.existsSync(docsPath)) { t.skip("docs/ not included in published npm tarball"); return; }
|
|
36
|
+
const a = fs.readFileSync(path.join(__dirname, "../src/wasm-deflate-vault.js"), "utf8");
|
|
37
|
+
const b = fs.readFileSync(docsPath, "utf8");
|
|
38
|
+
assert.equal(a, b);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("Deflate-in-WASM packs zlib 78 9c bundle, verifies it without running it, & rejects 1-byte tamper", async () => {
|
|
42
|
+
const packed = await packDeflateWasmContainer(SAMPLE_FILES);
|
|
43
|
+
assert.equal(packed.manifest.compressed, true);
|
|
44
|
+
assert.equal(packed.manifest.zlibHeaderHex, "789c");
|
|
45
|
+
assert.equal(packed.wasmBytes[0], 0x00);
|
|
46
|
+
assert.equal(packed.wasmBytes[1], 0x61);
|
|
47
|
+
assert.equal(packed.wasmBytes[2], 0x73);
|
|
48
|
+
assert.equal(packed.wasmBytes[3], 0x6d);
|
|
49
|
+
|
|
50
|
+
const unpacked = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: packed.publicKeyHex });
|
|
51
|
+
assert.equal(unpacked.ok, true);
|
|
52
|
+
assert.deepEqual(unpacked.checks, { magicOk: true, sha256Ok: true, keyTrusted: true, signatureOk: true, merkleOk: true });
|
|
53
|
+
assert.deepEqual(unpacked.files, SAMPLE_FILES);
|
|
54
|
+
|
|
55
|
+
// Flip 1 byte in active Data segment payload -> fails SHA-256 check fail-closed
|
|
56
|
+
const dataOff = findWasmDataSegmentByteOffset(packed.wasmBytes);
|
|
57
|
+
assert.ok(dataOff > 0);
|
|
58
|
+
const mutated = new Uint8Array(packed.wasmBytes);
|
|
59
|
+
mutated[dataOff + 5] ^= 0xff;
|
|
60
|
+
const tampered = await unpackAndVerifyWasmContainer(mutated, { expectedPublicKeys: packed.publicKeyHex });
|
|
61
|
+
assert.equal(tampered.ok, false);
|
|
62
|
+
assert.equal(tampered.checks.sha256Ok, false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("Per-file RFC 6962 Merkle sibling proofs verify clean file & reject 1-char edit", async () => {
|
|
66
|
+
const tree = await buildFileMerkleTree(SAMPLE_FILES);
|
|
67
|
+
const proof = getFileMerkleProof(tree, "LinkedList.java");
|
|
68
|
+
const valid = await verifyFileMerkleProof({
|
|
69
|
+
filename: "LinkedList.java",
|
|
70
|
+
content: SAMPLE_FILES["LinkedList.java"],
|
|
71
|
+
...proof,
|
|
72
|
+
});
|
|
73
|
+
assert.equal(valid.ok, true);
|
|
74
|
+
|
|
75
|
+
const forged = await verifyFileMerkleProof({
|
|
76
|
+
filename: "LinkedList.java",
|
|
77
|
+
content: SAMPLE_FILES["LinkedList.java"] + " ",
|
|
78
|
+
...proof,
|
|
79
|
+
});
|
|
80
|
+
assert.equal(forged.ok, false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("PKZIP (0x04034b50 + CRC-32) builder/inspector & Variation-Selector Ed25519 bridge carrier", async () => {
|
|
84
|
+
const zip = buildZipArchive(SAMPLE_FILES);
|
|
85
|
+
assert.equal(zip[0], 0x50); // 'P'
|
|
86
|
+
assert.equal(zip[1], 0x4b); // 'K'
|
|
87
|
+
const inspected = inspectZipArchive(zip);
|
|
88
|
+
assert.equal(inspected.ok, true);
|
|
89
|
+
assert.equal(inspected.entryCount, 3);
|
|
90
|
+
assert.ok(inspected.entries.every((e) => e.crcValid));
|
|
91
|
+
|
|
92
|
+
// Mutate 1 byte in first filename (CD mismatch) and 1 byte in data (CRC-32 mismatch) -> both fail
|
|
93
|
+
const badNameZip = new Uint8Array(zip);
|
|
94
|
+
badNameZip[35] ^= 0x01;
|
|
95
|
+
assert.equal(inspectZipArchive(badNameZip).ok, false);
|
|
96
|
+
|
|
97
|
+
const badDataZip = new Uint8Array(zip);
|
|
98
|
+
badDataZip[60] ^= 0xff;
|
|
99
|
+
assert.equal(inspectZipArchive(badDataZip).ok, false);
|
|
100
|
+
|
|
101
|
+
// Signed manifest bridge over Unicode Variation Selectors
|
|
102
|
+
const packed = await packDeflateWasmContainer(SAMPLE_FILES);
|
|
103
|
+
const bridged = encodeBridgeCarrier(packed.envelope, "📦");
|
|
104
|
+
const verifiedBridge = await decodeBridgeCarrier(bridged.carrier, { expectedPublicKeys: packed.publicKeyHex });
|
|
105
|
+
assert.equal(verifiedBridge.ok, true);
|
|
106
|
+
assert.equal(verifiedBridge.signatureValid, true);
|
|
107
|
+
assert.equal(verifiedBridge.manifest.merkleRootHex, packed.manifest.merkleRootHex);
|
|
108
|
+
|
|
109
|
+
// Mutate middle variation selector -> fails verification cleanly
|
|
110
|
+
const chars = Array.from(bridged.carrier);
|
|
111
|
+
chars[25] = "\uFE00";
|
|
112
|
+
const brokenBridge = await decodeBridgeCarrier(chars.join(""), { expectedPublicKeys: packed.publicKeyHex });
|
|
113
|
+
assert.equal(brokenBridge.ok, false);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
// Regression tests for the 2026-12-04 review.
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
const uleb = (v) => { const o = []; do { let b = v & 0x7f; v >>>= 7; if (v) b |= 0x80; o.push(b); } while (v); return o; };
|
|
121
|
+
const SMALL_ORDER_SPKI_B64 = Buffer.concat([Buffer.from("302a300506032b6570032100", "hex"), Buffer.from("01" + "00".repeat(31), "hex")]).toString("base64");
|
|
122
|
+
const ZERO_SIG_B64 = Buffer.from("01" + "00".repeat(63), "hex").toString("base64");
|
|
123
|
+
|
|
124
|
+
function replaceManifest(wasm, envelope) {
|
|
125
|
+
// swap the trailing wdv.manifest custom section for one holding `envelope`
|
|
126
|
+
let p = 8; let start = -1; let end = -1;
|
|
127
|
+
while (p < wasm.length) { const s = p, id = wasm[p++]; let len = 0, sh = 0, b; do { b = wasm[p++]; len |= (b & 0x7f) << sh; sh += 7; } while (b & 0x80); if (id === 0) { start = s; end = p + len; } p += len; }
|
|
128
|
+
const body = [0x0c, ...Buffer.from("wdv.manifest"), ...Buffer.from(JSON.stringify(envelope))];
|
|
129
|
+
return Uint8Array.from([...wasm.subarray(0, start), 0, ...uleb(body.length), ...body, ...wasm.subarray(end)]);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
test("Review: a module with a looping start function is refused without being run", async () => {
|
|
133
|
+
// This module would never return if instantiated (start = `loop br 0 end`). The verifier must not run it.
|
|
134
|
+
const custom = [0x0c, ...Buffer.from("wdv.manifest"), ...Buffer.from(JSON.stringify({ manifest: { fileCount: 1 }, signatureB64: "", publicKeySpkiB64: "" }))];
|
|
135
|
+
const wasm = Uint8Array.from([
|
|
136
|
+
0, 0x61, 0x73, 0x6d, 1, 0, 0, 0,
|
|
137
|
+
1, 4, 1, 0x60, 0, 0,
|
|
138
|
+
3, 2, 1, 0,
|
|
139
|
+
8, 1, 0,
|
|
140
|
+
10, 9, 1, 7, 0, 0x03, 0x40, 0x0c, 0, 0x0b, 0x0b,
|
|
141
|
+
0, custom.length, ...custom,
|
|
142
|
+
]);
|
|
143
|
+
assert.equal(WebAssembly.validate(wasm), true);
|
|
144
|
+
const original = WebAssembly.instantiate;
|
|
145
|
+
let instantiated = false;
|
|
146
|
+
WebAssembly.instantiate = async (...a) => { instantiated = true; return original(...a); };
|
|
147
|
+
try {
|
|
148
|
+
const t0 = Date.now();
|
|
149
|
+
const r = await unpackAndVerifyWasmContainer(wasm, { expectedPublicKeys: "11".repeat(32) });
|
|
150
|
+
assert.equal(r.ok, false);
|
|
151
|
+
assert.ok(Date.now() - t0 < 2000);
|
|
152
|
+
} finally {
|
|
153
|
+
WebAssembly.instantiate = original;
|
|
154
|
+
}
|
|
155
|
+
assert.equal(instantiated, false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("Review: a vault is never compiled or instantiated, even when it verifies", async () => {
|
|
159
|
+
const packed = await packDeflateWasmContainer({ "a.txt": "hello" });
|
|
160
|
+
const orig = { i: WebAssembly.instantiate, c: WebAssembly.compile, M: WebAssembly.Module };
|
|
161
|
+
let touched = false;
|
|
162
|
+
WebAssembly.instantiate = async () => { touched = true; throw new Error("no"); };
|
|
163
|
+
WebAssembly.compile = async () => { touched = true; throw new Error("no"); };
|
|
164
|
+
try {
|
|
165
|
+
const r = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: packed.publicKeyHex });
|
|
166
|
+
assert.equal(r.ok, true);
|
|
167
|
+
} finally {
|
|
168
|
+
WebAssembly.instantiate = orig.i; WebAssembly.compile = orig.c;
|
|
169
|
+
}
|
|
170
|
+
assert.equal(touched, false);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("Review: extra sections (e.g. a start function) make a vault non-canonical", async () => {
|
|
174
|
+
const packed = await packDeflateWasmContainer({ "a.txt": "hello" });
|
|
175
|
+
const w = packed.wasmBytes;
|
|
176
|
+
// insert an extra custom section right after the header
|
|
177
|
+
const extra = [0, 3, 1, 0x78, 0x00];
|
|
178
|
+
const mutated = Uint8Array.from([...w.subarray(0, 8), ...extra, ...w.subarray(8)]);
|
|
179
|
+
const r = await unpackAndVerifyWasmContainer(mutated, { expectedPublicKeys: packed.publicKeyHex });
|
|
180
|
+
assert.equal(r.ok, false);
|
|
181
|
+
assert.equal(r.error, "NON_CANONICAL_VAULT_MODULE");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("Review: no pinned key, a different key, or the small-order key is never trusted", async () => {
|
|
185
|
+
const signer = await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"]);
|
|
186
|
+
const other = await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"]);
|
|
187
|
+
const signerHex = await publicKeyToHex(signer.publicKey);
|
|
188
|
+
const otherHex = await publicKeyToHex(other.publicKey);
|
|
189
|
+
const packed = await packDeflateWasmContainer({ "invoice.txt": "pay alice" }, { keyPair: signer });
|
|
190
|
+
assert.equal(packed.publicKeyHex, signerHex);
|
|
191
|
+
|
|
192
|
+
const none = await unpackAndVerifyWasmContainer(packed.wasmBytes);
|
|
193
|
+
assert.equal(none.ok, false);
|
|
194
|
+
assert.equal(none.error, "NO_EXPECTED_PUBLIC_KEYS");
|
|
195
|
+
assert.equal(none.files, undefined);
|
|
196
|
+
|
|
197
|
+
const wrong = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: [otherHex] });
|
|
198
|
+
assert.equal(wrong.ok, false);
|
|
199
|
+
assert.equal(wrong.error, "SIGNER_NOT_TRUSTED");
|
|
200
|
+
|
|
201
|
+
const right = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: [otherHex, signerHex.toUpperCase()] });
|
|
202
|
+
assert.equal(right.ok, true);
|
|
203
|
+
|
|
204
|
+
// Keyless forgery: small-order key, R = identity, S = 0, edited manifest.
|
|
205
|
+
const forgedEnv = {
|
|
206
|
+
manifest: { ...packed.manifest, issuedAt: "1999-01-01T00:00:00Z", note: "no private key used" },
|
|
207
|
+
signatureB64: ZERO_SIG_B64,
|
|
208
|
+
publicKeySpkiB64: SMALL_ORDER_SPKI_B64,
|
|
209
|
+
edFxTag: "ED-FX:FORGED",
|
|
210
|
+
};
|
|
211
|
+
const forged = replaceManifest(packed.wasmBytes, forgedEnv);
|
|
212
|
+
const smallHex = "01" + "00".repeat(31);
|
|
213
|
+
assert.equal(isSmallOrderKey(smallHex), true);
|
|
214
|
+
for (const keys of [undefined, signerHex, smallHex, [smallHex, signerHex]]) {
|
|
215
|
+
const r = await unpackAndVerifyWasmContainer(forged, { expectedPublicKeys: keys });
|
|
216
|
+
assert.equal(r.ok, false);
|
|
217
|
+
assert.equal(r.checks.signatureOk, false);
|
|
218
|
+
}
|
|
219
|
+
const bridge = await decodeBridgeCarrier(encodeBridgeCarrier(forgedEnv).carrier, { expectedPublicKeys: smallHex });
|
|
220
|
+
assert.equal(bridge.ok, false);
|
|
221
|
+
assert.equal(bridge.error, "WEAK_SIGNER_KEY");
|
|
222
|
+
|
|
223
|
+
// A real signature from an unpinned key does not pass the bridge either.
|
|
224
|
+
const b2 = await decodeBridgeCarrier(encodeBridgeCarrier(packed.envelope).carrier);
|
|
225
|
+
assert.equal(b2.ok, false);
|
|
226
|
+
assert.equal(b2.signatureValid, false);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("Review: isSmallOrderKey covers the blocklist and the sign-bit variants", () => {
|
|
230
|
+
assert.equal(isSmallOrderKey("00".repeat(32)), true);
|
|
231
|
+
assert.equal(isSmallOrderKey("01" + "00".repeat(31)), true);
|
|
232
|
+
assert.equal(isSmallOrderKey("01" + "00".repeat(30) + "80"), true);
|
|
233
|
+
assert.equal(isSmallOrderKey("ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), true);
|
|
234
|
+
assert.equal(isSmallOrderKey("not-hex"), true);
|
|
235
|
+
assert.equal(isSmallOrderKey("3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"), false);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("Review: binary (Uint8Array) files round-trip exactly", async () => {
|
|
239
|
+
const bin = new Uint8Array(256);
|
|
240
|
+
for (let i = 0; i < 256; i++) bin[i] = i;
|
|
241
|
+
const files = { "bin.dat": bin, "note.txt": "text", "empty.bin": new Uint8Array(0) };
|
|
242
|
+
const packed = await packDeflateWasmContainer(files);
|
|
243
|
+
const r = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: packed.publicKeyHex });
|
|
244
|
+
assert.equal(r.ok, true);
|
|
245
|
+
assert.equal(r.checks.merkleOk, true);
|
|
246
|
+
assert.ok(r.files["bin.dat"] instanceof Uint8Array);
|
|
247
|
+
assert.deepEqual(Array.from(r.files["bin.dat"]), Array.from(bin));
|
|
248
|
+
assert.equal(r.files["note.txt"], "text");
|
|
249
|
+
assert.equal(r.files["empty.bin"].length, 0);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("Review: findWasmDataSegmentByteOffset points at the payload for every size", async () => {
|
|
253
|
+
for (const n of [1, 10, 60, 200, 8000, 40000]) {
|
|
254
|
+
const bytes = new Uint8Array(n);
|
|
255
|
+
crypto.getRandomValues(bytes.subarray(0, Math.min(n, 65536)));
|
|
256
|
+
const packed = await packDeflateWasmContainer({ "f.bin": bytes });
|
|
257
|
+
const off = findWasmDataSegmentByteOffset(packed.wasmBytes);
|
|
258
|
+
const payload = packed.wasmBytes.subarray(off, off + packed.manifest.payloadLen);
|
|
259
|
+
assert.equal(Buffer.from(payload.subarray(0, 2)).toString("hex"), packed.manifest.zlibHeaderHex, `n=${n}`);
|
|
260
|
+
const h = Buffer.from(await crypto.subtle.digest("SHA-256", payload)).toString("hex");
|
|
261
|
+
assert.equal(h, packed.manifest.payloadSha256Hex, `n=${n}`);
|
|
262
|
+
}
|
|
263
|
+
assert.equal(findWasmDataSegmentByteOffset(new Uint8Array([1, 2, 3])), -1);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("Review: inflating is capped", async () => {
|
|
267
|
+
const packed = await packDeflateWasmContainer({ "big.txt": "A".repeat(2 * 1024 * 1024) });
|
|
268
|
+
const r = await unpackAndVerifyWasmContainer(packed.wasmBytes, { expectedPublicKeys: packed.publicKeyHex, maxInflatedBytes: 1024 * 1024 });
|
|
269
|
+
assert.equal(r.ok, false);
|
|
270
|
+
assert.equal(r.error, "INFLATED_PAYLOAD_TOO_LARGE");
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("Review: bridge carrier ignores variation selectors outside the payload run", async () => {
|
|
274
|
+
const packed = await packDeflateWasmContainer({ "a.txt": "hi" });
|
|
275
|
+
const c = encodeBridgeCarrier(packed.envelope).carrier;
|
|
276
|
+
const r = await decodeBridgeCarrier("I \u2764\ufe0f this " + c, { expectedPublicKeys: packed.publicKeyHex });
|
|
277
|
+
assert.equal(r.ok, true);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("Review: buildZipArchive handles 70,000 entries", () => {
|
|
281
|
+
const many = Object.fromEntries(Array.from({ length: 70000 }, (_, i) => [`f${i}`, ""]));
|
|
282
|
+
const zip = buildZipArchive(many);
|
|
283
|
+
assert.equal(zip[0], 0x50);
|
|
284
|
+
});
|