Skip to content

Commit c1e089e

Browse files
committed
fix(telemetry): mask error text after credential words
1 parent a1fefe9 commit c1e089e

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎packages/opencode/src/altimate/telemetry/error-detail.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import { Telemetry } from "./index"
22

33
const MAX_MESSAGE = 200
44
const PLACEHOLDER = "<x>"
5+
const CREDENTIAL_WORDS: ReadonlySet<string> = new Set([
6+
"auth",
7+
"authorization",
8+
"bearer",
9+
"cookie",
10+
"credential",
11+
"credentials",
12+
"key",
13+
"passphrase",
14+
"passwd",
15+
"password",
16+
"pwd",
17+
"secret",
18+
"session",
19+
"token",
20+
])
21+
522
/**
623
* Fixed vocabulary of common error and diagnostic words. Credential words (password,
724
* secret, token, key, auth, cookie, session, ...) are deliberately absent. Keep sorted.
@@ -375,8 +392,9 @@ export const ERROR_VOCABULARY: ReadonlySet<string> = new Set([
375392
/**
376393
* Fixed-vocabulary template of an error's first line. Words are split on any non-letter
377394
* character and lowercased; a word survives only if it is in ERROR_VOCABULARY, every other
378-
* run becomes `<x>`. The output holds only vocabulary words, `<x>` and spaces, so user
379-
* text, identifiers, paths, values and SQL cannot appear.
395+
* run becomes `<x>`. A credential word and the rest of the line become one `<x>` too,
396+
* because a credential value may itself consist of vocabulary words. The output holds
397+
* only vocabulary words, `<x>` and spaces.
380398
*/
381399
export function templateErrorMessage(raw: string): string {
382400
const first = raw.split(/[\r\n\u2028\u2029]/, 1)[0]
@@ -385,8 +403,8 @@ export function templateErrorMessage(raw: string): string {
385403
if (!token) continue
386404
const word = token.toLowerCase()
387405
const piece = ERROR_VOCABULARY.has(word) ? word : PLACEHOLDER
388-
if (piece === PLACEHOLDER && out[out.length - 1] === PLACEHOLDER) continue
389-
out.push(piece)
406+
if (piece !== PLACEHOLDER || out[out.length - 1] !== PLACEHOLDER) out.push(piece)
407+
if (CREDENTIAL_WORDS.has(word)) break
390408
}
391409
// non-letter characters (digits, symbols) only separate words and are dropped; unknown words they separated collapse into one placeholder
392410
return out.join(" ").slice(0, MAX_MESSAGE)

‎packages/opencode/test/telemetry/error-detail.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ const CREDENTIAL_WORDS = [
1212
"token",
1313
"key",
1414
"auth",
15+
"authorization",
1516
"credential",
17+
"credentials",
1618
"bearer",
1719
"cookie",
1820
"session",
21+
"passphrase",
1922
]
2023

2124
describe("templateErrorMessage", () => {
@@ -28,6 +31,30 @@ describe("templateErrorMessage", () => {
2831
expect(templateErrorMessage(raw)).toBe(expected)
2932
})
3033

34+
test.each(CREDENTIAL_WORDS)("masks vocabulary words after %s", (word) => {
35+
expect(templateErrorMessage(`request failed with ${word}: connection refused`)).toBe("request failed with <x>")
36+
})
37+
38+
test.each([
39+
["password is connection refused", "<x>"],
40+
["password=wrong user", "<x>"],
41+
["connection failed: PaSsWoRd=wrong user", "connection failed <x>"],
42+
['request failed: "password": "connection refused"', "request failed <x>"],
43+
["request failed: api_key=wrong user", "request failed <x>"],
44+
["request failed: access-token=connection refused", "request failed access <x>"],
45+
["request failed: opaque password=wrong user", "request failed <x>"],
46+
["password", "<x>"],
47+
["password=1234 !!!", "<x>"],
48+
])("masks the credential suffix: %s", (raw, expected) => {
49+
expect(templateErrorMessage(raw)).toBe(expected)
50+
})
51+
52+
test("matches whole credential words rather than substrings", () => {
53+
expect(templateErrorMessage("keychain keyboard monkey tokenizer failed connection refused")).toBe(
54+
"<x> failed connection refused",
55+
)
56+
})
57+
3158
const adversarial = [
3259
"password: hunter",
3360
"password: supersecret",
@@ -79,6 +106,11 @@ describe("templateErrorMessage", () => {
79106
expect(templateErrorMessage("first line\nsecond line")).toBe("first line")
80107
expect(templateErrorMessage("error ".repeat(200)).length).toBeLessThanOrEqual(200)
81108
})
109+
110+
test.each(["\n", "\r", "\r\n", "\u2028", "\u2029"])("does not inspect later lines after %j", (newline) => {
111+
expect(templateErrorMessage(`connection refused${newline}password is wrong user`)).toBe("connection refused")
112+
expect(templateErrorMessage(`password is wrong user${newline}connection refused`)).toBe("<x>")
113+
})
82114
})
83115

84116
describe("error_fingerprint wiring", () => {
@@ -105,6 +137,23 @@ describe("error_fingerprint wiring", () => {
105137
expect("stack" in ev).toBe(false)
106138
})
107139

140+
test("credential suffix is hidden in the event without changing the hash", () => {
141+
const raw = "request failed: password is connection refused"
142+
const detail = describeToolError(raw)
143+
const event = buildErrorFingerprintEvent("s1", {
144+
...detail,
145+
errorClass: "unknown",
146+
toolName: "bash",
147+
toolCategory: "shell",
148+
recovered: false,
149+
recoveryTool: "",
150+
})
151+
expect(event).toMatchObject({
152+
error_message: "request failed <x>",
153+
error_hash: Telemetry.hashError(Telemetry.maskString(raw).slice(0, 500)),
154+
})
155+
})
156+
108157
test("prompt.ts emits through these helpers", async () => {
109158
const src = await Bun.file(new URL("../../src/session/prompt.ts", import.meta.url)).text()
110159
expect(src).toContain("...ErrorDetail.describeToolError(errorMsg)")

0 commit comments

Comments
 (0)