Skip to content
198 changes: 198 additions & 0 deletions packages/core/src/telemetry/qwen-logger/qwen-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ProtocolTagSanitizedEvent,
RipgrepRuntimeRecoveryEvent,
SubagentExecutionEvent,
InvalidChunkEvent,
type ToolCallEvent,
} from '../types.js';
import type { RumEvent, RumPayload } from './event-types.js';
Expand Down Expand Up @@ -1132,4 +1133,201 @@ describe('QwenLogger', () => {
expect(rumEvent.properties).not.toHaveProperty('mcp_server_name');
});
});

describe('error text redaction', () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
it('redacts URL credentials from telemetry error text', () => {
expect(
TEST_ONLY.redactTelemetryError(
'Command: git clone https://x-access-token:ghs_testsecret123@github.com/org/repo.git',
),
).toBe(
'Command: git clone https://***REDACTED***@github.com/org/repo.git',
);
});

it('redacts Authorization headers from telemetry error text', () => {
expect(
TEST_ONLY.redactTelemetryError(
'curl -H "Authorization: Bearer ghs_testsecret123" https://api.github.com/repos',
),
).toBe(
'curl -H "Authorization: ***REDACTED*** https://api.github.com/repos',
);
});

it('redacts secret flags from telemetry error text', () => {
expect(
TEST_ONLY.redactTelemetryError(
'npm publish --_authToken npm_testsecret123 --registry https://registry.npmjs.org',
),
).toBe(
'npm publish --_authToken ***REDACTED*** --registry https://registry.npmjs.org',
);
});

it('redacts KEY=value env-style secrets from telemetry error text', () => {
expect(
TEST_ONLY.redactTelemetryError(
'env GITHUB_TOKEN=ghs_testsecret123 npm publish',
),
).toBe('env GITHUB_TOKEN=***REDACTED*** npm publish');
});

it('redacts database DSN credentials', () => {
expect(
TEST_ONLY.redactTelemetryError(
'connect postgres://user:supersecret123@db.internal:5432/app',
),
).toBe('connect postgres://***REDACTED***@db.internal:5432/app');
});

it('preserves non-sensitive error text', () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
expect(
TEST_ONLY.redactTelemetryError('Request failed with status 429'),
).toBe('Request failed with status 429');
});

it('preserves non-secret key=value assignments and short counters', () => {
expect(
TEST_ONLY.redactTelemetryError(
'env USER=alice PATH=/usr/bin max_tokens=8192',
),
).toBe('env USER=alice PATH=/usr/bin max_tokens=8192');
});

it('redacts error_message on the enqueue boundary', () => {
const logger = QwenLogger.getInstance(mockConfig)!;
const enqueueSpy = vi.spyOn(logger, 'enqueueLogEvent');
const event = {
'event.name': 'tool_call',
'event.timestamp': '2025-01-01T12:00:00.000Z',
function_name: 'shell',
function_args: {},
duration_ms: 1,
status: 'error',
success: false,
error:
'Command: git clone https://token:ghs_testsecret123@github.com/org/repo.git',
error_type: 'exit_code',
prompt_id: 'prompt-tool',
tool_type: 'native',
} as ToolCallEvent;

logger.logToolCallEvent(event);

const errorMessage = enqueueSpy.mock.calls[0][0].properties?.[
'error_message'
] as string;
expect(errorMessage).not.toContain('ghs_testsecret123');
expect(errorMessage).toContain('***REDACTED***');
});

it('redacts top-level message on the enqueue boundary', () => {
const logger = QwenLogger.getInstance(mockConfig)!;
const enqueueSpy = vi.spyOn(logger, 'enqueueLogEvent');

logger.logInvalidChunkEvent(
new InvalidChunkEvent('Authorization: Bearer ghs_testsecret123'),
);

const rumEvent = enqueueSpy.mock.calls[0][0] as { message?: string };
expect(rumEvent.message).not.toContain('ghs_testsecret123');
expect(rumEvent.message).toContain('***REDACTED***');
});

it('redacts canonical LLM credential env vars (*_API_KEY, *_ACCESS_KEY*)', () => {
expect(
TEST_ONLY.redactTelemetryError(
'env OPENAI_API_KEY=sk_testsecret123 npm publish',
),
).toBe('env OPENAI_API_KEY=***REDACTED*** npm publish');
expect(
TEST_ONLY.redactTelemetryError(
'env AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG npm run',
),
).toBe('env AWS_SECRET_ACCESS_KEY=***REDACTED*** npm run');
});

