Skip to content

Commit 8e6309d

Browse files
anandgupta42claude
andcommitted
fix(telemetry): mask the rest of the line after any credential label
A token with a part starting with a credential stem (pass, secret, token, key, auth, credential, bearer, pwd, cookie, session) now masks everything after it on the line. `client_secret: a b c`, `access_token=x y` and `passphrase a b c` sent their values before. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5ea02f9 commit 8e6309d

2 files changed

Lines changed: 20 additions & 36 deletions

File tree

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,14 @@ const SENTENCE_STARTERS = new Set([
4646
"too",
4747
"failed",
4848
])
49-
const CREDENTIAL_WORDS = new Set([
50-
"password",
51-
"passwd",
52-
"pwd",
53-
"secret",
54-
"token",
55-
"key",
56-
"auth",
57-
"authorization",
58-
"credential",
59-
"credentials",
60-
"apikey",
61-
"bearer",
62-
])
63-
const CREDENTIAL_SPAN = 2
49+
// A token with a part starting with one of these stems (password, passphrase, client_secret,
50+
// access_token=..., apikey, Authorization, ...) ends the readable part of the line.
51+
const CREDENTIAL_STEMS = ["pass", "pwd", "secret", "token", "key", "auth", "credential", "bearer", "cookie", "session"]
52+
const isCredential = (token: string) =>
53+
token
54+
.toLowerCase()
55+
.split(/[^a-z]+/)
56+
.some((part) => CREDENTIAL_STEMS.some((stem) => part.startsWith(stem)))
6457

6558
/**
6659
* Allowlist template of an error's first line. Only short plain-letter words and
@@ -75,8 +68,6 @@ export function templateErrorMessage(raw: string): string {
7568
.replace(/['"].*$/, ` ${PLACEHOLDER}`)
7669
const out: string[] = []
7770
let position = 0
78-
let secretSpan = 0
79-
let secretToEol = false
8071
for (const chunk of first.split(/\s+/)) {
8172
if (!chunk) continue
8273
if (chunk === PLACEHOLDER) {
@@ -85,26 +76,15 @@ export function templateErrorMessage(raw: string): string {
8576
continue
8677
}
8778
const [, lead, core, trail] = PEEL.exec(chunk)!
88-
// the tokens after a credential keyword are values, however plain they look
89-
if (secretSpan > 0 && core !== "") {
90-
out.push(lead + PLACEHOLDER + trail)
91-
position++
92-
secretSpan = secretToEol ? 1 : /[,.)!?]/.test(trail) ? 0 : secretSpan - 1
93-
continue
94-
}
95-
if (
96-
core
97-
.toLowerCase()
98-
.split(/[_\-.]/)
99-
.some((part) => CREDENTIAL_WORDS.has(part))
100-
) {
101-
secretSpan = CREDENTIAL_SPAN
102-
// `label: value` / `label=value`: the value may be a multi-word passphrase, so mask to end of line
103-
secretToEol = /[:=]/.test(trail) || /[:=]/.test(lead)
79+
// everything after a credential label is a value, however plain it looks: mask to end of line
80+
if (isCredential(core)) {
81+
const label = WORD.test(core) ? lead + core + trail : lead + PLACEHOLDER + trail
82+
out.push(label, PLACEHOLDER)
83+
break
10484
}
10585
const plainWord = WORD.test(core) && !(core.length > 3 && core === core.toUpperCase())
10686
const lower = core.toLowerCase()
107-
const name = /^[A-Z]/.test(core) && (position > 0 || !(SENTENCE_STARTERS.has(lower) || CREDENTIAL_WORDS.has(lower)))
87+
const name = /^[A-Z]/.test(core) && (position > 0 || !SENTENCE_STARTERS.has(lower))
10888
out.push(core === "" || (plainWord && !name) ? chunk : lead + PLACEHOLDER + trail)
10989
position++
11090
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ describe("templateErrorMessage", () => {
3131
"/Users/bob/proj/a.ts",
3232
"C:\\Users\\bob\\a.ts",
3333
34-
"https://h.example.com/p?token=zzz",
3534
pgUri,
3635
"AKIAIOSFODNN7EXAMPLE",
3736
"ghp_abcdefghijklmnopqrstuvwxyz0123456789",
@@ -47,7 +46,12 @@ describe("templateErrorMessage", () => {
4746
test.each([
4847
["login failed password: supersecret", "login failed password: <x>"],
4948
["Authorization: Bearer secrettoken", "Authorization: <x>"],
50-
["bad api key hunter, retry", "bad api key <x>, retry"],
49+
["bad api key hunter, retry", "bad api key <x>"],
50+
["fetch https://h.example.com/p?token=zzz failed", "fetch <x>"],
51+
["auth failed: client_secret: abc def ghi", "auth <x>"],
52+
["bad access_token=hunter two three", "bad <x>"],
53+
["password correct horse battery staple", "password <x>"],
54+
["login failed for passphrase correct horse battery staple", "login failed for passphrase <x>"],
5155
["client_secret: supersecret", "<x>"],
5256
["access_token: secrettoken", "<x>"],
5357
["api-key: secretvalue", "<x>"],

0 commit comments

Comments
 (0)