Skip to content

Commit 87c8431

Browse files
CopilotMossaka
andauthored
Extract redactSecrets to testable module (#20)
* Initial plan * Extract redactSecrets function to separate module for testability Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Mossaka <5447827+Mossaka@users.noreply.github.com> Co-authored-by: Jiaxiao Zhou <duibao55328@gmail.com>
1 parent 6f84feb commit 87c8431

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/cli.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Command } from 'commander';
2+
import { redactSecrets } from './redact-secrets';
23
import { parseDomains } from './cli';
34

45
describe('cli', () => {
@@ -96,18 +97,6 @@ describe('cli', () => {
9697
});
9798

9899
describe('secret redaction', () => {
99-
const redactSecrets = (command: string): string => {
100-
return command
101-
// Redact Authorization: Bearer <token>
102-
.replace(/(Authorization:\s*Bearer\s+)(\S+)/gi, '$1***REDACTED***')
103-
// Redact Authorization: <token> (non-Bearer)
104-
.replace(/(Authorization:\s+(?!Bearer\s))(\S+)/gi, '$1***REDACTED***')
105-
// Redact tokens in environment variables
106-
.replace(/(\w*(?:TOKEN|SECRET|PASSWORD|KEY|AUTH)\w*)=(\S+)/gi, '$1=***REDACTED***')
107-
// Redact GitHub tokens (ghp_, gho_, ghu_, ghs_, ghr_)
108-
.replace(/\b(gh[pousr]_[a-zA-Z0-9]{36,255})/g, '***REDACTED***');
109-
};
110-
111100
it('should redact Bearer tokens', () => {
112101
const command = 'curl -H "Authorization: Bearer ghp_1234567890abcdef" https://api.github.com';
113102
const result = redactSecrets(command);

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
cleanupHostIptables,
2020
cleanupFirewallNetwork,
2121
} from './host-iptables';
22+
import { redactSecrets } from './redact-secrets';
2223

2324
/**
2425
* Parses a comma-separated list of domains into an array of trimmed, non-empty domain strings

src/redact-secrets.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Redacts sensitive information from command strings
3+
*/
4+
export function redactSecrets(command: string): string {
5+
return command
6+
// Redact Authorization: Bearer <token>
7+
.replace(/(Authorization:\s*Bearer\s+)(\S+)/gi, '$1***REDACTED***')
8+
// Redact Authorization: <token> (non-Bearer)
9+
.replace(/(Authorization:\s+(?!Bearer\s))(\S+)/gi, '$1***REDACTED***')
10+
// Redact tokens in environment variables (TOKEN, SECRET, PASSWORD, KEY, API_KEY, etc)
11+
.replace(/(\w*(?:TOKEN|SECRET|PASSWORD|KEY|AUTH)\w*)=(\S+)/gi, '$1=***REDACTED***')
12+
// Redact GitHub tokens (ghp_, gho_, ghu_, ghs_, ghr_)
13+
.replace(/\b(gh[pousr]_[a-zA-Z0-9]{36,255})/g, '***REDACTED***');
14+
}

0 commit comments

Comments
 (0)