@devries-cameron20 / composio-1 / commits / 2d409c741
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): verify codex-acp tarballs against the lockfile before packaging them

The release build downloads four `@zed-industries/codex-acp-<platform>` tarballs itself, because pnpm will only install the one matching the runner's platform and the archive has to name all four. Those bytes were extracted and copied, executable, into the published CLI archive without ever being checked against a known hash — the one dependency in the release that got none of the integrity verification `pnpm install --frozen-lockfile` applies to everything else. `pnpm-lock.yaml` already pins a sha512 for each of them, so the fix is to use it. The hash is read from the lockfile rather than the registry's own `dist.integrity`, since whoever can serve a tarball can serve the metadata vouching for it, and it is looked up before the download so a package the lockfile never resolved aborts the build instead of shipping unverified bytes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

composio-zen[bot] committed Aug 23, 2026 at 16:42 UTC 2d409c7417d0c5bc8427fb77a79c6dbb297dfdef
3 files changed +402 -1
ts/packages/cli/scripts/_acp-adapters.ts
+23 -1
@@ -1,10 +1,16 @@
1 import { chmod, copyFile, mkdir, mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises';
2 import { createRequire } from 'node:module';
3 import * as path from 'node:path';
4 +import { fileURLToPath } from 'node:url';
5 import {
6 RUN_CODEX_ACP_BINARY_TARGETS,
7 type RunCodexAcpBinaryTarget,
8 } from '../src/services/run-companion-modules';
9 +import {
10 + assertBytesMatchIntegrity,
11 + expectedIntegrityFor,
12 + loadLockfileIntegrity,
13 +} from './_tarball-integrity';
14
15 const GENERATED_ROOT_DIR = path.resolve('./.generated');
16 const ACP_ADAPTERS_CACHE_DIR = path.join(GENERATED_ROOT_DIR, 'acp-adapters');
@@ -231,6 +237,7 @@ const buildClaudeAdapterBundle = async (cacheDir: string): Promise<void> => {
237
238 const extractCodexBinaryFromTarball = async ({
239 binaryFileName,
240 + integrityByPackage,
241 packageName,
242 relativePath,
243 stageCacheDir,
@@ -238,12 +245,16 @@ const extractCodexBinaryFromTarball = async ({
245 version,
246 }: {
247 readonly binaryFileName: string;
248 + readonly integrityByPackage: ReadonlyMap<string, string>;
249 readonly packageName: string;
250 readonly relativePath: string;
251 readonly stageCacheDir: string;
252 readonly stageRootDir: string;
253 readonly version: string;
254 }): Promise<void> => {
255 + // Read before the download so a missing pin fails before any bytes are fetched.
256 + const expectedIntegrity = expectedIntegrityFor(integrityByPackage, packageName, version);
257 +
258 const metadata = await fetchRegistryJson(packageName, version);
259 const tarballUrl = metadata.dist?.tarball;
260 if (!tarballUrl) {
@@ -262,8 +273,14 @@ const extractCodexBinaryFromTarball = async ({
273 );
274
275 await downloadTarball(tarballUrl, tempArchivePath);
276 +
277 + // Checked against the lockfile rather than the registry's own `dist.integrity`:
278 + // whoever can serve the tarball can serve the metadata that vouches for it.
279 + const archiveBytes = await Bun.file(tempArchivePath).bytes();
280 + await assertBytesMatchIntegrity(archiveBytes, expectedIntegrity, `${packageName}@${version}`);
281 +
282 await mkdir(tempExtractDir, { recursive: true });
266 - const archive = new Bun.Archive(await Bun.file(tempArchivePath).bytes());
283 + const archive = new Bun.Archive(archiveBytes);
284 await archive.extract(tempExtractDir);
285
286 const extractedBinaryPath = path.join(tempExtractDir, 'package', 'bin', binaryFileName);
@@ -284,6 +301,10 @@ const materializeCodexAdapters = async (
301 stageRootDir: string,
302 stageCacheDir: string
303 ): Promise<void> => {
304 + const integrityByPackage = await loadLockfileIntegrity(
305 + path.dirname(fileURLToPath(import.meta.url))
306 + );
307 +
308 await Promise.all(
309 manifest.codex.map(target =>
310 extractCodexBinaryFromTarball({
@@ -291,6 +312,7 @@ const materializeCodexAdapters = async (
312 RUN_CODEX_ACP_BINARY_TARGETS.find(
313 candidate => candidate.packageName === target.packageName
314 )?.binaryFileName ?? 'codex-acp',
315 + integrityByPackage,
316 packageName: target.packageName,
317 relativePath: target.relativePath,
318 stageCacheDir,
ts/packages/cli/scripts/_tarball-integrity.ts new
+173
@@ -0,0 +1,173 @@
1 +import { readFile, stat } from 'node:fs/promises';
2 +import * as path from 'node:path';
3 +
4 +/**
5 + * Integrity verification for tarballs this repo downloads outside of pnpm.
6 + *
7 + * `pnpm install --frozen-lockfile` verifies every dependency it installs
8 + * against the `integrity` recorded in `pnpm-lock.yaml`. A tarball fetched by a
9 + * build script gets none of that: the script asks the registry where the bytes
10 + * live and writes whatever comes back. The codex-acp binaries are fetched that
11 + * way — they must be present for all four platforms on a single-platform runner,
12 + * which pnpm will not install — and they are then packaged, executable, inside
13 + * the published CLI release archive.
14 + *
15 + * The lockfile already carries the expected hash for exactly those packages, so
16 + * the fix is to use it. Verification is fail-closed: a package with no lockfile
17 + * entry, or a lockfile that cannot be found, aborts the build rather than
18 + * shipping unverified bytes.
19 + */
20 +
21 +const LOCKFILE_NAME = 'pnpm-lock.yaml';
22 +
23 +/** ` '@scope/name@1.2.3':` or ` name@1.2.3:` at the top level of `packages:`. */
24 +const PACKAGE_KEY_PATTERN = /^ {2}(?:'(.+)'|([^\s'][^\s]*?)):$/;
25 +
26 +/** `integrity: sha512-…` inside a `resolution: { … }` mapping. */
27 +const INTEGRITY_PATTERN = /^\s+resolution:\s*\{[^}]*\bintegrity:\s*(sha\d{3}-[A-Za-z0-9+/=]+)/;
28 +
29 +export class TarballIntegrityError extends Error {
30 + constructor(message: string) {
31 + super(message);
32 + this.name = 'TarballIntegrityError';
33 + }
34 +}
35 +
36 +/**
37 + * Maps every `name@version` in a pnpm lockfile to its recorded integrity hash.
38 + *
39 + * Deliberately line-based rather than a YAML parse: the lockfile is large, the
40 + * shape being read is two adjacent lines, and this keeps the build script free
41 + * of a YAML dependency. Entries whose `resolution` carries no `integrity` — git
42 + * and direct-tarball resolutions — are skipped, so asking for one fails closed
43 + * in `expectedIntegrityFor` instead of silently passing.
44 + */
45 +export const parseLockfileIntegrity = (lockfileText: string): ReadonlyMap<string, string> => {
46 + const integrityByPackage = new Map<string, string>();
47 + const lines = lockfileText.split('\n');
48 +
49 + for (let index = 0; index < lines.length - 1; index += 1) {
50 + const keyMatch = PACKAGE_KEY_PATTERN.exec(lines[index] ?? '');
51 + if (!keyMatch) {
52 + continue;
53 + }
54 +
55 + const integrityMatch = INTEGRITY_PATTERN.exec(lines[index + 1] ?? '');
56 + if (!integrityMatch) {
57 + continue;
58 + }
59 +
60 + const packageKey = keyMatch[1] ?? keyMatch[2];
61 + const integrity = integrityMatch[1];
62 + if (packageKey && integrity && !integrityByPackage.has(packageKey)) {
63 + integrityByPackage.set(packageKey, integrity);
64 + }
65 + }
66 +
67 + return integrityByPackage;
68 +};
69 +
70 +/**
71 + * The integrity hash the lockfile pins for `packageName@version`.
72 + *
73 + * Throws when the package is absent: a build script that downloads something
74 + * the lockfile never resolved has no pinned bytes to check against, which is
75 + * the situation this module exists to refuse.
76 + */
77 +export const expectedIntegrityFor = (
78 + integrityByPackage: ReadonlyMap<string, string>,
79 + packageName: string,
80 + version: string
81 +): string => {
82 + const packageKey = `${packageName}@${version}`;
83 + const integrity = integrityByPackage.get(packageKey);
84 +
85 + if (!integrity) {
86 + throw new TarballIntegrityError(
87 + `${packageKey} has no integrity hash in ${LOCKFILE_NAME}; refusing to package unverified bytes.`
88 + );
89 + }
90 +
91 + return integrity;
92 +};
93 +
94 +/** Lockfile hash prefix to the Web Crypto algorithm name that computes it. */
95 +const SUBTLE_ALGORITHM_BY_PREFIX: Readonly<Record<string, string>> = {
96 + sha1: 'SHA-1',
97 + sha256: 'SHA-256',
98 + sha384: 'SHA-384',
99 + sha512: 'SHA-512',
100 +};
101 +
102 +/** Subresource-integrity string for `bytes` under the given lockfile prefix. */
103 +const computeIntegrity = async (bytes: Uint8Array, prefix: string): Promise<string> => {
104 + const digest = await crypto.subtle.digest(
105 + SUBTLE_ALGORITHM_BY_PREFIX[prefix] as string,
106 + bytes as BufferSource
107 + );
108 + const base64 = btoa(String.fromCharCode(...new Uint8Array(digest)));
109 + return `${prefix}-${base64}`;
110 +};
111 +
112 +/**
113 + * Throws unless `bytes` hashes to `expectedIntegrity`.
114 + *
115 + * The algorithm is read from the expected value so a lockfile that moves off
116 + * sha512 keeps working, and an unrecognised one fails loudly here rather than
117 + * comparing two strings that could never match.
118 + */
119 +export const assertBytesMatchIntegrity = async (
120 + bytes: Uint8Array,
121 + expectedIntegrity: string,
122 + label: string
123 +): Promise<void> => {
124 + const prefix = expectedIntegrity.split('-')[0] ?? '';
125 + if (!(prefix in SUBTLE_ALGORITHM_BY_PREFIX)) {
126 + throw new TarballIntegrityError(
127 + `Unsupported integrity algorithm for ${label}: "${expectedIntegrity}".`
128 + );
129 + }
130 +
131 + const actualIntegrity = await computeIntegrity(bytes, prefix);
132 +
133 + if (actualIntegrity !== expectedIntegrity) {
134 + throw new TarballIntegrityError(
135 + `Integrity mismatch for ${label}.\n Expected: ${expectedIntegrity}\n Actual: ${actualIntegrity}`
136 + );
137 + }
138 +};
139 +
140 +/**
141 + * Nearest `pnpm-lock.yaml` at or above `startDir`.
142 + *
143 + * Walks up rather than resolving a fixed `../../..` so the script keeps working
144 + * from whichever directory a build or test invokes it in.
145 + */
146 +export const findLockfilePath = async (startDir: string): Promise<string> => {
147 + let directory = path.resolve(startDir);
148 +
149 + for (;;) {
150 + const candidate = path.join(directory, LOCKFILE_NAME);
151 + const found = await stat(candidate)
152 + .then(stats => stats.isFile())
153 + .catch(() => false);
154 +
155 + if (found) {
156 + return candidate;
157 + }
158 +
159 + const parent = path.dirname(directory);
160 + if (parent === directory) {
161 + throw new TarballIntegrityError(
162 + `Could not find ${LOCKFILE_NAME} at or above ${startDir}; cannot verify downloaded tarballs.`
163 + );
164 + }
165 + directory = parent;
166 + }
167 +};
168 +
169 +/** Lockfile integrity map for the workspace containing `startDir`. */
170 +export const loadLockfileIntegrity = async (
171 + startDir: string
172 +): Promise<ReadonlyMap<string, string>> =>
173 + parseLockfileIntegrity(await readFile(await findLockfilePath(startDir), 'utf8'));
ts/packages/cli/test/src/scripts/tarball-integrity.test.ts new
+206
@@ -0,0 +1,206 @@
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 +});