cve-lite-cli 1.19.1 → 1.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -1
- package/dist/cli/args.js +18 -0
- package/dist/cli/help.js +13 -7
- package/dist/cli/validate.js +9 -0
- package/dist/constants.js +4 -0
- package/dist/index.js +35 -4
- package/dist/output/formatters.js +8 -1
- package/dist/output/html-reporter.js +45 -26
- package/dist/output/printers.js +48 -12
- package/dist/parsers/bun-lock.js +75 -5
- package/dist/parsers/index.js +17 -12
- package/dist/parsers/package-lock.js +2 -1
- package/dist/parsers/yarn-lock.js +174 -15
- package/dist/remediation/fix-commands.js +3 -2
- package/dist/remediation/npm-transitive-resolution.js +43 -4
- package/dist/scanner.js +19 -3
- package/dist/utils/advisory.js +6 -0
- package/dist/utils/create-pr.js +212 -0
- package/package.json +1 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { severityOrder } from "../constants.js";
|
|
5
|
+
import { normalizeSeverity } from "../osv/severity.js";
|
|
6
|
+
import { pluralize } from "./string.js";
|
|
7
|
+
const DEPENDENCY_FILES_TO_STAGE = [
|
|
8
|
+
"package.json",
|
|
9
|
+
"package-lock.json",
|
|
10
|
+
"yarn.lock",
|
|
11
|
+
"pnpm-lock.yaml",
|
|
12
|
+
"bun.lock",
|
|
13
|
+
"npm-shrinkwrap.json",
|
|
14
|
+
];
|
|
15
|
+
export function findingsMeetFailOnThreshold(findings, failOn) {
|
|
16
|
+
const failLevel = normalizeSeverity(failOn);
|
|
17
|
+
return findings.some(finding => severityOrder[finding.severity] >= severityOrder[failLevel]);
|
|
18
|
+
}
|
|
19
|
+
export function defaultFixBranchName(date = new Date()) {
|
|
20
|
+
const year = date.getUTCFullYear();
|
|
21
|
+
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
|
|
22
|
+
const day = String(date.getUTCDate()).padStart(2, "0");
|
|
23
|
+
return `cve-lite/fix-${year}-${month}-${day}`;
|
|
24
|
+
}
|
|
25
|
+
export function buildPullRequestTitle(appliedCount, packageNames) {
|
|
26
|
+
const pkgList = packageNames.length <= 3
|
|
27
|
+
? packageNames.join(", ")
|
|
28
|
+
: `${packageNames.slice(0, 2).join(", ")} +${packageNames.length - 2} more`;
|
|
29
|
+
return `[CVE-Lite-CLI] fix: upgrade ${pkgList} (${appliedCount} ${pluralize(appliedCount, "vulnerability", "vulnerabilities")} resolved)`;
|
|
30
|
+
}
|
|
31
|
+
export function collectAdvisoryIdsForPackage(findings, packageName) {
|
|
32
|
+
const ids = new Set();
|
|
33
|
+
for (const finding of findings) {
|
|
34
|
+
if (finding.pkg.name !== packageName)
|
|
35
|
+
continue;
|
|
36
|
+
for (const vuln of finding.vulnerabilities) {
|
|
37
|
+
ids.add(vuln.id);
|
|
38
|
+
for (const alias of vuln.aliases ?? []) {
|
|
39
|
+
if (alias.startsWith("CVE-")) {
|
|
40
|
+
ids.add(alias);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return [...ids].sort();
|
|
46
|
+
}
|
|
47
|
+
export function buildPullRequestBody(params) {
|
|
48
|
+
const lines = [
|
|
49
|
+
"## Summary",
|
|
50
|
+
"",
|
|
51
|
+
"Automated security fixes applied by [CVE Lite CLI](https://github.com/OWASP/cve-lite-cli) using OSV-validated target versions.",
|
|
52
|
+
"",
|
|
53
|
+
"## Fixed packages",
|
|
54
|
+
"",
|
|
55
|
+
];
|
|
56
|
+
for (const item of params.fixResult.applied) {
|
|
57
|
+
const advisoryIds = collectAdvisoryIdsForPackage(params.findingsBeforeFix, item.package);
|
|
58
|
+
const advisoryText = advisoryIds.length > 0 ? advisoryIds.join(", ") : "n/a";
|
|
59
|
+
lines.push(`- **${item.package}**: \`${item.from}\` → \`${item.to}\``);
|
|
60
|
+
lines.push(` - Advisories: ${advisoryText}`);
|
|
61
|
+
}
|
|
62
|
+
lines.push("", "## Scan results", "", `- Findings before fix: **${params.findingsBeforeFix.length}**`, `- Findings after fix: **${params.findingsAfterFix.length}**`, "", "## Notes", "", "- Only validated **direct** dependency upgrades are included in this PR.", "- Transitive vulnerabilities may still require parent package upgrades or upstream releases.");
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
async function runCommand(command, args, cwd) {
|
|
66
|
+
return await new Promise(resolve => {
|
|
67
|
+
let stdout = "";
|
|
68
|
+
let stderr = "";
|
|
69
|
+
const child = spawn(command, args, {
|
|
70
|
+
cwd,
|
|
71
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
72
|
+
});
|
|
73
|
+
child.stdout?.on("data", chunk => {
|
|
74
|
+
stdout += String(chunk);
|
|
75
|
+
});
|
|
76
|
+
child.stderr?.on("data", chunk => {
|
|
77
|
+
stderr += String(chunk);
|
|
78
|
+
});
|
|
79
|
+
child.on("error", error => {
|
|
80
|
+
resolve({ stdout, stderr, status: null, error });
|
|
81
|
+
});
|
|
82
|
+
child.on("close", code => {
|
|
83
|
+
resolve({ stdout, stderr, status: code, error: null });
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async function assertGitRepository(projectPath) {
|
|
88
|
+
const result = await runCommand("git", ["rev-parse", "--is-inside-work-tree"], projectPath);
|
|
89
|
+
if (result.error || result.status !== 0 || result.stdout.trim() !== "true") {
|
|
90
|
+
throw new Error("--create-pr requires a git repository. Run this command from your project root.");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async function assertGhAvailable() {
|
|
94
|
+
const version = await runCommand("gh", ["--version"], process.cwd());
|
|
95
|
+
if (version.error || version.status !== 0) {
|
|
96
|
+
throw new Error("--create-pr requires the GitHub CLI (gh). Install it from https://cli.github.com/ or set GITHUB_TOKEN and ensure gh is on PATH.");
|
|
97
|
+
}
|
|
98
|
+
const auth = await runCommand("gh", ["auth", "status"], process.cwd());
|
|
99
|
+
if (auth.status !== 0 && !process.env.GITHUB_TOKEN) {
|
|
100
|
+
throw new Error("--create-pr requires GitHub authentication. Run `gh auth login` or set the GITHUB_TOKEN environment variable.");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async function hasAnyChanges(projectPath) {
|
|
104
|
+
const status = await runCommand("git", ["status", "--porcelain"], projectPath);
|
|
105
|
+
if (status.error || status.status !== 0) {
|
|
106
|
+
throw new Error(`Failed to inspect git status: ${status.stderr.trim() || status.error?.message || "unknown error"}`);
|
|
107
|
+
}
|
|
108
|
+
return status.stdout.trim().length > 0;
|
|
109
|
+
}
|
|
110
|
+
export async function stageDependencyFilesOnly(projectPath) {
|
|
111
|
+
for (const filePath of DEPENDENCY_FILES_TO_STAGE) {
|
|
112
|
+
if (!fs.existsSync(path.join(projectPath, filePath))) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const add = await runCommand("git", ["add", "--", filePath], projectPath);
|
|
116
|
+
if (add.error || add.status !== 0) {
|
|
117
|
+
throw new Error(`Failed to stage dependency file ${filePath}: ${add.stderr.trim() || add.error?.message || "unknown error"}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export async function hasStagedDependencyChanges(projectPath) {
|
|
122
|
+
const diff = await runCommand("git", ["diff", "--cached", "--name-only"], projectPath);
|
|
123
|
+
if (diff.error || diff.status !== 0) {
|
|
124
|
+
throw new Error(`Failed to inspect staged changes: ${diff.stderr.trim() || diff.error?.message || "unknown error"}`);
|
|
125
|
+
}
|
|
126
|
+
const stagedFiles = diff.stdout
|
|
127
|
+
.split("\n")
|
|
128
|
+
.map(line => line.trim())
|
|
129
|
+
.filter(Boolean);
|
|
130
|
+
return stagedFiles.some(filePath => DEPENDENCY_FILES_TO_STAGE.some(target => filePath === target || filePath.endsWith(`/${target}`)));
|
|
131
|
+
}
|
|
132
|
+
async function branchExists(projectPath, branchName) {
|
|
133
|
+
const result = await runCommand("git", ["rev-parse", "--verify", branchName], projectPath);
|
|
134
|
+
return result.status === 0;
|
|
135
|
+
}
|
|
136
|
+
export async function selectAvailableBranchName(projectPath, baseName) {
|
|
137
|
+
if (!(await branchExists(projectPath, baseName))) {
|
|
138
|
+
return baseName;
|
|
139
|
+
}
|
|
140
|
+
for (let suffix = 2; suffix <= 99; suffix++) {
|
|
141
|
+
const candidate = `${baseName}-${suffix}`;
|
|
142
|
+
if (!(await branchExists(projectPath, candidate))) {
|
|
143
|
+
return candidate;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`Failed to allocate a unique branch name based on ${baseName}`);
|
|
147
|
+
}
|
|
148
|
+
function extractPullRequestUrl(output) {
|
|
149
|
+
const match = output.match(/https:\/\/github\.com\/[^\s]+\/pull\/\d+/);
|
|
150
|
+
return match?.[0] ?? null;
|
|
151
|
+
}
|
|
152
|
+
export async function createPullRequestForFixes(params) {
|
|
153
|
+
const baseBranchName = defaultFixBranchName();
|
|
154
|
+
if (params.fixResult.appliedFixCount === 0) {
|
|
155
|
+
return {
|
|
156
|
+
branchName: baseBranchName,
|
|
157
|
+
prUrl: null,
|
|
158
|
+
skipped: true,
|
|
159
|
+
skipReason: "No direct dependency fixes were applied, so no pull request was created.",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
await assertGitRepository(params.projectPath);
|
|
163
|
+
await assertGhAvailable();
|
|
164
|
+
if (!(await hasAnyChanges(params.projectPath))) {
|
|
165
|
+
return {
|
|
166
|
+
branchName: baseBranchName,
|
|
167
|
+
prUrl: null,
|
|
168
|
+
skipped: true,
|
|
169
|
+
skipReason: "No lockfile or manifest changes were detected after applying fixes.",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const packageNames = params.fixResult.applied.map(a => a.package);
|
|
173
|
+
const title = buildPullRequestTitle(params.fixResult.appliedFixCount, packageNames);
|
|
174
|
+
const body = buildPullRequestBody({
|
|
175
|
+
fixResult: params.fixResult,
|
|
176
|
+
findingsBeforeFix: params.findingsBeforeFix,
|
|
177
|
+
findingsAfterFix: params.findingsAfterFix,
|
|
178
|
+
});
|
|
179
|
+
await stageDependencyFilesOnly(params.projectPath);
|
|
180
|
+
if (!(await hasStagedDependencyChanges(params.projectPath))) {
|
|
181
|
+
return {
|
|
182
|
+
branchName: baseBranchName,
|
|
183
|
+
prUrl: null,
|
|
184
|
+
skipped: true,
|
|
185
|
+
skipReason: "No dependency manifest or lockfile changes were detected after applying fixes.",
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const branchName = await selectAvailableBranchName(params.projectPath, baseBranchName);
|
|
189
|
+
const checkout = await runCommand("git", ["checkout", "-b", branchName], params.projectPath);
|
|
190
|
+
if (checkout.error || checkout.status !== 0) {
|
|
191
|
+
const message = checkout.stderr.trim() || checkout.error?.message || "unknown error";
|
|
192
|
+
throw new Error(`Failed to create branch ${branchName}: ${message}`);
|
|
193
|
+
}
|
|
194
|
+
const commit = await runCommand("git", ["commit", "-m", title], params.projectPath);
|
|
195
|
+
if (commit.error || commit.status !== 0) {
|
|
196
|
+
throw new Error(`Failed to commit dependency changes: ${commit.stderr.trim() || commit.error?.message || "unknown error"}`);
|
|
197
|
+
}
|
|
198
|
+
const push = await runCommand("git", ["push", "-u", "origin", branchName], params.projectPath);
|
|
199
|
+
if (push.error || push.status !== 0) {
|
|
200
|
+
throw new Error(`Failed to push branch ${branchName}: ${push.stderr.trim() || push.error?.message || "unknown error"}`);
|
|
201
|
+
}
|
|
202
|
+
const create = await runCommand("gh", ["pr", "create", "--base", params.baseBranch, "--head", branchName, "--title", title, "--body", body], params.projectPath);
|
|
203
|
+
if (create.error || create.status !== 0) {
|
|
204
|
+
throw new Error(`Failed to open pull request: ${create.stderr.trim() || create.error?.message || "unknown error"}`);
|
|
205
|
+
}
|
|
206
|
+
const prUrl = extractPullRequestUrl(`${create.stdout}\n${create.stderr}`);
|
|
207
|
+
return {
|
|
208
|
+
branchName,
|
|
209
|
+
prUrl,
|
|
210
|
+
skipped: false,
|
|
211
|
+
};
|
|
212
|
+
}
|