@sanning/proof 0.11.1 → 0.13.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.
Files changed (51) hide show
  1. package/README.md +43 -8
  2. package/dist/agent-proof.d.ts +2 -2
  3. package/dist/agent-proof.js +4 -4
  4. package/dist/case-file.d.ts +126 -0
  5. package/dist/case-file.js +349 -0
  6. package/dist/case.d.ts +199 -43
  7. package/dist/case.js +522 -146
  8. package/dist/cli.d.ts +132 -1
  9. package/dist/cli.js +1140 -96
  10. package/dist/crypto.d.ts +1 -1
  11. package/dist/crypto.js +5 -1
  12. package/dist/declaration.d.ts +58 -52
  13. package/dist/declaration.js +256 -33
  14. package/dist/evidence.d.ts +2 -0
  15. package/dist/evidence.js +134 -19
  16. package/dist/exit-codes.d.ts +17 -0
  17. package/dist/exit-codes.js +50 -0
  18. package/dist/grading.d.ts +28 -13
  19. package/dist/grading.js +90 -31
  20. package/dist/handoff.d.ts +16 -1
  21. package/dist/handoff.js +91 -16
  22. package/dist/index.d.ts +10 -7
  23. package/dist/index.js +15 -7
  24. package/dist/message-parts.d.ts +11 -9
  25. package/dist/message-parts.js +11 -9
  26. package/dist/types.d.ts +1 -0
  27. package/dist/verifier.d.ts +9 -2
  28. package/dist/verifier.js +28 -12
  29. package/dist/zip-reader.d.ts +50 -0
  30. package/dist/zip-reader.js +220 -0
  31. package/package.json +2 -2
  32. package/src/agent-proof.ts +4 -4
  33. package/src/case-file.ts +419 -0
  34. package/src/case.ts +651 -156
  35. package/src/cli.ts +1325 -100
  36. package/src/crypto.ts +5 -1
  37. package/src/declaration.ts +248 -52
  38. package/src/evidence.ts +142 -21
  39. package/src/exit-codes.ts +54 -0
  40. package/src/grading.ts +109 -52
  41. package/src/handoff.ts +108 -17
  42. package/src/index.ts +60 -3
  43. package/src/message-parts.ts +11 -9
  44. package/src/types.ts +5 -0
  45. package/src/verifier.ts +35 -13
  46. package/src/zip-reader.ts +229 -0
  47. package/vectors/README.md +56 -3
  48. package/vectors/case-file-index.json +668 -0
  49. package/vectors/case-split.json +1102 -0
  50. package/vectors/pack-grading.json +68 -4
  51. package/vectors/stamp-on-chain.json +3 -3
