Repository navigation
fix(ratchet): gate like --fail-on when no baseline exists #1314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theluckystrike
wants to merge
5
commits into
OWASP:main
Choose a base branch
from
theluckystrike:fix/1293-ratchet-no-baseline-gate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
899aa5a
fix(ratchet): gate like --fail-on when no baseline exists
theluckystrike 24ea7aa
fix(ratchet): gate first-run multi-folder scans and guard output
theluckystrike b8254c3
Merge remote-tracking branch 'origin/main' into fix/1293-ratchet-no-b…
theluckystrike a4dc02d
Merge remote-tracking branch 'origin/main' into fix/1293-ratchet-no-b…
theluckystrike 8c98b84
fix(ratchet): print one baseline commit hint and document the first-r…
theluckystrike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { existsSync, readFileSync, rmSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { runCli, mkProject, rmProject, npmProject, SEEDED_DB } from "./harness.js"; | ||
|
|
||
| const CI_ENV = { CI: "1" } as const; | ||
| const VULNERABLE_PACKAGE = "cve-lite-scenario-vuln-high"; | ||
|
|
||
| function projectFiles(vulnerable: boolean) { | ||
| const name = vulnerable ? VULNERABLE_PACKAGE : "cve-lite-scenario-clean"; | ||
| return npmProject({ name: "ratchet-test", dependencies: { [name]: "1.0.0" } }, { [name]: "1.0.0" }); | ||
| } | ||
|
|
||
| function makeProject(multi: boolean, vulnerable: boolean): string { | ||
| const files = projectFiles(vulnerable); | ||
| if (!multi) { | ||
| return mkProject(files); | ||
| } | ||
| return mkProject(Object.fromEntries( | ||
| ["a", "b"].flatMap(folder => Object.entries(files).map(([name, value]) => [`${folder}/${name}`, value])), | ||
| )); | ||
| } | ||
|
|
||
| function scan(project: string, flags: string[] = []) { | ||
| return runCli([project, "--ratchet", "--fail-on", "high", "--offline-db", SEEDED_DB, ...flags], { | ||
| cwd: project, | ||
| env: CI_ENV, | ||
| }); | ||
| } | ||
|
|
||
| describe.each([false, true])("first-run ratchet (multi-folder: %s)", multi => { | ||
| it("fails on unaccepted high findings, writes baselines, then accepts the existing baseline", () => { | ||
| const project = makeProject(multi, true); | ||
| try { | ||
| const first = scan(project); | ||
| expect(first.status).toBe(1); | ||
| expect(first.stdout).toContain("Failing: 1 vulnerability finding at or above high"); | ||
| const folders = multi ? ["a", "b"] : [""]; | ||
| for (const folder of folders) { | ||
| const baselinePath = join(project, folder, ".cve-lite", "baseline.json"); | ||
| const baseline = JSON.parse(readFileSync(baselinePath, "utf8")); | ||
| expect(baseline.findings).toHaveLength(1); | ||
| } | ||
| const baselinePaths = folders.map(folder => `${folder ? `${folder}/` : ""}.cve-lite/baseline.json`).join(", "); | ||
| expect(first.stdout).toContain(`Commit ${baselinePaths} to accept the existing debt`); | ||
| expect(first.stdout.split("accept the existing debt").length - 1).toBe(1); | ||
| const second = scan(project); | ||
| expect(second.status).toBe(0); | ||
| expect(second.stdout).toContain("No new findings above baseline"); | ||
| if (multi) { | ||
| rmSync(join(project, "b", ".cve-lite"), { recursive: true }); | ||
| const mixed = scan(project); | ||
| expect(mixed.status).toBe(1); | ||
| expect(mixed.stdout).toContain("a/: No new findings above baseline"); | ||
| expect(mixed.stdout).toContain("b/: Failing:"); | ||
| } | ||
| } finally { | ||
| rmProject(project); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts a clean first run without claiming existing debt", () => { | ||
| const project = makeProject(multi, false); | ||
| try { | ||
| const result = scan(project); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).toContain("Baseline saved"); | ||
| expect(result.stdout).not.toContain("Failing:"); | ||
| expect(result.stdout).not.toContain("accept the existing debt"); | ||
| for (const folder of multi ? ["a", "b"] : [""]) { | ||
| expect(existsSync(join(project, folder, ".cve-lite", "baseline.json"))).toBe(true); | ||
| } | ||
| } finally { | ||
| rmProject(project); | ||
| } | ||
| }); | ||
|
|
||
| it("keeps the gate active while suppressing both explanatory lines under --json", () => { | ||
| const project = makeProject(multi, true); | ||
| try { | ||
| const result = scan(project, ["--json"]); | ||
| expect(result.status).toBe(1); | ||
| expect(result.stdout).not.toContain("Failing:"); | ||
| expect(result.stdout).not.toContain("accept the existing debt"); | ||
| } finally { | ||
| rmProject(project); | ||
| } | ||
| }); | ||
|
|
||
| it("does not fail when all findings are below --fail-on", () => { | ||
| const project = makeProject(multi, true); | ||
| try { | ||
| const result = scan(project, ["--fail-on", "critical"]); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).not.toContain("Failing:"); | ||
| } finally { | ||
| rmProject(project); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("preserves single-folder --fix exit 0 with an unfixable high finding and no baseline", () => { | ||
| const project = mkProject(npmProject( | ||
| { name: "ratchet-fix-test", dependencies: { parent: "1.0.0" } }, | ||
| { parent: "1.0.0", [VULNERABLE_PACKAGE]: "1.0.0" }, | ||
| )); | ||
| try { | ||
| const result = scan(project, ["--fix"]); | ||
| expect(result.status).toBe(0); | ||
| const fixResult = JSON.parse(readFileSync(join(project, "cve-lite-fix-result.json"), "utf8")); | ||
| expect(fixResult.findingsBeforeFix).toBe(1); | ||
| expect(fixResult.findingsAfterFix).toBe(1); | ||
| expect(fixResult.appliedFixCount).toBe(0); | ||
| expect(result.stdout).not.toContain("Failing:"); | ||
| } finally { | ||
| rmProject(project); | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.