it('redacts non-Bearer Authorization schemes', () => {
expect(
TEST_ONLY.redactTelemetryError(
'curl -H "Authorization: Basic dXNlcjpwYXNz" https://api.example.com',
),
).toBe('curl -H "Authorization: ***REDACTED*** https://api.example.com');
});

it('redacts secrets delimited by tabs and newlines', () => {
expect(
TEST_ONLY.redactTelemetryError(
'npm publish --_authToken\tnpm_testsecret123 --registry x',
),
).not.toContain('npm_testsecret123');
expect(
TEST_ONLY.redactTelemetryError(
'npm publish --_authToken\nnpm_testsecret123 --registry x',
),
).not.toContain('npm_testsecret123');
});

it('redacts quote-opened secret values', () => {
expect(TEST_ONLY.redactTelemetryError('TOKEN="ghs_testsecret123"')).toBe(
'TOKEN=***REDACTED***',
);
expect(
TEST_ONLY.redactTelemetryError("--token 'ghs_testsecret123'"),
).toBe('--token ***REDACTED***');
});

it('redacts long flags containing a secret keyword', () => {
expect(
TEST_ONLY.redactTelemetryError(
'aws --aws-access-key AKIA_testsecret123',
),
).toBe('aws --aws-access-key ***REDACTED***');
});

it('does not consume a lone backslash as a complete secret value', () => {
const redacted = TEST_ONLY.redactTelemetryError(
'npm publish --token=\\' + '\n' + 'greatsecret1234',
);
// The trailing `\` is a shell line continuation, not a value. The
// redactor must not emit a redaction marker while leaving the real
// credential in cleartext immediately after it.
expect(redacted).not.toContain('***REDACTED***');
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
});

it('does not swallow the next argument after an empty secret flag value', () => {
expect(
TEST_ONLY.redactTelemetryError('git push --password= origin main'),
).toBe('git push --password= origin main');
});

it('redacts the hook error property on the enqueue boundary', () => {
const configWithLogPrompts = makeFakeConfig({
getTelemetryLogPromptsEnabled: () => true,
});
const logger = QwenLogger.getInstance(configWithLogPrompts)!;
const enqueueSpy = vi.spyOn(logger, 'enqueueLogEvent');

logger.logHookCallEvent(
new HookCallEvent(
'PostToolUse',
'command',
'cleanup.sh',
{ tool_name: 'shell' },
200,
false,
undefined,
1,
'',
'stderr',
'hook failed: Authorization: Bearer ghs_testsecret123',
),
);

const error = enqueueSpy.mock.calls[0][0].properties?.['error'] as string;
expect(error).not.toContain('ghs_testsecret123');
expect(error).toContain('***REDACTED***');
});
});
});
98 changes: 98 additions & 0 deletions packages/core/src/telemetry/qwen-logger/qwen-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ import { sanitizeHookName } from '../sanitize.js';
import { InstallationManager } from '../../config/installationManager.js';
import { FixedDeque } from 'mnemonist';
import { AuthType } from '../../core/contentGenerator.js';
import {
redactUrlCredentials,
REDACTED_URL_CREDENTIAL,
} from '../../extension/redaction.js';
import { stripAnsiAndControl } from '../../utils/textUtils.js';

// Usage statistics collection endpoint
const USAGE_STATS_HOSTNAME = 'gb4w8c3ygj-default-sea.rum.aliyuncs.com';
Expand Down Expand Up @@ -103,6 +108,80 @@ const MAX_EVENTS = 1000;
*/
const MAX_RETRY_EVENTS = 100;

/**
* Error-text keys in `properties` that can carry raw shell output or error
* text (command lines, HTTP headers, provider error bodies, hook failures).
*/
const ERROR_TEXT_PROPERTY_KEYS = ['error_message', 'error_excerpt', 'error'];
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

/**
* A secret value in free-form error text: an optional opening quote, a run of
* non-whitespace/non-quote/non-backslash characters, and an optional closing
* quote. Accepting the opening quote means `TOKEN="x"`, `--token 'x'` and
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
* `Authorization: Bearer "x"` all redact instead of failing on the quote. It
* never crosses whitespace, so a following token is left untouched. Backslash
* is excluded so a lone shell line-continuation `\` (or an escaped nested
* quote) is never consumed as a complete value, which would otherwise leave
* the real credential cleartext immediately after the redaction marker.
*/
const SECRET_VALUE = String.raw`["'\`]?[^\s"'\`\\]+["'\`]?`;
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

