diff --git a/changelog.d/cso-npm-bundled-dependencies.md b/changelog.d/cso-npm-bundled-dependencies.md new file mode 100644 index 000000000..e82d3b36a --- /dev/null +++ b/changelog.d/cso-npm-bundled-dependencies.md @@ -0,0 +1,6 @@ +--- +important: blocker +--- +### Fixed + +- **`/cso --comprehensive` can now prepare npm apps whose lockfile lists bundled dependencies, such as Next.js apps on Tailwind CSS 4.** npm records a package that ships inside another package's tarball with `"inBundle": true` and no download URL, and preparation used to refuse the whole lockfile with `UNPINNED_ARCHIVE`. A bundled entry is now accepted when the package that bundles it has a pinned registry archive (an npmjs.org URL and a SHA-256 or SHA-512 hash). It is still refused when that parent is missing, is a local workspace link, or is itself unpinned, and an ordinary entry with no URL is still refused. (#4777) diff --git a/lib/cso/preparation.ts b/lib/cso/preparation.ts index 35612e9ee..0c1b5bcad 100644 --- a/lib/cso/preparation.ts +++ b/lib/cso/preparation.ts @@ -310,6 +310,22 @@ function inspectNode(root: string, plan: PreparationPlan) { if (!NAME.test(name) || typeof raw.version !== 'string' || !VERSION.test(raw.version)) fail('INVALID_METADATA', 'Locked npm package needs an exact name and version.', lockPath); for (const field of DEPENDENCY_FIELDS) dependencySpecs(raw[field], lockPath); + if (raw.inBundle === true && raw.resolved === undefined) { + // A bundled package ships inside its bundler's tarball, so that archive must be pinned instead. + let owner = path; + do { + const cut = owner.lastIndexOf('/node_modules/'); + if (cut < 0) fail('UNPINNED_ARCHIVE', 'Bundled npm package has no pinned parent archive.', lockPath); + owner = owner.slice(0, cut); + } while (record(lock.packages[owner]) && lock.packages[owner].inBundle === true); + const bundler = lock.packages[owner]; + if (!record(bundler) || bundler.link === true || !owner.split('/').includes('node_modules')) + fail('UNPINNED_ARCHIVE', 'Bundled npm package has no pinned parent archive.', lockPath); + publicUrl(bundler.resolved, ['registry.npmjs.org'], lockPath); + integrity(bundler.integrity, lockPath); + delete clean.packages[path].scripts; + continue; + } plan.inputs.push({ kind: 'public', name, diff --git a/test/cso-preparation.test.ts b/test/cso-preparation.test.ts index 174ebd8b3..290591123 100644 --- a/test/cso-preparation.test.ts +++ b/test/cso-preparation.test.ts @@ -74,6 +74,50 @@ describe('CSO inert Node preparation', () => { const files = nodeFiles(); files['package-lock.json'].packages['node_modules/cookie'].resolved = url; const plan = inspectPreparation(fixture(files)); expect(plan.status).toBe('prerequisites'); expect(plan.acquisition).toEqual([]); }); + const bundledFiles = () => { + const files: any = nodeFiles(); + const packages = files['package-lock.json'].packages; + packages['node_modules/@tailwindcss/oxide-wasm32-wasi'] = { version: '4.1.11', resolved: 'https://registry.npmjs.org/@tailwindcss/oxide-wasm32-wasi/-/oxide-wasm32-wasi-4.1.11.tgz', integrity: sri, bundleDependencies: ['@emnapi/core', 'tslib'], optional: true }; + packages['node_modules/@tailwindcss/oxide-wasm32-wasi/node_modules/@emnapi/core'] = { version: '1.4.3', inBundle: true, optional: true, dependencies: { '@emnapi/wasi-threads': '1.0.2' } }; + packages['node_modules/@tailwindcss/oxide-wasm32-wasi/node_modules/@emnapi/core/node_modules/@emnapi/wasi-threads'] = { version: '1.0.2', inBundle: true, optional: true }; + packages['node_modules/@tailwindcss/oxide-wasm32-wasi/node_modules/tslib'] = { version: '2.8.0', inBundle: true, license: '0BSD', optional: true }; + return files; + }; + test('accepts bundled npm entries covered by their parent archive', () => { + const plan = inspectPreparation(fixture(bundledFiles())); + expect(plan.status).toBe('ready'); + expect(plan.inputs.map(i => i.name).sort()).toEqual(['@tailwindcss/oxide-wasm32-wasi', 'cookie']); + }); + for (const [label, edit] of [ + ['missing', ({ 'package-lock.json': { packages: p } }: any) => { delete p['node_modules/@tailwindcss/oxide-wasm32-wasi']; }], + ['unresolved', ({ 'package-lock.json': { packages: p } }: any) => { delete p['node_modules/@tailwindcss/oxide-wasm32-wasi'].resolved; }], + ['unhashed', ({ 'package-lock.json': { packages: p } }: any) => { p['node_modules/@tailwindcss/oxide-wasm32-wasi'].integrity = 'sha1-weak'; }], + ['off-registry', ({ 'package-lock.json': { packages: p } }: any) => { p['node_modules/@tailwindcss/oxide-wasm32-wasi'].resolved = 'https://evil.invalid/oxide.tgz'; }], + ['workspace link', (files: any) => { const p = files['package-lock.json'].packages; p['node_modules/@tailwindcss/oxide-wasm32-wasi'] = { resolved: 'packages/local', link: true }; p['packages/local'] = { name: 'local', version: '1.0.0' }; files['packages/local/package.json'] = { name: 'local', version: '1.0.0' }; }], + ['itself bundled without an archive', ({ 'package-lock.json': { packages: p } }: any) => { p['node_modules/@tailwindcss/oxide-wasm32-wasi'] = { version: '4.1.11', inBundle: true }; }], + ] as const) test(`rejects a bundled npm entry whose parent is ${label}`, () => { + const files = bundledFiles(); edit(files); + const plan = inspectPreparation(fixture(files)); + expect(plan.status).toBe('prerequisites'); expect(plan.acquisition).toEqual([]); + }); + test('rejects a bundled npm entry with no parent package', () => { + const files: any = nodeFiles(); + files['package-lock.json'].packages['node_modules/tslib'] = { version: '2.8.0', inBundle: true }; + const plan = inspectPreparation(fixture(files)); + expect(plan.prerequisites[0].code).toBe('UNPINNED_ARCHIVE'); expect(plan.acquisition).toEqual([]); + }); + test('keeps refusing a non-bundled npm entry without a resolved archive', () => { + const files: any = bundledFiles(); + delete files['package-lock.json'].packages['node_modules/@tailwindcss/oxide-wasm32-wasi/node_modules/tslib'].inBundle; + const plan = inspectPreparation(fixture(files)); + expect(plan.prerequisites[0].code).toBe('UNPINNED_ARCHIVE'); expect(plan.acquisition).toEqual([]); + }); + test('still validates a bundled npm entry that names its own archive', () => { + const files: any = bundledFiles(); + files['package-lock.json'].packages['node_modules/@tailwindcss/oxide-wasm32-wasi/node_modules/tslib'].resolved = 'https://evil.invalid/tslib.tgz'; + const plan = inspectPreparation(fixture(files)); + expect(plan.prerequisites[0].code).toBe('UNSUPPORTED_SOURCE'); expect(plan.acquisition).toEqual([]); + }); test('rejects archive without cryptographic integrity', () => { const files = nodeFiles(); files['package-lock.json'].packages['node_modules/cookie'].integrity = 'sha1-weak'; expect(inspectPreparation(fixture(files)).prerequisites[0].code).toBe('UNPINNED_ARCHIVE');