:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mcp): redact secrets from ci failure-log and broken-tests responses (#121063)
posthog[bot]ae03c789dc6c
diff --git a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md b/docs/published/handbook/engineering/ai/implementing-mcp-tools.mdindex db440997cc6a..76e62b5c047e 100644--- a/docs/published/handbook/engineering/ai/implementing-mcp-tools.md+++ b/docs/published/handbook/engineering/ai/implementing-mcp-tools.md@@ -671,7 +671,9 @@ Product teams own their definitions and control which operations are exposed as`src/tools/featureFlags/updateFeatureFlagHooks.ts` is the reference.Do not shadow a generated tool with a hand-written tool of the same name.- The Logs tools use `logs/redactCredentialsHooks` to mask credentials in response strings and structured fields. This covers credential parameters, JSON and header values, URL passwords, bearer tokens, and private PostHog tokens. Nested URL encoding and JSON escaping do not bypass masking. The hook preserves unrelated text, except that strings with more than eight encoding layers are masked in full to bound parsing work. It does not change stored logs or responses outside MCP.+ The Logs tools use `logs/redactCredentialsHooks` to mask credentials in response strings and structured fields. This covers credential parameters, JSON and header values, URL passwords, bearer tokens, private PostHog and GitHub tokens, credential command-line flags such as `--password VALUE` and `-u user:pass`, and private key blocks inside one string. Nested URL encoding and JSON escaping do not bypass masking. The hook preserves unrelated text, except that strings with more than eight encoding layers are masked in full to bound parsing work. It does not change stored logs or responses outside MCP.++ The Engineering analytics CI failure-log tools and the broken-tests tool use `engineeringAnalytics/redactFailureLogsHooks`. It applies the same masking, but with a narrower list of credential names: CI output uses names such as `key`, `code` and `input_tokens` for diagnostics. It also masks the lines of a private key that spans several log lines in one job, including body lines next to an omitted gap, and keeps each line's original line number.#### Typed-confirm paradigm for destructive toolsdiff --git a/products/engineering_analytics/mcp/tools.yaml b/products/engineering_analytics/mcp/tools.yamlindex f63795b689e6..92cb21d8a9f2 100644--- a/products/engineering_analytics/mcp/tools.yaml+++ b/products/engineering_analytics/mcp/tools.yaml@@ -33,6 +33,7 @@ tools:breaking/resolved distinction needs the job-level source synced: without it those failures fall through toflaky/pr_only rather than being misreported. Counts are absolute, never rates (passing runs aren't in thisdata).+ hooks: engineeringAnalytics/redactFailureLogsHooksfeature_flag: engineering-analyticsengineering-analytics-ci-failure-logs:operation: engineering_analytics_ci_failure_logs@@ -53,6 +54,7 @@ tools:pushes' failures are included too. logs_available is false when CI hasn't failed, the logs aged out of theshort Logs retention, or a fork PR has no run association to resolve. Use pr-runs or workflow-jobs for therun/job metadata behind these logs. Jobs carry ci_engine; null means unknown historical provenance.+ hooks: engineeringAnalytics/redactFailureLogsHooksfeature_flag: engineering-analyticsengineering-analytics-dora:operation: engineering_analytics_dora@@ -154,6 +156,7 @@ tools:or its logs aged out of the short Logs retention. Use ci-failure-logs instead when you have a PR number andwant all its runs' failures. Pass ci_engine with run_id (latest_ci_engine for a triage row). Ambiguousinteger IDs return 400. Null engine means unknown historical provenance.+ hooks: engineeringAnalytics/redactFailureLogsHooksfeature_flag: engineering-analyticsengineering-analytics-sources:operation: engineering_analytics_sourcesdiff --git a/services/mcp/src/lib/credential-redaction.ts b/services/mcp/src/lib/credential-redaction.tsindex 3b7a8e31e1ef..97b79c04e384 100644--- a/services/mcp/src/lib/credential-redaction.ts+++ b/services/mcp/src/lib/credential-redaction.ts@@ -1,10 +1,16 @@import { assignKey, isRecord } from '@/lib/plain-object'-const REDACTED_VALUE = '[REDACTED]'+export const REDACTED_VALUE = '[REDACTED]'const MAX_ENCODING_LAYERS = 8const CREDENTIAL_NAME_FRAGMENTS = ['apikey', 'token', 'secret', 'password', 'passwd', 'credential', 'signature']const CREDENTIAL_NAMES = new Set(['key', 'pwd', 'auth', 'authorization', 'sig', 'code', 'jwt', 'session', 'sessionid'])+// Receives a name lowercased with every non-alphanumeric character removed, for example `accesstoken`.+export type CredentialNameRule = (name: string) => boolean++export const isLogCredentialName: CredentialNameRule = (name) =>+ CREDENTIAL_NAMES.has(name) || CREDENTIAL_NAME_FRAGMENTS.some((part) => name.includes(part))+const ASSIGNMENT_PATTERN =/(?:^|(?<=[?&;\s{,("'\\]))(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|([A-Za-z0-9_%.[\]-]+))\s*([=:])\s*/gconst QUOTED_VALUE_PATTERN = /^("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')/@@ -12,6 +18,20 @@ const URL_PASSWORD_PATTERN = /\b[a-z][a-z\d+.-]*:\/\/[^\s/"'<>\\]*@/gi// Project tokens (phc_) are public; these prefixes identify private tokens in posthog/models/utils.py.const POSTHOG_TOKEN_PATTERN = /\b(?:phx|phs|pha|phr)_([A-Za-z0-9_-]{16,})/gconst BEARER_PATTERN = /\bBearer\s+([^\s"'\\,;}]+)/gi+// The label check is a lookahead because two overlapping repeats around `PRIVATE KEY` backtrack quadratically.+const PRIVATE_KEY_LABEL = '(?=[A-Z0-9 ]*?PRIVATE KEY)[A-Z0-9 ]*-----'+export const PRIVATE_KEY_HEADER_PATTERN = new RegExp(`-----BEGIN ${PRIVATE_KEY_LABEL}`, 'g')+export const PRIVATE_KEY_FOOTER_PATTERN = new RegExp(`-----END ${PRIVATE_KEY_LABEL}`, 'g')+// The body runs to the end of the string when the footer is absent, so a key cut off mid-string stays masked.+const PRIVATE_KEY_PATTERN = new RegExp(+ `(${PRIVATE_KEY_HEADER_PATTERN.source})([\\s\\S]*?)(?:${PRIVATE_KEY_FOOTER_PATTERN.source}|$)`,+ 'g'+)+// Classic, OAuth, user-to-server, server-to-server, refresh and fine-grained GitHub tokens.+const GITHUB_TOKEN_PATTERN = /\b(?:gh[pousr]|github_pat)_([A-Za-z0-9_]{36,})/g+const FLAG_VALUE_PATTERN =+ /(?<=^|\s)--([A-Za-z0-9_-]*(?:password|passwd|token|secret|api[_-]?key))\s+(?!-)("[^"]*"|'[^']*'|[^\s"']+)/gi+const USER_PASSWORD_FLAG_PATTERN = /(?<=^|\s)(?:-u|--user)(?:\s+|=)?(["']?)([^\s:"']+):([^\s"']+)\1/gconst ESCAPE_PATTERN = /%([0-9a-f]{2})|\\u([0-9a-f]{4})|\\(["'\\/])/gifunction decodeEscape(@@ -23,21 +43,25 @@ function decodeEscape(return percent || unicode ? String.fromCharCode(Number.parseInt(percent || unicode!, 16)) : escaped}-function isCredentialName(name: string): boolean {- let decoded = name+// Returns null when the text has more than MAX_ENCODING_LAYERS layers of encoding.+export function decodeEscapes(text: string): string | null {+ let decoded = textlet layers = 0for (;;) {const next = decoded.replace(ESCAPE_PATTERN, decodeEscape)if (next === decoded) {- break+ return decoded}if (++layers > MAX_ENCODING_LAYERS) {- return true+ return null}decoded = next}- const normalized = decoded.toLowerCase().replace(/[^a-z0-9]/g, '')- return CREDENTIAL_NAMES.has(normalized) || CREDENTIAL_NAME_FRAGMENTS.some((part) => normalized.includes(part))+}++function isCredentialName(name: string, rule: CredentialNameRule): boolean {+ const decoded = decodeEscapes(name)+ return decoded === null || rule(decoded.toLowerCase().replace(/[^a-z0-9]/g, ''))}interface RedactionRange {@@ -59,10 +83,10 @@ function containerLength(text: string): number {return text.length}-function findCredentialRanges(text: string): RedactionRange[] {+function findCredentialRanges(text: string, rule: CredentialNameRule): RedactionRange[] {const ranges: RedactionRange[] = []for (const match of text.matchAll(ASSIGNMENT_PATTERN)) {- if (!isCredentialName(match[1] ?? match[2] ?? match[3]!)) {+ if (!isCredentialName(match[1] ?? match[2] ?? match[3]!, rule)) {continue}const start = match.index + match[0].length@@ -89,22 +113,35 @@ function findCredentialRanges(text: string): RedactionRange[] {ranges.push({ start: passwordStart + 1, end })}}- for (const pattern of [POSTHOG_TOKEN_PATTERN, BEARER_PATTERN]) {+ for (const match of text.matchAll(PRIVATE_KEY_PATTERN)) {+ if (match[2]) {+ const start = match.index + match[1]!.length+ ranges.push({ start, end: start + match[2].length })+ }+ }+ for (const pattern of [POSTHOG_TOKEN_PATTERN, BEARER_PATTERN, GITHUB_TOKEN_PATTERN, FLAG_VALUE_PATTERN]) {for (const match of text.matchAll(pattern)) {const end = match.index + match[0].length- ranges.push({ start: end - match[1]!.length, end })+ ranges.push({ start: end - match.at(-1)!.length, end })+ }+ }+ for (const match of text.matchAll(USER_PASSWORD_FLAG_PATTERN)) {+ // `-u 1000:1000` is a Docker user and group id, not a credential.+ if (!/^\d+$/.test(match[2]!) || !/^\d+$/.test(match[3]!)) {+ const end = match.index + match[0].length - match[1]!.length+ ranges.push({ start: end - match[3]!.length, end })}}return ranges}-export function redactCredentialsInText(text: string): string {+export function redactCredentialsInText(text: string, rule: CredentialNameRule = isLogCredentialName): string {const ranges: RedactionRange[] = []let decoded = textlet offsets = Array.from({ length: text.length + 1 }, (_, index) => index)let layers = 0for (;;) {- for (const range of findCredentialRanges(decoded)) {+ for (const range of findCredentialRanges(decoded, rule)) {ranges.push({ start: offsets[range.start]!, end: offsets[range.end]! })}@@ -150,20 +187,20 @@ export function redactCredentialsInText(text: string): string {return result + text.slice(previous)}-export function redactCredentials(value: unknown): unknown {+export function redactCredentials(value: unknown, rule: CredentialNameRule = isLogCredentialName): unknown {if (typeof value === 'string') {- return redactCredentialsInText(value)+ return redactCredentialsInText(value, rule)}if (Array.isArray(value)) {- return value.map(redactCredentials)+ return value.map((entry) => redactCredentials(entry, rule))}if (isRecord(value)) {const redacted: Record<string, unknown> = {}for (const [key, entry] of Object.entries(value)) {assignKey(redacted,- redactCredentialsInText(key),- isCredentialName(key) ? REDACTED_VALUE : redactCredentials(entry)+ redactCredentialsInText(key, rule),+ isCredentialName(key, rule) ? REDACTED_VALUE : redactCredentials(entry, rule))}return redacteddiff --git a/services/mcp/src/tools/engineeringAnalytics/redactFailureLogsHooks.ts b/services/mcp/src/tools/engineeringAnalytics/redactFailureLogsHooks.tsnew file mode 100644index 000000000000..23eac89f32bf--- /dev/null+++ b/services/mcp/src/tools/engineeringAnalytics/redactFailureLogsHooks.ts@@ -0,0 +1,98 @@+import {+ type CredentialNameRule,+ PRIVATE_KEY_FOOTER_PATTERN,+ PRIVATE_KEY_HEADER_PATTERN,+ REDACTED_VALUE,+ decodeEscapes,+ redactCredentials,+} from '@/lib/credential-redaction'+import { isRecord } from '@/lib/plain-object'+import type { ToolHooks } from '@/tools/tool-hooks'+import type { Context } from '@/tools/types'++// CI output prints names such as `key`, `code`, `session` and `input_tokens` for diagnostics, not credentials.+const CI_CREDENTIAL_NAME_FRAGMENTS = ['apikey', 'secret', 'password', 'passwd', 'credential', 'privatekey', 'signature']+const CI_CREDENTIAL_NAMES = new Set(['pwd', 'authorization', 'jwt', 'sig', 'sessionid'])++const isCiCredentialName: CredentialNameRule = (name) =>+ CI_CREDENTIAL_NAMES.has(name) ||+ name.endsWith('token') ||+ CI_CREDENTIAL_NAME_FRAGMENTS.some((part) => name.includes(part))++function lastIndexOf(pattern: RegExp, text: string): number {+ return Array.from(text.matchAll(pattern)).at(-1)?.index ?? -1+}++// A header followed by more text on its line is a string in code, such as an assertion, not the start of a key.+const KEY_OPENING_PATTERN = new RegExp(`${PRIVATE_KEY_HEADER_PATTERN.source}\\s*$`)+// Thinning can move a key header or footer into an omitted gap, so a bare base64 body line counts too.+const KEY_BODY_LINE_PATTERN = /^\s*(?=[^\s]*[A-Z])(?=[^\s]*[a-z])(?=[^\s]*\d)[A-Za-z0-9+/]{40,}={0,2}\s*$/+// The last body line of a key is shorter than the others, so it only counts directly after a body line.+const KEY_LAST_LINE_PATTERN = /^\s*[A-Za-z0-9+/]{1,63}={0,2}\s*$/+// The log fetcher inserts this marker with a line number where it drops the middle of an oversized log.+const TRUNCATION_MARKER = '... [log truncated] ...'++// The emitter stores one log line per record, so a private key spans several lines of a job.+function redactPrivateKeyLines(lines: unknown[]): unknown[] {+ let insideKey = false+ let afterBodyLine = false+ let lastGap = -1+ const redacted: unknown[] = []+ for (const line of lines) {+ if (!isRecord(line) || typeof line.text !== 'string') {+ redacted.push(line)+ continue+ }+ const plain = decodeEscapes(line.text) ?? line.text+ // Thinning omission markers have no original line and carry only a line count.+ if (line.original_line == null || plain.trim() === TRUNCATION_MARKER) {+ // A gap can hide the footer, so the lines after it are body lines only by their shape.+ insideKey = false+ afterBodyLine = false+ lastGap = redacted.length+ redacted.push(line)+ continue+ }+ let text = line.text+ let isBodyLine = false+ const footer = plain.search(PRIVATE_KEY_FOOTER_PATTERN)+ if (footer >= 0 && lastIndexOf(PRIVATE_KEY_HEADER_PATTERN, plain) < 0) {+ text = REDACTED_VALUE + plain.slice(footer)+ if (!insideKey && lastGap >= 0) {+ // The header is in the omitted gap, so the body runs from the gap to this footer.+ for (let i = lastGap + 1; i < redacted.length; i++) {+ redacted[i] = { ...(redacted[i] as Record<string, unknown>), text: REDACTED_VALUE }+ }+ }+ insideKey = false+ } else if (+ insideKey ||+ KEY_BODY_LINE_PATTERN.test(plain) ||+ (afterBodyLine && KEY_LAST_LINE_PATTERN.test(plain))+ ) {+ text = REDACTED_VALUE+ isBodyLine = KEY_BODY_LINE_PATTERN.test(plain)+ }+ if (KEY_OPENING_PATTERN.test(plain)) {+ insideKey = true+ }+ afterBodyLine = isBodyLine+ redacted.push({ ...line, text })+ }+ return redacted+}++// CI log lines are build output, so they can print environment values and keys that GitHub did not mask.+function afterResponse(_context: Context, _params: unknown, result: unknown): unknown {+ if (isRecord(result) && Array.isArray(result.jobs)) {+ result = {+ ...result,+ jobs: result.jobs.map((job) =>+ isRecord(job) && Array.isArray(job.lines) ? { ...job, lines: redactPrivateKeyLines(job.lines) } : job+ ),+ }+ }+ return redactCredentials(result, isCiCredentialName)+}++export default { afterResponse } satisfies ToolHooks<unknown>diff --git a/services/mcp/src/tools/generated/engineering_analytics.ts b/services/mcp/src/tools/generated/engineering_analytics.tsindex fd535498bd83..214191336646 100644--- a/services/mcp/src/tools/generated/engineering_analytics.ts+++ b/services/mcp/src/tools/generated/engineering_analytics.ts@@ -3,6 +3,10 @@ import { z } from 'zod'import type { Schemas } from '@/api/generated'import * as orvalSchemas from '@/generated/engineering_analytics/api'+import hooks_engineeringAnalyticsBrokenTests from '@/tools/engineeringAnalytics/redactFailureLogsHooks'+import hooks_engineeringAnalyticsCiFailureLogs from '@/tools/engineeringAnalytics/redactFailureLogsHooks'+import hooks_engineeringAnalyticsRunFailureLogs from '@/tools/engineeringAnalytics/redactFailureLogsHooks'+import { withToolHooks } from '@/tools/tool-hooks'import { withPostHogUrl, type WithPostHogUrl } from '@/tools/tool-utils'import type { Context, ToolBase, ZodObjectAny } from '@/tools/types'@@ -17,18 +21,21 @@ const engineeringAnalyticsBrokenTests = (): ToolBase<> => ({name: 'engineering-analytics-broken-tests',schema: EngineeringAnalyticsBrokenTestsSchema(),- handler: async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsBrokenTestsSchema>>) => {- const projectId = await context.stateManager.getProjectId()- const result = await context.api.request<Schemas.BrokenTestsResult>({- method: 'GET',- path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/broken_tests/`,- query: {- repo: params.repo,- source_id: params.source_id,- },- })- return result- },+ handler: withToolHooks(+ hooks_engineeringAnalyticsBrokenTests,+ async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsBrokenTestsSchema>>) => {+ const projectId = await context.stateManager.getProjectId()+ const result = await context.api.request<Schemas.BrokenTestsResult>({+ method: 'GET',+ path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/broken_tests/`,+ query: {+ repo: params.repo,+ source_id: params.source_id,+ },+ })+ return result+ }+ ),})const EngineeringAnalyticsCiFailureLogsSchema = () => {@@ -42,19 +49,22 @@ const engineeringAnalyticsCiFailureLogs = (): ToolBase<> => ({name: 'engineering-analytics-ci-failure-logs',schema: EngineeringAnalyticsCiFailureLogsSchema(),- handler: async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsCiFailureLogsSchema>>) => {- const projectId = await context.stateManager.getProjectId()- const result = await context.api.request<Schemas.CIFailureLogs>({- method: 'GET',- path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/ci_failure_logs/`,- query: {- pr_number: params.pr_number,- repo: params.repo,- source_id: params.source_id,- },- })- return result- },+ handler: withToolHooks(+ hooks_engineeringAnalyticsCiFailureLogs,+ async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsCiFailureLogsSchema>>) => {+ const projectId = await context.stateManager.getProjectId()+ const result = await context.api.request<Schemas.CIFailureLogs>({+ method: 'GET',+ path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/ci_failure_logs/`,+ query: {+ pr_number: params.pr_number,+ repo: params.repo,+ source_id: params.source_id,+ },+ })+ return result+ }+ ),})const EngineeringAnalyticsFlakyTestsSchema = () => {@@ -124,20 +134,23 @@ const engineeringAnalyticsRunFailureLogs = (): ToolBase<> => ({name: 'engineering-analytics-run-failure-logs',schema: EngineeringAnalyticsRunFailureLogsSchema(),- handler: async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsRunFailureLogsSchema>>) => {- const projectId = await context.stateManager.getProjectId()- const result = await context.api.request<Schemas.RunFailureLogs>({- method: 'GET',- path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/run_failure_logs/`,- query: {- ci_engine: params.ci_engine,- repo: params.repo,- run_id: params.run_id,- source_id: params.source_id,- },- })- return result- },+ handler: withToolHooks(+ hooks_engineeringAnalyticsRunFailureLogs,+ async (context: Context, params: z.infer<ReturnType<typeof EngineeringAnalyticsRunFailureLogsSchema>>) => {+ const projectId = await context.stateManager.getProjectId()+ const result = await context.api.request<Schemas.RunFailureLogs>({+ method: 'GET',+ path: `/api/projects/${encodeURIComponent(String(projectId))}/engineering_analytics/run_failure_logs/`,+ query: {+ ci_engine: params.ci_engine,+ repo: params.repo,+ run_id: params.run_id,+ source_id: params.source_id,+ },+ })+ return result+ }+ ),})const EngineeringAnalyticsSourcesSchema = () => z.object({})diff --git a/services/mcp/tests/unit/credential-redaction.test.ts b/services/mcp/tests/unit/credential-redaction.test.tsindex 3551641d1102..d51787ce050f 100644--- a/services/mcp/tests/unit/credential-redaction.test.ts+++ b/services/mcp/tests/unit/credential-redaction.test.ts@@ -1,6 +1,8 @@import { describe, expect, it, vi } from 'vitest'import { redactCredentials, redactCredentialsInText } from '@/lib/credential-redaction'+import redactFailureLogsHooks from '@/tools/engineeringAnalytics/redactFailureLogsHooks'+import { GENERATED_TOOLS as ENGINEERING_ANALYTICS_TOOLS } from '@/tools/generated/engineering_analytics'import { GENERATED_TOOLS } from '@/tools/generated/logs'import redactCredentialsHooks from '@/tools/logs/redactCredentialsHooks'import type { Context } from '@/tools/types'@@ -8,6 +10,7 @@ import type { Context } from '@/tools/types'// Invented values. None of them is a real credential.const PERSONAL_KEY = 'phx_INVENTEDpersonalKEY0000000000000000'const OTHER_SECRET = 'INVENTEDsecret1111'+const GITHUB_TOKEN = 'ghp_INVENTEDgithubTOKEN000000000000000000'describe('credential redaction', () => {it.each([@@ -122,6 +125,27 @@ describe('credential redaction', () => {input: 'Authorization: [REDACTED]\n/?password=[REDACTED]&safe=1',expected: 'Authorization: [REDACTED]\n/?password=[REDACTED]&safe=1',},+ {+ name: 'a private key block in one string',+ input: '{"key":"x","pem":"-----BEGIN RSA PRIVATE KEY-----\\nINVENTEDkeyBODY\\n-----END RSA PRIVATE KEY-----"}',+ expected:+ '{"key":"[REDACTED]","pem":"-----BEGIN RSA PRIVATE KEY-----[REDACTED]-----END RSA PRIVATE KEY-----"}',+ },+ {+ name: 'a GitHub token outside a parameter',+ input: `git clone https://${GITHUB_TOKEN}@github.com/example/repo`,+ expected: 'git clone https://ghp_[REDACTED]@github.com/example/repo',+ },+ {+ name: 'credential command-line flags with a separate value',+ input: `+ mysql --password ${OTHER_SECRET} --user root && deploy --api-key "${OTHER_SECRET}"`,+ expected: '+ mysql --password [REDACTED] --user root && deploy --api-key [REDACTED]',+ },+ {+ name: 'a user and password flag',+ input: `+ curl -u deploy:${OTHER_SECRET} https://example.com --user='ci:${OTHER_SECRET}'`,+ expected: "+ curl -u deploy:[REDACTED] https://example.com --user='ci:[REDACTED]'",+ },{name: 'excessively nested encoding',input: Array.from({ length: 10 }).reduce<string>((value) => encodeURIComponent(value), '/?token=secret'),@@ -132,6 +156,11 @@ describe('credential redaction', () => {input: String.raw`{"message":"keyword=shoes monkey=1","url":"https://example.com/a%2Fb?next=hello%20world&limit=10","safe":"a\"b"}`,expected: String.raw`{"message":"keyword=shoes monkey=1","url":"https://example.com/a%2Fb?next=hello%20world&limit=10","safe":"a\"b"}`,},+ {+ name: 'a Docker user id and a password read from stdin',+ input: 'docker run -u 1000:1000 app && docker login --password-stdin ghcr.io',+ expected: 'docker run -u 1000:1000 app && docker login --password-stdin ghcr.io',+ },{name: 'safe parameters and a public project token',input: '/e?ip=1&ver=1.2&keyword=shoes&monkey=1&api_host=phc_INVENTEDprojectTOKEN00000',@@ -141,6 +170,112 @@ describe('credential redaction', () => {expect(redactCredentialsInText(input)).toBe(expected)})+ it('parses an unclosed private key label in linear time', () => {+ // A backtracking header pattern takes tens of seconds on this input, far past the test timeout.+ const input = `-----BEGIN ${'PRIVATE KEY '.repeat(100_000)}`++ expect(redactCredentialsInText(input)).toBe(input)+ })++ it.each([+ { name: 'a dictionary key', input: "assert {'key': 'beta-feature'} == {'key': 'beta-feature-2'}" },+ { name: 'an HTTP status code', input: 'Request failed with status code: 500' },+ { name: 'a Node error code', input: "Error: Cannot find module { code: 'ERR_MODULE_NOT_FOUND' }" },+ { name: 'an npm error code', input: 'npm error code: E404' },+ { name: 'a token count', input: '{"input_tokens": 1200, "session": "warm"}' },+ ])('keeps $name in CI output', ({ input }) => {+ expect(redactFailureLogsHooks.afterResponse({} as Context, {}, input)).toBe(input)+ })++ it.each([+ { input: `+ export DATABASE_PASSWORD=${OTHER_SECRET}`, expected: '+ export DATABASE_PASSWORD=[REDACTED]' },+ { input: ` GITHUB_TOKEN: ${OTHER_SECRET}`, expected: ' GITHUB_TOKEN: [REDACTED]' },+ { input: `/cb?access_token=${OTHER_SECRET}&a=1`, expected: '/cb?access_token=[REDACTED]&a=1' },+ { input: `{"api_key": "${OTHER_SECRET}"}`, expected: '{"api_key": "[REDACTED]"}' },+ { input: `client_secret: ${OTHER_SECRET}`, expected: 'client_secret: [REDACTED]' },+ { input: `private_key: ${OTHER_SECRET}`, expected: 'private_key: [REDACTED]' },+ { input: `AWS_SECRET_ACCESS_KEY: ${OTHER_SECRET}`, expected: 'AWS_SECRET_ACCESS_KEY: [REDACTED]' },+ { input: `X-Hub-Signature: ${OTHER_SECRET}`, expected: 'X-Hub-Signature: [REDACTED]' },+ { input: `?sig=${OTHER_SECRET}&a=1`, expected: '?sig=[REDACTED]&a=1' },+ { input: `sessionid=${OTHER_SECRET}`, expected: 'sessionid=[REDACTED]' },+ ])('masks $expected in CI output', ({ input, expected }) => {+ expect(redactFailureLogsHooks.afterResponse({} as Context, {}, input)).toBe(expected)+ })++ it.each([+ {+ name: 'a header inside an assertion does not mask the lines after it',+ lines: [+ [1, "assert pem.startswith('-----BEGIN PRIVATE KEY-----')"],+ [2, 'FAILED tests/test_pem.py::test_header'],+ [3, '##[error]Process completed with exit code 1.'],+ ],+ expected: [+ "assert pem.startswith('-----BEGIN PRIVATE KEY-----[REDACTED]",+ 'FAILED tests/test_pem.py::test_header',+ '##[error]Process completed with exit code 1.',+ ],+ },+ {+ name: 'an omitted gap that hides the footer ends the key',+ lines: [+ [1, '-----BEGIN PRIVATE KEY-----'],+ [2, 'INVENTEDkeyLINEone'],+ [null, '... 30 lines omitted ...'],+ [33, 'FAILED tests/test_deploy.py::test_key'],+ ],+ expected: [+ '-----BEGIN PRIVATE KEY-----',+ '[REDACTED]',+ '... 30 lines omitted ...',+ 'FAILED tests/test_deploy.py::test_key',+ ],+ },+ {+ name: 'a byte-truncation marker counts as a gap before a footer',+ lines: [+ [1, 'INVENTEDnotAkey'],+ [2, '... [log truncated] ...'],+ [3, 'INVENTEDshortLINE'],+ [4, '-----END PRIVATE KEY-----'],+ ],+ expected: [+ 'INVENTEDnotAkey',+ '... [log truncated] ...',+ '[REDACTED]',+ '[REDACTED]-----END PRIVATE KEY-----',+ ],+ },+ {+ name: 'an escaped header opens the key',+ lines: [+ [1, String.raw`\u002D\u002D\u002D\u002D\u002DBEGIN EC PRIVATE KEY\u002D\u002D\u002D\u002D\u002D`],+ [2, 'INVENTEDshortLINE'],+ ],+ expected: [+ String.raw`\u002D\u002D\u002D\u002D\u002DBEGIN EC PRIVATE KEY\u002D\u002D\u002D\u002D\u002D`,+ '[REDACTED]',+ ],+ },+ {+ name: 'a short last body line next to a long body line',+ lines: [+ [null, '... 9 lines omitted ...'],+ [10, 'Zx8Yw7Vu6Ts5Rq4Po3Nm2Lk1Ji0Hg9Fe8Dc7Ba6Zx8Yw7Vu6Ts5Rq4Po3Nm2Lk1J'],+ [11, 'INVENTEDtail=='],+ [null, '... 2 lines omitted ...'],+ [14, 'Done'],+ ],+ expected: ['... 9 lines omitted ...', '[REDACTED]', '[REDACTED]', '... 2 lines omitted ...', 'Done'],+ },+ ])('$name', ({ lines, expected }) => {+ const response = { jobs: [{ lines: lines.map(([original_line, text]) => ({ original_line, text })) }] }++ const redacted = redactFailureLogsHooks.afterResponse({} as Context, {}, response) as typeof response++ expect(redacted.jobs[0]!.lines.map((line) => line.text)).toEqual(expected)+ })+it('masks structured credential fields without mutating the response', () => {const input = { attributes: { authorization: 'Bearer secret', password: ['secret'], safe: 42 }, body: null }@@ -211,4 +346,96 @@ describe('credential redaction', () => {expect(await GENERATED_TOOLS[tool]!().handler(context, {})).toEqual(expected)})++ it.each([+ 'engineering-analytics-ci-failure-logs',+ 'engineering-analytics-run-failure-logs',+ 'engineering-analytics-broken-tests',+ ])('redacts environment values and multiline private keys through the generated %s handler', async (tool) => {+ const line = (original_line: number | null, text: string): Record<string, unknown> => ({+ original_line,+ text,+ })+ const response = {+ logs_available: true,+ jobs: [+ {+ job_id: 1,+ lines: [+ line(10, ` AWS_SECRET_ACCESS_KEY: ${OTHER_SECRET}`),+ line(11, ' GITHUB_TOKEN: ***'),+ line(12, `+ export DATABASE_PASSWORD=${OTHER_SECRET}`),+ line(13, ' NODE_ENV: production'),+ line(14, '-----BEGIN OPENSSH PRIVATE KEY-----'),+ line(15, 'INVENTEDkeyLINEone'),+ line(null, '... 3 lines omitted ...'),+ line(19, 'INVENTEDkeyLINEtwo-----END OPENSSH PRIVATE KEY-----'),+ line(20, 'FAILED tests/test_deploy.py::test_key'),+ ],+ },+ {+ job_id: 2,+ lines: [+ line(1, 'INVENTEDnotAkey'),+ line(null, '... 40 lines omitted ...'),+ line(42, 'aB3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE9fG1hJ3kL5mN7pQ9rS1tU3v'),+ line(43, 'short'),+ line(44, '-----END PRIVATE KEY-----'),+ line(45, 'sha256 0123456789abcdef0123456789abcdef0123456789abcdef'),+ ],+ },+ {+ job_id: 3,+ lines: [+ line(null, '... 9 lines omitted ...'),+ line(10, 'Zx8Yw7Vu6Ts5Rq4Po3Nm2Lk1Ji0Hg9Fe8Dc7Ba6Zx8Yw7Vu6Ts5Rq4Po3Nm2Lk1J'),+ line(11, '##[error]Process completed with exit code 1.'),+ ],+ },+ ],+ }+ const context = {+ stateManager: { getProjectId: vi.fn().mockResolvedValue(1) },+ api: { request: vi.fn().mockResolvedValue(response) },+ } as unknown as Context++ expect(await ENGINEERING_ANALYTICS_TOOLS[tool]!().handler(context, {})).toEqual({+ logs_available: true,+ jobs: [+ {+ job_id: 1,+ lines: [+ line(10, ' AWS_SECRET_ACCESS_KEY: [REDACTED]'),+ line(11, ' GITHUB_TOKEN: [REDACTED]'),+ line(12, '+ export DATABASE_PASSWORD=[REDACTED]'),+ line(13, ' NODE_ENV: production'),+ line(14, '-----BEGIN OPENSSH PRIVATE KEY-----'),+ line(15, '[REDACTED]'),+ line(null, '... 3 lines omitted ...'),+ line(19, '[REDACTED]-----END OPENSSH PRIVATE KEY-----'),+ line(20, 'FAILED tests/test_deploy.py::test_key'),+ ],+ },+ {+ job_id: 2,+ lines: [+ line(1, 'INVENTEDnotAkey'),+ line(null, '... 40 lines omitted ...'),+ line(42, '[REDACTED]'),+ line(43, '[REDACTED]'),+ line(44, '[REDACTED]-----END PRIVATE KEY-----'),+ line(45, 'sha256 0123456789abcdef0123456789abcdef0123456789abcdef'),+ ],+ },+ {+ job_id: 3,+ lines: [+ line(null, '... 9 lines omitted ...'),+ line(10, '[REDACTED]'),+ line(11, '##[error]Process completed with exit code 1.'),+ ],+ },+ ],+ })+ })})