1
+import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
2
+import { tmpdir } from 'node:os';
3
+import * as path from 'node:path';
4
+import { fileURLToPath } from 'node:url';
5
+import { afterAll, describe, expect, it } from 'vitest';
6
+
7
+import {
8
+ assertBytesMatchIntegrity,
9
+ expectedIntegrityFor,
10
+ findLockfilePath,
11
+ loadLockfileIntegrity,
12
+ parseLockfileIntegrity,
13
+ TarballIntegrityError,
14
+} from '../../../scripts/_tarball-integrity';
15
+
16
+/**
17
+ * A pnpm v9 lockfile excerpt covering the shapes that matter: a quoted scoped
18
+ * key, an unquoted bare key, an entry whose resolution carries no integrity,
19
+ * and a `snapshots:` repeat of a key already seen in `packages:`.
20
+ */
21
+const LOCKFILE = `lockfileVersion: '9.0'
22
+
23
+packages:
24
+
25
+ '@zed-industries/codex-acp-linux-x64@0.16.0':
26
+ resolution: {integrity: sha512-xs5zZBLpJuciEbZNx6ZSNL0qCa9h3i/zWpj40sp6QtF+L4Ow/7qzHdBzboGhHdcz1jrLedfZeRFDA2Elj8TLMA==}
27
+ cpu: [x64]
28
+ os: [linux]
29
+ hasBin: true
30
+
31
+ extract-zip@2.0.1:
32
+ resolution: {integrity: sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==}
33
+ engines: {node: '>= 10.17.0'}
34
+
35
+ some-git-dep@1.0.0:
36
+ resolution: {type: git, repo: git@example.test:owner/repo.git, commit: abc123}
37
+
38
+snapshots:
39
+
40
+ extract-zip@2.0.1:
41
+ dependencies:
42
+ debug: 4.4.3
43
+`;
44
+
45
+const integrityOf = async (bytes: Uint8Array, prefix = 'sha512'): Promise<string> => {
46
+ const algorithm = prefix === 'sha512' ? 'SHA-512' : 'SHA-256';
47
+ const digest = await crypto.subtle.digest(algorithm, bytes as BufferSource);
48
+ return `${prefix}-${btoa(String.fromCharCode(...new Uint8Array(digest)))}`;
49
+};
50
+
51
+const tempDirs: Array<string> = [];
52
+
53
+const makeTempDir = async (): Promise<string> => {
54
+ const dir = await mkdtemp(path.join(tmpdir(), 'tarball-integrity-'));
55
+ tempDirs.push(dir);
56
+ return dir;
57
+};
58
+
59
+afterAll(async () => {
60
+ await Promise.all(tempDirs.map(dir => rm(dir, { force: true, recursive: true })));
61
+});
62
+
63
+describe('parseLockfileIntegrity', () => {
64
+ it('reads quoted scoped keys and unquoted bare keys', () => {
65
+ const parsed = parseLockfileIntegrity(LOCKFILE);
66
+
67
+ expect(parsed.get('@zed-industries/codex-acp-linux-x64@0.16.0')).toBe(
68
+ 'sha512-xs5zZBLpJuciEbZNx6ZSNL0qCa9h3i/zWpj40sp6QtF+L4Ow/7qzHdBzboGhHdcz1jrLedfZeRFDA2Elj8TLMA=='
69
+ );
70
+ expect(parsed.get('extract-zip@2.0.1')).toBe(
71
+ 'sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg=='
72
+ );
73
+ });
74
+
75
+ it('skips resolutions that carry no integrity', () => {
76
+ expect(parseLockfileIntegrity(LOCKFILE).has('some-git-dep@1.0.0')).toBe(false);
77
+ });
78
+
79
+ it('is empty for a lockfile with no packages', () => {
80
+ expect(parseLockfileIntegrity("lockfileVersion: '9.0'\n").size).toBe(0);
81
+ });
82
+});
83
+
84
+describe('expectedIntegrityFor', () => {
85
+ const parsed = parseLockfileIntegrity(LOCKFILE);
86
+
87
+ it('returns the pinned hash for a package the lockfile resolved', () => {
88
+ expect(expectedIntegrityFor(parsed, 'extract-zip', '2.0.1')).toMatch(/^sha512-/);
89
+ });
90
+
91
+ it('refuses a package the lockfile never resolved', () => {
92
+ expect(() => expectedIntegrityFor(parsed, 'extract-zip', '9.9.9')).toThrow(
93
+ TarballIntegrityError
94
+ );
95
+ });
96
+
97
+ it('refuses a version the lockfile pins under a different number', () => {
98
+ expect(() =>
99
+ expectedIntegrityFor(parsed, '@zed-industries/codex-acp-linux-x64', '0.17.0')
100
+ ).toThrow(/no integrity hash/);
101
+ });
102
+});
103
+
104
+describe('assertBytesMatchIntegrity', () => {
105
+ const bytes = new TextEncoder().encode('codex-acp binary bytes');
106
+
107
+ it('accepts bytes that hash to the expected value', async () => {
108
+ await expect(
109
+ assertBytesMatchIntegrity(bytes, await integrityOf(bytes), 'pkg@1.0.0')
110
+ ).resolves.toBeUndefined();
111
+ });
112
+
113
+ it('rejects bytes swapped after the hash was pinned', async () => {
114
+ const tampered = new TextEncoder().encode('codex-acp binary bytes + backdoor');
115
+
116
+ await expect(
117
+ assertBytesMatchIntegrity(tampered, await integrityOf(bytes), 'pkg@1.0.0')
118
+ ).rejects.toThrow(TarballIntegrityError);
119
+ });
120
+
121
+ it('rejects a single flipped byte', async () => {
122
+ const tampered = new Uint8Array(bytes);
123
+ tampered[0] ^= 0xff;
124
+
125
+ await expect(
126
+ assertBytesMatchIntegrity(tampered, await integrityOf(bytes), 'pkg@1.0.0')
127
+ ).rejects.toThrow(TarballIntegrityError);
128
+ });
129
+
130
+ it('names the package and both hashes so a mismatch is diagnosable', async () => {
131
+ const tampered = new TextEncoder().encode('other');
132
+
133
+ await expect(
134
+ assertBytesMatchIntegrity(tampered, await integrityOf(bytes), 'pkg@1.0.0')
135
+ ).rejects.toThrow(/pkg@1\.0\.0[\s\S]*Expected:[\s\S]*Actual:/);
136
+ });
137
+
138
+ it('rejects an integrity string whose algorithm it cannot compute', async () => {
139
+ await expect(assertBytesMatchIntegrity(bytes, 'md6-abcdef', 'pkg@1.0.0')).rejects.toThrow(
140
+ /Unsupported integrity algorithm/
141
+ );
142
+ });
143
+
144
+ it('verifies under whichever sha algorithm the lockfile used', async () => {
145
+ await expect(
146
+ assertBytesMatchIntegrity(bytes, await integrityOf(bytes, 'sha256'), 'pkg@1.0.0')
147
+ ).resolves.toBeUndefined();
148
+ });
149
+});
150
+
151
+describe('findLockfilePath', () => {
152
+ it('walks up from a nested directory to the workspace lockfile', async () => {
153
+ const root = await makeTempDir();
154
+ const nested = path.join(root, 'ts', 'packages', 'cli', 'scripts');
155
+ await mkdir(nested, { recursive: true });
156
+ await writeFile(path.join(root, 'pnpm-lock.yaml'), LOCKFILE, 'utf8');
157
+
158
+ await expect(findLockfilePath(nested)).resolves.toBe(path.join(root, 'pnpm-lock.yaml'));
159
+ });
160
+
161
+ it('fails closed when no lockfile exists above the start directory', async () => {
162
+ const root = await makeTempDir();
163
+
164
+ await expect(findLockfilePath(root)).rejects.toThrow(TarballIntegrityError);
165
+ });
166
+});
167
+
168
+describe('loadLockfileIntegrity', () => {
169
+ it('parses the nearest lockfile found above the start directory', async () => {
170
+ const root = await makeTempDir();
171
+ const nested = path.join(root, 'scripts');
172
+ await mkdir(nested, { recursive: true });
173
+ await writeFile(path.join(root, 'pnpm-lock.yaml'), LOCKFILE, 'utf8');
174
+
175
+ const parsed = await loadLockfileIntegrity(nested);
176
+
177
+ expect(parsed.get('extract-zip@2.0.1')).toMatch(/^sha512-/);
178
+ });
179
+});
180
+
181
+/**
182
+ * Named literally rather than imported from `RUN_CODEX_ACP_BINARY_TARGETS`, for
183
+ * the same reason `verify-archive-companions.sh` spells its list out: importing
184
+ * that module pulls in `@composio/core`, and this file otherwise needs nothing
185
+ * built. Adding a codex target there must extend this list.
186
+ */
187
+const CODEX_ACP_PACKAGES = [
188
+ '@zed-industries/codex-acp-darwin-arm64',
189
+ '@zed-industries/codex-acp-darwin-x64',
190
+ '@zed-industries/codex-acp-linux-arm64',
191
+ '@zed-industries/codex-acp-linux-x64',
192
+];
193
+
194
+describe('this repository', () => {
195
+ it('pins an integrity hash for every codex-acp binary the release packages', async () => {
196
+ const parsed = await loadLockfileIntegrity(path.dirname(fileURLToPath(import.meta.url)));
197
+ const keys = [...parsed.keys()];
198
+
199
+ for (const packageName of CODEX_ACP_PACKAGES) {
200
+ expect(
201
+ keys.find(key => key.startsWith(`${packageName}@`)),
202
+ `${packageName} has no integrity hash in pnpm-lock.yaml`
203
+ ).toBeDefined();
204
+ }
205
+ });
206
+});