-
Notifications
You must be signed in to change notification settings - Fork 0
feat(protect): default vendor-secret redaction + prefilter anchors + ReDoS test #107
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createProtection } from '../../src/protect/runtime.js'; | ||
|
|
||
| // The default response ruleset ships vendor API-key redaction, so a leaked provider token is masked | ||
| // out of the box (no per-app rule authoring). Prefix-anchored + high-signal → low false-positive. | ||
| // | ||
| // NOTE: sample tokens are assembled from split fragments at runtime so no contiguous secret-shaped | ||
| // literal appears in this source file (which would trip secret-scanning push protection). The | ||
| // assembled string still matches the default rule's regex. | ||
|
|
||
| const textResp = (body: string) => | ||
| new Response(body, { status: 200, headers: { 'content-type': 'application/json' } }); | ||
|
|
||
| const body = (len: number) => 'x'.repeat(len); | ||
| const SAMPLES: Record<string, string> = { | ||
| stripe: 'sk_' + 'live_' + body(24), | ||
| github: 'gh' + 'p_' + body(36), | ||
| githubPat: 'github' + '_pat_' + body(62), | ||
| gitlab: 'gl' + 'pat-' + body(24), | ||
| slack: 'xox' + 'b-' + '123456789012-' + body(12), | ||
| anthropic: 'sk-' + 'ant-' + body(30), | ||
| googleOAuth: 'ya' + '29.' + body(40), | ||
| npm: 'np' + 'm_' + body(36), | ||
| }; | ||
|
|
||
| describe('default response rules — vendor API-key redaction', () => { | ||
| for (const [name, token] of Object.entries(SAMPLES)) { | ||
| it(`masks a leaked ${name} token by default`, async () => { | ||
| const p: any = await createProtection({ mode: 'block' }); // default response rules | ||
| const out = await p.screenResponse(textResp(JSON.stringify({ config: token }))); | ||
| const masked = await out.text(); | ||
| expect(masked.includes(token)).toBe(false); | ||
| expect(masked.includes('[REDACTED]')).toBe(true); | ||
| }); | ||
| } | ||
|
|
||
| it('leaves an ordinary response untouched (no false positive)', async () => { | ||
| const p: any = await createProtection({ mode: 'block' }); | ||
| const clean = JSON.stringify({ user: 'ada@example.com', note: 'the quick brown fox', id: 42 }); | ||
| const out = await p.screenResponse(textResp(clean)); | ||
| expect(await out.text()).toBe(clean); | ||
| }); | ||
| }); | ||
|
|
||
| describe('default response rules — performance / ReDoS safety', () => { | ||
| it('screens a large adversarial body in linear time (no catastrophic backtracking)', async () => { | ||
| const p: any = await createProtection({ mode: 'block' }); | ||
| // ~280 KB (< the 512 KiB screen cap, so it IS screened) of near-miss inputs that stress the | ||
| // default regexes: repeated token prefixes, a long alnum run, and JWT- / stack-frame-ish noise. | ||
| const adversarial = | ||
| ('sk_' + 'live_').repeat(10000) + | ||
| 'A'.repeat(100000) + | ||
| ('eyJ' + '_').repeat(10000) + | ||
| ('at x(y.js:1:').repeat(5000); | ||
| const start = performance.now(); | ||
| const out = await p.screenResponse(textResp(adversarial)); | ||
| await out.text(); | ||
| const ms = performance.now() - start; | ||
| expect(ms).toBeLessThan(1000); // linear-time; a ReDoS pattern would blow far past this | ||
| }); | ||
| }); |
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.
should those be hardcoded?