diff --git a/scripts/ci/scan-supply-chain-iocs.js b/scripts/ci/scan-supply-chain-iocs.js index 7b4c95698..813bf38ab 100755 --- a/scripts/ci/scan-supply-chain-iocs.js +++ b/scripts/ci/scan-supply-chain-iocs.js @@ -459,12 +459,27 @@ function walkFiles(rootDir, files = []) { return files; } +// Symbolic links are not followed: pnpm's top-level entries and workspace +// packages are links, and following them could loop or leave the scan root. +// pnpm keeps the real package directories in its virtual store instead, +// node_modules/.pnpm//node_modules/, which is walked directly. +function walkPnpmVirtualStore(storeDir, files) { + for (const entry of fs.readdirSync(storeDir, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + walkNodeModules(path.join(storeDir, entry.name, 'node_modules'), files); + } +} + function walkNodeModules(nodeModulesDir, files) { if (!fs.existsSync(nodeModulesDir)) return; for (const entry of fs.readdirSync(nodeModulesDir, { withFileTypes: true })) { - if (entry.name.startsWith('.')) continue; const fullPath = path.join(nodeModulesDir, entry.name); + if (entry.name === '.pnpm' && entry.isDirectory()) { + walkPnpmVirtualStore(fullPath, files); + continue; + } + if (entry.name.startsWith('.')) continue; if (entry.isDirectory()) { if (entry.name.startsWith('@')) { for (const scopedEntry of fs.readdirSync(fullPath, { withFileTypes: true })) { @@ -492,6 +507,12 @@ function inspectPackageDir(packageDir, files) { files.push(candidate); } } + + // npm nests a dependency under its dependent when versions conflict. + const nestedNodeModules = path.join(packageDir, 'node_modules'); + if (fs.existsSync(nestedNodeModules) && fs.lstatSync(nestedNodeModules).isDirectory()) { + walkNodeModules(nestedNodeModules, files); + } } function readText(filePath) { diff --git a/tests/ci/scan-supply-chain-iocs.test.js b/tests/ci/scan-supply-chain-iocs.test.js index 91a714d46..a5e93a9b1 100755 --- a/tests/ci/scan-supply-chain-iocs.test.js +++ b/tests/ci/scan-supply-chain-iocs.test.js @@ -466,6 +466,45 @@ function run() { }); })) passed++; else failed++; + if (test('rejects installed payload filenames in a pnpm node_modules layout', () => { + // pnpm keeps real package directories in node_modules/.pnpm//node_modules/ + // and links the top-level node_modules/ entries to them. + const storeDir = 'node_modules/.pnpm/@tanstack+react-router@1.169.5/node_modules/@tanstack/react-router'; + withFixture({ + [`${storeDir}/package.json`]: JSON.stringify({ name: '@tanstack/react-router', version: '1.169.5' }), + [`${storeDir}/router_init.js`]: '/* payload */', + }, rootDir => { + fs.mkdirSync(path.join(rootDir, 'node_modules', '@tanstack'), { recursive: true }); + try { + fs.symlinkSync( + path.join(rootDir, storeDir), + path.join(rootDir, 'node_modules', '@tanstack', 'react-router'), + 'junction', + ); + } catch { + // The virtual store alone must still be scanned where links are unavailable. + } + const result = scanSupplyChainIocs({ rootDir }); + assert.ok( + result.findings.some(finding => finding.indicator === 'router_init.js'), + `Expected router_init.js finding, got ${JSON.stringify(result.findings)}`, + ); + }); + })) passed++; else failed++; + + if (test('rejects installed payload filenames in nested node_modules', () => { + withFixture({ + 'node_modules/some-app/package.json': JSON.stringify({ name: 'some-app', version: '1.0.0' }), + 'node_modules/some-app/node_modules/@tanstack/react-router/router_init.js': '/* payload */', + }, rootDir => { + const result = scanSupplyChainIocs({ rootDir }); + assert.ok( + result.findings.some(finding => finding.indicator === 'router_init.js'), + `Expected router_init.js finding, got ${JSON.stringify(result.findings)}`, + ); + }); + })) passed++; else failed++; + if (test('supports CLI JSON output and non-zero exit on findings', () => { withFixture({ 'package.json': JSON.stringify({ dependencies: { '@opensearch-project/opensearch': '3.8.0' } }, null, 2),