@@ -0,0 +1,229 @@
1
+ /**
2
+ * The one ZIP reader `case` uses, for a case file named on the command line.
3
+ *
4
+ * The TypeScript half of `src/sanning_proof/zip_reader.py`. Node has no ZIP
5
+ * reader, and Python's `zipfile` is not used, so the two kernels read one
6
+ * archive by one rule rather than by two libraries that disagree on the edges:
7
+ * a central directory that contradicts a local header, a name that is not
8
+ * UTF-8, data after the end of a deflate stream. Where either library would
9
+ * have made a choice, this module refuses instead.
10
+ *
11
+ * What it reads (evidence-bundle.md 5.6.1): the central directory, and each
12
+ * entry's data through its local header. Stored and deflate only. What it
13
+ * REFUSES, as a whole archive: no end-of-central-directory record, ZIP64, more
14
+ * than one disk, an entry the central directory places outside the archive, a
15
+ * local header that does not match its central entry, a size or CRC-32 that
16
+ * does not match the data, two entries with one name, a name that is not
17
+ * UTF-8, more than MAX_ZIP_ENTRIES entries or MAX_ZIP_BYTES bytes in all, and
18
+ * a name nested deeper than MAX_ZIP_DEPTH folders. What it REPORTS and never
19
+ * reads, entry by entry: an encrypted entry, a symbolic link, a method other
20
+ * than stored or deflate, and a name that is absolute, holds a backslash or a
21
+ * NUL, or has an empty, `.` or `..` segment.
22
+ *
23
+ * Pure: bytes in, entries out. The caller turns them into the tree `case`
24
+ * splits.
25
+ */
26
+
27
+ import { inflateRawSync } from "node:zlib";
28
+
29
+ import { utf8Strict } from "./verifier.js";
30
+
31
+ /** More entries than this refuse the archive: the walk's own bound on names. */
32
+ export const MAX_ZIP_ENTRIES = 50_000;
33
+ /** More uncompressed bytes than this, across every entry, refuse the archive. */
34
+ export const MAX_ZIP_BYTES = 2 ** 30;
35
+ /** A name nested deeper than this many folders refuses the archive. */
36
+ export const MAX_ZIP_DEPTH = 32;
37
+
38
+ const EOCD = 0x06054b50;
39
+ const CENTRAL = 0x02014b50;
40
+ const LOCAL = 0x04034b50;
41
+ const S_IFMT = 0o170000;
42
+ const S_IFLNK = 0o120000;
43
+ const UNIX = 3;
44
+
45
+ /** The archive cannot be read as a case file. `message` is the whole report, identical in both kernels. */
46
+ export class ZipReadError extends Error {
47
+ constructor(message: string) {
48
+ super(message);
49
+ this.name = "ZipReadError";
50
+ }
51
+ }
52
+
53
+ /**
54
+ * One file entry. `refused` is why the entry is reported and not read, or
55
+ * `null`; `data` is its bytes where it was read.
56
+ */
57
+ export interface ZipEntry {
58
+ name: string;
59
+ refused: string | null;
60
+ data: Uint8Array | null;
61
+ }
62
+
63
+ const CRC_TABLE = (() => {
64
+ const table = new Uint32Array(256);
65
+ for (let n = 0; n < 256; n++) {
66
+ let c = n;
67
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
68
+ table[n] = c >>> 0;
69
+ }
70
+ return table;
71
+ })();
72
+
73
+ function crc32(bytes: Uint8Array): number {
74
+ let c = 0xffffffff;
75
+ for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
76
+ return (c ^ 0xffffffff) >>> 0;
77
+ }
78
+
79
+ function refusal(name: string): string | null {
80
+ if (name.startsWith("/")) return "an absolute name";
81
+ if (name.includes("\\")) return "a name with a backslash";
82
+ if (name.includes("\u0000")) return "a name with a NUL";
83
+ for (const segment of name.split("/")) {
84
+ if (segment === "" || segment === "." || segment === "..") {
85
+ return "a name with an empty, . or .. segment";
86
+ }
87
+ }
88
+ return null;
89
+ }
90
+
91
+ function sameBytes(a: Uint8Array, b: Uint8Array): boolean {
92
+ if (a.length !== b.length) return false;
93
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
94
+ return true;
95
+ }
96
+
97
+ /**
98
+ * Every FILE entry of a ZIP archive, in central-directory order. A directory
99
+ * entry (a name ending in `/`) is not a file and is not returned. Throws
100
+ * `ZipReadError`.
101
+ */
102
+ export function readZip(data: Uint8Array): ZipEntry[] {
103
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
104
+ const n = data.length;
105
+ const u32 = (at: number) => view.getUint32(at, true);
106
+ const u16 = (at: number) => view.getUint16(at, true);
107
+ // The end-of-central-directory record: 22 bytes plus a comment of at most
108
+ // 65535, searched backwards so the LAST signature wins.
109
+ let eocd = -1;
110
+ const lowest = Math.max(0, n - 22 - 0xffff);
111
+ for (let at = n - 22; at >= lowest; at--) {
112
+ if (u32(at) === EOCD && at + 22 + u16(at + 20) === n) {
113
+ eocd = at;
114
+ break;
115
+ }
116
+ }
117
+ if (eocd < 0) throw new ZipReadError("is not a readable .zip archive");
118
+ const disk = u16(eocd + 4);
119
+ const cdDisk = u16(eocd + 6);
120
+ const hereCount = u16(eocd + 8);
121
+ const count = u16(eocd + 10);
122
+ const cdSize = u32(eocd + 12);
123
+ const cdOffset = u32(eocd + 16);
124
+ if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff) {
125
+ throw new ZipReadError("is a ZIP64 archive, which case does not read");
126
+ }
127
+ if (disk !== 0 || cdDisk !== 0 || hereCount !== count) {
128
+ throw new ZipReadError("spans more than one disk, which case does not read");
129
+ }
130
+ if (count > MAX_ZIP_ENTRIES) {
131
+ throw new ZipReadError(`holds more than ${MAX_ZIP_ENTRIES} entries; refusing to read it`);
132
+ }
133
+ if (cdOffset + cdSize > eocd) throw new ZipReadError("is not a readable .zip archive");
134
+
135
+ const entries: ZipEntry[] = [];
136
+ const names = new Set<string>();
137
+ let total = 0;
138
+ let at = cdOffset;
139
+ for (let e = 0; e < count; e++) {
140
+ if (at + 46 > eocd || u32(at) !== CENTRAL) {
141
+ throw new ZipReadError("is not a readable .zip archive");
142
+ }
143
+ const madeBy = u16(at + 4);
144
+ const flags = u16(at + 8);
145
+ const method = u16(at + 10);
146
+ const crc = u32(at + 16);
147
+ const csize = u32(at + 20);
148
+ const usize = u32(at + 24);
149
+ const nameLen = u16(at + 28);
150
+ const extraLen = u16(at + 30);
151
+ const commentLen = u16(at + 32);
152
+ const external = u32(at + 38);
153
+ const localAt = u32(at + 42);
154
+ const rawName = data.subarray(at + 46, at + 46 + nameLen);
155
+ at += 46 + nameLen + extraLen + commentLen;
156
+ if (at > eocd) throw new ZipReadError("is not a readable .zip archive");
157
+ let name: string;
158
+ try {
159
+ name = utf8Strict(rawName);
160
+ } catch {
161
+ throw new ZipReadError(
162
+ "holds a name that is not valid UTF-8; rename it, then run case again",
163
+ );
164
+ }
165
+ if (names.has(name)) throw new ZipReadError(`holds ${name} twice`);
166
+ names.add(name);
167
+ if (name.endsWith("/")) continue;
168
+ if (name.split("/").length - 1 > MAX_ZIP_DEPTH) {
169
+ throw new ZipReadError(
170
+ `is nested more than ${MAX_ZIP_DEPTH} folders deep; refusing to read it`,
171
+ );
172
+ }
173
+ let refused = refusal(name);
174
+ if (refused === null && flags & 0x1) refused = "an encrypted entry";
175
+ if (
176
+ refused === null &&
177
+ madeBy >> 8 === UNIX &&
178
+ ((external >>> 16) & S_IFMT) === S_IFLNK
179
+ ) {
180
+ refused = "a symbolic link";
181
+ }
182
+ if (refused === null && method !== 0 && method !== 8) {
183
+ refused = "a compression method other than stored or deflate";
184
+ }
185
+ if (refused !== null) {
186
+ entries.push({ name, refused, data: null });
187
+ continue;
188
+ }
189
+
190
+ total += usize;
191
+ if (total > MAX_ZIP_BYTES) {
192
+ throw new ZipReadError(
193
+ `holds more than ${MAX_ZIP_BYTES} bytes uncompressed; refusing to read it`,
194
+ );
195
+ }
196
+ const unreadable = () => new ZipReadError(`holds ${name}, which cannot be read`);
197
+ if (localAt + 30 > cdOffset || u32(localAt) !== LOCAL) throw unreadable();
198
+ const localNameLen = u16(localAt + 26);
199
+ const localExtraLen = u16(localAt + 28);
200
+ if (!sameBytes(data.subarray(localAt + 30, localAt + 30 + localNameLen), rawName)) {
201
+ throw unreadable();
202
+ }
203
+ const start = localAt + 30 + localNameLen + localExtraLen;
204
+ if (start + csize > cdOffset) throw unreadable();
205
+ const packed = data.subarray(start, start + csize);
206
+ let body: Uint8Array;
207
+ if (method === 0) {
208
+ body = packed;
209
+ } else {
210
+ // `info: true` hands back the engine, whose `bytesWritten` is how many
211
+ // input bytes the stream consumed. Anything left over is data after the
212
+ // end of the deflate stream, which Python's `unused_data` refuses too.
213
+ let result: { buffer: Buffer; engine: { bytesWritten: number } };
214
+ try {
215
+ result = inflateRawSync(packed, {
216
+ info: true,
217
+ maxOutputLength: usize + 1,
218
+ }) as unknown as { buffer: Buffer; engine: { bytesWritten: number } };
219
+ } catch {
220
+ throw unreadable();
221
+ }
222
+ if (result.engine.bytesWritten !== packed.length) throw unreadable();
223
+ body = new Uint8Array(result.buffer.buffer, result.buffer.byteOffset, result.buffer.length);
224
+ }
225
+ if (body.length !== usize || crc32(body) !== crc) throw unreadable();
226
+ entries.push({ name, refused: null, data: body.slice() });
227
+ }
228
+ return entries;
229
+ }
package/vectors/README.md CHANGED
@@ -107,13 +107,66 @@ happened, and the reason this file moved here.
107
107
 
