@stigmer/runner 3.12.0 → 3.12.1

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 (41) hide show
  1. package/dist/.build-fingerprint +1 -1
  2. package/dist/activities/execute-deep-agent/attachment-injector.d.ts +23 -8
  3. package/dist/activities/execute-deep-agent/attachment-injector.js +104 -105
  4. package/dist/activities/execute-deep-agent/attachment-injector.js.map +1 -1
  5. package/dist/activities/execute-deep-agent/prompt-builder.js +11 -1
  6. package/dist/activities/execute-deep-agent/prompt-builder.js.map +1 -1
  7. package/dist/activities/execute-deep-agent/setup.js +7 -3
  8. package/dist/activities/execute-deep-agent/setup.js.map +1 -1
  9. package/dist/middleware/path-normalization.d.ts +15 -2
  10. package/dist/middleware/path-normalization.js +39 -5
  11. package/dist/middleware/path-normalization.js.map +1 -1
  12. package/dist/shared/mcp-enabled-tools.d.ts +6 -2
  13. package/dist/shared/mcp-enabled-tools.js +6 -2
  14. package/dist/shared/mcp-enabled-tools.js.map +1 -1
  15. package/dist/shared/plan-mode-permissions.d.ts +46 -10
  16. package/dist/shared/plan-mode-permissions.js +56 -12
  17. package/dist/shared/plan-mode-permissions.js.map +1 -1
  18. package/dist/shared/zip-extract.d.ts +24 -6
  19. package/dist/shared/zip-extract.js +31 -90
  20. package/dist/shared/zip-extract.js.map +1 -1
  21. package/dist/shared/zip-structure.d.ts +61 -0
  22. package/dist/shared/zip-structure.js +128 -0
  23. package/dist/shared/zip-structure.js.map +1 -0
  24. package/package.json +2 -2
  25. package/src/__test-utils__/zip-fixtures.ts +206 -0
  26. package/src/activities/execute-cursor/__tests__/skill-resolver.test.ts +3 -42
  27. package/src/activities/execute-deep-agent/__tests__/attachment-injector.test.ts +165 -126
  28. package/src/activities/execute-deep-agent/__tests__/plan-mode-path-normalization.test.ts +246 -37
  29. package/src/activities/execute-deep-agent/__tests__/prompt-builder.test.ts +7 -2
  30. package/src/activities/execute-deep-agent/__tests__/subagent-plan-mode-permissions.test.ts +35 -4
  31. package/src/activities/execute-deep-agent/attachment-injector.ts +146 -142
  32. package/src/activities/execute-deep-agent/prompt-builder.ts +11 -1
  33. package/src/activities/execute-deep-agent/setup.ts +7 -3
  34. package/src/middleware/__tests__/path-normalization.test.ts +29 -3
  35. package/src/middleware/path-normalization.ts +42 -5
  36. package/src/shared/__tests__/plan-mode-permissions.test.ts +58 -0
  37. package/src/shared/__tests__/zip-extract.test.ts +106 -89
  38. package/src/shared/mcp-enabled-tools.ts +6 -2
  39. package/src/shared/plan-mode-permissions.ts +59 -12
  40. package/src/shared/zip-extract.ts +35 -117
  41. package/src/shared/zip-structure.ts +181 -0
