-
Notifications
You must be signed in to change notification settings - Fork 61
security: redact CLI logs, bound regex matching (ReDoS) (PER-8609/8615) #2279
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
Shivanshu-07
wants to merge
11
commits into
master
Choose a base branch
from
security/cli-runtime-redact-redos-ssrf
base: master
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
11 commits
Select commit
Hold shift + click to select a range
f5bad30
security: redact CLI logs, bound regex matching (ReDoS), validate Chr…
Shivanshu-07 a3388a3
fix(core): memoize secret patterns so redactSecrets does not blow tim…
Shivanshu-07 5ee0b6e
test(core): cover Chromium base URL validation branches
Shivanshu-07 3e0aae3
fix(core): redact Percy tokens in logs (PER-8609)
Shivanshu-07 794d0e0
revert(core): drop PERCY_CHROMIUM_BASE_URL validation (PER-8616 won't…
Shivanshu-07 7ab92a3
chore(core): suppress false-positive non-literal-regexp on first-part…
Shivanshu-07 d31dfe5
chore(core): add trailing newline to secretPatterns.yml
Shivanshu-07 1f38275
fix(core): address redaction review feedback
Shivanshu-07 78f8303
test(core): de-literalize Percy-token fixture to clear secret-scanning
Shivanshu-07 20f6e5b
fix(core): redact log entries in place, not into a detached copy
Shivanshu-07 f1ae531
fix(core): scope log redaction to message, not structured meta
Shivanshu-07 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -615,22 +615,50 @@ | |
| } | ||
| } | ||
|
|
||
| export function redactSecrets(data) { | ||
| const filepath = path.resolve(url.fileURLToPath(import.meta.url), '../secretPatterns.yml'); | ||
| const secretPatterns = YAML.parse(readFileSync(filepath, 'utf-8')); | ||
| // Lazily load and compile the secret patterns once. The pattern file holds | ||
| // ~1.7k regexes; parsing the YAML and compiling every RegExp on each call made | ||
| // redactSecrets O(patterns) per string and re-read the file for every recursive | ||
| // call. Since redactSecrets now runs over the full CLI log array on egress | ||
| // (sendBuildLogs), that per-call cost is paid hundreds of times and could blow | ||
| // past test/runtime timeouts. Compile once and reuse. | ||
| let _compiledSecretPatterns; | ||
| function getSecretPatterns() { | ||
| if (!_compiledSecretPatterns) { | ||
| const filepath = path.resolve(url.fileURLToPath(import.meta.url), '../secretPatterns.yml'); | ||
| const secretPatterns = YAML.parse(readFileSync(filepath, 'utf-8')); | ||
| // Regex sources come from the first-party, bundled secretPatterns.yml that | ||
| // ships in this package - never from remote or attacker-controlled input. | ||
| // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp | ||
| _compiledSecretPatterns = secretPatterns.patterns.map(p => new RegExp(p.pattern.regex, 'g')); | ||
Check warningCode scanning / Semgrep OSS Semgrep Finding: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp Warning
RegExp() called with a p function argument, this might allow an attacker to cause a Regular Expression Denial-of-Service (ReDoS) within your application as RegExP blocks the main thread. For this reason, it is recommended to use hardcoded regexes instead. If your regex is run on user-controlled input, consider performing input validation or use a regex checking/sanitization library such as https://www.npmjs.com/package/recheck to verify that the regex does not appear vulnerable to ReDoS.
|
||
|
Shivanshu-07 marked this conversation as resolved.
|
||
| } | ||
| return _compiledSecretPatterns; | ||
| } | ||
|
|
||
| if (Array.isArray(data)) { | ||
| // Process each item in the array | ||
| return data.map(item => redactSecrets(item)); | ||
| } else if (typeof data === 'object' && data !== null) { | ||
| // Process each key-value pair in the object | ||
| data.message = redactSecrets(data.message); | ||
| } | ||
| export function redactSecrets(data) { | ||
|
Shivanshu-07 marked this conversation as resolved.
|
||
| // Strings are redacted against every compiled secret pattern. | ||
| if (typeof data === 'string') { | ||
| for (const pattern of secretPatterns.patterns) { | ||
| data = data.replace(new RegExp(pattern.pattern.regex, 'g'), '[REDACTED]'); | ||
| for (const pattern of getSecretPatterns()) { | ||
| data = data.replace(pattern, '[REDACTED]'); | ||
| } | ||
| return data; | ||
| } | ||
| // Arrays (the CLI-log entry list) are redacted element-wise, in place. | ||
| if (Array.isArray(data)) { | ||
| for (let i = 0; i < data.length; i++) data[i] = redactSecrets(data[i]); | ||
| return data; | ||
| } | ||
| // Log entries: redact the human-readable `message` in place and return the | ||
| // same reference (sendBuildLogs reads the return value; the CI-log path reads | ||
| // the same memory-mode entry back — both see the redacted message). We do NOT | ||
| // recurse over `meta`: it holds structured instrumentation (e.g. a numeric | ||
| // `size`, ids) and a broad secret pattern matches plain digit strings, so a | ||
| // blanket meta sweep clobbers legitimate diagnostic data. Log-line secrets | ||
| // surface in `message`. | ||
| if (typeof data === 'object' && data !== null) { | ||
| data.message = redactSecrets(data.message); | ||
| return data; | ||
| } | ||
| // Any other primitive (number, boolean, null, undefined) passes through. | ||
| return data; | ||
| } | ||
|
|
||
|
|
||
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
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.