Skip to content

Commit e573a20

Browse files
fix(telemetry): close the truncation floors, complete the home anchor, keep adjacent paths whole
Review round 3: - truncation: no floors — the head is cut at whitespace of any kind (a whitespace-free window emits nothing), half-open quotes are closed with an escape-aware scan mirroring the quote rules, and the head is masked both alone and with 2 KB of lookahead; only the prefix both readings agree on is emitted, so a spaced component whose proving separator falls past the cut can no longer strand its words - the mid-token home anchor is built from the same case fragments as the rule body (`/USERS/`, `/HOME/`, `/homes/` glued to a word masked nothing) - a trailing word never eats the drive letter of a following path: `:` ends it only before whitespace/end (`copy C:\a C:\b` -> `<path> <path>`) - drive-relative runs exclude `:` and the separator-free form needs the canonical uppercase drive letter with no `:` after the extension — Snowflake VARIANT SQL (`v:col`, `v:geo.city::string`) survives masked_sql, and the colon-dense worst case at the cap drops from ~170 ms to <5 ms - posix home root accepts `/homes/` like the other two layers - HTTP request targets after a method/route keyword are not filesystem paths - spaced filenames and directory components are bounded by the 255-byte component limit, not a word count (past a cap the remainder shipped clear) - tests: drive-colon growth shape now passes the gate, absolute per-call budgets at the cap, SQL-body suite, adjacent-path and cut-band regressions Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GayJFsfg2q2FoAG2XSVsZF
1 parent 77d613e commit e573a20

2 files changed

