@stigmer/zip-structure 0.1.0-dev.20261017020314.g644422724c27

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 ADDED
@@ -0,0 +1,194 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Stigmer Contributors
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
191
+
192
+ Except for the contents of the ee/ directory, which are licensed under the
193
+ Stigmer Enterprise License (see ee/LICENSE), everything in this repository
194
+ is licensed under the Apache License, Version 2.0, above.
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { EOCD_MIN_SIZE, crc32, parseZipStructure, type ZipStructuralEntry, } from "./zip-structure.js";
2
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,EACL,iBAAiB,EACjB,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC"}
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { EOCD_MIN_SIZE, crc32, parseZipStructure, } from "./zip-structure.js";
2
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,EACL,iBAAiB,GAElB,MAAM,oBAAoB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@stigmer/zip-structure",
3
+ "version": "0.1.0-dev.20261017020314.g644422724c27",
4
+ "description": "Policy-free structural ZIP parsing shared by Stigmer's TypeScript processes — central-directory-based entry enumeration (never local-header walks) plus the byte-level archive builder its consumers' tests craft fixtures with",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/stigmer/stigmer.git",
11
+ "directory": "backend/libs/ts/zip-structure"
12
+ },
13
+ "engines": {
14
+ "node": "^22.13.0 || >=23.4.0"
15
+ },
16
+ "keywords": [
17
+ "stigmer",
18
+ "zip",
19
+ "central-directory",
20
+ "structural-parsing"
21
+ ],
22
+ "main": "./index.js",
23
+ "types": "./index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./index.d.ts",
27
+ "import": "./index.js",
28
+ "default": "./index.js"
29
+ },
30
+ "./testing": {
31
+ "types": "./testing.d.ts",
32
+ "import": "./testing.js",
33
+ "default": "./testing.js"
34
+ }
35
+ }
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export {
2
+ EOCD_MIN_SIZE,
3
+ crc32,
4
+ parseZipStructure,
5
+ type ZipStructuralEntry,
6
+ } from "./zip-structure.js";
package/src/testing.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Byte-level ZIP fixture builder for the structural parser's consumers'
3
+ * tests (exported as `@stigmer/zip-structure/testing`). Moved here from the
4
+ * runner's src/__test-utils__/zip-fixtures.ts when the lib was extracted —
5
+ * the lib's own tests, the runner's, and the TS server's skill-gate tests
6
+ * all craft archives with it, and one builder keeps their fixture shapes
7
+ * from drifting apart.
8
+ *
9
+ * Emits complete, real-shaped archives — local file headers (optionally
10
+ * streaming-style, the Go stdlib writer's default), payloads, central
11
+ * directory, and EOCD — because that is the only shape that can reach the
12
+ * runner: both editions' skill push gates validate artifacts with
13
+ * central-directory-based readers (see zip-structure.ts's module doc).
14
+ * Earlier fixtures emitted local headers only, a shape no real ZIP writer
15
+ * produces, and were coupled to the old parser's front-to-back walk.
16
+ *
17
+ * Mirrors the conformance suite's `zipFilesStreaming()`
18
+ * (test/conformance/src/support/skills.ts) so unit fixtures and
19
+ * cross-edition fixtures share one shape.
20
+ */
21
+
22
+ import { deflateRawSync } from "node:zlib";
23
+
24
+ import { crc32 } from "./zip-structure.js";
25
+
26
+ export interface ZipFixtureFile {
27
+ name: string;
28
+ /** Text content is UTF-8 encoded; pass bytes directly for binary payloads. */
29
+ content: string | Uint8Array;
30
+ /** Compression method for the entry. Defaults to stored. */
31
+ method?: "stored" | "deflated";
32
+ /**
33
+ * Emit the entry the way Go's archive/zip does by default: general-purpose
34
+ * flag bit 3 set, zeroed sizes in the local header, and a trailing data
35
+ * descriptor (with the conventional signature) carrying the real values.
36
+ */
37
+ streaming?: boolean;
38
+ /**
39
+ * Lie about the entry's uncompressed size everywhere the writer would
40
+ * declare it (local header, data descriptor, central directory). Models a
41
+ * crafted archive whose declarations disagree with its actual payload —
42
+ * the input class the attachment injector's declared-size enforcement
43
+ * exists to reject (issue #567).
44
+ */
45
+ declaredUncompressedSize?: number;
46
+ /**
47
+ * The central directory's "version made by" field. Defaults to 20
48
+ * (MS-DOS creator, the historical fixture value). Set the high byte to
49
+ * 3 (`0x0300 | 20`) to model a Unix creator, which makes readers
50
+ * interpret `externalAttributes`' high 16 bits as a POSIX mode — how
51
+ * symlink and device entries are represented (the server's
52
+ * safearchive-parity pre-filter fixtures need this).
53
+ */
54
+ versionMadeBy?: number;
55
+ /** The central directory's external file attributes field. Defaults to 0. */
56
+ externalAttributes?: number;
57
+ }
58
+
59
+ export interface ZipFixtureOptions {
60
+ /** Trailing archive comment appended after the EOCD record. */
61
+ comment?: string;
62
+ /**
63
+ * Drop the central directory and EOCD, leaving only local headers and
64
+ * payloads. No real ZIP writer produces this shape — it models a download
65
+ * truncated before the archive's index.
66
+ */
67
+ omitCentralDirectory?: boolean;
68
+ }
69
+
70
+ /** Build a ZIP archive from path + content pairs. */
71
+ export function buildZip(files: ZipFixtureFile[], options?: ZipFixtureOptions): Uint8Array {
72
+ const out = new ByteWriter();
73
+
74
+ interface WrittenEntry {
75
+ nameBytes: Uint8Array;
76
+ payload: Uint8Array;
77
+ declaredUncompressed: number;
78
+ crc: number;
79
+ flags: number;
80
+ method: number;
81
+ versionMadeBy: number;
82
+ externalAttributes: number;
83
+ localHeaderOffset: number;
84
+ }
85
+ const written: WrittenEntry[] = [];
86
+
87
+ for (const file of files) {
88
+ const contentBytes =
89
+ typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content;
90
+ const deflated = file.method === "deflated";
91
+ const payload = deflated ? new Uint8Array(deflateRawSync(contentBytes)) : contentBytes;
92
+ const entry: WrittenEntry = {
93
+ nameBytes: new TextEncoder().encode(file.name),
94
+ payload,
95
+ declaredUncompressed: file.declaredUncompressedSize ?? contentBytes.length,
96
+ crc: crc32(contentBytes),
97
+ flags: file.streaming ? 0x0008 : 0,
98
+ method: deflated ? 8 : 0,
99
+ versionMadeBy: file.versionMadeBy ?? 20,
100
+ externalAttributes: file.externalAttributes ?? 0,
101
+ localHeaderOffset: out.length,
102
+ };
103
+ written.push(entry);
104
+
105
+ out.u32(0x04034b50); // local file header signature
106
+ out.u16(20); // version needed to extract
107
+ out.u16(entry.flags);
108
+ out.u16(entry.method);
109
+ out.u16(0); // mod time
110
+ out.u16(0); // mod date
111
+ out.u32(file.streaming ? 0 : entry.crc);
112
+ out.u32(file.streaming ? 0 : payload.length);
113
+ out.u32(file.streaming ? 0 : entry.declaredUncompressed);
114
+ out.u16(entry.nameBytes.length);
115
+ out.u16(0); // extra field length
116
+ out.raw(entry.nameBytes);
117
+ out.raw(payload);
118
+
119
+ if (file.streaming) {
120
+ out.u32(0x08074b50); // data descriptor signature (Go writes it)
121
+ out.u32(entry.crc);
122
+ out.u32(payload.length);
123
+ out.u32(entry.declaredUncompressed);
124
+ }
125
+ }
126
+
127
+ if (options?.omitCentralDirectory) {
128
+ return out.toUint8Array();
129
+ }
130
+
131
+ const centralDirectoryOffset = out.length;
132
+ for (const entry of written) {
133
+ out.u32(0x02014b50); // central directory record signature
134
+ out.u16(entry.versionMadeBy);
135
+ out.u16(20); // version needed to extract
136
+ out.u16(entry.flags);
137
+ out.u16(entry.method);
138
+ out.u16(0); // mod time
139
+ out.u16(0); // mod date
140
+ out.u32(entry.crc);
141
+ out.u32(entry.payload.length);
142
+ out.u32(entry.declaredUncompressed);
143
+ out.u16(entry.nameBytes.length);
144
+ out.u16(0); // extra field length
145
+ out.u16(0); // comment length
146
+ out.u16(0); // disk number start
147
+ out.u16(0); // internal attributes
148
+ out.u32(entry.externalAttributes);
149
+ out.u32(entry.localHeaderOffset);
150
+ out.raw(entry.nameBytes);
151
+ }
152
+ const centralDirectorySize = out.length - centralDirectoryOffset;
153
+
154
+ const commentBytes = new TextEncoder().encode(options?.comment ?? "");
155
+ out.u32(0x06054b50); // EOCD signature
156
+ out.u16(0); // disk number
157
+ out.u16(0); // disk with central directory
158
+ out.u16(written.length); // entries on this disk
159
+ out.u16(written.length); // total entries
160
+ out.u32(centralDirectorySize);
161
+ out.u32(centralDirectoryOffset);
162
+ out.u16(commentBytes.length);
163
+ out.raw(commentBytes);
164
+
165
+ return out.toUint8Array();
166
+ }
167
+
168
+ // Chunked assembly instead of a number[]-per-byte accumulator: fixtures at
169
+ // the injector's 100 MB zip-bomb limit are built from payload-sized chunks,
170
+ // which a per-byte spread-push cannot survive (argument-count overflow).
171
+ class ByteWriter {
172
+ private readonly chunks: Uint8Array[] = [];
173
+ private size = 0;
174
+
175
+ get length(): number {
176
+ return this.size;
177
+ }
178
+
179
+ u16(v: number): void {
180
+ this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff]));
181
+ }
182
+
183
+ u32(v: number): void {
184
+ this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff]));
185
+ }
186
+
187
+ raw(b: Uint8Array): void {
188
+ this.chunks.push(b);
189
+ this.size += b.length;
190
+ }
191
+
192
+ toUint8Array(): Uint8Array {
193
+ const result = new Uint8Array(this.size);
194
+ let offset = 0;
195
+ for (const chunk of this.chunks) {
196
+ result.set(chunk, offset);
197
+ offset += chunk.length;
198
+ }
199
+ return result;
200
+ }
201
+ }
202
+
203
+ // CRC-32 comes from the structural module (one implementation for the
204
+ // parser's consumers and this builder; semantically identical to the
205
+ // conformance suite's inline bit-loop helper).
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
3
+ * location plus central-directory walk, producing one record per entry.
4
+ *
5
+ * Lived in the runner (src/shared/zip-structure.ts) until the TS server's
6
+ * skill push gate became its second consumer; extracted to backend/libs/ts/
7
+ * under the shared-library rule (a library moves here when a second
8
+ * consumer exists — @stigmer/temporal-codecs is the precedent).
9
+ *
10
+ * Parsing is *central-directory-based* — never a front-to-back walk of
11
+ * local file headers. This is a correctness decision, not a style choice
12
+ * (issues #450 and #567): local headers of streaming entries
13
+ * (general-purpose flag bit 3) carry zeroed sizes, and the only
14
+ * local-only way to recover them is scanning the payload for the
15
+ * data-descriptor signature — which silently truncates any stored entry
16
+ * whose *content* happens to contain those four bytes, and
17
+ * desynchronizes every entry after it. The central directory always
18
+ * carries the real sizes, and every consumer here holds the complete
19
+ * archive bytes, so nothing a local-header walk could offer is needed.
20
+ *
21
+ * This layer maps bytes to entry records and nothing else. Policy — what
22
+ * a structural failure means, which entries are acceptable, how payloads
23
+ * are decoded — belongs to the consumers, and they differ on purpose:
24
+ *
25
+ * - runner shared/zip-extract.ts (skill artifacts): structural failure
26
+ * is NON-FATAL — both editions' push gates validated every artifact
27
+ * with a central-directory-based reader before storage, so a defect
28
+ * here can only be a truncated or corrupted download.
29
+ * - runner shared/attachment-zip.ts (user attachments, both
30
+ * harnesses): structural failure is FAIL-HARD — the input is an
31
+ * untrusted upload and nothing upstream vouched for it.
32
+ * - stigmer-server src/domain/skill/storage/ (the push gate):
33
+ * FAIL-HARD, plus a safearchive-parity entry pre-filter decoded from
34
+ * the raw creator/attribute fields exposed below.
35
+ *
36
+ * ZIP64 is deliberately unsupported: every consumer caps input far below
37
+ * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
38
+ * attachment uploads: 10MB).
39
+ *
40
+ * Throws plain `Error`s on structural defects (no valid EOCD, a central
41
+ * directory or local header record that does not parse, a payload slice
42
+ * that runs past the end of the buffer); consumers translate those into
43
+ * their own error models.
44
+ */
45
+
46
+ // ─── Entry records ───────────────────────────────────────────────────────
47
+
48
+ export interface ZipStructuralEntry {
49
+ /** Entry path exactly as recorded in the central directory. */
50
+ readonly name: string;
51
+ readonly isDirectory: boolean;
52
+ readonly compressionMethod: number;
53
+ /**
54
+ * The central directory's declared uncompressed size — authoritative for
55
+ * pre-extraction accounting (e.g. ZIP-bomb budgeting), but still a
56
+ * *declaration*: a consumer that distrusts its input must enforce it
57
+ * against the actual decompressed output.
58
+ */
59
+ readonly uncompressedSize: number;
60
+ /** The central directory's declared compressed size (the payload length). */
61
+ readonly compressedSize: number;
62
+ /**
63
+ * The central directory's CRC-32 of the uncompressed content, verbatim.
64
+ * Raw fact for consumers that verify payload integrity after
65
+ * decompression (Go's archive/zip checks it on every read); consumers
66
+ * that trust their input ignore it.
67
+ */
68
+ readonly crc32: number;
69
+ /**
70
+ * The central directory's "version made by" field, verbatim. The high
71
+ * byte identifies the creator OS (0 = MS-DOS/FAT, 3 = Unix, 19 = macOS),
72
+ * which decides how `externalAttributes` is interpreted. Raw facts for
73
+ * policy layers that decode entry file modes; most consumers ignore it.
74
+ */
75
+ readonly versionMadeBy: number;
76
+ /**
77
+ * The central directory's external file attributes field, verbatim.
78
+ * Unix creators store the POSIX mode in the high 16 bits; MS-DOS
79
+ * creators store FAT attribute bits in the low byte. Interpretation is
80
+ * policy — see the server's safearchive-parity pre-filter.
81
+ */
82
+ readonly externalAttributes: number;
83
+ /** The entry's raw payload slice (a view, not a copy) of the archive buffer. */
84
+ readonly compressedData: Uint8Array;
85
+ }
86
+
87
+ // ─── Format constants ────────────────────────────────────────────────────
88
+
89
+ const LOCAL_FILE_HEADER_SIG = 0x04034b50;
90
+ const CENTRAL_DIRECTORY_SIG = 0x02014b50;
91
+ const EOCD_SIG = 0x06054b50;
92
+
93
+ /** Fixed size of the EOCD record, excluding the variable-length comment. */
94
+ export const EOCD_MIN_SIZE = 22;
95
+ /** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
96
+ const EOCD_MAX_COMMENT = 0xffff;
97
+
98
+ const LOCAL_HEADER_SIZE = 30;
99
+ const CD_RECORD_SIZE = 46;
100
+
101
+ // ─── Parsing ─────────────────────────────────────────────────────────────
102
+
103
+ /**
104
+ * Enumerate the archive's entries from its central directory.
105
+ *
106
+ * Throws on any structural defect: no valid EOCD, a central directory or
107
+ * local header record that doesn't parse, or a payload slice that runs
108
+ * past the end of the buffer.
109
+ */
110
+ export function parseZipStructure(data: Uint8Array): ZipStructuralEntry[] {
111
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
112
+ const eocd = findEndOfCentralDirectory(data, view);
113
+
114
+ const entries: ZipStructuralEntry[] = [];
115
+ let offset = eocd.centralDirectoryOffset;
116
+
117
+ for (let i = 0; i < eocd.entryCount; i++) {
118
+ if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
119
+ throw new Error(`invalid central directory record at offset ${offset}`);
120
+ }
121
+
122
+ const versionMadeBy = view.getUint16(offset + 4, true);
123
+ const compressionMethod = view.getUint16(offset + 10, true);
124
+ const crc32 = view.getUint32(offset + 16, true);
125
+ const compressedSize = view.getUint32(offset + 20, true);
126
+ const uncompressedSize = view.getUint32(offset + 24, true);
127
+ const fileNameLength = view.getUint16(offset + 28, true);
128
+ const extraFieldLength = view.getUint16(offset + 30, true);
129
+ const commentLength = view.getUint16(offset + 32, true);
130
+ const externalAttributes = view.getUint32(offset + 38, true);
131
+ const localHeaderOffset = view.getUint32(offset + 42, true);
132
+
133
+ const fileName = new TextDecoder().decode(
134
+ data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength),
135
+ );
136
+
137
+ // The local header is consulted for one thing only: the payload start.
138
+ // Its name/extra field lengths can legitimately differ from the central
139
+ // directory's (streaming writers pad the extra field), so the offset
140
+ // cannot be derived from CD fields alone.
141
+ const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
142
+ if (dataStart + compressedSize > data.length) {
143
+ throw new Error(`entry "${fileName}" payload runs past end of archive`);
144
+ }
145
+
146
+ entries.push({
147
+ name: fileName,
148
+ isDirectory: fileName.endsWith("/"),
149
+ compressionMethod,
150
+ uncompressedSize,
151
+ compressedSize,
152
+ crc32,
153
+ versionMadeBy,
154
+ externalAttributes,
155
+ compressedData: data.subarray(dataStart, dataStart + compressedSize),
156
+ });
157
+
158
+ offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
159
+ }
160
+
161
+ return entries;
162
+ }
163
+
164
+ interface EndOfCentralDirectory {
165
+ centralDirectoryOffset: number;
166
+ entryCount: number;
167
+ }
168
+
169
+ /**
170
+ * Locate and validate the end-of-central-directory record.
171
+ *
172
+ * Scans backward from the end of the archive across the maximum comment
173
+ * span. A signature match alone is not trusted (the four bytes can occur
174
+ * inside a trailing comment): the record must also point at an offset
175
+ * that actually holds a central directory record, or be a genuinely
176
+ * empty archive.
177
+ */
178
+ function findEndOfCentralDirectory(data: Uint8Array, view: DataView): EndOfCentralDirectory {
179
+ const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
180
+
181
+ for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
182
+ if (view.getUint32(pos, true) !== EOCD_SIG) continue;
183
+
184
+ const entryCount = view.getUint16(pos + 10, true);
185
+ const centralDirectorySize = view.getUint32(pos + 12, true);
186
+ const centralDirectoryOffset = view.getUint32(pos + 16, true);
187
+
188
+ const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
189
+ const directoryLooksReal =
190
+ entryCount === 0 ||
191
+ (centralDirectoryOffset + 4 <= data.length &&
192
+ view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
193
+
194
+ if (directoryEndsAtRecord && directoryLooksReal) {
195
+ return { centralDirectoryOffset, entryCount };
196
+ }
197
+ }
198
+
199
+ throw new Error("no end-of-central-directory record found");
200
+ }
201
+
202
+ // ─── CRC-32 ──────────────────────────────────────────────────────────────
203
+
204
+ // Standard CRC-32 (IEEE 802.3, the ZIP checksum), table-driven so callers
205
+ // hashing payloads at the consumers' 100 MB caps stay cheap.
206
+ const CRC_TABLE = (() => {
207
+ const table = new Uint32Array(256);
208
+ for (let n = 0; n < 256; n++) {
209
+ let c = n;
210
+ for (let bit = 0; bit < 8; bit++) {
211
+ c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
212
+ }
213
+ table[n] = c;
214
+ }
215
+ return table;
216
+ })();
217
+
218
+ /**
219
+ * CRC-32 of `data` — the ZIP checksum. Exported for consumers that verify
220
+ * decompressed payloads against an entry's declared `crc32` (Go's
221
+ * archive/zip does this on every read) and for the fixture builder.
222
+ */
223
+ export function crc32(data: Uint8Array): number {
224
+ let crc = 0xffffffff;
225
+ for (let i = 0; i < data.length; i++) {
226
+ crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ data[i]!) & 0xff]!;
227
+ }
228
+ return (crc ^ 0xffffffff) >>> 0;
229
+ }
230
+
231
+ /** Resolve where an entry's payload begins, from its local file header. */
232
+ function payloadStart(
233
+ data: Uint8Array,
234
+ view: DataView,
235
+ localHeaderOffset: number,
236
+ fileName: string,
237
+ ): number {
238
+ if (
239
+ localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
240
+ view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG
241
+ ) {
242
+ throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
243
+ }
244
+
245
+ const localNameLength = view.getUint16(localHeaderOffset + 26, true);
246
+ const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
247
+ return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
248
+ }
package/testing.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Byte-level ZIP fixture builder for the structural parser's consumers'
3
+ * tests (exported as `@stigmer/zip-structure/testing`). Moved here from the
4
+ * runner's src/__test-utils__/zip-fixtures.ts when the lib was extracted —
5
+ * the lib's own tests, the runner's, and the TS server's skill-gate tests
6
+ * all craft archives with it, and one builder keeps their fixture shapes
7
+ * from drifting apart.
8
+ *
9
+ * Emits complete, real-shaped archives — local file headers (optionally
10
+ * streaming-style, the Go stdlib writer's default), payloads, central
11
+ * directory, and EOCD — because that is the only shape that can reach the
12
+ * runner: both editions' skill push gates validate artifacts with
13
+ * central-directory-based readers (see zip-structure.ts's module doc).
14
+ * Earlier fixtures emitted local headers only, a shape no real ZIP writer
15
+ * produces, and were coupled to the old parser's front-to-back walk.
16
+ *
17
+ * Mirrors the conformance suite's `zipFilesStreaming()`
18
+ * (test/conformance/src/support/skills.ts) so unit fixtures and
19
+ * cross-edition fixtures share one shape.
20
+ */
21
+ export interface ZipFixtureFile {
22
+ name: string;
23
+ /** Text content is UTF-8 encoded; pass bytes directly for binary payloads. */
24
+ content: string | Uint8Array;
25
+ /** Compression method for the entry. Defaults to stored. */
26
+ method?: "stored" | "deflated";
27
+ /**
28
+ * Emit the entry the way Go's archive/zip does by default: general-purpose
29
+ * flag bit 3 set, zeroed sizes in the local header, and a trailing data
30
+ * descriptor (with the conventional signature) carrying the real values.
31
+ */
32
+ streaming?: boolean;
33
+ /**
34
+ * Lie about the entry's uncompressed size everywhere the writer would
35
+ * declare it (local header, data descriptor, central directory). Models a
36
+ * crafted archive whose declarations disagree with its actual payload —
37
+ * the input class the attachment injector's declared-size enforcement
38
+ * exists to reject (issue #567).
39
+ */
40
+ declaredUncompressedSize?: number;
41
+ /**
42
+ * The central directory's "version made by" field. Defaults to 20
43
+ * (MS-DOS creator, the historical fixture value). Set the high byte to
44
+ * 3 (`0x0300 | 20`) to model a Unix creator, which makes readers
45
+ * interpret `externalAttributes`' high 16 bits as a POSIX mode — how
46
+ * symlink and device entries are represented (the server's
47
+ * safearchive-parity pre-filter fixtures need this).
48
+ */
49
+ versionMadeBy?: number;
50
+ /** The central directory's external file attributes field. Defaults to 0. */
51
+ externalAttributes?: number;
52
+ }
53
+ export interface ZipFixtureOptions {
54
+ /** Trailing archive comment appended after the EOCD record. */
55
+ comment?: string;
56
+ /**
57
+ * Drop the central directory and EOCD, leaving only local headers and
58
+ * payloads. No real ZIP writer produces this shape — it models a download
59
+ * truncated before the archive's index.
60
+ */
61
+ omitCentralDirectory?: boolean;
62
+ }
63
+ /** Build a ZIP archive from path + content pairs. */
64
+ export declare function buildZip(files: ZipFixtureFile[], options?: ZipFixtureOptions): Uint8Array;
65
+ //# sourceMappingURL=testing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAMH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC;IAC7B,4DAA4D;IAC5D,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAC;IAC/B;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;;;;OAMG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,6EAA6E;IAC7E,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,iBAAiB;IAChC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED,qDAAqD;AACrD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,cAAc,EAAE,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,UAAU,CA+FzF"}
package/testing.js ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Byte-level ZIP fixture builder for the structural parser's consumers'
3
+ * tests (exported as `@stigmer/zip-structure/testing`). Moved here from the
4
+ * runner's src/__test-utils__/zip-fixtures.ts when the lib was extracted —
5
+ * the lib's own tests, the runner's, and the TS server's skill-gate tests
6
+ * all craft archives with it, and one builder keeps their fixture shapes
7
+ * from drifting apart.
8
+ *
9
+ * Emits complete, real-shaped archives — local file headers (optionally
10
+ * streaming-style, the Go stdlib writer's default), payloads, central
11
+ * directory, and EOCD — because that is the only shape that can reach the
12
+ * runner: both editions' skill push gates validate artifacts with
13
+ * central-directory-based readers (see zip-structure.ts's module doc).
14
+ * Earlier fixtures emitted local headers only, a shape no real ZIP writer
15
+ * produces, and were coupled to the old parser's front-to-back walk.
16
+ *
17
+ * Mirrors the conformance suite's `zipFilesStreaming()`
18
+ * (test/conformance/src/support/skills.ts) so unit fixtures and
19
+ * cross-edition fixtures share one shape.
20
+ */
21
+ import { deflateRawSync } from "node:zlib";
22
+ import { crc32 } from "./zip-structure.js";
23
+ /** Build a ZIP archive from path + content pairs. */
24
+ export function buildZip(files, options) {
25
+ const out = new ByteWriter();
26
+ const written = [];
27
+ for (const file of files) {
28
+ const contentBytes = typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content;
29
+ const deflated = file.method === "deflated";
30
+ const payload = deflated ? new Uint8Array(deflateRawSync(contentBytes)) : contentBytes;
31
+ const entry = {
32
+ nameBytes: new TextEncoder().encode(file.name),
33
+ payload,
34
+ declaredUncompressed: file.declaredUncompressedSize ?? contentBytes.length,
35
+ crc: crc32(contentBytes),
36
+ flags: file.streaming ? 0x0008 : 0,
37
+ method: deflated ? 8 : 0,
38
+ versionMadeBy: file.versionMadeBy ?? 20,
39
+ externalAttributes: file.externalAttributes ?? 0,
40
+ localHeaderOffset: out.length,
41
+ };
42
+ written.push(entry);
43
+ out.u32(0x04034b50); // local file header signature
44
+ out.u16(20); // version needed to extract
45
+ out.u16(entry.flags);
46
+ out.u16(entry.method);
47
+ out.u16(0); // mod time
48
+ out.u16(0); // mod date
49
+ out.u32(file.streaming ? 0 : entry.crc);
50
+ out.u32(file.streaming ? 0 : payload.length);
51
+ out.u32(file.streaming ? 0 : entry.declaredUncompressed);
52
+ out.u16(entry.nameBytes.length);
53
+ out.u16(0); // extra field length
54
+ out.raw(entry.nameBytes);
55
+ out.raw(payload);
56
+ if (file.streaming) {
57
+ out.u32(0x08074b50); // data descriptor signature (Go writes it)
58
+ out.u32(entry.crc);
59
+ out.u32(payload.length);
60
+ out.u32(entry.declaredUncompressed);
61
+ }
62
+ }
63
+ if (options?.omitCentralDirectory) {
64
+ return out.toUint8Array();
65
+ }
66
+ const centralDirectoryOffset = out.length;
67
+ for (const entry of written) {
68
+ out.u32(0x02014b50); // central directory record signature
69
+ out.u16(entry.versionMadeBy);
70
+ out.u16(20); // version needed to extract
71
+ out.u16(entry.flags);
72
+ out.u16(entry.method);
73
+ out.u16(0); // mod time
74
+ out.u16(0); // mod date
75
+ out.u32(entry.crc);
76
+ out.u32(entry.payload.length);
77
+ out.u32(entry.declaredUncompressed);
78
+ out.u16(entry.nameBytes.length);
79
+ out.u16(0); // extra field length
80
+ out.u16(0); // comment length
81
+ out.u16(0); // disk number start
82
+ out.u16(0); // internal attributes
83
+ out.u32(entry.externalAttributes);
84
+ out.u32(entry.localHeaderOffset);
85
+ out.raw(entry.nameBytes);
86
+ }
87
+ const centralDirectorySize = out.length - centralDirectoryOffset;
88
+ const commentBytes = new TextEncoder().encode(options?.comment ?? "");
89
+ out.u32(0x06054b50); // EOCD signature
90
+ out.u16(0); // disk number
91
+ out.u16(0); // disk with central directory
92
+ out.u16(written.length); // entries on this disk
93
+ out.u16(written.length); // total entries
94
+ out.u32(centralDirectorySize);
95
+ out.u32(centralDirectoryOffset);
96
+ out.u16(commentBytes.length);
97
+ out.raw(commentBytes);
98
+ return out.toUint8Array();
99
+ }
100
+ // Chunked assembly instead of a number[]-per-byte accumulator: fixtures at
101
+ // the injector's 100 MB zip-bomb limit are built from payload-sized chunks,
102
+ // which a per-byte spread-push cannot survive (argument-count overflow).
103
+ class ByteWriter {
104
+ chunks = [];
105
+ size = 0;
106
+ get length() {
107
+ return this.size;
108
+ }
109
+ u16(v) {
110
+ this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff]));
111
+ }
112
+ u32(v) {
113
+ this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff]));
114
+ }
115
+ raw(b) {
116
+ this.chunks.push(b);
117
+ this.size += b.length;
118
+ }
119
+ toUint8Array() {
120
+ const result = new Uint8Array(this.size);
121
+ let offset = 0;
122
+ for (const chunk of this.chunks) {
123
+ result.set(chunk, offset);
124
+ offset += chunk.length;
125
+ }
126
+ return result;
127
+ }
128
+ }
129
+ // CRC-32 comes from the structural module (one implementation for the
130
+ // parser's consumers and this builder; semantically identical to the
131
+ // conformance suite's inline bit-loop helper).
132
+ //# sourceMappingURL=testing.js.map
package/testing.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.js","sourceRoot":"","sources":["src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AA8C3C,qDAAqD;AACrD,MAAM,UAAU,QAAQ,CAAC,KAAuB,EAAE,OAA2B;IAC3E,MAAM,GAAG,GAAG,IAAI,UAAU,EAAE,CAAC;IAa7B,MAAM,OAAO,GAAmB,EAAE,CAAC;IAEnC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,YAAY,GAChB,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC;QAC3F,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,KAAK,UAAU,CAAC;QAC5C,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QACvF,MAAM,KAAK,GAAiB;YAC1B,SAAS,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;YAC9C,OAAO;YACP,oBAAoB,EAAE,IAAI,CAAC,wBAA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AM,CAAC;IAChB,CAAC;CACF;AAED,sEAAsE;AACtE,qEAAqE;AACrE,+CAA+C"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
3
+ * location plus central-directory walk, producing one record per entry.
4
+ *
5
+ * Lived in the runner (src/shared/zip-structure.ts) until the TS server's
6
+ * skill push gate became its second consumer; extracted to backend/libs/ts/
7
+ * under the shared-library rule (a library moves here when a second
8
+ * consumer exists — @stigmer/temporal-codecs is the precedent).
9
+ *
10
+ * Parsing is *central-directory-based* — never a front-to-back walk of
11
+ * local file headers. This is a correctness decision, not a style choice
12
+ * (issues #450 and #567): local headers of streaming entries
13
+ * (general-purpose flag bit 3) carry zeroed sizes, and the only
14
+ * local-only way to recover them is scanning the payload for the
15
+ * data-descriptor signature — which silently truncates any stored entry
16
+ * whose *content* happens to contain those four bytes, and
17
+ * desynchronizes every entry after it. The central directory always
18
+ * carries the real sizes, and every consumer here holds the complete
19
+ * archive bytes, so nothing a local-header walk could offer is needed.
20
+ *
21
+ * This layer maps bytes to entry records and nothing else. Policy — what
22
+ * a structural failure means, which entries are acceptable, how payloads
23
+ * are decoded — belongs to the consumers, and they differ on purpose:
24
+ *
25
+ * - runner shared/zip-extract.ts (skill artifacts): structural failure
26
+ * is NON-FATAL — both editions' push gates validated every artifact
27
+ * with a central-directory-based reader before storage, so a defect
28
+ * here can only be a truncated or corrupted download.
29
+ * - runner shared/attachment-zip.ts (user attachments, both
30
+ * harnesses): structural failure is FAIL-HARD — the input is an
31
+ * untrusted upload and nothing upstream vouched for it.
32
+ * - stigmer-server src/domain/skill/storage/ (the push gate):
33
+ * FAIL-HARD, plus a safearchive-parity entry pre-filter decoded from
34
+ * the raw creator/attribute fields exposed below.
35
+ *
36
+ * ZIP64 is deliberately unsupported: every consumer caps input far below
37
+ * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
38
+ * attachment uploads: 10MB).
39
+ *
40
+ * Throws plain `Error`s on structural defects (no valid EOCD, a central
41
+ * directory or local header record that does not parse, a payload slice
42
+ * that runs past the end of the buffer); consumers translate those into
43
+ * their own error models.
44
+ */
45
+ export interface ZipStructuralEntry {
46
+ /** Entry path exactly as recorded in the central directory. */
47
+ readonly name: string;
48
+ readonly isDirectory: boolean;
49
+ readonly compressionMethod: number;
50
+ /**
51
+ * The central directory's declared uncompressed size — authoritative for
52
+ * pre-extraction accounting (e.g. ZIP-bomb budgeting), but still a
53
+ * *declaration*: a consumer that distrusts its input must enforce it
54
+ * against the actual decompressed output.
55
+ */
56
+ readonly uncompressedSize: number;
57
+ /** The central directory's declared compressed size (the payload length). */
58
+ readonly compressedSize: number;
59
+ /**
60
+ * The central directory's CRC-32 of the uncompressed content, verbatim.
61
+ * Raw fact for consumers that verify payload integrity after
62
+ * decompression (Go's archive/zip checks it on every read); consumers
63
+ * that trust their input ignore it.
64
+ */
65
+ readonly crc32: number;
66
+ /**
67
+ * The central directory's "version made by" field, verbatim. The high
68
+ * byte identifies the creator OS (0 = MS-DOS/FAT, 3 = Unix, 19 = macOS),
69
+ * which decides how `externalAttributes` is interpreted. Raw facts for
70
+ * policy layers that decode entry file modes; most consumers ignore it.
71
+ */
72
+ readonly versionMadeBy: number;
73
+ /**
74
+ * The central directory's external file attributes field, verbatim.
75
+ * Unix creators store the POSIX mode in the high 16 bits; MS-DOS
76
+ * creators store FAT attribute bits in the low byte. Interpretation is
77
+ * policy — see the server's safearchive-parity pre-filter.
78
+ */
79
+ readonly externalAttributes: number;
80
+ /** The entry's raw payload slice (a view, not a copy) of the archive buffer. */
81
+ readonly compressedData: Uint8Array;
82
+ }
83
+ /** Fixed size of the EOCD record, excluding the variable-length comment. */
84
+ export declare const EOCD_MIN_SIZE = 22;
85
+ /**
86
+ * Enumerate the archive's entries from its central directory.
87
+ *
88
+ * Throws on any structural defect: no valid EOCD, a central directory or
89
+ * local header record that doesn't parse, or a payload slice that runs
90
+ * past the end of the buffer.
91
+ */
92
+ export declare function parseZipStructure(data: Uint8Array): ZipStructuralEntry[];
93
+ /**
94
+ * CRC-32 of `data` — the ZIP checksum. Exported for consumers that verify
95
+ * decompressed payloads against an entry's declared `crc32` (Go's
96
+ * archive/zip does this on every read) and for the fixture builder.
97
+ */
98
+ export declare function crc32(data: Uint8Array): number;
99
+ //# sourceMappingURL=zip-structure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-structure.d.ts","sourceRoot":"","sources":["src/zip-structure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAIH,MAAM,WAAW,kBAAkB;IACjC,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACnC;;;;;OAKG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,6EAA6E;IAC7E,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B;;;;;OAKG;IACH,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,gFAAgF;IAChF,QAAQ,CAAC,cAAc,EAAE,UAAU,CAAC;CACrC;AAQD,4EAA4E;AAC5E,eAAO,MAAM,aAAa,KAAK,CAAC;AAShC;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,GAAG,kBAAkB,EAAE,CAoDxE;AAwDD;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAM9C"}
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
3
+ * location plus central-directory walk, producing one record per entry.
4
+ *
5
+ * Lived in the runner (src/shared/zip-structure.ts) until the TS server's
6
+ * skill push gate became its second consumer; extracted to backend/libs/ts/
7
+ * under the shared-library rule (a library moves here when a second
8
+ * consumer exists — @stigmer/temporal-codecs is the precedent).
9
+ *
10
+ * Parsing is *central-directory-based* — never a front-to-back walk of
11
+ * local file headers. This is a correctness decision, not a style choice
12
+ * (issues #450 and #567): local headers of streaming entries
13
+ * (general-purpose flag bit 3) carry zeroed sizes, and the only
14
+ * local-only way to recover them is scanning the payload for the
15
+ * data-descriptor signature — which silently truncates any stored entry
16
+ * whose *content* happens to contain those four bytes, and
17
+ * desynchronizes every entry after it. The central directory always
18
+ * carries the real sizes, and every consumer here holds the complete
19
+ * archive bytes, so nothing a local-header walk could offer is needed.
20
+ *
21
+ * This layer maps bytes to entry records and nothing else. Policy — what
22
+ * a structural failure means, which entries are acceptable, how payloads
23
+ * are decoded — belongs to the consumers, and they differ on purpose:
24
+ *
25
+ * - runner shared/zip-extract.ts (skill artifacts): structural failure
26
+ * is NON-FATAL — both editions' push gates validated every artifact
27
+ * with a central-directory-based reader before storage, so a defect
28
+ * here can only be a truncated or corrupted download.
29
+ * - runner shared/attachment-zip.ts (user attachments, both
30
+ * harnesses): structural failure is FAIL-HARD — the input is an
31
+ * untrusted upload and nothing upstream vouched for it.
32
+ * - stigmer-server src/domain/skill/storage/ (the push gate):
33
+ * FAIL-HARD, plus a safearchive-parity entry pre-filter decoded from
34
+ * the raw creator/attribute fields exposed below.
35
+ *
36
+ * ZIP64 is deliberately unsupported: every consumer caps input far below
37
+ * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
38
+ * attachment uploads: 10MB).
39
+ *
40
+ * Throws plain `Error`s on structural defects (no valid EOCD, a central
41
+ * directory or local header record that does not parse, a payload slice
42
+ * that runs past the end of the buffer); consumers translate those into
43
+ * their own error models.
44
+ */
45
+ // ─── Format constants ────────────────────────────────────────────────────
46
+ const LOCAL_FILE_HEADER_SIG = 0x04034b50;
47
+ const CENTRAL_DIRECTORY_SIG = 0x02014b50;
48
+ const EOCD_SIG = 0x06054b50;
49
+ /** Fixed size of the EOCD record, excluding the variable-length comment. */
50
+ export const EOCD_MIN_SIZE = 22;
51
+ /** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
52
+ const EOCD_MAX_COMMENT = 0xffff;
53
+ const LOCAL_HEADER_SIZE = 30;
54
+ const CD_RECORD_SIZE = 46;
55
+ // ─── Parsing ─────────────────────────────────────────────────────────────
56
+ /**
57
+ * Enumerate the archive's entries from its central directory.
58
+ *
59
+ * Throws on any structural defect: no valid EOCD, a central directory or
60
+ * local header record that doesn't parse, or a payload slice that runs
61
+ * past the end of the buffer.
62
+ */
63
+ export function parseZipStructure(data) {
64
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
65
+ const eocd = findEndOfCentralDirectory(data, view);
66
+ const entries = [];
67
+ let offset = eocd.centralDirectoryOffset;
68
+ for (let i = 0; i < eocd.entryCount; i++) {
69
+ if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
70
+ throw new Error(`invalid central directory record at offset ${offset}`);
71
+ }
72
+ const versionMadeBy = view.getUint16(offset + 4, true);
73
+ const compressionMethod = view.getUint16(offset + 10, true);
74
+ const crc32 = view.getUint32(offset + 16, true);
75
+ const compressedSize = view.getUint32(offset + 20, true);
76
+ const uncompressedSize = view.getUint32(offset + 24, true);
77
+ const fileNameLength = view.getUint16(offset + 28, true);
78
+ const extraFieldLength = view.getUint16(offset + 30, true);
79
+ const commentLength = view.getUint16(offset + 32, true);
80
+ const externalAttributes = view.getUint32(offset + 38, true);
81
+ const localHeaderOffset = view.getUint32(offset + 42, true);
82
+ const fileName = new TextDecoder().decode(data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength));
83
+ // The local header is consulted for one thing only: the payload start.
84
+ // Its name/extra field lengths can legitimately differ from the central
85
+ // directory's (streaming writers pad the extra field), so the offset
86
+ // cannot be derived from CD fields alone.
87
+ const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
88
+ if (dataStart + compressedSize > data.length) {
89
+ throw new Error(`entry "${fileName}" payload runs past end of archive`);
90
+ }
91
+ entries.push({
92
+ name: fileName,
93
+ isDirectory: fileName.endsWith("/"),
94
+ compressionMethod,
95
+ uncompressedSize,
96
+ compressedSize,
97
+ crc32,
98
+ versionMadeBy,
99
+ externalAttributes,
100
+ compressedData: data.subarray(dataStart, dataStart + compressedSize),
101
+ });
102
+ offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
103
+ }
104
+ return entries;
105
+ }
106
+ /**
107
+ * Locate and validate the end-of-central-directory record.
108
+ *
109
+ * Scans backward from the end of the archive across the maximum comment
110
+ * span. A signature match alone is not trusted (the four bytes can occur
111
+ * inside a trailing comment): the record must also point at an offset
112
+ * that actually holds a central directory record, or be a genuinely
113
+ * empty archive.
114
+ */
115
+ function findEndOfCentralDirectory(data, view) {
116
+ const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
117
+ for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
118
+ if (view.getUint32(pos, true) !== EOCD_SIG)
119
+ continue;
120
+ const entryCount = view.getUint16(pos + 10, true);
121
+ const centralDirectorySize = view.getUint32(pos + 12, true);
122
+ const centralDirectoryOffset = view.getUint32(pos + 16, true);
123
+ const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
124
+ const directoryLooksReal = entryCount === 0 ||
125
+ (centralDirectoryOffset + 4 <= data.length &&
126
+ view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
127
+ if (directoryEndsAtRecord && directoryLooksReal) {
128
+ return { centralDirectoryOffset, entryCount };
129
+ }
130
+ }
131
+ throw new Error("no end-of-central-directory record found");
132
+ }
133
+ // ─── CRC-32 ──────────────────────────────────────────────────────────────
134
+ // Standard CRC-32 (IEEE 802.3, the ZIP checksum), table-driven so callers
135
+ // hashing payloads at the consumers' 100 MB caps stay cheap.
136
+ const CRC_TABLE = (() => {
137
+ const table = new Uint32Array(256);
138
+ for (let n = 0; n < 256; n++) {
139
+ let c = n;
140
+ for (let bit = 0; bit < 8; bit++) {
141
+ c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
142
+ }
143
+ table[n] = c;
144
+ }
145
+ return table;
146
+ })();
147
+ /**
148
+ * CRC-32 of `data` — the ZIP checksum. Exported for consumers that verify
149
+ * decompressed payloads against an entry's declared `crc32` (Go's
150
+ * archive/zip does this on every read) and for the fixture builder.
151
+ */
152
+ export function crc32(data) {
153
+ let crc = 0xffffffff;
154
+ for (let i = 0; i < data.length; i++) {
155
+ crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ data[i]) & 0xff];
156
+ }
157
+ return (crc ^ 0xffffffff) >>> 0;
158
+ }
159
+ /** Resolve where an entry's payload begins, from its local file header. */
160
+ function payloadStart(data, view, localHeaderOffset, fileName) {
161
+ if (localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
162
+ view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG) {
163
+ throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
164
+ }
165
+ const localNameLength = view.getUint16(localHeaderOffset + 26, true);
166
+ const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
167
+ return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
168
+ }
169
+ //# sourceMappingURL=zip-structure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-structure.js","sourceRoot":"","sources":["src/zip-structure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AA2CH,4EAA4E;AAE5E,MAAM,qBAAqB,GAAG,UAAU,CAAC;AACzC,MAAM,qBAAqB,GAAG,UAAU,CAAC;AACzC,MAAM,QAAQ,GAAG,UAAU,CAAC;AAE5B,4EAA4E;AAC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