-
Notifications
You must be signed in to change notification settings - Fork 0
Redact high-entropy tokens from spoken alerts #582
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
9 commits
Select commit
Hold shift + click to select a range
22d80d2
Redact high-entropy tokens from spoken alerts
nedtwigg a998f5d
tests: keep the OSC title-source test scoped to title sources
nedtwigg b6b2ca6
redact: collapse the alphabet ladder into a tier table
nedtwigg 21dc457
docs: state the redaction rule once, move its cost to the rationale
nedtwigg d7a1480
Clarify entropy tradeoffs and complete boundary coverage
nedtwigg 82d952f
Handle grouped hex and preserve separators during redaction
nedtwigg ef481b3
Document grouped-hex and padding redaction tradeoffs
nedtwigg 6f500c5
Detect embedded hex keys and document base32 false positives
nedtwigg f4cedae
Pin the trailing boundary of embedded hex runs
nedtwigg 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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { redactHighEntropyTokens } from './redact-high-entropy'; | ||
|
|
||
| describe('redactHighEntropyTokens', () => { | ||
| it.each([ | ||
| ['hex', '8b7d0c4e9f2a61035e8c9d1f04a76b23'], | ||
| ['uppercase hex', '8B7D0C4E9F2A61035E8C9D1F04A76B23'], | ||
| ['UUID', '3f2504e0-4f89-11d3-9a0c-0305e82c3301'], | ||
| ['grouped hex', '8b7d-0c4e-9f2a-6103'], | ||
| ['underscore-grouped hex', '8B7D_0C4E_9F2A_6103'], | ||
| ['base32', 'K7QW2MXP5RZV3NAT6YHC4BSD'], | ||
| ['lowercase base32', 'k7qw2mxp5rzv3nat6yhc4bsd'], | ||
| ['padded base32', 'K7QW2MXP5RZV3NAT6YHC4BSD======'], | ||
| ['base64', 'k8Xq+W2m/P5rZ9vN3aT6yHc4BsD0EfGj'], | ||
| ['padded base64', 'k8Xq+W2m/P5rZ9vN3aT6yA=='], | ||
| ['base64url', 'k8Xq-W2m_P5rZ9vN3aT6yHc4BsD0EfGj'], | ||
| ['prefixed token', 'ghp_k8XqW2mP5rZ9vN3aT6yHc4BsD0EfGj'], | ||
| ])('redacts an entire %s token', (_kind, token) => { | ||
| expect(redactHighEntropyTokens(`key="${token}" done`)).toBe('key="REDACTED" done'); | ||
| }); | ||
|
|
||
| it('replaces every occurrence while preserving surrounding text', () => { | ||
| const token = '8b7d0c4e9f2a61035e8c9d1f04a76b23'; | ||
| expect(redactHighEntropyTokens(`first=${token}; second=${token}!`)) | ||
| .toBe('first=REDACTED; second=REDACTED!'); | ||
| }); | ||
|
|
||
| it('preserves equals separators while removing trailing padding', () => { | ||
| expect(redactHighEntropyTokens('CargoBuildFinished=ok BackgroundTaskScheduler==finished')) | ||
| .toBe('REDACTED=ok REDACTED==finished'); | ||
| expect(redactHighEntropyTokens('CargoBuildFinished== next')) | ||
| .toBe('REDACTED next'); | ||
| }); | ||
|
|
||
| it('normalizes separators only for grouped hex, counting only its digits', () => { | ||
| expect(redactHighEntropyTokens('PostgreSQL_Connection_Manager implementation_details_v2')) | ||
| .toBe('PostgreSQL_Connection_Manager implementation_details_v2'); | ||
| expect(redactHighEntropyTokens('8-b-7-d-0-c-4-e-9-f-2-a-6-1-0')) | ||
| .toBe('8-b-7-d-0-c-4-e-9-f-2-a-6-1-0'); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'pod-3f2504e0-4f89-11d3-9a0c-0305e82c3301', | ||
| '3f2504e0-4f89-11d3-9a0c-0305e82c3301-log', | ||
| 'session_8b7d0c4e9f2a61035e8c9d1f04a76b23', | ||
| 'job_8b7d-0c4e-9f2a-6103_output', | ||
| ])('redacts a whole candidate containing an embedded hex key: %s', (token) => { | ||
| expect(redactHighEntropyTokens(token)).toBe('REDACTED'); | ||
| }); | ||
|
|
||
| it('checks every hex run without exempting the entire enclosing token', () => { | ||
| expect(redactHighEntropyTokens(`8b7d0c4e9f2a6103_${'x'.repeat(100)}`)).toBe('REDACTED'); | ||
| expect(redactHighEntropyTokens(`${'0'.repeat(100)}_job_8b7d0c4e9f2a6103`)).toBe('REDACTED'); | ||
| expect(redactHighEntropyTokens('pod_8b7d0c4e9f2a610')).toBe('pod_8b7d0c4e9f2a610'); | ||
| expect(redactHighEntropyTokens('8b7d0c4e9f2a610_dop')).toBe('8b7d0c4e9f2a610_dop'); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['ordinary text', 'pnpm test: build finished'], | ||
| ['a long word under every entropy cutoff', 'internationalization configuration'], | ||
| ['non-ASCII text', '构建完成。終了コード:0'], | ||
| ['a uniform run', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], | ||
| ])('preserves %s', (_kind, text) => { | ||
| expect(redactHighEntropyTokens(text)).toBe(text); | ||
| }); | ||
|
|
||
| // Pin the narrower alphabets separately from the base64 tier. | ||
| it.each([ | ||
| ['hex at the 3.0 cutoff', '8b7d0c4e8b7d0c4e', true], | ||
| ['hex below it', '8b7d0c4e8b7d0c4d', false], | ||
| ['15-char hex, too short', '8b7d0c4e9f2a610', false], | ||
| ['16-char base32, too short for the base64 tier', 'K7QW2MXP5RZV3NAT', true], | ||
| ['15-char base32, too short', 'K7QW2MXP5RZV3NA', false], | ||
| ['base32 at the 3.5 cutoff', 'ABCDEFGHJKLMABCD', true], | ||
| ['16-char base32 below the 3.5 cutoff', 'k7qw2mxpk7qw2mxp', false], | ||
| ['16-char base64, too short for any tier', 'k8Xq+W2m/P5rZ9vN', false], | ||
| ['19-char base64, too short', 'k8Xq+W2m/P5rZ9vN3aT', false], | ||
| ['20-char base64', 'k8Xq+W2m/P5rZ9vN3aT6', true], | ||
| ['base64 at the 4.0 cutoff', '0123456789ghijkl0123456789ghijkl', true], | ||
| ['base64 below it', '0123456789ghijkl0123456789ghijkk', false], | ||
| ])('%s', (_kind, token, redacted) => { | ||
| expect(redactHighEntropyTokens(token)).toBe(redacted ? 'REDACTED' : token); | ||
| }); | ||
|
|
||
| it('folds case on case-insensitive alphabets, so `A` and `a` are one symbol', () => { | ||
| // Case-sensitive entropy is 3.5 bits; folding gives 2.75 bits, below the | ||
| // hex cutoff. Treating case variants as distinct would wrongly redact it. | ||
| expect(redactHighEntropyTokens('aAbBcCdDeEfF0000')).toBe('aAbBcCdDeEfF0000'); | ||
| }); | ||
|
|
||
| it('scores the whole token, never a prefix', () => { | ||
| const token = `8b7d0c4e9f2a6103${'a'.repeat(10_000)}`; | ||
| expect(redactHighEntropyTokens(token)).toBe(token); | ||
| }); | ||
| }); | ||
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,62 @@ | ||
| /** One opaque-token shape: the alphabet, the length below which a sample is too | ||
| * short to judge, and the bits/character above which it reads as random. Ordered | ||
| * narrowest alphabet first, so a token is scored against the tightest one it fits. | ||
| * Finite samples often fall below their alphabet's maximum entropy. */ | ||
| interface TokenTier { | ||
| readonly alphabet: RegExp; | ||
| readonly minLength: number; | ||
| readonly minEntropy: number; | ||
| /** Remove encoding separators and fold case before measuring length or entropy. */ | ||
| readonly normalize: (value: string) => string; | ||
| } | ||
|
|
||
| const HEX_GROUPS = '[0-9a-f]+(?:[-_][0-9a-f]+)*'; | ||
| // Match whole groups: the final `d` in `pod-<hex>` is not a key digit. | ||
| const HEX_RUNS = new RegExp(`(?<![A-Za-z0-9])${HEX_GROUPS}(?![A-Za-z0-9])`, 'gi'); | ||
| const HEX_TIER: TokenTier = { | ||
| alphabet: new RegExp(`^${HEX_GROUPS}$`, 'i'), | ||
| minLength: 16, | ||
| minEntropy: 3, | ||
| normalize: (value) => value.replace(/[-_]/g, '').toLowerCase(), | ||
| }; | ||
|
|
||
| const TIERS: readonly TokenTier[] = [ | ||
| HEX_TIER, | ||
| { alphabet: /^[a-z2-7]+$/i, minLength: 16, minEntropy: 3.5, normalize: (value) => value.toLowerCase() }, | ||
|
dormouse-bot marked this conversation as resolved.
|
||
| { alphabet: /^[A-Za-z0-9+/_-]+$/, minLength: 20, minEntropy: 4, normalize: (value) => value }, | ||
| ]; | ||
|
|
||
| /** Shannon entropy in bits per character over an ASCII histogram. */ | ||
| function entropyOf(value: string): number { | ||
| const counts = new Uint32Array(128); | ||
| for (let i = 0; i < value.length; i++) counts[value.charCodeAt(i)]++; | ||
| let entropy = 0; | ||
| for (let code = 0; code < counts.length; code++) { | ||
| const count = counts[code]; | ||
| if (count === 0) continue; | ||
| const probability = count / value.length; | ||
| entropy -= probability * Math.log2(probability); | ||
| } | ||
| return entropy; | ||
| } | ||
|
|
||
| function isHighEntropy(value: string, tier: TokenTier): boolean { | ||
| const counted = tier.normalize(value); | ||
| return counted.length >= tier.minLength && entropyOf(counted) >= tier.minEntropy; | ||
| } | ||
|
|
||
| /** Replace opaque ASCII tokens; this is a randomness heuristic, not a guarantee | ||
| * that all secrets (or only secrets) are removed. Trailing padding joins the | ||
| * replaced span but not the entropy estimate. */ | ||
| export function redactHighEntropyTokens(text: string): string { | ||
| return text.replace(/([A-Za-z0-9+/_-]{16,})(?:=+(?![A-Za-z0-9+/_=-]))?/g, (token, value: string) => { | ||
| // A non-hex prefix/suffix must not force an embedded key to use the higher | ||
| // base64 cutoff. Replace the whole candidate when any hex run qualifies. | ||
| for (const [hexRun] of value.matchAll(HEX_RUNS)) { | ||
| if (isHighEntropy(hexRun, HEX_TIER)) return 'REDACTED'; | ||
| } | ||
| const tier = TIERS.find((t) => t.alphabet.test(value)); | ||
| if (!tier) return token; | ||
| return isHighEntropy(value, tier) ? 'REDACTED' : token; | ||
| }); | ||
| } | ||
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
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.