Lines changed: 224 additions & 40 deletions

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ function isAutomatedRun(): boolean {
7171
const PM_R = "(?:[^\\s\\/\\\\'\"`]|'(?=[\\p{L}\\p{N}_]))"
7272
// slash-delimited variant: backslash is path content, not a separator
7373
const PM_R_P = "(?:[^\\s\\/'\"`]|'(?=[\\p{L}\\p{N}_]))"
74+
// drive-relative run (`C:file.sql`, `C:dir\x`): PM_R minus the colon — a second
75+
// colon inside the run is never a path (Snowflake `v:col`, `c::int`, `k:v`),
76+
// and stopping at it keeps every `x:` anchor of a colon-dense run O(1).
77+
// The separator-free form (`C:file.sql`) is the weakest evidence of all, so
78+
// it also requires the canonical uppercase drive letter and no `:` after
79+
// the extension — `v:geo.city::string` is a VARIANT traversal, not a file.
80+
const PM_DR = "(?:[^\\s\\/\\\\'\"`:]|'(?=[\\p{L}\\p{N}_]))"
7481
const PM_WORD = "(?:[\\p{L}\\p{M}\\p{N}_‘’-]|'(?=[\\p{L}\\p{N}_]))"
7582
const PM_ANCHOR = "(^|[\\s\"'`=(,[{:;<|>)\\]}&])"
7683
const PM_SP = "[^\\S\\t\\n\\r\\v\\f]"
@@ -103,26 +110,56 @@ const PM_SEG_L = "(?=[^\\s'\"`]{0,128}[\\p{L}\\p{M}]|[\\s'\"`,;)\\]}>]|$)"
103110
// source raw/orders') do not.
104111
const PM_SEG_B = "(?=[^\\s\\/\\\\'\"`]{0,64}[\\p{L}\\p{M}]|[\\s'\"`,;)\\]}>]|$)"
105112
const PM_SEG_DEEP = "(?=[^\\s'\"`]{0,128}(?:[\\/\\\\]|" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])))"
113+
// Component-length bounds. Spaced runs are bounded by the filesystem's
114+
// 255-byte component limit — never by a word count: past a word cap the rest
115+
// of the component would ship in the clear, the wrong failure mode. pmComp:
116+
// the rest of this component reaches its proving separator within the
117+
// limit; PM_COMP_EXT: it ends in a dotted extension within the limit.
118+
const pmComp = (sep: string) => "(?=[^\\/\\\\\\n]{0,255}" + sep + ")"
119+
const PM_COMP_EXT = "(?=[^\\/\\\\\\n]{0,255}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?]))"
106120
// a spaced bridge + its proving separator: <=2 words with the first-segment
107-
// proof, or 3-6 words with the deep-continuation proof as well
121+
// proof, or 3+ words (one component) with the deep-continuation proof as well
108122
const pmBridgeLight = (sep: string) =>
109123
"(?:(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){1,2}" + sep + PM_SEG_B + ")"
110124
const pmBridge = (sep: string) =>
111125
"(?:(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){1,2}" + sep + PM_SEG_B +
112-
"|(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){3,6}" + sep + PM_SEG_B + PM_SEG_DEEP + ")"
113-
const PM_ANCHOR_HOME = "(^|[\\s\"'`=(,[{:;<|>)\\]}&]|(?<=[^\\s:\\/\\\\])(?=\\/(?:[Uu]sers|[Hh]ome)\\/))"
126+
"|" + pmComp(sep) + "(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){3,64}" + sep + PM_SEG_B + PM_SEG_DEEP + ")"
127+
const PM_ANCHOR_HOME = "(^|[\\s\"'`=(,[{:;<|>)\\]}&]|(?<=[^\\s:\\/\\\\])(?=\\/(?:" + pmCI("users") + "|" + pmCI("home") + "[sS]?)\\/))"
114128
const pmSpan = (sep: string) =>
115129
"(?:[^\\s'\"`)\\]},;>]|'(?=[\\p{L}\\p{N}_])|[,;)\\]}>](?=" + pmR(sep) + "{0,256}(?:" + sep + PM_SEG_L + "|(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){1,2}" + sep + PM_SEG_B + "|" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])))|[\"'`](?=" + pmR(sep) + "{1,256}(?:" + sep + PM_SEG_L + "|(?:" + PM_SP + "{1,2}" + pmWR(sep) + "{1,64}){1,2}" + sep + PM_SEG_B + "|" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?]))))"
116130
const pmChunks = (sep: string) => "(?:" + pmBridge(sep) + pmSpan(sep) + "*){0,2}"
117-
// terminal dotted filename: up to four spaced words that END in an extension
118-
const pmSpFile = (sep: string) => "(?:(?:" + PM_SP + "{1,2}" + pmR(sep) + "+){1,4}(?<=" + PM_EXT + "))?"
119-
const PM_TERM_COND = "(?:(?<!" + PM_EXT + ")" + PM_SP + "{1,2}" + PM_WORD + "+(?=$|[.,;:)\\]}!?]))?"
131+
// terminal dotted filename: spaced words that END in an extension, bounded
132+
// by component length (PM_COMP_EXT), not by a word count
133+
const pmSpFile = (sep: string) => "(?:" + PM_COMP_EXT + "(?:" + PM_SP + "{1,2}" + pmR(sep) + "+){1,64}(?<=" + PM_EXT + "))?"
134+
// a trailing word is never the drive letter of a following path: `:` ends
135+
// it only when whitespace or the end follows (`copy C:\\a C:\\b`, `moved
136+
// C:\\x D:y.sql` keep both paths whole; `at /x/y line: 3` still terminates)
137+
const PM_TERM_COND = "(?:(?<!" + PM_EXT + ")" + PM_SP + "{1,2}" + PM_WORD + "+(?=$|[.,;)\\]}!?]|:(?=$|\\s)))?"
120138
const PM_TERM_UNC =
121-
"(?:(?<!" + PM_EXT + ")" + PM_SP + "{1,2}" + PM_WORD + "+(?:" + PM_SP + "{1,2}(?:[\\p{Lu}\\p{Lo}]" + PM_WORD + "*|(?:v[ao]n|de[nrl]?|d[aiou]|dos|la|les?|los|bin|ibn|al|el|te[nr])(?=" + PM_SP + ")))*)?"
139+
"(?:(?<!" + PM_EXT + ")" + PM_SP + "{1,2}" + PM_WORD + "+(?:" + PM_SP + "{1,2}(?:[\\p{Lu}\\p{Lo}]" + PM_WORD + "*|(?:v[ao]n|de[nrl]?|d[aiou]|dos|la|les?|los|bin|ibn|al|el|te[nr])(?=" + PM_SP + ")))*(?!:\\S))?"
122140
const PM_WC = "(?:[\\p{L}\\p{N}_#@().'-]{2,}|[\\p{L}\\p{N}_#@().'+&-]{3,})"
123141
const PM_WCC = "[\\p{L}\\p{N}_#@().'+&-]+"
124-
const pmSpFileX = (sep: string) => "(?:(?:" + PM_SP + "{1,2}" + pmR(sep) + "{1,64}){1,12}(?<=" + PM_EXT + "))?"
142+
const pmSpFileX = (sep: string) => "(?:" + PM_COMP_EXT + "(?:" + PM_SP + "{1,2}" + pmR(sep) + "{1,64}){1,64}(?<=" + PM_EXT + "))?"
125143
const pmTail = (sep: string, term: string) => pmSpan(sep) + "*" + pmChunks(sep) + pmSpFile(sep) + term
144+
// an absolute HTTP request target (`GET /api/v1/x`, `route /a/b`) is not a
145+
// filesystem path — masking it destroys HTTP error identity. Home-rooted
146+
// targets are still masked by the home rules (which carry no such guard).
147+
const PM_NOT_ROUTE = "(?<!(?:GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|route|endpoint)\\s)"
148+
// input cap and the lookahead the rules may see past it (wider than any
149+
// proof scan — all are bounded at the 255-byte component limit)
150+
const PM_CAP = 8192
151+
const PM_LOOKAHEAD = 2048
152+
// is a `q`-quoted span still open at the end of `s`? Mirrors the quote rules'
153+
// grammar exactly (`q(?:[^q\\]|\\.)*q`): a backslash escapes only inside a span
154+
function pmQuoteOpen(s: string, q: string): boolean {
155+
let open = false
156+
for (let i = 0; i < s.length; i++) {
157+
const c = s[i]
158+
if (open && c === "\\") i++
159+
else if (c === q) open = !open
160+
}
161+
return open
162+
}
126163
// Known-prefix literals — the local user's home and cwd are KNOWN values,
127164
// replaced by exact match AFTER the structural rules (structure must see the
128165
// original string: stripping the prefix first orphans terminal spaced
@@ -165,10 +202,10 @@ function pmKnownPrefixes(): RegExp[] {
165202
}
166203
const PATH_RULES = {
167204
cloud: new RegExp(PM_ANCHOR + "(?:(?:" + [pmCI("gs"), pmCI("s3") + "[anAN]?", pmCI("abfs") + "[sS]?", pmCI("wasb") + "[sS]?", pmCI("adl"), pmCI("dbfs"), pmCI("hdfs")].join("|") + "):\\/\\/|" + pmCI("file") + ":\\/{1,3})" + pmTail(SEP_P, PM_TERM_UNC), "gu"),
168-
windowsHome: new RegExp(PM_ANCHOR + "(?:(?:\\\\\\\\\\?\\\\)?[A-Za-z]:" + SEP_W + "{0,2}|(?:\\\\\\\\(?:\\?\\\\" + pmCI("unc") + "\\\\)?|(?<!:)\\/\\/(?=[^\\s\\/\\\\]+" + SEP_W + "))(?:" + PM_R + "{1,256}(?:" + SEP_W + "{1,2}" + PM_SEG_L + "|" + pmBridgeLight(SEP_W) + ")){0,8}|" + SEP_W + "{1,2})(?:" + pmCI("users") + "|" + pmCI("home") + "s?|" + pmCI("documents") + " " + pmCI("and") + " " + pmCI("settings") + ")" + SEP_W + "{1,2}" + pmTail(SEP_W, PM_TERM_UNC), "gu"),
169-
windows: new RegExp(PM_ANCHOR + "(?:[A-Za-z]:" + SEP_W + "|(?<!:)\\/\\/(?=[^\\s\\/\\\\.]+" + SEP_W + ")|[A-Za-z]:(?=" + PM_R + "{1,256}(?:" + PM_SP + "{1,2}" + PM_R + "{1,256})*\\\\|(?=[^\\s\\/\\\\]{0,256}[\\p{L}])" + PM_R + "{1,256}(?:" + PM_SP + "{1,2}" + PM_R + "{1,256})*\\/|" + PM_R + "{1,256}(?:" + PM_SP + "{1,2}" + PM_R + "{1,256}){0,6}(?<=\\.[\\p{L}\\p{M}\\p{N}-]{0,29})(?<=[\\p{L}\\p{M}][\\p{L}\\p{M}\\p{N}-]{0,29})(?=$|[\\s.,;:)\\]}!?]))|\\\\\\\\|\\.{1,2}\\\\(?=" + PM_R + "{1,256}(?:" + PM_SP + "{1,2}" + PM_R + "{1,256})*\\\\|[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?]))|\\\\(?=(?:" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")*\\\\){2}|" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")*\\\\[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])|" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")+\\\\[\\p{L}\\p{N}]|[\\p{L}\\p{N}_-]\\\\(?:[\\p{L}\\p{N}_-]{2,}|\\.[\\p{L}\\p{N}_-]{2,})|[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])))" + pmSpan(SEP_W) + "+" + pmChunks(SEP_W) + pmSpFile(SEP_W) + PM_TERM_COND, "gu"),
170-
posixHome: new RegExp(PM_ANCHOR_HOME + "\\/(?:" + PM_R_P + "{1,256}(?:\\/" + PM_SEG_L + "|" + pmBridgeLight(SEP_P) + "))*(?:" + pmCI("users") + "|" + pmCI("home") + ")\\/" + pmTail(SEP_P, PM_TERM_UNC), "gu"),
171-
posix: new RegExp(PM_ANCHOR + "(?:\\.{0,2}\\/(?:" + PM_R_P + "{1,256}(?:\\/" + PM_SEG_L + "|" + pmBridgeLight(SEP_P) + "))+" + pmTail(SEP_P, PM_TERM_COND) + "|(?:\\.{1,2}\\/|\\/(?!\\/))" + pmSpan(SEP_P) + "+" + pmSpFileX(SEP_P) + "(?<=" + PM_EXT + ")(?=$|[\\s.,;:)\\]}!?]))", "gu"),
205+
windowsHome: new RegExp(PM_ANCHOR + "(?:(?:\\\\\\\\\\?\\\\)?[A-Za-z]:" + SEP_W + "{0,2}|(?:\\\\\\\\(?:\\?\\\\" + pmCI("unc") + "\\\\)?|(?<!:)\\/\\/(?=[^\\s\\/\\\\]+" + SEP_W + "))(?:" + PM_R + "{1,256}(?:" + SEP_W + "{1,2}" + PM_SEG_L + "|" + pmBridgeLight(SEP_W) + ")){0,8}|" + SEP_W + "{1,2})(?:" + pmCI("users") + "|" + pmCI("home") + "[sS]?|" + pmCI("documents") + " " + pmCI("and") + " " + pmCI("settings") + ")" + SEP_W + "{1,2}" + pmTail(SEP_W, PM_TERM_UNC), "gu"),
206+
windows: new RegExp(PM_ANCHOR + "(?:[A-Za-z]:" + SEP_W + "|(?<!:)\\/\\/(?=[^\\s\\/\\\\.]+" + SEP_W + ")|[A-Za-z]:(?=" + PM_DR + "{1,256}(?:" + PM_SP + "{1,2}" + PM_DR + "{1,256}){0,8}\\\\|(?=[^\\s\\/\\\\:]{0,256}[\\p{L}])" + PM_DR + "{1,256}(?:" + PM_SP + "{1,2}" + PM_DR + "{1,256}){0,8}\\/)|[A-Z]:(?=" + PM_DR + "{1,255}(?<=\\.[\\p{L}\\p{M}\\p{N}-]{0,29})(?<=[\\p{L}\\p{M}][\\p{L}\\p{M}\\p{N}-]{0,29})(?=$|[\\s.,;)\\]}!?]))|\\\\\\\\|\\.{1,2}\\\\(?=" + PM_R + "{1,256}(?:" + PM_SP + "{1,2}" + PM_R + "{1,256})*\\\\|[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?]))|\\\\(?=(?:" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")*\\\\){2}|" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")*\\\\[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])|" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")+\\\\[\\p{L}\\p{N}]|[\\p{L}\\p{N}_-]\\\\(?:[\\p{L}\\p{N}_-]{2,}|\\.[\\p{L}\\p{N}_-]{2,})|[^\\s\\\\]{1,256}" + PM_EXT + "(?=$|[\\s.,;:)\\]}!?])))" + pmSpan(SEP_W) + "+" + pmChunks(SEP_W) + pmSpFile(SEP_W) + PM_TERM_COND, "gu"),
207+
posixHome: new RegExp(PM_ANCHOR_HOME + "\\/(?:" + PM_R_P + "{1,256}(?:\\/" + PM_SEG_L + "|" + pmBridgeLight(SEP_P) + "))*(?:" + pmCI("users") + "|" + pmCI("home") + "[sS]?)\\/" + pmTail(SEP_P, PM_TERM_UNC), "gu"),
208+
posix: new RegExp(PM_ANCHOR + PM_NOT_ROUTE + "(?:\\.{0,2}\\/(?:" + PM_R_P + "{1,256}(?:\\/" + PM_SEG_L + "|" + pmBridgeLight(SEP_P) + "))+" + pmTail(SEP_P, PM_TERM_COND) + "|(?:\\.{1,2}\\/|\\/(?!\\/))" + pmSpan(SEP_P) + "+" + pmSpFileX(SEP_P) + "(?<=" + PM_EXT + ")(?=$|[\\s.,;:)\\]}!?]))", "gu"),
172209
tilde: new RegExp(PM_ANCHOR + "~[\\p{L}\\p{M}\\p{N}_.-]*(?:\\/|\\\\(?=" + PM_WC + "(?:" + PM_SP + "{1,2}" + PM_WCC + ")*[\\\\\\/]|\\.[\\p{L}\\p{N}_-]{2,}[\\\\\\/]|[\\p{L}\\p{N}_-]\\\\(?:[\\p{L}\\p{N}_-]{2,}|\\.[\\p{L}\\p{N}_-]{2,})))" + pmTail(SEP_W, PM_TERM_UNC), "gu"),
173210
}
174211
// altimate_change end
@@ -1493,25 +1530,40 @@ export namespace Telemetry {
14931530

14941531
export function maskString(s: string): string {
14951532
// Consumers truncate masked output to <= 2000 chars; masking beyond 8 KB
1496-
// of input buys nothing, and unbounded input is what turns any super-
1497-
// linear rule into a stall. The cut happens FIRST so every rule — the
1498-
// linear credential/quote passes included — does bounded work, but it
1499-
// must never fail a rule open across the boundary: it backs off to the
1500-
// last whitespace of any kind (no token straddles the cut), then to
1501-
// before any unbalanced quote (a quoted value never survives half-open).
1502-
if (s.length > 8192) {
1503-
let cut = 8192
1504-
const ws = s.slice(0, 8192).search(/\s\S*$/)
1505-
if (ws > 4096) cut = ws
1506-
for (const q of ['"', "'"]) {
1507-
const window = s.slice(0, cut)
1508-
if ((window.split(q).length - 1) % 2 === 1) {
1509-
const open = window.lastIndexOf(q)
1510-
if (open > 2048) cut = open
1511-
}
1512-
}
1513-
s = s.slice(0, cut)
1514-
}
1533+
// buys nothing, and unbounded input is what turns any super-linear rule
1534+
// into a stall. Input is cut FIRST so every rule — the linear credential/
1535+
// quote passes included — does bounded work, and the cut must never fail
1536+
// a rule open across the boundary. No floor gates any of this: a floor is
1537+
// a leak past the floor.
1538+
if (s.length <= PM_CAP) return pmMask(s)
1539+
// the head ends at whitespace of any kind, so no token straddles it (a
1540+
// head with no whitespace at all is one token: nothing is emitted)
1541+
const ws = s.slice(0, PM_CAP).search(/\s\S*$/)
1542+
const at = ws >= 0 ? ws : 0
1543+
// a quote the cut left half-open is closed (escape-aware, mirroring the
1544+
// quote rules, in their order) so the quote rule collapses the value
1545+
// instead of emitting it verbatim
1546+
let head = s.slice(0, at)
1547+
if (pmQuoteOpen(head, "'")) head += "'"
1548+
if (pmQuoteOpen(head.replace(/'(?:[^'\\]|\\.)*'/g, "?"), '"')) head += '"'
1549+
// Every rule proves a path by what FOLLOWS it — a spaced component whose
1550+
// proving separator falls past the cut would otherwise leak its words.
1551+
// So the head is masked twice: alone, and with the text past the cut in
1552+
// view. Only the prefix on which both agree is emitted: text the rules
1553+
// masked the same way with and without the continuation, cut back to
1554+
// whitespace. Whatever the continuation changes is dropped, never
1555+
// emitted half-proven.
1556+
const alone = pmMask(head)
1557+
const seen = pmMask(s.slice(0, at + PM_LOOKAHEAD))
1558+
let n = 0
1559+
while (n < alone.length && alone[n] === seen[n]) n++
1560+
if (n === alone.length) return alone
1561+
const back = alone.slice(0, n).search(/\s\S*$/)
1562+
return back >= 0 ? alone.slice(0, back).trimEnd() : ""
1563+
}
1564+
1565+
// the masking chain proper, on bounded input (see maskString)
1566+
function pmMask(s: string): string {
15151567
let out = s
15161568
// ANSI CSI sequences (colored subprocess stderr) would otherwise split
15171569
// tokens so neither credential nor path rules can see them
@@ -1522,7 +1574,7 @@ export namespace Telemetry {
15221574
.replace(/"(?:[^"\\]|\\.)*"/g, "?")
15231575
// Fast path: a string with no separator cannot contain a path — skip the
15241576
// whole path stack (most telemetry strings carry no path at all).
1525-
if (out.includes("/") || out.includes("\\") || /(?<![A-Za-z0-9])[A-Za-z]:[^\s:]{1,255}(?: [^\s:]{1,255}){0,2}\.[A-Za-z]/.test(out)) {
1577+
if (out.includes("/") || out.includes("\\") || /(?<![A-Za-z0-9])[A-Z]:[^\s:\\\/]{1,255}\.[A-Za-z]/.test(out)) {
15261578
out = out
15271579
// altimate_change start — mask filesystem paths in error text
15281580
// Six masking rules (cloud URIs, Windows home, Windows/UNC incl. .\ and

0 commit comments

Comments
 (0)