Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

redact: cua-driver's element tokens go as written (#3782)

JoeyWu-tech1cbdda25be1b
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 57d2ee4d0..298d391f5 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -137,7 +137,7 @@ recorded as a prediction failure (`noPrediction`).
## Runtime path
1. `handle(proto)` reads the body and the model (`requestBody`, `requestModel`), then calls `serveAgent`. Every route that reads an agent's body (each API, count_tokens, Gemini's, embeddings, images, videos, System One, the Codex backend path) reads it through `readBoundedRequestBody` in [`request_bounds.go`](../../internal/gateway/request_bounds.go). It decodes a `Content-Encoding` of `gzip` or `zstd` (`decodedBody`; Codex sends zstd to a custom provider's `/v1/responses` too, #1223) and drops the header, so the provider is sent plain JSON, with one exception: a ChatGPT account's turn (POST `CodexBase`/responses, from the accounts loop in `forwardOnce` or relayed by `codexUpstreamOn`) is compressed again with zstd and a window of at most 2 MiB, as Codex's own client sends it (`provider.CodexCompressed`; openai/codex `responses_request_compression` and `http-client/src/request.rs`). It is compressed once the body is final, and again on each try. `/responses/compact`, an API key's requests and every other provider stay plain. Sent plain, a 1.1 MB text-only turn was answered 503 (ylorn on X). The plain body is what magpie keeps and shows, and `codexDropped` judges a reset by the bytes that went on the wire. `codexDropped` also reads Envoy's 507 "exceeded request buffer limit while retrying upstream" (the backend failed and the front couldn't retry a body that large) as that reset, at any size, told as a 503 so magpie, Codex and pi ask again and nobody rests (#2034). The size limit (`defaultBodyLimit`, 512 MB) holds for the decoded bytes (413), and any other encoding is refused 415. It was 128 MB until a long Codex session that had read many images went past it decoded, its inline screenshots resent each turn, and got the 413 on all five of Codex's retries (#1422, `TestImageHeavyCodexTurnIsRead`). magpie's own 413 says it is magpie's gateway limit, with the size, and that the provider was not asked (`overLimit`); `writeError` adds to any 413 it isn't holding that this is a byte limit, not the model's context, and to `/compact` or start a new conversation (`TestOverLimitSaysWhoseAndWhatToDo`). Every shape but Gemini's types a 413 `request_too_large`, Anthropic's own type, which pi's overflow table knows: pi compacts and tries once more rather than stopping on an OpenAI-shaped `api_error` (@wilsonxuuuu on X, `TestPayloadTooLargeSaysRequestTooLargeToPi`). `keyLimited` reads a limited key's compressed body the same way, so its reservation is the decoded size. `serveAgent` runs the [middleware](gateway-middleware.md) chain, then `serve`.
-2. `serve` redacts secrets in the request (`redacted`; `redact.MaskJSON` leaves ids alone, and in any text a backend's id (a UUID, or 32 hex digits or more) goes as written: no rule, model label or value known from before masks a piece of one (a phone number at the end of `…-a13800138000`, a card number across a UUID's groups), and a whole one is masked only by the user's own words and rules or as a credential, by the password, API key and token rules or a secret rule whose field is named for one (`api_key=`, `refresh_token`), never one named for an id, a cursor, a request or a digest (`secret_id`, `next_page_token`, `Idempotency-Key`, `secret_hash`), a bare `key`, a serial number, or what the model takes a UUID for (`noIDs`, [`internal/redact/ids.go`](../../internal/redact/ids.go)); and a tool's name and its MCP server's (`namespace`, `server_label`, `server_name`, #3143); the request's tool definitions and output schemas, found by their place in each protocol (top-level `tools`, `tool_choice`, `response_format`, `text`, `generationConfig`, `stream_options`, the client's ids in `client_metadata` and the like, and the tools of a Responses `additional_tools` or `tool_search_output` item), go as written save their descriptions, so a word the user masks can't break a grammar or an enum (#1911); and what the vendor sealed with a signature, a thinking block, Gemini's thoughts or Responses' encrypted reasoning, goes back as the vendor wrote it: only the values masked elsewhere in the request, and secrets masked before, are put back as their placeholders; a value masked once, at least 8 characters long, is masked again wherever a request has it, as an exact match whatever words are around it, so what the vendor wrote about a placeholder doesn't go back with the value in it, `redact/known.go`; a text masked for a vendor before, such as a system prompt or a turn already sent, is masked as it was then, so a value a rule first finds later in a conversation doesn't change the prompt the vendor has cached and rewrite all of it to the cache, `sentKnownSpans`), resolves the model to a provider or a group, and refuses what can't be served. The reply has the values put back on its way to the agent (`redact.Writer`, a stream event by event, a whole body at its end): a placeholder the model wrote as it was given, or as models rewrite one (spaces inside the braces, a brace or both dropped, another case), is the value again when what is left names one magpie made, and one split across events is held until it is whole (`redact.Restore`, `partialTail`, [`internal/redact/redact.go`](../../internal/redact/redact.go)). A request whose provider and fallbacks, or whose group's models, all skip redaction (`unredactedRoute`, `provider.SkipsRedaction`: set *Send requests unmasked*, `Unredacted`, with every address on this machine or the local network) goes to them as the agent wrote it; the log and recent calls keep the masked body. count_tokens and embeddings follow the same rule. What a routing group's Jev is told of the turn (the message, the user's message before it and the reply to it) is masked all the same unless Jev's own provider skips redaction too (`maskedState` in `askJev`, [`decide.go`](../../internal/gateway/decide.go)): a remote Jev is none of the group's members. The masking is the built-in plugin `redact`'s (`gwmw.Masker`, [Plugin runtime](plugin-runtime.md)); when the settings ask for masking and no masker serves, or it fails, the request is a 503 and nothing is sent (`redacted`, `gwmw.Masking`).
+2. `serve` redacts secrets in the request (`redacted`; `redact.MaskJSON` leaves ids alone, and in any text a backend's id (a UUID, or 32 hex digits or more), or an element token cua-driver hands the model for computer use (`s0000002a:14`, #3782), goes as written: no rule, model label or value known from before masks a piece of one (a phone number at the end of `…-a13800138000`, a card number across a UUID's groups), and a whole one is masked only by the user's own words and rules or as a credential, by the password, API key and token rules or a secret rule whose field is named for one (`api_key=`, `refresh_token`), never one named for an id, a cursor, a request or a digest (`secret_id`, `next_page_token`, `Idempotency-Key`, `secret_hash`), nor cua-driver's own `element_token` (any other name with `element` in it, `element_api_key`, is a credential's as before), a bare `key`, a serial number, or what the model takes a UUID or an element token for, nor an element token known from before (one the model labelled in prose a turn earlier) (`noIDs`, [`internal/redact/ids.go`](../../internal/redact/ids.go)); and a tool's name and its MCP server's (`namespace`, `server_label`, `server_name`, #3143); the request's tool definitions and output schemas, found by their place in each protocol (top-level `tools`, `tool_choice`, `response_format`, `text`, `generationConfig`, `stream_options`, the client's ids in `client_metadata` and the like, and the tools of a Responses `additional_tools` or `tool_search_output` item), go as written save their descriptions, so a word the user masks can't break a grammar or an enum (#1911); and what the vendor sealed with a signature, a thinking block, Gemini's thoughts or Responses' encrypted reasoning, goes back as the vendor wrote it: only the values masked elsewhere in the request, and secrets masked before, are put back as their placeholders; a value masked once, at least 8 characters long, is masked again wherever a request has it, as an exact match whatever words are around it, so what the vendor wrote about a placeholder doesn't go back with the value in it, `redact/known.go`; a text masked for a vendor before, such as a system prompt or a turn already sent, is masked as it was then, so a value a rule first finds later in a conversation doesn't change the prompt the vendor has cached and rewrite all of it to the cache, `sentKnownSpans`), resolves the model to a provider or a group, and refuses what can't be served. The reply has the values put back on its way to the agent (`redact.Writer`, a stream event by event, a whole body at its end): a placeholder the model wrote as it was given, or as models rewrite one (spaces inside the braces, a brace or both dropped, another case), is the value again when what is left names one magpie made, and one split across events is held until it is whole (`redact.Restore`, `partialTail`, [`internal/redact/redact.go`](../../internal/redact/redact.go)). A request whose provider and fallbacks, or whose group's models, all skip redaction (`unredactedRoute`, `provider.SkipsRedaction`: set *Send requests unmasked*, `Unredacted`, with every address on this machine or the local network) goes to them as the agent wrote it; the log and recent calls keep the masked body. count_tokens and embeddings follow the same rule. What a routing group's Jev is told of the turn (the message, the user's message before it and the reply to it) is masked all the same unless Jev's own provider skips redaction too (`maskedState` in `askJev`, [`decide.go`](../../internal/gateway/decide.go)): a remote Jev is none of the group's members. The masking is the built-in plugin `redact`'s (`gwmw.Masker`, [Plugin runtime](plugin-runtime.md)); when the settings ask for masking and no masker serves, or it fails, the request is a 503 and nothing is sent (`redacted`, `gwmw.Masking`).
- With *Mask personal data* on, each kind in `redact.Categories` ([`internal/redact/more.go`](../../internal/redact/more.go)) is masked unless the user turned it off (`redactKinds` in settings, `Options.Kinds`). On by default: US SSNs, passport numbers, IBANs that pass mod 97, birthdays next to a word that says so (a date over a year ago: one in the last year or ahead is the day an agent runs on, `dateOK`), MAC addresses, and serial numbers next to S/N or the like. Off by default: the user name in a home folder, user@host in a prompt or ssh command, public IPv4 and IPv6 addresses (never private, loopback, link-local, CGNAT or documentation ranges) and cloud bucket names, since agents send them in nearly every request and the model must copy the placeholder exactly inside a path or URL. Context rules run only near their words (`rule.matches`). What reads as an address but ends in a file's extension that no domain has (`…@2x.jpg`, `[email protected]`; `fileExt` in `realEmail`), or a scale before an extension that is a domain too (`[email protected]`, `[email protected]`; `scaleExt`), is a file's name, for the rule and the local model alike (#3029). `[email protected]` is still an address. The Settings rows come from `REDACT_KINDS` in app.js, which `TestRedactKindsAreRedacts` holds to the same ids and defaults.
- With *Find with a local model* on (`settings.RedactModel`, this computer's own, kept by `KeepOwn`), a local model finds what no rule can, a name or an address in free text, under the same switches: a name, address, phone, email or account number is personal data, a secret a secret (`modelKinds`, `modelSpans` in [`internal/redact/model.go`](../../internal/redact/model.go)). `MaskJSON` asks it once per request, for every text it would mask, each once (`withModel`); its spans join the rules' with a rule's kind kept where both find a value, and ids a vendor or tool made (`req_…`, `toolu_…`) are never taken, nor a snake_case word as a name (Codex's `sequential_cutoff`, #3144), nor as a secret what names a thing: a UUID, a digest with its algorithm's name, a file's path (`namesThing`; the model takes all three for secrets). The model misses many Chinese names (张伟 after 给, 陈建国 after 客户, however low its threshold) and runs a name it finds on into the words after it (「王芳,她下周一」), so with it on a Chinese name is also found by its surname and a word around it that says a person is meant (客户, 我叫, 给…发, …先生, …说, joined by 和 to another name), and the model's are cut at punctuation and titles ([`zhname.go`](../../internal/redact/zhname.go)); a surname with nothing around it (王国的, 张开的) is left. The model is the user's pick (`settings.RedactModelName`, `pii.Choose`; the app's menu beside the switch, `pmPick`): AstrLink Guard (QuantumNous/astrlink-guard at a pinned revision, Apache-2.0, a 39 MB int8 Multilingual-MiniLM token classifier for Chinese and English), run in magpie's own process on the CPU by [`internal/pii/guard`](../../internal/pii/guard) (an ONNX reader, the XLM-R tokenizer and int8 kernels in Go, NEON/SDOT on arm64 and SSE2 on amd64; its labels named as Privacy Filter's, `guardLabels`), and on a Mac through CoreML on the Neural Engine or the GPU ([`coreml.go`](../../internal/pii/guard/coreml.go), [`internal/pii/guard/coreml`](../../internal/pii/guard/coreml); yyhhyyyyyy on X): the same network from the same int8 weights in float16, written from the downloaded model into a program made by `coreml_gen.py` and compiled once into the model's folder, 512 tokens in about 12 ms where the CPU takes 400, its probabilities within .07 of the publisher's fp32 model where the CPU's are within .13 (the CPU quantizes activations as the int8 model does, CoreML doesn't), so a span near the threshold can fall the other side (a go.mod module path is a secret at .81 on CoreML, .79 on the CPU, .785 in fp32); a window under 16 tokens (`coremlMin`), a Mac before macOS 13 or any failure goes to the CPU, loaded on the first request and let go after 10 minutes with none; or OpenAI's Privacy Filter. With no pick, a user who had Privacy Filter on or downloaded keeps it and everyone else gets AstrLink Guard (#3578): on 234 segments of real code and config it found 2 spans where Privacy Filter found 100 (38 of them go.sum hashes after redact's own checks), and with the rules it found 42 of 42 test values to Privacy Filter's 41. Each model has its own download, cache file and turn (`backend`). Both find at a floor of 0.5 (`pii.Floor`) and give each span its score (AstrLink Guard: the least of its tokens' probabilities, rounded to a float32 so a cached span reads back the same; Privacy Filter: pf-serve's own), and masking keeps those at the model's threshold or over (`pii.Threshold` in `masking.model`): the user's for that model (`settings.RedactModelScore`, this computer's own; Settings → Privacy's Confidence threshold, `pmScore`, 0.5 to 0.95), else the model's own (`MinScore`: Privacy Filter 0.5, AstrLink Guard 0.8, its publisher's). The defaults stay because on the eval a higher one mostly loses values the checks after the model would have kept (Privacy Filter at 0.85 lost a Chinese name; AstrLink Guard at 0.9 a phone in a tool result), while the false alarms it would drop are mostly dropped by those checks already (Yayoi_no_yume on X). Privacy Filter is a q8 GGUF (1.6 GB), run by `pf-serve` ([`native/privacy-filter`](../../native/privacy-filter), built from privacy-filter.cpp on ggml and released under `pf-v*` tags; on a GPU it holds the model once, in the device's memory, #3590; `pf-serve-vulkan` runs its MoE in 2048-token chunks, `PF_MOE_CHUNK`, 1.77× faster than its default 4096 on an RX 470/580-class GPU and a little slower on Metal, so Vulkan's alone, and a value the user set is kept, `moeChunk`): Settings → Privacy's On downloads both (`pii.Download`, each checked against its pinned sha256, through magpie's proxy (`netproxy.Func`, as its other requests; hh0592821 on Discord, whose download went direct before), from hf-mirror first in China (`source.chinaFirst`) and from hf-mirror after Hugging Face everywhere else, the switch off too (`source.modelMirror`), so a poisoned or blocked huggingface.co doesn't stop it; hf-mirror serves AstrLink Guard's files itself and sends Privacy Filter's back to Hugging Face) and saves the setting only once they are on disk (`/api/privacy-model`, [`privacymodel.go`](../../internal/gui/privacymodel.go)). `pii.Find` starts `pf-serve` on the first request (Metal, Vulkan, else the CPU; one that dies on the GPU, doesn't load there in 40s (`gpuLoadTimeout`, well inside the gateway's 2 minutes), or doesn't answer in time there twice in a row, is started on the CPU, and Settings says why; a request it didn't answer in time once is tried on the GPU again, since a slow request on a weak GPU is no lost GPU (#3682). The GPU is tried again 10 minutes later, doubling to 2 hours while it keeps failing, with no restart (`gpuRetryAfter`), and one that answers is the model's again), joins short texts into 12 KB chunks and maps the spans back, cuts a text over 12 KB into pieces of 12 KB at most that overlap by 1 KB (`pieces`: after a line's end, else a space, else at a rune's start), each kept by its own hash, their spans put back at the text's offsets and merged where pieces overlap (`merge`: one found twice is one, one cut by a piece's end is whole), and asks pf-serve a chunk at a time (#3682: a 480 KB text whole took an RX 470/580 90s and kept nothing when cut off), keeps the last 20000 answers by hash, on disk too (`found-<model>.bin` beside the model: hashes, offsets, labels and each span's score, never text; a `pfound01` file from before scores is read with every span sure, so it masks as it did; written through temp-and-rename, a bad read taken as empty), so a restart doesn't ask the model about every history again (#3682), and ends it after 10 minutes with no request. Its requests take turns on its one pipe (`turn`), and a request reads the cache again when its turn comes; a request waits for its turn only while its context lives (`pii.FindContext`, through `gwmw.ContextTeller`), so one whose client went away leaves the queue (the gateway's 2 minutes leave masking's time out, #3682); the chunk pf-serve is on is finished and kept, so a request sent again asks only about what is left, and between chunks the turn is given up and taken again, so a short request goes before the rest of a long one. The status (`pii.Now`), `pii.Stop` and the idle timer never wait for a load or a request: `srv.mu` is held only to read or set the state, and `Stop` ends a load or a request under way at once. A request held before routing while it waits for the model is answered 504 saying so, and how to send without it (`unroutedMsg`); one of an older tag (`oldTags`, magpie updated since the download) runs until the pinned tag's is fetched (`upgrade`, on its start), which is then started on the next request and the old deleted ([`internal/pii`](../../internal/pii)). A failure names what ended `pf-serve`: magpie's own end of a request it didn't answer in time, else its exit status or signal and its last stderr lines past ggml's startup chatter (`died`, `lastSaid`). After one, requests are held back at once with that failure, not after a load each, until the model is tried again 30s later, doubling to 10 minutes while it keeps failing (`heldBack`, `retryAfter`; lazyeo on Discord, an M2 whose every request waited out the 504). A model that is on and can't answer (not on disk, didn't start, hung, held back) panics, so the request is held back with the 503 above rather than sent with the rules' masking alone (`masking.model`).
- With *Mask secrets* on, the user's own masking rules (`redactRules`, `redact.Rule`: a placeholder name and a prefix or an RE2 pattern, [`internal/redact/rules.go`](../../internal/redact/rules.go)) run before magpie's. Settings → Privacy adds them, removes them, and edits one in place (`POST /api/settings/redact-rule` with the rule's index and its current value, `editRedactRule` in [`internal/gui/api.go`](../../internal/gui/api.go)). Every rule is checked by `redact.CheckRules` when settings are saved; an edit that doesn't compile, matches the empty string, repeats another rule or names a rule no longer at that index is refused with the rules left as they were. The masker reads the rules from settings on each call (`options` in [`internal/masking`](../../internal/masking/masking.go)), so a change applies to the next request.
diff --git a/internal/redact/ids.go b/internal/redact/ids.go
index 511845224..fec1e506d 100644
--- a/internal/redact/ids.go
+++ b/internal/redact/ids.go
@@ -20,6 +20,13 @@ import (
// and never as what the model takes it for (a name, an account), but for
// hex digits without hyphens it calls a secret (an API key in prose). What
// the user chose (their words, their rules) stays theirs.
+//
+// An element token cua-driver made for computer use is one too: s and the
+// snapshot's counter in 8 hex digits, a colon and the element's index
+// (s0000002a:14). The model clicks by it, and a masked one is a click on
+// nothing (#3782). Its field is named for an element (element_token), not
+// a credential. Without the index, s and 8 hex digits is one only as such
+// a field's value: alone in a text it may be a student's number.
// noIDs is found without what is in an id of s, but for what the user
// chose and the credentials named for one.
@@ -37,12 +44,16 @@ func noIDs(s string, found []span) []span {
}
switch f.by {
case ByKnown:
- // masked before as what it is in full: a credential
- return false
+ // masked before as what it is in full: a credential. Not an
+ // element token: one the model labelled in prose a turn before
+ // ("click s00000001") would be a click on nothing in every
+ // element_token after it
+ return elementToken(s, l, r)
case ByModel:
// a key the model found in prose: 32 hex digits it calls a
- // secret ("the api key is 9f8e…"); never a UUID
- return f.kind != "SECRET" || uuidRe.MatchString(s[l:r])
+ // secret ("the api key is 9f8e…"); never a UUID or an element
+ // token
+ return f.kind != "SECRET" || uuidRe.MatchString(s[l:r]) || elementToken(s, l, r)
case ByRule:
switch f.kind {
case "PASSWORD", "API_KEY", "TOKEN", "PRIVATE_KEY":
@@ -56,9 +67,13 @@ func noIDs(s string, found []span) []span {
}
// idAround is the id that s[a:b] is in, or is: the run of hex digits and
-// hyphens around it, when that run is a UUID or 32 hex digits or more.
-// ok is false where s[a:b] reaches past such a run.
+// hyphens around it, when that run is a UUID or 32 hex digits or more, or
+// s[a:b] when it is an element token. ok is false where s[a:b] reaches
+// past such a run.
func idAround(s string, a, b int) (l, r int, ok bool) {
+ if elementToken(s, a, b) {
+ return a, b, true
+ }
l, r = a, b
for l > 0 && hexOrHyphen(s[l-1]) {
l--
@@ -79,6 +94,45 @@ func idAround(s string, a, b int) (l, r int, ok bool) {
return l, r, true
}
+// elementToken says s[a:b] is an element token, or its snapshot's part
+// (s0000002a, where the secret rule's value stops, at the colon): s and 8
+// lowercase hex digits, then a colon and the index, with no letter, digit
+// or _ on either side. Without an index, only the value of an element_token
+// field is one ("element_token": "s00000001").
+func elementToken(s string, a, b int) bool {
+ if b-a < 9 || s[a] != 's' || a > 0 && wordByte(s[a-1]) {
+ return false
+ }
+ for _, c := range []byte(s[a+1 : a+9]) {
+ if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
+ return false
+ }
+ }
+ end := a + 9
+ if end < len(s) && s[end] == ':' {
+ i := end + 1
+ for i < len(s) && s[i] >= '0' && s[i] <= '9' {
+ i++
+ }
+ if i == end+1 {
+ return false // a colon and no index
+ }
+ end = i
+ }
+ if b != a+9 && b != end || end < len(s) && wordByte(s[end]) {
+ return false
+ }
+ return end > a+9 || elementField(fieldWords(s, a))
+}
+
+// elementField says a field's name words are cua-driver's element_token
+// (elementToken, element-token): only that name, not any other with
+// "element" in it (element_api_key, element_password), which is a
+// credential's as before.
+func elementField(words []string) bool {
+ return slices.Equal(words, []string{"element", "token"})
+}
+
func hexOrHyphen(c byte) bool {
return c == '-' || c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
}
@@ -91,25 +145,11 @@ func idShaped(v string) bool {
// credentialField says the field whose value starts at s[at] is named for
// a credential: refresh_token, DB_PASSWORD, x-api-key; not one named for
-// an id, a cursor or a request, and not a bare "key".
+// an id, a cursor, a request or cua-driver's element_token, and not a
+// bare "key".
func credentialField(s string, at int) bool {
- i := at
- for i > 0 && strings.IndexByte(" \t\"'\\", s[i-1]) >= 0 {
- i--
- }
- if i == 0 || s[i-1] != ':' && s[i-1] != '=' {
- return false
- }
- i--
- for i > 0 && strings.IndexByte(" \t\"'\\", s[i-1]) >= 0 {
- i--
- }
- j := i
- for j > 0 && strings.IndexByte(alnum+"_.-", s[j-1]) >= 0 {
- j--
- }
- words := nameWords(s[j:i])
- if len(words) < 2 && (len(words) == 0 || words[0] == "key") {
+ words := fieldWords(s, at)
+ if len(words) < 2 && (len(words) == 0 || words[0] == "key") || elementField(words) {
return false
}
switch words[len(words)-1] {
@@ -126,6 +166,27 @@ func credentialField(s string, at int) bool {
return true
}
+// fieldWords are the words of the name of the field whose value starts at
+// s[at] (nameWords), none where no field's name is before it.
+func fieldWords(s string, at int) []string {
+ i := at
+ for i > 0 && strings.IndexByte(" \t\"'\\", s[i-1]) >= 0 {
+ i--
+ }
+ if i == 0 || s[i-1] != ':' && s[i-1] != '=' {
+ return nil
+ }
+ i--
+ for i > 0 && strings.IndexByte(" \t\"'\\", s[i-1]) >= 0 {
+ i--
+ }
+ j := i
+ for j > 0 && strings.IndexByte(alnum+"_.-", s[j-1]) >= 0 {
+ j--
+ }
+ return nameWords(s[j:i])
+}
+
// nameWords are a field's name's words in lower case: next_page_token,
// nextPageToken and X-Next-Page-Token are next, page, token.
func nameWords(name string) []string {
diff --git a/internal/redact/ids_test.go b/internal/redact/ids_test.go
index 818750d7c..06be4880b 100644
--- a/internal/redact/ids_test.go
+++ b/internal/redact/ids_test.go
@@ -92,3 +92,134 @@ func TestIDsGoAsWritten(t *testing.T) {
}
forgetIDs()
}
+
+// cua-driver's element tokens go as written, in the window states it
+// hands the model and in the clicks the model asks for, as #3782's
+// request had them: JSON laid out and compact, in a shell command, in a
+// log line, inside a JSON string, and whatever the local model takes one
+// for. In a field named for a credential, or not shaped as one, a value is
+// masked as before, and so is s and 8 digits with no index outside an
+// element's field (a student's number); the user's own words still mask
+// one.
+func TestElementTokensGoAsWritten(t *testing.T) {
+ forget := func() {
+ mu.Lock()
+ values = map[string]string{}
+ reindexKnown()
+ mu.Unlock()
+ }
+ kinds := map[string]bool{}
+ for _, c := range Categories {
+ kinds[c.ID] = true
+ }
+ o := Options{Secrets: true, Personal: true, Kinds: kinds}
+ tokens := []string{"s00000001", "s00000003:74", "s0000002a:14", "s00007a3f:42", "s00010001:3"}
+ asWritten := []string{
+ "\"element_index\": 0,\n\"element_token\": \"%s\",\n\"enabled\": true,",
+ `"depth":2,"element_index":74,"element_token":"%s","enabled":true`,
+ `"element_index":74,"element_token": "%s","enabled":true`,
+ `cua-driver click '{"pid":844,"window_id":10725,"element_token": "%s","session":"run-1"}'`,
+ "element_token=%s",
+ `\"element_token\":\"%s\"`,
+ `{"elementToken": "%s"}`,
+ }
+ for _, tok := range tokens {
+ for _, c := range asWritten {
+ forget()
+ in := strings.ReplaceAll(c, "%s", tok)
+ if out, n := Mask(in, o); out != in || n != 0 {
+ t.Errorf("%q went as %q", in, out)
+ }
+ }
+ }
+
+ // a window's state and a click in one request, a password beside them
+ forget()
+ body := `{"messages":[{"role":"tool","tool_call_id":"c1","content":"{\"elements\":[{\"element_index\":14,\"element_token\":\"s0000002a:14\"}],\"env\":\"DB_PASSWORD=hunter2abc9\"}"},` +
+ `{"role":"assistant","tool_calls":[{"id":"c2","type":"function","function":{"name":"click","arguments":"{\"pid\":844,\"element_token\":\"s0000002a:14\"}"}}]}]}`
+ if got, n := MaskJSON([]byte(body), o); strings.Count(string(got), "s0000002a:14") != 2 || strings.Contains(string(got), "hunter2abc9") || n != 1 {
+ t.Errorf("%d masked: %s", n, got)
+ }
+
+ // the local model's every label: as written, with its index in a text
+ // and without one in an element's field
+ for label := range modelKinds {
+ for v, text := range map[string]string{
+ "s0000002a:14": "click s0000002a:14 again",
+ "s00000001": `{\"element_index\": 0, \"element_token\": \"s00000001\"}`,
+ } {
+ forget()
+ mo := o
+ mo.Model = (&fakeModel{values: map[string]string{v: label}}).find
+ body := `{"messages":[{"role":"user","content":"` + text + `"}]}`
+ if got, n := MaskJSON([]byte(body), mo); string(got) != body || n != 0 {
+ t.Errorf("the model's %s: %s", label, got)
+ }
+ }
+ }
+ // s and 8 digits with no index, outside an element's field: what the
+ // model takes it for
+ for _, label := range []string{"account_number", "secret"} {
+ for _, text := range []string{"my student number is s12345678", `"student_no": "s12345678"`} {
+ forget()
+ mo := o
+ mo.Model = (&fakeModel{values: map[string]string{"s12345678": label}}).find
+ if out, n := Mask(text, mo); strings.Contains(out, "s12345678") || n != 1 {
+ t.Errorf("the model's %s went out: %q as %q", label, text, out)
+ }
+ }
+ }
+
+ // a field named for a credential: masked
+ for _, v := range []string{"s00000001", "s00000003:74"} {
+ for _, c := range []string{"password=%s", "token: %s", `"refresh_token": "%s"`, `"api_key": "%s"`} {
+ forget()
+ in := strings.ReplaceAll(c, "%s", v)
+ if out, n := Mask(in, o); strings.Contains(out, v[:9]) || n != 1 {
+ t.Errorf("a credential went out: %q as %q", in, out)
+ }
+ }
+ }
+ // a value of another shape in an element's field: masked
+ for _, v := range []string{"abc123def456", "s0000000g1", "S0000002A", "s000000012", "s00000001x", "s00000001:x9"} {
+ forget()
+ in := `"element_token": "` + v + `"`
+ if out, n := Mask(in, o); out == in || n != 1 {
+ t.Errorf("%q went as %q", in, out)
+ }
+ }
+ // a field with "element" in its name that is no element_token is a
+ // credential's: a UUID or the token's shape in it is masked
+ for _, c := range []string{`"element_api_key": "%s"`, `"elementSecret": "%s"`, `"element_secret_token": "%s"`} {
+ for _, v := range []string{"3f2504e0-4f89-11d3-9a0c-0305e82c3301", "s00000001"} {
+ forget()
+ in := strings.ReplaceAll(c, "%s", v)
+ if out, n := Mask(in, o); strings.Contains(out, v) || n != 1 {
+ t.Errorf("a credential went out: %q as %q", in, out)
+ }
+ }
+ }
+ // a token the model labelled in prose, masked there, still goes as
+ // written in the element_token of the click after it
+ forget()
+ mo := o
+ mo.Model = (&fakeModel{values: map[string]string{"s00000001": "secret"}}).find
+ prose := `{"messages":[{"role":"assistant","content":"I will click s00000001 now"}]}`
+ if got, n := MaskJSON([]byte(prose), mo); strings.Contains(string(got), "s00000001") || n != 1 {
+ t.Fatalf("the model's label in prose: %s", got)
+ }
+ click := `{"messages":[{"role":"assistant","content":"I will click s00000001 now"},` +
+ `{"role":"assistant","tool_calls":[{"id":"c2","type":"function","function":{"name":"click","arguments":"{\"element_token\":\"s00000001\"}"}}]}]}`
+ if got, _ := MaskJSON([]byte(click), mo); !strings.Contains(string(got), `element_token\":\"s00000001`) {
+ t.Errorf("a token known from prose was masked in the click: %s", got)
+ }
+
+ // the user's own words
+ forget()
+ uo := o
+ uo.Words = []string{"s0000002a"}
+ if out, n := Mask(`"element_token": "s0000002a:14"`, uo); strings.Contains(out, "s0000002a") || n != 1 {
+ t.Errorf("the user's word went out: %q", out)
+ }
+ forget()
+}