-
Notifications
You must be signed in to change notification settings - Fork 23
fix(logging): redact sensitive tokens in workflow logs #310
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,46 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test'; | ||
|
|
||
| import { LogIngestService } from '../log-ingest.service'; | ||
| import type { LogStreamRepository } from '../../trace/log-stream.repository'; | ||
|
|
||
| describe('LogIngestService', () => { | ||
| const originalEnv = { ...process.env }; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.LOG_KAFKA_BROKERS = 'localhost:9092'; | ||
| process.env.LOKI_URL = 'http://localhost:3100'; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = { ...originalEnv }; | ||
| }); | ||
|
|
||
| it('redacts sensitive data before pushing to Loki', async () => { | ||
| const repository = { | ||
| upsertMetadata: mock(async () => undefined), | ||
| } as unknown as LogStreamRepository; | ||
|
|
||
| const service = new LogIngestService(repository); | ||
| const push = mock(async () => undefined); | ||
| (service as any).lokiClient = { push }; | ||
|
|
||
| await (service as any).processEntry({ | ||
| runId: 'run-1', | ||
| nodeRef: 'node-1', | ||
| stream: 'stdout', | ||
| message: 'token=abc123 authorization=Bearer super-secret', | ||
| timestamp: '2026-02-21T00:00:00.000Z', | ||
| organizationId: 'org-1', | ||
| }); | ||
|
|
||
| expect(push).toHaveBeenCalledTimes(1); | ||
| const call = push.mock.calls[0] as unknown[] | undefined; | ||
| expect(call).toBeTruthy(); | ||
| const lines = (call?.[1] ?? []) as { message: string }[]; | ||
| expect(lines).toHaveLength(1); | ||
| expect(lines[0]?.message).toContain('token=[REDACTED]'); | ||
| expect(lines[0]?.message).toContain('authorization=[REDACTED]'); | ||
| expect(lines[0]?.message).not.toContain('abc123'); | ||
| expect(lines[0]?.message).not.toContain('super-secret'); | ||
| }); | ||
| }); |
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,48 @@ | ||
| import { describe, expect, it } from 'bun:test'; | ||
|
|
||
| import { redactSensitiveData } from '../redact-sensitive'; | ||
|
|
||
| describe('redactSensitiveData', () => { | ||
| it('redacts common secret key-value pairs', () => { | ||
| const input = | ||
| 'authorization=Bearer abcdefghijklmnop token=123456 password=hunter2 api_key=xyz987'; | ||
| const redacted = redactSensitiveData(input); | ||
|
|
||
| expect(redacted).toContain('authorization=[REDACTED]'); | ||
| expect(redacted).toContain('token=[REDACTED]'); | ||
| expect(redacted).toContain('password=[REDACTED]'); | ||
| expect(redacted).toContain('api_key=[REDACTED]'); | ||
| }); | ||
|
|
||
| it('redacts JSON-style secret fields', () => { | ||
| const input = '{"access_token":"abc123","client_secret":"super-secret"}'; | ||
| const redacted = redactSensitiveData(input); | ||
|
|
||
| expect(redacted).toBe('{"access_token":"[REDACTED]","client_secret":"[REDACTED]"}'); | ||
| }); | ||
|
|
||
| it('redacts token-like standalone values and URL params', () => { | ||
| const input = | ||
| 'https://example.com?token=abc123&foo=1 Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.aGVsbG8td29ybGQ.signature ghp_abcdefghijklmnopqrstuvwxyz1234 sk-abcdefghijklmnopqrstuvwxyz123456'; | ||
| const redacted = redactSensitiveData(input); | ||
|
|
||
| expect(redacted).toContain('?token=[REDACTED]&foo=1'); | ||
| expect(redacted).not.toContain('eyJhbGciOiJIUzI1Ni'); | ||
| expect(redacted).not.toContain('ghp_abcdefghijklmnopqrstuvwxyz1234'); | ||
| expect(redacted).not.toContain('sk-abcdefghijklmnopqrstuvwxyz123456'); | ||
| }); | ||
|
|
||
| it('redacts github clone URLs with embedded x-access-token credentials', () => { | ||
| const input = | ||
| 'CLONE_URL=https://x-access-token:ghs_abcdefghijklmnopqrstuvwxyz1234567890@github.com/LuD1161/git-test-repo.git'; | ||
| const redacted = redactSensitiveData(input); | ||
|
|
||
| expect(redacted).toContain('CLONE_URL=https://x-access-token:[REDACTED]@github.com/'); | ||
| expect(redacted).not.toContain('ghs_abcdefghijklmnopqrstuvwxyz1234567890'); | ||
| }); | ||
|
|
||
| it('preserves non-sensitive text', () => { | ||
| const input = 'workflow finished successfully in 245ms'; | ||
| expect(redactSensitiveData(input)).toBe(input); | ||
| }); | ||
| }); |
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,42 @@ | ||
| const REDACTED = '[REDACTED]'; | ||
|
|
||
| // Patterns for high-signal secret forms commonly seen in logs. | ||
| const SECRET_KEY_PATTERN = | ||
| '(?:access_token|refresh_token|id_token|token|api[_-]?key|apikey|client_secret|secret|password|authorization|x-api-key|private_key|session_token)'; | ||
|
|
||
| const JSON_SECRET_PAIR_REGEX = new RegExp( | ||
| `("(${SECRET_KEY_PATTERN})"\\s*:\\s*")([^"\\r\\n]{3,})(")`, | ||
| 'gi', | ||
| ); | ||
| const AUTH_SCHEME_ASSIGNMENT_REGEX = /\bauthorization\b\s*([=:])\s*(?:Bearer|Basic)\s+[^\s,;&]+/gi; | ||
| const ASSIGNMENT_SECRET_PAIR_REGEX = new RegExp( | ||
| `(\\b${SECRET_KEY_PATTERN}\\b\\s*[=:]\\s*)([^\\s,;&@]+)`, | ||
| 'gi', | ||
| ); | ||
| const URL_SECRET_PARAM_REGEX = new RegExp(`([?&](?:${SECRET_KEY_PATTERN})=)([^&#\\s]+)`, 'gi'); | ||
| const BEARER_REGEX = /\bBearer\s+[A-Za-z0-9._~+/=-]{8,}\b/gi; | ||
| const BASIC_REGEX = /\bBasic\s+[A-Za-z0-9+/=]{8,}\b/gi; | ||
| const JWT_REGEX = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g; | ||
| const GITHUB_TOKEN_REGEX = /\b(?:gh[pousr]_[A-Za-z0-9_]{20,}|github_pat_[A-Za-z0-9_]{20,})\b/g; | ||
| const GENERIC_SK_TOKEN_REGEX = /\bsk-[A-Za-z0-9]{20,}\b/g; | ||
|
|
||
| export function redactSensitiveData(input: string): string { | ||
| if (!input) { | ||
| return input; | ||
| } | ||
|
|
||
| let output = input; | ||
|
|
||
| output = output.replace(JSON_SECRET_PAIR_REGEX, `$1${REDACTED}$4`); | ||
| output = output.replace(AUTH_SCHEME_ASSIGNMENT_REGEX, `authorization$1${REDACTED}`); | ||
| output = output.replace(ASSIGNMENT_SECRET_PAIR_REGEX, `$1${REDACTED}`); | ||
| output = output.replace(URL_SECRET_PARAM_REGEX, `$1${REDACTED}`); | ||
|
|
||
| output = output.replace(BEARER_REGEX, `Bearer ${REDACTED}`); | ||
| output = output.replace(BASIC_REGEX, `Basic ${REDACTED}`); | ||
| output = output.replace(JWT_REGEX, REDACTED); | ||
| output = output.replace(GITHUB_TOKEN_REGEX, REDACTED); | ||
| output = output.replace(GENERIC_SK_TOKEN_REGEX, REDACTED); | ||
|
|
||
| return output; | ||
| } | ||
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
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.
The JSON redaction regex only matches secret values with length
>= 3, so payloads like{"token":"ab"}or{"password":"x"}are returned unredacted and can still leak credentials in logs. This affects any secret field serialized as JSON with short values, which is plausible for test tokens, short passwords, or one-time codes, and it bypasses the intended protection inredactSensitiveData.Useful? React with 👍 / 👎.