magpie icon indicating copy to clipboard operation
magpie copied to clipboard

redact: RedactShape's made-up values reach the client un-restored in an ordinary streamed reply, and a failed restore is silent

Open PanXinglin opened this issue 1 week ago • 8 comments

Version: 0.1.4708 (macOS) Settings: redactShape on

What happens

With shape-preserving masking (redactShape) on, a secret in the model's reply is replaced by a random string of the same shape. Every agent request is streamed. When that made-up string straddles two SSE chunks, the restore step cannot match it, and the client receives the made-up value instead of the original.

The failure is silent. There is no marker, no placeholder syntax, nothing that says "this is not the real value". A user who asks an assistant to repeat a stored password gets back a string with the right length and the right character classes - and no way to tell it is fake.

Why this is not #4384

#4384 (closed as not_planned) describes a made-up token inside a tool_calls URL argument, where the consequence is a 401 on that one call. That is a narrow path.

This report is the main path: the user asks a question, the assistant answers in ordinary prose, and the password or key inside that prose is a fake. The consequence is a wrong credential copied into a real system - worse than a failed request, because nothing signals the failure.

Evidence: the two replacement forms are mixed

From masking records on one machine over one day (2,638 substitutions total):

kind count replacement form
EMAIL 1027 placeholder {{EMAIL_...}}
TERM (word list) 367 placeholder {{TERM_...}}
PASSWORD 643 mixed - placeholder and bare same-shape string
USERNAME 355 bare same-shape string
SECRET 246 bare same-shape string

The mixing is the signature of the bug. The same kind takes two different paths depending on whether the value looks shape-eligible. Only the placeholder path is reliably restorable: it has delimiters, so internal/redact/writer.go's partial-tail detection can hold back an incomplete one across a chunk boundary. A bare same-shape string has no delimiters, so there is nothing to hold back - and nothing marks it as generated.

Steps to reproduce

  1. magpie settings set redact on
  2. magpie settings set redactShape on
  3. send a streamed chat completion whose prompt contains password=Example9xQ2vL7m
  4. the reply streams the made-up value; when it lands on a chunk boundary, the client keeps the made-up value

