Skip to content

Commit

Permalink
Update find-indirect-vulnerable-deps.cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyMitch authored Jul 12, 2024
1 parent 5f3b370 commit 39b5d75
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions .github/helpers/npm-audit/find-indirect-vulnerable-deps.cjs
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");

// Runs runNpmAudit and adds parent dependencies if they can be found in the package-lock.json
const findIndirectVulnerableDependencies = async (auditResult, directoryPath) => {
const findIndirectVulnerableDependencies = async (
auditResult,
directoryPath
) => {
try {
const { vulnerabilities } = auditResult;

if (vulnerabilities.length === 0) {
// No vulnerabilities found
return { ...auditResult, parentDependencies: {} };
return { ...auditResult, parentDependencies: [] };
}

const packageLockPath = path.join(process.cwd(), path.resolve(__dirname, `../../../${directoryPath}/package-lock.json`));
const packageLockPath = path.resolve(
__dirname,
`../../../${directoryPath}/package-lock.json`
);

if (!fs.existsSync(packageLockPath)) {
throw new Error('package-lock.json not found in the current directory.');
throw new Error("package-lock.json not found in the current directory.");
}

const packageLock = JSON.parse(fs.readFileSync(packageLockPath, 'utf-8'));
const packageLock = JSON.parse(fs.readFileSync(packageLockPath, "utf-8"));
const vulnerableDeps = vulnerabilities
.filter((vuln) => !vuln.isDirect)
.map((vuln) => vuln.name);

const parentDependencies = {};
const parentDependencies = [];

const cleanDependencyName = (name) => name.replace(/^node_modules\//, '');
const cleanDependencyName = (name) => name.replace(/^node_modules\//, "");

const findVulnerableChildren = (dependencies, parentChain = []) => {
if (!dependencies) return;
Expand All @@ -35,11 +42,16 @@ const findIndirectVulnerableDependencies = async (auditResult, directoryPath) =>
if (!parentDependencies[cleanDepName]) {
parentDependencies[cleanDepName] = [];
}
parentDependencies[cleanDepName].push(...parentChain.map(cleanDependencyName));
parentDependencies[cleanDepName].push(
...parentChain.map(cleanDependencyName)
);
}

if (depData.dependencies) {
findVulnerableChildren(depData.dependencies, [...parentChain, cleanDepName]);
findVulnerableChildren(depData.dependencies, [
...parentChain,
cleanDepName,
]);
}
}
};
Expand Down Expand Up @@ -67,7 +79,7 @@ const findIndirectVulnerableDependencies = async (auditResult, directoryPath) =>
vulnerabilities: updatedVulnerabilities,
};
} catch (error) {
console.error('Error:', error);
console.error("Error:", error);
throw error;
}
};
Expand Down

0 comments on commit 39b5d75

Please sign in to comment.