/**
* `Authorization: <scheme> <value>` / `authorization=<scheme> <value>` inside a
* shell command line. Any auth scheme (Bearer, Basic, Digest, token, …) is
* skipped before the value rather than only `Bearer`.
*/
const AUTHORIZATION_PATTERN = new RegExp(
String.raw`\b(authorization\s*[:=]\s*)(?:[A-Za-z0-9._~+/-]+\s+)?` +
SECRET_VALUE,
'gi',
);

/**
* Secret-bearing long flags: `--token x`, `--password=y`, `--aws-access-key
* w`. The secret keyword may sit anywhere inside the flag name, so long flags
* like `--github-api-key` or `--_authToken` are caught too, not only the exact
* spellings `--token` / `--password`.
*/
const SECRET_FLAG_PATTERN = new RegExp(
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
String.raw`(--[A-Za-z0-9_-]*?(?:token|password|secret|credential|key)[A-Za-z0-9_-]*(?:[=:]|\s+))` +
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
SECRET_VALUE,
'gi',
);

/**
* `KEY=value` env-style secrets: `GITHUB_TOKEN=ghs_xxx`,
* `OPENAI_API_KEY=sk_xxx`, `AWS_SECRET_ACCESS_KEY=…`, `DB_PASSWORD=secret`.
* The key must name a secret — any of the canonical secret words, including
* `key` for the `*_API_KEY` / `*_ACCESS_KEY*` LLM credential variables — and
* the value must be at least 10 non-whitespace characters, so short counters
* like `tokens_used=8192` and ordinary assignments like `USER=alice` pass
* through untouched.
*/
const ENV_SECRET_PATTERN = new RegExp(
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
String.raw`\b([A-Za-z0-9_]*(?:token|password|secret|credential|key)[A-Za-z0-9_]*\s*=\s*)\S{10,}`,
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
'gi',
);

/**
* Redacts error text before it enters the usage-statistics sink. Shell
* command lines are the dominant leak vector — they can embed URL
* credentials, Authorization headers, or secret flags — so this pass runs on
* the single enqueue choke point rather than each call site.
*
* Redaction runs on the raw text (before `stripAnsiAndControl`) so the
* whitespace-delimited patterns still see their `\n` / `\t` delimiters; the
* control characters are stripped from the result afterwards.
*/
function redactTelemetryError(text: string): string {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
return stripAnsiAndControl(
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
redactUrlCredentials(text)
.replace(AUTHORIZATION_PATTERN, `$1${REDACTED_URL_CREDENTIAL}`)
.replace(SECRET_FLAG_PATTERN, `$1${REDACTED_URL_CREDENTIAL}`)
.replace(ENV_SECRET_PATTERN, `$1${REDACTED_URL_CREDENTIAL}`),
);
}

export interface LogResponse {
nextRequestWaitMs?: number;
}
Expand Down Expand Up @@ -181,6 +260,7 @@ export class QwenLogger {

enqueueLogEvent(event: RumEvent): void {
try {
this.redactEventErrorText(event);
// Manually handle overflow for FixedDeque, which throws when full.
const wasAtCapacity = this.events.size >= MAX_EVENTS;

Expand All @@ -200,6 +280,23 @@ export class QwenLogger {
}
}

private redactEventErrorText(event: RumEvent): void {
const properties = event.properties;
if (properties) {
for (const key of ERROR_TEXT_PROPERTY_KEYS) {
const value = properties[key];
if (typeof value === 'string') {
properties[key] = redactTelemetryError(value);
}
}
}

const message = (event as RumExceptionEvent).message;
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
if (typeof message === 'string') {
(event as RumExceptionEvent).message = redactTelemetryError(message);
}
}

createRumEvent(
eventType: 'view' | 'action' | 'exception' | 'resource',
type: string,
Expand Down Expand Up @@ -1172,4 +1269,5 @@ export const TEST_ONLY = {
MAX_RETRY_EVENTS,
MAX_EVENTS,
FLUSH_INTERVAL_MS,
redactTelemetryError,
};
Loading