Files
plugins/packages/privacy-gate/docs/design.md
T
Muyang d34785ae88 privacy-gate 0.3.1: guide prompts name a coarse media class, never the raw media_type (#838)
privacy-gate 0.3.1 (siml1169, #838): guide mode names a fixed coarse media
class (image, pdf, office, audio, video, unknown) instead of copying the
part's media_type into the prompt, so no clipped or fake MIME prefix can
reach the vendor. Parameters are stripped first, a candidate over 256
characters is ignored before it is normalised, and the part's kind is the
fallback. Bytes are still withheld. Fixes #795.

Reviewed at 2b74b6e69ecaf0d20bd9f5fd4580f673da266736 (base = main b8100001;
mote, Bun 1.4.2, temp HOME):
- packages/privacy-gate `bun test`: 67 pass, 0 fail
- `bun scripts/check.mjs privacy-gate` and `bun scripts/check.mjs`: ok
- repo root `bun test`: 6822 pass, 20 skip, 0 fail
- broken by hand: main's middleware 10 red; no 256 guard, no parameter
  strip, no office mapping, no kind fallback each 1 red (its own case);
  echoing the raw candidate 9 red
- CI run 42377224277 green
Not tried: a real guide-mode request through magpie.
2026-11-07 22:06:22 +08:00

6.8 KiB

ShyPie

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

  1. No unmaskable bytes leave. Image, file, audio, video and document parts must never reach a non-local provider.
  2. No encoded document bypass. Base64, hex, data URIs and PDF/ZIP magic smuggled through a text field are rejected.
  3. Deterministic policy. A malformed option must not silently disable a check; an unreadable request must not be passed.
  4. Two user outcomes. fail rejects; guide withholds the bytes and asks the model to use a local extractor.
  5. 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:

  • onRequest knows the model; it allows a binary part only when the model is in localModels.
  • onProvider knows the provider; it blanks content unless the provider is in localProviders.
  • 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- or PK\x03\x04 file 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 to fail; a present-but-malformed allowModels refuses every request instead of silently removing the allowlist.
  • Unreadable body, unsupported protocol, inspection limit: 403.
  • Guide rewrite or verification limit: 403.
  • onProvider cannot 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.

Guide metadata

Guide mode never copies a raw MIME value into the prompt. It maps the detected type to one fixed coarse class: image, pdf, office, audio, video or unknown. Exact subtypes and parameters are deliberately omitted; the model only needs enough information to choose a local extractor. MIME parameters are stripped before classification, and oversized candidates are ignored before lowercasing so untrusted metadata cannot add unbounded normalization work.

Verification

  • 67 cases in cases.json, run through scripts/middleware.mjs using 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.