@indigoai-us/hq-cli 5.32.0 → 5.33.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.
@@ -0,0 +1,459 @@
1
+ /**
2
+ * safe-extract adversarial suite (US-020).
3
+ *
4
+ * These tests assert the hardened extractor refuses hostile archives — zip-slip
5
+ * / `..` traversal, absolute-path entries, symlink/hardlink escape,
6
+ * decompression-bomb caps, and mid-extract failure rollback. This is the seam
7
+ * US-024's adversarial suite also hits; these are the first line.
8
+ *
9
+ * MALICIOUS FIXTURES ARE BUILT DETERMINISTICALLY (writeMaliciousTarGz), NOT by
10
+ * shelling to the system `tar`. GNU tar (Linux / CI / prod) STRIPS the leading
11
+ * `../` and absolute `/` when CREATING an archive, so a "malicious" fixture
12
+ * built via `tar -c` is benign on Linux and the guard has nothing to reject —
13
+ * the test passes on macOS bsdtar (preserves them) but fails on Linux. By
14
+ * emitting the tar bytes ourselves the hostile entry names are recorded
15
+ * verbatim and identically on every platform; the READING side (`tar -tvf`,
16
+ * which the pre-flight uses) never strips, so the guard sees the real attack
17
+ * everywhere. BENIGN fixtures (in-tree names that no tar strips) still use the
18
+ * system `tar` for fidelity to the real extraction path.
19
+ *
20
+ * Everything is contained to per-test tmp dirs — nothing is ever written
21
+ * outside an os.tmpdir() scratch tree.
22
+ */
23
+
24
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
25
+ import { execFileSync } from 'child_process';
26
+ import * as fs from 'fs';
27
+ import * as os from 'os';
28
+ import * as path from 'path';
29
+ import { writeMaliciousTarGz } from './__fixtures__/make-tar.js';
30
+ import {
31
+ DEFAULT_CAPS,
32
+ UnsafeArchiveError,
33
+ isContained,
34
+ resolveContainedPath,
35
+ assertLinkTargetContained,
36
+ parseTarListing,
37
+ validateEntries,
38
+ safeExtractTarball,
39
+ type ExtractCaps,
40
+ type TarEntry,
41
+ } from './safe-extract.js';
42
+
43
+ // ---------------------------------------------------------------------------
44
+ // Helpers — build tarballs from a scratch source tree.
45
+ // ---------------------------------------------------------------------------
46
+
47
+ let scratch: string;
48
+
49
+ beforeEach(() => {
50
+ scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'safe-extract-test-'));
51
+ });
52
+ afterEach(() => {
53
+ fs.rmSync(scratch, { recursive: true, force: true });
54
+ });
55
+
56
+ /** Make a gzip tarball from explicit member paths inside `srcRoot`. */
57
+ function tarball(name: string, srcRoot: string, members: string[]): string {
58
+ const out = path.join(scratch, name);
59
+ execFileSync('tar', ['-czf', out, '-C', srcRoot, ...members]);
60
+ return out;
61
+ }
62
+
63
+ // ---------------------------------------------------------------------------
64
+ // Unit: containment primitives
65
+ // ---------------------------------------------------------------------------
66
+
67
+ describe('containment primitives', () => {
68
+ it('isContained: nested true, sibling-prefix false, self true', () => {
69
+ expect(isContained('/a/base', '/a/base/x/y')).toBe(true);
70
+ expect(isContained('/a/base', '/a/base')).toBe(true);
71
+ expect(isContained('/a/base', '/a/baseEVIL')).toBe(false); // trailing-sep guard
72
+ expect(isContained('/a/base', '/a/other')).toBe(false);
73
+ });
74
+
75
+ it('resolveContainedPath: rejects `..` traversal', () => {
76
+ expect(() => resolveContainedPath('/tmp/target', '../escape')).toThrow(
77
+ UnsafeArchiveError,
78
+ );
79
+ expect(() => resolveContainedPath('/tmp/target', 'a/../../escape')).toThrow(
80
+ UnsafeArchiveError,
81
+ );
82
+ });
83
+
84
+ it('resolveContainedPath: rejects absolute entry names', () => {
85
+ expect(() => resolveContainedPath('/tmp/target', '/etc/passwd')).toThrow(
86
+ UnsafeArchiveError,
87
+ );
88
+ });
89
+
90
+ it('resolveContainedPath: accepts an in-tree relative name', () => {
91
+ const r = resolveContainedPath('/tmp/target', 'sub/dir/file');
92
+ expect(r).toBe(path.resolve('/tmp/target', 'sub/dir/file'));
93
+ });
94
+
95
+ it('assertLinkTargetContained: rejects absolute escape and `..` escape', () => {
96
+ const entryAbs = '/tmp/pack/link';
97
+ expect(() => assertLinkTargetContained('/tmp/pack', entryAbs, '/etc')).toThrow(
98
+ UnsafeArchiveError,
99
+ );
100
+ expect(() =>
101
+ assertLinkTargetContained('/tmp/pack', entryAbs, '../../etc'),
102
+ ).toThrow(UnsafeArchiveError);
103
+ // In-tree relative target is fine.
104
+ expect(() =>
105
+ assertLinkTargetContained('/tmp/pack', entryAbs, './sibling'),
106
+ ).not.toThrow();
107
+ });
108
+ });
109
+
110
+ // ---------------------------------------------------------------------------
111
+ // Unit: tar listing parser
112
+ // ---------------------------------------------------------------------------
113
+
114
+ describe('parseTarListing', () => {
115
+ it('parses files, dirs, and symlinks (bsdtar verbose form)', () => {
116
+ const sample = [
117
+ 'drwxr-xr-x 0 user group 0 Jan 1 00:00 package/',
118
+ '-rw-r--r-- 0 user group 1234 Jan 1 00:00 package/file.txt',
119
+ 'lrwxr-xr-x 0 user group 0 Jan 1 00:00 package/link -> ../../etc/passwd',
120
+ ].join('\n');
121
+ const entries = parseTarListing(sample);
122
+ expect(entries).toEqual<TarEntry[]>([
123
+ { type: 'dir', name: 'package', size: 0, linkTarget: undefined },
124
+ { type: 'file', name: 'package/file.txt', size: 1234, linkTarget: undefined },
125
+ {
126
+ type: 'symlink',
127
+ name: 'package/link',
128
+ size: 0,
129
+ linkTarget: '../../etc/passwd',
130
+ },
131
+ ]);
132
+ });
133
+
134
+ // SECURITY-CRITICAL CROSS-PLATFORM REGRESSION. GNU tar (Linux / prod
135
+ // Lambdas) renders `tar -tvf` with a slash-joined owner/group, NO link-count
136
+ // column, and an ISO date (`YYYY-MM-DD HH:MM`) — unlike bsdtar's
137
+ // `Mon DD HH:MM`. A parser that anchors on the month abbreviation matches
138
+ // NOTHING under GNU tar and returns [], so the pre-flight containment scan
139
+ // sees zero entries and every malicious archive sails through. This guards
140
+ // that the parser stays format-agnostic across both tar implementations.
141
+ it('parses GNU tar verbose form (ISO date, slash owner, no link-count col)', () => {
142
+ const gnu = [
143
+ 'drwxr-xr-x user/group 0 2026-01-01 00:00 package/',
144
+ '-rw-r--r-- user/group 1234 2026-01-01 00:00 package/file.txt',
145
+ 'lrwxrwxrwx user/group 0 2026-01-01 00:00 package/link -> ../../etc/passwd',
146
+ 'hrw-r--r-- user/group 0 2026-01-01 00:00 package/hard link to package/file.txt',
147
+ '-rw-r--r-- user/group 11 2026-06-05 09:20:33 with-seconds.txt',
148
+ ].join('\n');
149
+ const entries = parseTarListing(gnu);
150
+ expect(entries).toEqual<TarEntry[]>([
151
+ { type: 'dir', name: 'package', size: 0, linkTarget: undefined },
152
+ { type: 'file', name: 'package/file.txt', size: 1234, linkTarget: undefined },
153
+ {
154
+ type: 'symlink',
155
+ name: 'package/link',
156
+ size: 0,
157
+ linkTarget: '../../etc/passwd',
158
+ },
159
+ {
160
+ type: 'hardlink',
161
+ name: 'package/hard',
162
+ size: 0,
163
+ linkTarget: 'package/file.txt',
164
+ },
165
+ { type: 'file', name: 'with-seconds.txt', size: 11, linkTarget: undefined },
166
+ ]);
167
+ });
168
+
169
+ // SECURITY-CRITICAL: the GNU `h... name link to target` hardlink form must be
170
+ // parsed into a hardlink entry whose extracted target is then FLAGGED AS
171
+ // ESCAPING by the containment check — proving the end-to-end guard rejects an
172
+ // escaping hardlink under GNU tar even though CI only runs GNU and we can only
173
+ // emit a synthetic GNU listing locally (bsdtar). We feed a hand-written
174
+ // GNU-format listing line straight to parseTarListing, then push the parsed
175
+ // entry through validateEntries (the same code path safeExtractTarball uses)
176
+ // and assert it throws. This is the platform-variance backstop: even though
177
+ // the fixture bytes are identical everywhere, this proves the GNU LISTING
178
+ // string parses + rejects.
179
+ it('extracts an escaping target from a synthetic GNU hardlink listing line and flags it as escaping', () => {
180
+ const gnuHardlink =
181
+ 'hrw-r--r-- 0/0 0 1970-01-01 00:00 escape-hard link to ../../../../etc/passwd';
182
+ const parsed = parseTarListing(gnuHardlink);
183
+ expect(parsed).toEqual<TarEntry[]>([
184
+ {
185
+ type: 'hardlink',
186
+ name: 'escape-hard',
187
+ size: 0,
188
+ linkTarget: '../../../../etc/passwd',
189
+ },
190
+ ]);
191
+ // The extracted target is fed through the SAME containment check the
192
+ // pre-flight uses, and it must reject (escaping hardlink → UnsafeArchiveError).
193
+ expect(() => validateEntries(parsed, '/tmp/pack-target')).toThrow(
194
+ UnsafeArchiveError,
195
+ );
196
+ // And it is specifically the LINK-ESCAPE check that fires (not name traversal).
197
+ expect(() => validateEntries(parsed, '/tmp/pack-target')).toThrow(
198
+ /Link escape blocked/,
199
+ );
200
+ });
201
+
202
+ it('returns [] only for genuinely empty input — never silently on a real listing', () => {
203
+ // A non-empty listing in EITHER format must yield entries; an all-noise or
204
+ // empty string yields []. This catches a regression where one tar dialect
205
+ // parses to zero entries (the cross-platform hole).
206
+ expect(parseTarListing('')).toEqual([]);
207
+ expect(parseTarListing('\n \n')).toEqual([]);
208
+ expect(
209
+ parseTarListing('-rw-r--r-- user/group 5 2026-01-01 00:00 a').length,
210
+ ).toBe(1);
211
+ expect(
212
+ parseTarListing('-rw-r--r-- 0 u g 5 Jan 1 00:00 a').length,
213
+ ).toBe(1);
214
+ });
215
+ });
216
+
217
+ // ---------------------------------------------------------------------------
218
+ // Guard 1: zip-slip / path traversal — END TO END through a real tarball
219
+ // ---------------------------------------------------------------------------
220
+
221
+ describe('GUARD 1: zip-slip / traversal (end-to-end)', () => {
222
+ it('rejects a tarball with a `../` escaping entry; nothing written outside', () => {
223
+ // Build a tarball whose recorded member name genuinely escapes by emitting
224
+ // the tar bytes directly — the stored entry name is literally
225
+ // `../evil-escape` on EVERY platform (GNU tar would have stripped the `../`
226
+ // had we used `tar -c`, defeating the test on Linux).
227
+ const tb = writeMaliciousTarGz(path.join(scratch, 'slip.tgz'), [
228
+ { name: '../evil-escape', content: 'pwned' },
229
+ ]);
230
+
231
+ // Sanity: the recorded entry name does escape with `..` (the READING side
232
+ // never strips, so this holds on bsdtar and GNU tar alike).
233
+ const listing = execFileSync('tar', ['-tf', tb], { encoding: 'utf-8' });
234
+ expect(listing).toMatch(/\.\.\//);
235
+
236
+ const finalDir = path.join(scratch, 'out');
237
+ const canary = path.join(scratch, 'evil-escape'); // sibling of finalDir
238
+ expect(() => safeExtractTarball(tb, finalDir)).toThrow(UnsafeArchiveError);
239
+ // Nothing written outside the (never-created) target.
240
+ expect(fs.existsSync(finalDir)).toBe(false);
241
+ expect(fs.existsSync(canary)).toBe(false);
242
+ // No leftover staging dirs.
243
+ expect(fs.readdirSync(scratch).filter((n) => n.includes('.out.staging-'))).toHaveLength(0);
244
+ });
245
+
246
+ it('rejects an absolute-path entry via validateEntries', () => {
247
+ const entries: TarEntry[] = [
248
+ { type: 'file', name: '/etc/cron.d/evil', size: 10 },
249
+ ];
250
+ expect(() => validateEntries(entries, '/tmp/target')).toThrow(
251
+ /Path traversal blocked/,
252
+ );
253
+ });
254
+
255
+ it('rejects a tarball with an absolute-path entry END TO END; nothing written', () => {
256
+ // Deterministic absolute-path fixture: GNU tar strips the leading `/` on
257
+ // create, so this must be written byte-for-byte to genuinely carry an
258
+ // absolute entry name on Linux too.
259
+ const tb = writeMaliciousTarGz(path.join(scratch, 'abs.tgz'), [
260
+ { name: '/tmp/hq-evil-absolute', content: 'pwned' },
261
+ ]);
262
+
263
+ // Sanity: the recorded entry name is absolute (READING never strips).
264
+ const listing = execFileSync('tar', ['-tf', tb], { encoding: 'utf-8' });
265
+ expect(listing).toMatch(/^\/tmp\/hq-evil-absolute/m);
266
+
267
+ const finalDir = path.join(scratch, 'out-abs');
268
+ expect(() => safeExtractTarball(tb, finalDir)).toThrow(UnsafeArchiveError);
269
+ expect(fs.existsSync(finalDir)).toBe(false);
270
+ // The absolute target was never created (pre-flight rejected before extract).
271
+ expect(fs.existsSync('/tmp/hq-evil-absolute')).toBe(false);
272
+ });
273
+ });
274
+
275
+ // ---------------------------------------------------------------------------
276
+ // Guard 1b: symlink / hardlink escape
277
+ // ---------------------------------------------------------------------------
278
+
279
+ describe('GUARD 1b: symlink / hardlink escape (end-to-end)', () => {
280
+ it('rejects a tarball containing a symlink whose target escapes the pack dir', () => {
281
+ // Deterministic symlink-escape fixture (typeflag '2', linkname escapes).
282
+ // Built byte-for-byte so the escaping linkname is preserved on every
283
+ // platform; the pre-flight link-target containment check rejects it.
284
+ const tb = writeMaliciousTarGz(path.join(scratch, 'symlink.tgz'), [
285
+ { name: 'escape-link', type: 'symlink', linkname: '../../../../etc/passwd' },
286
+ { name: 'ok.txt', content: 'fine' },
287
+ ]);
288
+
289
+ // Sanity: the listing records the escaping link target verbatim.
290
+ const listing = execFileSync('tar', ['-tvf', tb], { encoding: 'utf-8' });
291
+ expect(listing).toMatch(/escape-link -> \.\.\/\.\.\/\.\.\/\.\.\/etc\/passwd/);
292
+
293
+ const finalDir = path.join(scratch, 'out');
294
+ expect(() => safeExtractTarball(tb, finalDir)).toThrow(UnsafeArchiveError);
295
+ expect(fs.existsSync(finalDir)).toBe(false);
296
+ });
297
+
298
+ it('rejects a tarball containing a hardlink whose target escapes the pack dir', () => {
299
+ // Deterministic hardlink-escape fixture (typeflag '1', linkname escapes).
300
+ // The bytes are identical on every platform, but the two `tar` dialects
301
+ // REJECT the escape via DIFFERENT mechanisms, so we assert the SECURITY
302
+ // OUTCOME (throws + nothing escapes + nothing wired), not a specific error
303
+ // type:
304
+ // - bsdtar (macOS) preserves the `../` hardlink target in `-tvf`, so the
305
+ // pre-flight link-target containment check throws UnsafeArchiveError
306
+ // BEFORE extraction.
307
+ // - GNU tar (Linux/prod) STRIPS the leading `../` from hardlink targets
308
+ // on both read and extract, so the listing looks contained and the
309
+ // pre-flight can't flag it — but GNU tar then refuses the link at
310
+ // extract time ("Cannot hard link to 'etc/passwd'") and exits non-zero,
311
+ // so safeExtractTarball throws an extraction error and the atomic
312
+ // staging is rolled back. Either way the escape is neutralized.
313
+ const tb = writeMaliciousTarGz(path.join(scratch, 'hardlink.tgz'), [
314
+ { name: 'escape-hard', type: 'hardlink', linkname: '../../../../etc/passwd' },
315
+ { name: 'ok.txt', content: 'fine' },
316
+ ]);
317
+
318
+ const finalDir = path.join(scratch, 'out-hard');
319
+ const canary = '/etc/passwd-hq-hardlink-canary'; // never created by the guard
320
+ // The escaping hardlink MUST be rejected (strict — it MUST throw); the
321
+ // error TYPE differs by tar dialect (guard UnsafeArchiveError vs tar
322
+ // extraction error), so accept any throw and prove the escape outcome below.
323
+ expect(() => safeExtractTarball(tb, finalDir)).toThrow();
324
+ // Anti-vacuity: nothing wired (no final dir), no staging leftover, and the
325
+ // escaping target was never reachable for a write.
326
+ expect(fs.existsSync(finalDir)).toBe(false);
327
+ expect(fs.existsSync(canary)).toBe(false);
328
+ expect(
329
+ fs.readdirSync(scratch).filter((n) => n.includes('.out-hard.staging-')),
330
+ ).toHaveLength(0);
331
+ });
332
+
333
+ it('accepts a symlink whose target stays inside the pack dir', () => {
334
+ const src = path.join(scratch, 'src-ok');
335
+ fs.mkdirSync(src, { recursive: true });
336
+ fs.writeFileSync(path.join(src, 'real.txt'), 'data');
337
+ fs.symlinkSync('real.txt', path.join(src, 'alias.txt')); // in-tree relative
338
+ const tb = tarball('symlink-ok.tgz', src, ['real.txt', 'alias.txt']);
339
+
340
+ const finalDir = path.join(scratch, 'out-ok');
341
+ expect(() => safeExtractTarball(tb, finalDir)).not.toThrow();
342
+ expect(fs.existsSync(path.join(finalDir, 'real.txt'))).toBe(true);
343
+ expect(fs.lstatSync(path.join(finalDir, 'alias.txt')).isSymbolicLink()).toBe(true);
344
+ });
345
+ });
346
+
347
+ // ---------------------------------------------------------------------------
348
+ // Guard 2: decompression-bomb caps
349
+ // ---------------------------------------------------------------------------
350
+
351
+ describe('GUARD 2: decompression-bomb caps', () => {
352
+ const tinyCaps: ExtractCaps = {
353
+ maxUncompressedBytes: 1024,
354
+ maxFileCount: 5,
355
+ maxSingleFileBytes: 512,
356
+ };
357
+
358
+ it('aborts on file-count cap (many entries) before extracting', () => {
359
+ const src = path.join(scratch, 'many');
360
+ fs.mkdirSync(src, { recursive: true });
361
+ const members: string[] = [];
362
+ for (let i = 0; i < 20; i++) {
363
+ const n = `f${i}.txt`;
364
+ fs.writeFileSync(path.join(src, n), 'x');
365
+ members.push(n);
366
+ }
367
+ const tb = tarball('many.tgz', src, members);
368
+
369
+ const finalDir = path.join(scratch, 'out-many');
370
+ expect(() => safeExtractTarball(tb, finalDir, { caps: tinyCaps })).toThrow(
371
+ /Decompression-bomb guard.*entries/,
372
+ );
373
+ expect(fs.existsSync(finalDir)).toBe(false);
374
+ });
375
+
376
+ it('aborts on per-file size cap', () => {
377
+ const src = path.join(scratch, 'big');
378
+ fs.mkdirSync(src, { recursive: true });
379
+ fs.writeFileSync(path.join(src, 'big.bin'), Buffer.alloc(2048, 0)); // > 512
380
+ const tb = tarball('big.tgz', src, ['big.bin']);
381
+
382
+ const finalDir = path.join(scratch, 'out-big');
383
+ expect(() => safeExtractTarball(tb, finalDir, { caps: tinyCaps })).toThrow(
384
+ /per-file limit/,
385
+ );
386
+ expect(fs.existsSync(finalDir)).toBe(false);
387
+ });
388
+
389
+ it('aborts on cumulative uncompressed size cap', () => {
390
+ const src = path.join(scratch, 'cum');
391
+ fs.mkdirSync(src, { recursive: true });
392
+ // Each 400 bytes (< per-file 512) but together > 1024 total.
393
+ for (const n of ['a', 'b', 'c']) {
394
+ fs.writeFileSync(path.join(src, n), Buffer.alloc(400, 0));
395
+ }
396
+ const tb = tarball('cum.tgz', src, ['a', 'b', 'c']);
397
+
398
+ const finalDir = path.join(scratch, 'out-cum');
399
+ expect(() => safeExtractTarball(tb, finalDir, { caps: tinyCaps })).toThrow(
400
+ /cumulative uncompressed size exceeded/,
401
+ );
402
+ expect(fs.existsSync(finalDir)).toBe(false);
403
+ });
404
+
405
+ it('default caps are sane (documented constants present)', () => {
406
+ expect(DEFAULT_CAPS.maxUncompressedBytes).toBe(256 * 1024 * 1024);
407
+ expect(DEFAULT_CAPS.maxFileCount).toBe(20_000);
408
+ expect(DEFAULT_CAPS.maxSingleFileBytes).toBe(64 * 1024 * 1024);
409
+ });
410
+ });
411
+
412
+ // ---------------------------------------------------------------------------
413
+ // Guard 3: staged + atomic — mid-extract failure rolls back fully
414
+ // ---------------------------------------------------------------------------
415
+
416
+ describe('GUARD 3: atomic staging + rollback', () => {
417
+ function benignTarball(): string {
418
+ const src = path.join(scratch, 'benign');
419
+ fs.mkdirSync(path.join(src, 'package'), { recursive: true });
420
+ fs.writeFileSync(path.join(src, 'package', 'package.yaml'), 'name: hq-pack-x\n');
421
+ fs.writeFileSync(path.join(src, 'package', 'README.md'), '# x\n');
422
+ return tarball('benign.tgz', src, ['package']);
423
+ }
424
+
425
+ it('commits atomically on success — final dir appears, no staging left', () => {
426
+ const tb = benignTarball();
427
+ const finalDir = path.join(scratch, 'committed');
428
+ safeExtractTarball(tb, finalDir);
429
+ expect(fs.existsSync(path.join(finalDir, 'package', 'package.yaml'))).toBe(true);
430
+ expect(fs.readdirSync(scratch).filter((n) => n.includes('.committed.staging-'))).toHaveLength(0);
431
+ });
432
+
433
+ it('mid-extract failure aborts: NO final dir, NO staging dir remains', () => {
434
+ const tb = benignTarball();
435
+ const finalDir = path.join(scratch, 'failed');
436
+ expect(() =>
437
+ safeExtractTarball(tb, finalDir, {
438
+ afterExtractHook: () => {
439
+ throw new Error('injected mid-extract failure');
440
+ },
441
+ }),
442
+ ).toThrow(/injected mid-extract failure/);
443
+
444
+ // Full rollback: nothing wired, nothing staged.
445
+ expect(fs.existsSync(finalDir)).toBe(false);
446
+ const leftovers = fs.readdirSync(scratch).filter((n) => n.includes('.failed.staging-'));
447
+ expect(leftovers).toHaveLength(0);
448
+ });
449
+
450
+ it('refuses to extract over an existing final dir (no clobber)', () => {
451
+ const tb = benignTarball();
452
+ const finalDir = path.join(scratch, 'exists');
453
+ fs.mkdirSync(finalDir, { recursive: true });
454
+ fs.writeFileSync(path.join(finalDir, 'keep.txt'), 'precious');
455
+ expect(() => safeExtractTarball(tb, finalDir)).toThrow(/refusing to extract over/);
456
+ // Pre-existing content untouched.
457
+ expect(fs.readFileSync(path.join(finalDir, 'keep.txt'), 'utf-8')).toBe('precious');
458
+ });
459
+ });