fix(ci): scan pnpm and nested node_modules in the IOC scanner

walkNodeModules skipped every dotted entry and only looked at
node_modules/<name> and node_modules/@scope/<name>. pnpm keeps real
package directories under node_modules/.pnpm and links the top-level
entries, which Dirent.isDirectory() reports as non-directories, so a pnpm
project had zero installed packages inspected. npm's nested
node_modules were never reached either.

Walk the .pnpm virtual store and nested node_modules directories. Links
are still not followed.
This commit is contained in:
Divya Chukkapalli committed 2026-11-17 04:20:14 -08:00
1 parent 3f14a103a8
commit a36d68cb7d
2 files changed
+61 -1

No files matched your search

+22 -1
View File
@@ -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/<id>/node_modules/<name>, 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) {
+39
View File
@@ -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/<id>/node_modules/<name>
// and links the top-level node_modules/<name> 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),