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

Hu9956 (#3782): with masking on, DeepSeek Harness's computer use
(cua-driver: a window's state, then a click on one of its elements) went
to the vendor with element_token values as {{SECRET_...}}, found by the
rules. cua-driver hands the model a token for each element of a window:
s, the snapshot's counter in 8 hex digits, a colon and the element's
index (s0000002a:14). The model clicks by it. The SECRET rule takes a
value of 8 or more letters and digits after a field whose name holds
"token", and element_token is one, so each token in a window's state, in
a click's arguments and in a shell command's JSON went as a placeholder
and its index ({{SECRET_...}}:74). Two of the report's places show the
token with no index ("element_token": "s00000001"), so that is taken as
it is written too.

noIDs, which lets a backend's id (a UUID, 32 hex digits or more) go as
written unless its field is named for a credential, now takes an element
token for one too (elementToken): s and 8 lowercase hex digits, then a
colon and the index, with no letter, digit or _ on either side. Without
the index, s and 8 hex digits is one only as the value of a field named
for an element: alone in a text it may be a student's number
(s12345678), which the local model may call an account. A field named
for an element is no credential's, as one named for a page, a cursor or
a request isn't (credentialField, whose field name reading is now
fieldWords). So element_token's value goes as written, while the same
value after password=, token:, "refresh_token": or "api_key": is
masked, and so is a value of any other shape in element_token. A token
the local model takes for a secret, or anything else, goes as written,
as a UUID does. The user's own words still mask one.
docs/subsystems/gateway-routing.md says so.

A UUID in a field named for an element (element_token) goes as written
now too, as one in next_page_token does.

What was run (tree bb52fa4d, on origin/main 5215108e, go1.26.8,
linux/amd64):
- TestElementTokensGoAsWritten, new. Five tokens (s00000001,
  s00000003:74, s0000002a:14, s00007a3f:42, s00010001:3) in seven
  places, as the report has them (JSON laid out and compact, a space
  after the colon, a shell command's JSON, element_token=, inside a JSON
  string, elementToken), go as written. MaskJSON of a window's state and
  a click, DB_PASSWORD=hunter2abc9 beside them, masks the password
  alone. The local model's every label goes as written on s0000002a:14
  in a text and on s00000001 in element_token. s12345678, in a sentence
  or under student_no, labelled an account number or a secret by the
  model, is masked. s00000001 and s00000003:74 after password=, token:,
  "refresh_token": and "api_key" are masked, and so is element_token
  holding abc123def456, s0000000g1, S0000002A, s000000012, s00000001x or
  s00000001:x9. The user's own word masks one.
- It and TestIDsGoAsWritten -count=50: ok. internal/redact,
  internal/maskrec, internal/gwmw, internal/masking and plugins/redact:
  ok.
- Broken by hand, each red in TestElementTokensGoAsWritten:
  - idAround not taking an element token: every kept place, the
    MaskJSON body, the model's every label on s00000001 in
    element_token (the rule masks it there), and its address and secret
    labels on s0000002a:14.
  - "element" not among credentialField's words: every kept place, the
    MaskJSON body and the model's every label on s00000001 in
    element_token (the rule, again).
  - the model's secret kept on a token: its secret label on both.
  - s and 8 hex digits a token anywhere: s12345678's four cases.
  - a token only with its index: s00000001 in its seven places and the
    model's labels on it.
- A real run: magpie built at 5215108e and at this tree (go build -tags
  nogui -buildvcs=false), in a sandbox HOME with masking on
  (settings.json "redact": true), a local server standing in for the
  vendor that keeps each body it is sent. One request on
  /v1/chat/completions with the report's three places: a window's state
  as compact JSON with "element_token": "s00000003:74", one laid out
  with "element_token": "s00000001", a shell command's cua-driver click
  with "element_token": "s0000002a", a click by s00000003:74, and a user
  message with DB_PASSWORD=hunter2abc9. 5215108e: the vendor got each
  token as a {{SECRET_...}} placeholder (s00000003's with its :74), four
  places. This tree: each as written, the password still a placeholder.
- gofmt -l; go vet -tags nogui and go build -tags nogui for linux,
  windows and darwin, and go vet and go build for windows without the
  tag: exit 0 each. go vet and go build without the tag on linux: exit
  1 (this box has no GTK or WebKitGTK dev packages).
- go test -tags nogui -count=1 -timeout 45m ./..., Bun 1.4.2 on PATH:
  68 ok, and 7 red, none in what this changes: agent
  TestCodexDriftReached and TestCodexBehindReverseProxy (#3398);
  TestHowInstalled, TestCursorFolderFromTheEditorIsNotTheCLI,
  TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir
  and TestZedAppPath (#3141);
  TestReasonixCLIRequiresSupportedNativeVersion and
  TestReasonixCLIRecoversAfterProbeTimeout (#3641); edit
  TestWriteAtomicReadOnlyHardLink and testenv TestProgram (#3640);
  gateway TestPluginToldWhereTheAgentWorks (#3675); gui
  TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie
  (#3649); proc TestProbeTimeoutEndsWhatItStarted (#3848, passes 40 of
  40 alone); provider's "close of synctest channel from outside bubble"
  from TestUnreadLoginsWriteErrorsKeepCredentials (#3632), so the
  package's tests after it didn't run: go test ./internal/provider
  -skip '^TestUnreadLoginsWriteErrorsKeepCredentials$', Bun 1.4.2 on
  PATH: ok.

Not tried: DeepSeek Harness and cua-driver themselves (no macOS here);
the request was written by hand from the report's places.

Review, applied on origin/main 8e2ba36de (git am, sha256 of the patch
matches the issue's 1db65568…), with two changes to the patch:
- The patch made any field with "element" among its name's words no
  credential's (credentialField) and the field an index-less token may
  sit in. That widened a key-name exemption: a UUID in element_api_key or
  elementSecret went as written. Now only cua-driver's own name,
  element_token (elementToken, element-token), is that field
  (elementField); any other name is a credential's as before.
- A token known from before (ByKnown) was still masked as a credential.
  One the local model labelled in prose ("I will click s00000001 now")
  was then masked in every element_token after it, a click on nothing.
  An element token is no longer masked by a value known from before;
  the user's own words and rules, and the password, API key and token
  rules, still mask one.
Not scoped to tool calls and results by place: the exemption is held to
the token's shape (s, 8 hex digits, :index; index-less only as
element_token's value), so a value of any other shape under that key is
masked, and every route masks through redact.mask (masking.Masker's
MaskJSON and Text), so each route gets it.

What was run in review (darwin/arm64, go1.26.8, sandbox HOME):
- TestElementTokensGoAsWritten red with origin/main's ids.go (the
  report's laid-out "element_token": "s00000001" went as
  {{SECRET_…}}), green with this.
- Broken by hand: elementField back to "any word element": red, a UUID
  and s00000001 in element_api_key and elementSecret went out; ByKnown
  masking a token again: red, the click's element_token masked after
  the prose turn.
- go test -tags nogui ./internal/redact/... ./internal/masking/...
  ./internal/maskrec/... ./internal/gwmw/... ./plugins/redact/... and
  internal/gateway -run 'Redact|Mask|Masked': ok. gofmt -l: nothing;
  GOOS=linux go vet -tags nogui and GOOS=windows go vet of
  internal/redact: ok.
- Real run: magpie built at 8e2ba36de and at this tree, `magpie serve`
  in a sandbox HOME with "redact": true, a local fake vendor recording
  bodies; one /v1/chat/completions request with the report's three
  places (a laid-out window state with s00000001, a compact one with
  s00000003:74, a Bash call's cua-driver click with s0000002a) and
  DB_PASSWORD=hunter2abc9. Base: all three tokens went as {{SECRET_…}}
  (the second with its :74). This: all three as written, the password
  still a placeholder.
Not run: the full suite (one suite per machine; other sessions' runs
are on this box). Not tried: DeepSeek Harness and cua-driver
themselves.
This commit is contained in:
JoeyWu-tech
2026-10-27 15:42:08 +08:00
committed by yetone
parent 7aeae59670
commit 1cbdda25be
3 changed files with 217 additions and 25 deletions
File diff suppressed because one or more lines are too long
+85 -24
View File
@@ -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 {
+131
View File
@@ -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()
}