privacy-gate 0.3.0: branded bilingual docs, ShyPie icon and README.
Installs with `magpie plugin add @magpie-community/middleware-privacy-gate`
(from this repo's dist index until npm has the package). Names the
built-in "JS gateway plugins" / "JS 网关插件" (id `middleware`). Icons
are a pie, a shield and a gold chevron, with no bird. magpie.icon points
at packages/privacy-gate/assets/shypie-icon.svg. The middleware code
(privacy-gate.middleware.js) is unchanged since e0a4548a.
Reviewed at 85dbd121b833017982d0a97a2f3fab50be98caab. The last round
(67ae63c3b..85dbd121b) changed only docs/operations.md:10 (ends the
sentence) and the aria-label of the two shypie-icon SVGs ("with a gold
chevron").
Run at 85dbd121b on mote (macOS), Bun 1.4.2, HOME=$(mktemp -d):
- bun test packages/privacy-gate: 62 pass, 0 fail.
- bun scripts/check.mjs privacy-gate: ok, exit 0.
- bun scripts/check.mjs (whole repo): exit 0.
- bun test (root): 6449 pass, 20 skip, 0 fail, 472 files.
- npm pack --dry-run: 12 files, version 0.3.0.
- git merge-tree with origin/main ab0d3fe54: clean.
Not tried: installing 0.3.0 into a running magpie (dist has 0.1.0 only
until this merges). No mover or min in yetone/magpie references it.
6.8 KiB
Design and threat model
This document describes the structural gate's design, the protocol positions it trusts, the request budgets, and the limits that remain.
Goals
- No unmaskable bytes leave. Image, file, audio, video and document parts must never reach a non-local provider.
- No encoded document bypass. Base64, hex, data URIs and PDF/ZIP magic smuggled through a text field are rejected.
- Deterministic policy. A malformed option must not silently disable a check; an unreadable request must not be passed.
- Two user outcomes.
failrejects;guidewithholds the bytes and asks the model to use a local extractor. - Cheap and bounded. Magpie gives a request hook 250 ms. The inspection and guide walk must have hard value/depth limits and no clocks, I/O or imports.
Non-goals
- This plugin does not mask text. Magpie's Privacy setting does that.
- It does not execute OCR or PDF extraction. The DSH bundle or a local MCP tool does that.
- It does not make a timed-out Magpie hook fail closed. That is an engine property, not something a middleware can guarantee.
Request lifecycle
sequenceDiagram
participant A as Agent
participant G as privacy-gate
participant M as Magpie
participant P as Provider
A->>M: request + protocol body
M->>G: onRequest(body, ctx)
alt clean request
G-->>M: body unchanged
else binary part, non-local model, policy=fail
G-->>M: reject(403)
else binary part, non-local model, policy=guide
G-->>M: text-only body with metadata
end
M->>G: onProvider(body, ctx)
alt provider is local
G-->>M: body unchanged
else provider is not local
G-->>M: body with content containers blanked
end
M->>P: provider request
M->>G: onUsage(row)
Policy modes
| Input | binaryPolicy: fail |
binaryPolicy: guide |
|---|---|---|
| No binary parts | pass | pass |
| Binary part, model + provider local | pass | pass |
| Binary part, non-local | 403 | replace every binary part with text metadata and a local-tool prompt |
| Binary part and encoded blob | 403 | guide walk scans all text; blob → 403 |
Malformed binaryPolicy |
falls back to fail |
— |
localModels and localProviders are independent:
onRequestknows the model; it allows a binary part only when the model is inlocalModels.onProviderknows the provider; it blanks content unless the provider is inlocalProviders.- Therefore both must match for bytes to reach a local endpoint.
Structural inspection
The walker is protocol-position-aware. A part is treated as binary only when it sits where that protocol puts content parts:
| Protocol | Site where binary parts are recognized |
|---|---|
| OpenAI chat | messages[].content[] |
| OpenAI Responses | input[] items and their content parts |
| Anthropic Messages | messages[].content[] |
| Gemini | contents[].parts[] |
That matters because a tool argument or a tool result can contain arbitrary
objects. tool_use.input, tool_result.content, function_call.arguments,
functionResponse.response and message text are scanned as ordinary data, not
as trusted protocol content positions.
Binary part types
The gate recognizes image, file, audio, video and document content shapes,
including image_url, input_image, input_file, image, file, document,
inlineData, inline_data, fileData and their common protocol variants.
Encoded text
A text field is rejected when it contains:
- a base64/base64url run at or above
blobBase64Chars(default 400), - a hex run at or above
blobHexChars(default 400), - a
data:URI, %PDF-orPK\x03\x04file magic.
The run-length scan is linear; the old {400,} regexes are gone.
Vendor-sealed replay fields
Some values are returned by a vendor and replayed on every later turn. They are exempt only inside their vendor block, and only when the field belongs to that protocol:
| Protocol | Exempt field |
|---|---|
| Anthropic thinking | signature |
| Anthropic redacted thinking | data |
| Responses reasoning | encrypted_content, vendor id |
| Chat/DSH assistant tool call | id, call_id, thoughtSignature |
| Gemini model-role part | thoughtSignature, textSignature |
The same key in tool arguments or message text is still scanned. The exemption is scoped by position and by type — never by key name alone.
Budgets
| Budget | Default | Purpose |
|---|---|---|
scanChars |
200,000 | Total text scanned per request. Conversation content is newest-first; each field may use at most an eighth of the budget. |
maxNodes |
40,000 | Every visited value, including primitives and array elements. Overflow refuses the request. |
maxDepth |
40 | Nesting bound. |
The guide walk uses the same site rules and the same value bound. It refuses the request if it hits a bound rather than passing a partially rewritten body.
Measured in the Magpie engine (moejs on the reference machine): a 2,000-turn Claude-Code-shaped body (about 3.5 MB / 40k values) is refused at the value cap in about 76 ms; a 200-turn body passes in about 38 ms. These are capacity measurements, not a guarantee that the engine's 250 ms hook limit is never hit.
Failure semantics
- Options are parsed defensively.
binaryPolicy: "Guide"falls back tofail; a present-but-malformedallowModelsrefuses every request instead of silently removing the allowlist. - Unreadable body, unsupported protocol, inspection limit: 403.
- Guide rewrite or verification limit: 403.
onProvidercannot reject. If a binary part reaches it, it blanks the content containers and lets a text-only request continue.- The host may still drop the hook on timeout and send the original body. That is the engine's fail-open behavior, tracked in magpie#5305.
Known issue
Issue #795: guide-mode
guideMime validates a 100-character clip rather than the whole media_type.
A long base64-like value with a slash in the first 100 characters can contribute
a fake MIME prefix to the guide prompt. This is a metadata-validation bug, not a
binary egress bypass; the part's bytes are still withheld.
Verification
- 62 cases in
cases.json, run throughscripts/middleware.mjsusing the same hook calls Magpie performs. - The package has no network, timers, Node APIs or imports; the middleware file is one plain JavaScript module.
- The tests cover fail/guide mode, malformed options, all four protocols, vendor-sealed replay fields, site scoping, blob scanning, and guide-mode bounds.