@@ -0,0 +1,181 @@
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
+ * Parsing is *central-directory-based* — never a front-to-back walk of
6
+ * local file headers. This is a correctness decision, not a style choice
7
+ * (issues #450 and #567): local headers of streaming entries
8
+ * (general-purpose flag bit 3) carry zeroed sizes, and the only
9
+ * local-only way to recover them is scanning the payload for the
10
+ * data-descriptor signature — which silently truncates any stored entry
11
+ * whose *content* happens to contain those four bytes, and
12
+ * desynchronizes every entry after it. The central directory always
13
+ * carries the real sizes, and every consumer here holds the complete
14
+ * archive bytes, so nothing a local-header walk could offer is needed.
15
+ *
16
+ * This layer maps bytes to entry records and nothing else. Policy — what
17
+ * a structural failure means, which entries are acceptable, how payloads
18
+ * are decoded — belongs to the consumers, and they differ on purpose:
19
+ *
20
+ * - shared/zip-extract.ts (skill artifacts): structural failure is
21
+ * NON-FATAL — both editions' push gates validated every artifact
22
+ * with a central-directory-based reader before storage, so a defect
23
+ * here can only be a truncated or corrupted download.
24
+ * - execute-deep-agent/attachment-injector.ts (user attachments):
25
+ * structural failure is FAIL-HARD — the input is an untrusted
26
+ * upload and nothing upstream vouched for it.
27
+ *
28
+ * ZIP64 is deliberately unsupported: both consumers cap input far below
29
+ * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
30
+ * attachment uploads: 10MB).
31
+ *
32
+ * Throws plain `Error`s on structural defects (no valid EOCD, a central
33
+ * directory or local header record that does not parse, a payload slice
34
+ * that runs past the end of the buffer); consumers translate those into
35
+ * their own error models.
36
+ */
37
+
38
+ // ─── Entry records ───────────────────────────────────────────────────────
39
+
40
+ export interface ZipStructuralEntry {
41
+ /** Entry path exactly as recorded in the central directory. */
42
+ readonly name: string;
43
+ readonly isDirectory: boolean;
44
+ readonly compressionMethod: number;
45
+ /**
46
+ * The central directory's declared uncompressed size — authoritative for
47
+ * pre-extraction accounting (e.g. ZIP-bomb budgeting), but still a
48
+ * *declaration*: a consumer that distrusts its input must enforce it
49
+ * against the actual decompressed output.
50
+ */
51
+ readonly uncompressedSize: number;
52
+ /** The entry's raw payload slice (a view, not a copy) of the archive buffer. */
53
+ readonly compressedData: Uint8Array;
54
+ }
55
+
56
+ // ─── Format constants ────────────────────────────────────────────────────
57
+
58
+ const LOCAL_FILE_HEADER_SIG = 0x04034b50;
59
+ const CENTRAL_DIRECTORY_SIG = 0x02014b50;
60
+ const EOCD_SIG = 0x06054b50;
61
+
62
+ /** Fixed size of the EOCD record, excluding the variable-length comment. */
63
+ export const EOCD_MIN_SIZE = 22;
64
+ /** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
65
+ const EOCD_MAX_COMMENT = 0xffff;
66
+
67
+ const LOCAL_HEADER_SIZE = 30;
68
+ const CD_RECORD_SIZE = 46;
69
+
70
+ // ─── Parsing ─────────────────────────────────────────────────────────────
71
+
72
+ /**
73
+ * Enumerate the archive's entries from its central directory.
74
+ *
75
+ * Throws on any structural defect: no valid EOCD, a central directory or
76
+ * local header record that doesn't parse, or a payload slice that runs
77
+ * past the end of the buffer.
78
+ */
79
+ export function parseZipStructure(data: Uint8Array): ZipStructuralEntry[] {
80
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
81
+ const eocd = findEndOfCentralDirectory(data, view);
82
+
83
+ const entries: ZipStructuralEntry[] = [];
84
+ let offset = eocd.centralDirectoryOffset;
85
+
86
+ for (let i = 0; i < eocd.entryCount; i++) {
87
+ if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
88
+ throw new Error(`invalid central directory record at offset ${offset}`);
89
+ }
90
+
91
+ const compressionMethod = view.getUint16(offset + 10, true);
92
+ const compressedSize = view.getUint32(offset + 20, true);
93
+ const uncompressedSize = view.getUint32(offset + 24, true);
94
+ const fileNameLength = view.getUint16(offset + 28, true);
95
+ const extraFieldLength = view.getUint16(offset + 30, true);
96
+ const commentLength = view.getUint16(offset + 32, true);
97
+ const localHeaderOffset = view.getUint32(offset + 42, true);
98
+
99
+ const fileName = new TextDecoder().decode(
100
+ data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength),
101
+ );
102
+
103
+ // The local header is consulted for one thing only: the payload start.
104
+ // Its name/extra field lengths can legitimately differ from the central
105
+ // directory's (streaming writers pad the extra field), so the offset
106
+ // cannot be derived from CD fields alone.
107
+ const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
108
+ if (dataStart + compressedSize > data.length) {
109
+ throw new Error(`entry "${fileName}" payload runs past end of archive`);
110
+ }
111
+
112
+ entries.push({
113
+ name: fileName,
114
+ isDirectory: fileName.endsWith("/"),
115
+ compressionMethod,
116
+ uncompressedSize,
117
+ compressedData: data.subarray(dataStart, dataStart + compressedSize),
118
+ });
119
+
120
+ offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
121
+ }
122
+
123
+ return entries;
124
+ }
125
+
126
+ interface EndOfCentralDirectory {
127
+ centralDirectoryOffset: number;
128
+ entryCount: number;
129
+ }
130
+
131
+ /**
132
+ * Locate and validate the end-of-central-directory record.
133
+ *
134
+ * Scans backward from the end of the archive across the maximum comment
135
+ * span. A signature match alone is not trusted (the four bytes can occur
136
+ * inside a trailing comment): the record must also point at an offset
137
+ * that actually holds a central directory record, or be a genuinely
138
+ * empty archive.
139
+ */
140
+ function findEndOfCentralDirectory(data: Uint8Array, view: DataView): EndOfCentralDirectory {
141
+ const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
142
+
143
+ for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
144
+ if (view.getUint32(pos, true) !== EOCD_SIG) continue;
145
+
146
+ const entryCount = view.getUint16(pos + 10, true);
147
+ const centralDirectorySize = view.getUint32(pos + 12, true);
148
+ const centralDirectoryOffset = view.getUint32(pos + 16, true);
149
+
150
+ const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
151
+ const directoryLooksReal =
152
+ entryCount === 0 ||
153
+ (centralDirectoryOffset + 4 <= data.length &&
154
+ view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
155
+
156
+ if (directoryEndsAtRecord && directoryLooksReal) {
157
+ return { centralDirectoryOffset, entryCount };
158
+ }
159
+ }
160
+
161
+ throw new Error("no end-of-central-directory record found");
162
+ }
163
+
164
+ /** Resolve where an entry's payload begins, from its local file header. */
165
+ function payloadStart(
166
+ data: Uint8Array,
167
+ view: DataView,
168
+ localHeaderOffset: number,
169
+ fileName: string,
170
+ ): number {
171
+ if (
172
+ localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
173
+ view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG
174
+ ) {
175
+ throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
176
+ }
177
+
178
+ const localNameLength = view.getUint16(localHeaderOffset + 26, true);
179
+ const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
180
+ return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
181
+ }