Repository navigation
fix(apps): parse csp-check scripts with jsdom and scan dist tarballs with gitleaks #233
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db71605
fix(apps): parse csp-check scripts with jsdom and scan dist tarballs …
nickvigilante d60ab8f
fix(dist-release): fail closed on gitleaks errors and require the sca…
nickvigilante 6f845fa
fix(offlinedocs): check scripts in template content and SVG script hrefs
nickvigilante 3d85a37
test(dist-release): build the allow-marker fixture without a single r…
nickvigilante 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,8 @@ | ||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| const hasGitleaks = spawnSync("gitleaks", ["version"]).status === 0; | ||
|
|
||
| // In CI a missing gitleaks fails these tests rather than skipping them. | ||
| export const needsGitleaks = { | ||
| skip: hasGitleaks || process.env.CI ? false : "gitleaks is not on PATH", | ||
| }; |
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,66 @@ | ||
| import { execFile } from "node:child_process"; | ||
| import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { promisify, stripVTControlCharacters } from "node:util"; | ||
|
|
||
| const run = promisify(execFile); | ||
| // gitleaks exits 1 on fatal errors as well as on leaks, so leaks get a code of their own. | ||
| const LEAKS_FOUND = 3; | ||
| const CONFIG = "[extend]\nuseDefault = true\n"; | ||
|
|
||
| /** | ||
| * Scan an unpacked package with gitleaks' built-in rules only. A finding names the rule, file and | ||
| * line, never the matched text. A missing gitleaks fails the scan unless `required` is false. | ||
| */ | ||
| export async function findSecrets(dir, { command = "gitleaks", required = true } = {}) { | ||
| const entries = await readdir(dir); | ||
| if (entries.includes(".gitleaksignore")) { | ||
| return [".gitleaksignore would suppress findings: .gitleaksignore"]; | ||
| } | ||
| const work = await mkdtemp(path.join(tmpdir(), "gitleaks-")); | ||
| const report = path.join(work, "report.json"); | ||
| const config = path.join(work, "gitleaks.toml"); | ||
| try { | ||
| await writeFile(config, CONFIG); | ||
| await run( | ||
| command, | ||
| [ | ||
| "dir", | ||
| "--no-banner", | ||
| "--redact", | ||
| "--ignore-gitleaks-allow", | ||
| "--config", | ||
| config, | ||
| "--gitleaks-ignore-path", | ||
| work, | ||
| "--exit-code", | ||
| String(LEAKS_FOUND), | ||
| "--report-format", | ||
| "json", | ||
| "--report-path", | ||
| report, | ||
| dir, | ||
| ], | ||
| { cwd: work, maxBuffer: 16 * 1024 * 1024 }, | ||
| ); | ||
| return []; | ||
| } catch (error) { | ||
| if (error.code === "ENOENT" && error.path === command) { | ||
| if (required) return [`gitleaks is required but was not found on PATH (${command})`]; | ||
| console.warn(`secret scan skipped: gitleaks was not found on PATH (${command})`); | ||
| return []; | ||
| } | ||
| if (error.code !== LEAKS_FOUND) { | ||
| const stderr = stripVTControlCharacters(String(error.stderr ?? "")).trim(); | ||
| throw new Error(`gitleaks exited ${error.code}: ${stderr}`, { cause: error }); | ||
| } | ||
| const findings = JSON.parse(await readFile(report, "utf8")); | ||
| if (!findings.length) throw new Error("gitleaks reported leaks but wrote no findings"); | ||
| return findings.map( | ||
| (f) => `secret (${f.RuleID}): ${path.relative(dir, f.File)}:${f.StartLine}`, | ||
| ); | ||
| } finally { | ||
| await rm(work, { recursive: true, force: true }); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: the tarball is extracted even when
checkEntryTypeshas already flagged symlinks or hardlinks. The risk is low, since we produce the tarball ourselves and tar has its own protections, but throwing before extraction is cheap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d60ab8f. A new
extractTarballincontents.mjsrunscheckEntryTypesand throws before callingtar, andpackPackageuses it for both extractions. Before the final unpack it also throws on any problem found so far. A test checks that a tarball with a symlink is refused and the destination stays empty.