(Step 4 is timing-dependent on the live path. The deterministic version is #4384's unit test, which still fails against the current writer.)

What would fix it

Any one of these:

  1. Buffer a tail long enough to cover the longest shape-generated value before flushing a chunk, the way the placeholder path already does for {{. #4384's test would then pass.
  2. Stop generating bare same-shape values. Give every substitution a delimited form. This costs shape fidelity and costs nothing in correctness.
  3. Do not allow redactShape to stay on where it cannot be honoured - and say so in Settings, instead of letting a user believe they are masked while receiving fakes.

Point 3 matters most to me: a mask that silently fails is worse than no mask, because the user trusts the output and carries the fake onward.

PanXinglin avatar Nov 25 '26 09:11 PanXinglin

Investigated on 0.1.4783-equivalent (origin/main e66d872a8). Confirming the bug is real and locating it a level deeper than "no delimiters":

The mechanism is confirmed — a made-up shape value split across two SSE events is NOT restored. Writer.event holds an event whose text tail may be a placeholder start (partialTail → braceTail/wordTail/shapeTail), writes p.cut, and prepends the held tail to the next piece. shapeTail does detect a made-up key's prefix — but the leak shows up specifically in a tool call's arguments (an escaped JSON string). In Writer.event's walk callback the string at …function.arguments is the unescaped inner text ({"url":"…?token=<fake>), and partialTail correctly returns the fake's prefix length — yet the piece that goes out still carries an unrestored tail, because walk re-escapes the value it hands back while pending.tails/pending.cut were computed against a different representation of that same string. Two representations of the tail (escaped in the SSE text vs unescaped in walk's s) disagree, so the held tail never lines up with the next event's prefix and the fake survives to the client. Ordinary content prose restores correctly across the same chunk boundary (I verified a multi-cut test on the content path passes) — so this is the args/escaped-string path specifically, matching #4384's tool-call report inside this wider one.

A second, independent bug I found while reproducing: shapes and values are shared, process-global tables (maxValues = 200_000), and restoreShapes iterates every registered shape for every reply. A made-up key that is a prefix/suffix collision with a fake minted by an earlier, unrelated request is mis-restored — and shaped() registers values[f]=k under the same map that also holds placeholder→value entries, so the two namespaces collide. In my reproduction a restoreShapes call on a fresh masked output returned a {{SECRET_…}} placeholder string instead of the secret, i.e. the table already contained a stale/conflicting mapping. This is a separate correctness hole in the same code path and it makes the leak non-deterministic — worth treating as its own defect.

Happy to pair a failing unit test with a fix if a maintainer confirms the intended semantics for shapes/values scoping (per-request vs process-global). The args-path hold/restore mismatch is the concrete, deterministically-reproducible bug behind this report.

joei4cm avatar Nov 25 '26 10:11 joei4cm

Found it after the comment above: the stream case is the same class as the cross-value leak, in two more places.

A USERNAME/IP stand-in is eight lowercase letters, matched in any case. A made-up SECRET key has lowercase letters too, so a stand-in's letters can lie inside one — and three places took those letters for the stand-in itself:

  1. restoreShapes put a user name's stand-in back with no boundary check at all (whole was nil for every kind but IP). The letters inside another value's made-up key were replaced with the user's real name — one user's name landing inside another secret's place, which is worse than an unrestored key.
  2. shapeTail's whole loop counted those letters as a made-up key that "came whole", which caps the held tail at len(s)-whole. A made-up key split across two SSE events whose first piece contains the letters was therefore never held — the first event went out with the partial key as it came. This is the streamed-reply case in the report.
  3. outsideShapes skipped masking spans overlapping the letters, so a candidate secret sharing them with a stand-in inside another word would go to the vendor unmasked.

All three now use the boundary restoreShapes already had for addresses (wholeAddr): a stand-in counts only where it stands as a whole name — for restore, for the hold, and for the skip. Three tests in the new shape_collision_test.go, each red on main with the fix stashed off.

Branch: https://github.com/yetone/magpie/compare/main...joei4cm:magpie:fix/7723-stand-in-whole-everywhere (redact only, 053aa2d18 + one commit; go test -tags nogui ./internal/redact green, vet/gofmt clean, rest of the suite left to CI).

The second finding from my earlier comment (placeholder and shape entries sharing the values map, shapes process-global across requests) is untouched and still wants its own change.

joei4cm avatar Nov 25 '26 11:11 joei4cm

Independent maintenance check: L0NE-6/magpie-panel now carries the redaction stand-in fixes as 23542477 and 58545de7. Stand-ins are restored/held only as whole names, including case-folded usernames at stream boundaries. The full internal/redact suite and go vet -tags nogui ./internal/redact pass. The separate process-global table isolation concern remains open; this is a fork integration, not an upstream merge.

L0NE-6 avatar Nov 25 '26 13:11 L0NE-6

@L0NE-6 thanks for the independent maintenance check — glad the redaction stand-in fixes landed in L0NE-6/magpie-panel as 23542477 and 58545de7, and that the internal/redact suite plus go vet -tags nogui pass.

Since this account cannot open a pull request against yetone/magpie (no collaborator access), may I ask a favour: would you be willing to open the upstream PR for the stand-in fixes? Your branch already has the verification done.

Our fix branch is joei4cm/magpie → fix/7723-stand-in-whole-everywhere (645b61273). The shape of it: stand-ins are restored and held only as whole names, including case-folded usernames at stream boundaries, so one value's stand-in can no longer leak inside another value's made-up key. Happy to supply a clean diff or answer review questions. Co-authorship however you prefer.

joei4cm avatar Nov 25 '26 14:11 joei4cm

Maintenance verification: the panel carries this fix as 23542477 plus the case-fold follow-up 58545de7. The complete go test -tags nogui ./internal/redact -count=1 suite passes and go vet -tags nogui ./internal/redact is clean. The separate process-global stand-in table isolation concern in the discussion remains open and is not claimed fixed here.

L0NE-6 avatar Nov 25 '26 14:11 L0NE-6

@yetone sorry for the direct ping — this one has two independently written and independently verified fix branches ready to pick.

Cherry-pickable branches (redaction stand-ins restored only as whole names):

  • joei4cm/magpie:fix/7723-stand-in-whole-everywhere (645b61273)
  • L0NE-6/magpie-panel (23542477, 58545de7)

Both keep stand-ins restored and held only as whole names — including case-folded usernames at stream boundaries — so one value's stand-in can no longer leak inside another value's made-up key. Both report the full internal/redact suite and go vet -tags nogui ./internal/redact passing. The separate process-global table isolation concern stays open and is untouched by these branches. Our account cannot open an upstream PR (no collaborator access) and L0NE-6 reports upstream PR creation is currently disabled — so either branch is available for you to cherry-pick as-is.

joei4cm avatar Nov 25 '26 14:11 joei4cm

Another case, possibly with the same cause but in placeholder mode, not redactShape. It is in git history, so a mask reached a tool call's arguments and was never restored.

Community commit 86cded006 (joei4cm), landed on main as 482f654de for #7729, has the author email {{EMAIL_mtdn4cm6}}, byte for byte as the author wrote it. The likely path is that their agent ran git commit --author "… <email>" through magpie with email masking on, and the masked placeholder in the model's tool-call arguments was never turned back into the real email before the agent ran the command.

I don't know the agent, the protocol or the magpie version. Asking the author is the next step. If it reproduces, the fix should be looked at in the streamed tool-call argument path (input_json_delta / function.arguments pieces), where a placeholder can be split across deltas the same way this issue describes for text.

yetone avatar Nov 26 '26 10:11 yetone

Confirmed — that commit is ours, and yes, the masked email went through unreplaced.

What we verified from this side:

  • 86cded006 is a commit our agent made inside the yetone/magpie gateway in a /tmp/magpie-7729 worktree, and the worktree's local .git config literally contains {{EMAIL_mtdn4cm6}} — so the placeholder was written by a git config user.email "<masked>" call (or a git -c/--author one; another commit shows it rode the arguments without the config being set). Masking was on at the time.
  • The transcript's wire record for the tool call shows {{EMAIL_mtdn4cm6}} inside the git config user.email argument as the agent wrote it — i.e., the masked placeholder came back unreplaced in tool-call arguments, and the harness executed the command with it byte for byte.
  • It is not only 86cded006: the same literal is on several more of our branch commits (2d00624f1, 0c0914de4, c95e1a9c8, 065d2406f, a9465cd23, d52252215, 1b146baae); a couple have a real email, so not every worktree was poisoned. Once one placeholder lands as the config value, every later commit made in that worktree echoes it verbatim — one unreplaced placeholder is enough to keep producing them.

On your streamed tool-call argument hypothesis — agree, and it matches our own earlier finding on this issue. Our first comment named two pieces; we fixed only the whole-name collision class in fix/7723-stand-in-whole-everywhere (645b61273, restoreShapes / shapeTail / outsideShapes all bounded). The other piece — walk re-escaping the value it hands back while pending.tails/pending.cut were computed against a different representation of that same string in a tool call's arguments — is NOT covered by that branch, and nothing upstream since has touched it. This {{EMAIL_…}} commit looks like exactly that second case in the wild: a placeholder minted for an email, split across input_json_delta pieces, delivered to the agent unreplaced.

So the agent didn't invent the address — it faithfully used the masked string it received, and the leak is on the restore side of the streamed arguments path. If you want it, we can chase it with a fixture built from the real failing shape (an escaped {{EMAIL_…}} split mid-placeholder inside function.arguments/partial_json) — that is, the same reproduction shape our 11-25 analysis used for the content path, applied to arguments.

joei4cm avatar Nov 26 '26 13:11 joei4cm