108
108
  | Key | Meaning |
109
109
  |---|---|
110
- | `ruleVersion` | Bumped when a case's expectation changes **or a case is added**. An implementation asserts the version it was written against, so a table it does not understand fails loudly rather than passing vacuously. Currently **3**. |
111
- | `kernelEventClasses` | The four per-event outcome classes a verification result can carry, named so a case can be written as counts rather than as a hand-built bundle. |
110
+ | `ruleVersion` | Bumped when a case's expectation changes **or a case is added**. An implementation asserts the version it was written against, so a table it does not understand fails loudly rather than passing vacuously. Currently **6**: `evidence-bundle.md` §5.4.5, where a record withheld by a readable declaration is counted apart and holds the pack at nothing. |
111
+ | `kernelEventClasses` | The six per-event outcome classes a verification result can carry, named so a case can be written as counts rather than as a hand-built bundle. The two added at rule version 6, `withheldBound` and `withheldUnbound`, carry an `eventId` and a `payloadBindingOk`; `howToUse` says how an adapter builds them. |
112
112
  | `cases[].kernel` | The verification result to grade, as class counts. |
113
113
  | `cases[].logs` | The disclosure account: how many records the bundle presents, and how many disclosed usable bytes. |
114
114
  | `cases[].omissions` | Completeness notes — a checkpoint's signed `leaf_count` against the records the pack presents for it. |
115
- | `cases[].expect` | The verdict, the three counts, and how many causes the grade must carry. |
115
+ | `cases[].expect` | The verdict, the three counts, how many causes the grade must carry, and, from rule version 6, `recordsWithheld` where a case pins it. |
116
116
 
117
117
  Nothing in a case names a transport. A directory read and a browser drop produce
118
118
  the same account and must produce the same verdict; a table that encoded one
119
119
  surface's mechanism would let the other one drift while staying green.
120
+
121
+ ## `case-file-index.json`
122
+
123
+ How a reader compares a case file's index, `contents.json`, with the folder it
124
+ sits at the root of (`evidence-bundle.md` §5.6.3, v1.20 A25). One pure function
125
+ in both kernels, `readCaseFileIndex` in [`../src/case-file.ts`](../src/case-file.ts)
126
+ and `read_case_file_index` in `src/sanning_proof/case_file.py`, asserted against
127
+ this table by [`../test/case-file-index.test.ts`](../test/case-file-index.test.ts)
128
+ **and by the Python kernel against the same bytes**, in
129
+ `tests/test_case_file_index.py`. Each row gives the index bytes as hex (or
130
+ `null` for a folder with no index) and every file in the folder with its
131
+ SHA-256, and expects the index state, its `case_id` label and the findings in
132
+ the pinned order. **No finding moves a verdict**: the index is never an input
133
+ to the case reading, and a surface that took one from it would be trusting
134
+ whoever assembled the drop. Written by
135
+ `tools/gen-vectors/gen_case_file_index_vectors.py`, whose expectations are
136
+ literals.
137
+
138
+ ## `case-split.json`
139
+
140
+ How `case` turns the folders it was handed into packs: the verify page's
141
+ splitting rule (`sanning-io/verify-page` `src/lib/case.ts` `splitCase`,
142
+ `src/lib/pack.ts` `assemble`) over a walk whose rules are the command's own,
143
+ restated in [`../src/cli.ts`](../src/cli.ts) and `src/sanning_proof/cli.py`
144
+ because the page's code lives in the page. Two tables and the limits:
145
+
146
+ - `trees`: a directory tree to create (files, folders, symbolic links, a name
147
+ given as bytes, or a folder filled with N empty files), the arguments, and
148
+ the packs, the outside paths and the paths that are not regular files, or
149
+ the refusal. The rows include the decision file unpacked, a symbolic link
150
+ under `logs/`, each side of the depth and entry limits, names that are not
151
+ UTF-8, and (v1.20, A25) a case file unpacked and as a `.zip` argument, with
152
+ the entries a reader lists and never reads and the archives it refuses.
153
+ - `parses`: how `bundle.json` bytes become JSON through the reader `verify`
154
+ and `case` share, including each side of the nesting limit.
155
+ - `limits`: the walk depth (32), the entry count (50,000) and the JSON nesting
156
+ depth (512), which both kernels assert they state.
157
+
158
+ Asserted by [`../test/case-cli.test.ts`](../test/case-cli.test.ts) **and by
159
+ the Python kernel against the same trees**, in `tests/test_case_cli.py`, and
160
+ run through both commands by `cross-kernel/run_case_cli.sh`.
161
+
162
+ Every expectation is a literal written in
163
+ `tools/gen-vectors/gen_case_split_vectors.py`, not the output of either
164
+ kernel. The splitting rows were graded against the page's own code when the
165
+ table was first written. The walk rows (links, depth, entry count, names, a
166
+ `.zip` argument) and the reader rows are the command's own rules, and the page
167
+ does not share them: it opens a dropped `.zip`, drops a leading byte-order
168
+ mark, and reads whatever the browser hands it for a link, a bad name or a deep
169
+ tree. One splitting row disagrees with the page on purpose: `record-file-names` keeps
170
+ a record named `__proto__`, which the page's `assemble` loses because it keys
171
+ disclosed content by a plain object. A page that vendors this table fails that
172
+ row until it keys content by a `Map`.