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.
This commit is contained in:
Muyang
2026-11-07 22:06:22 +08:00
committed by GitHub
parent 1df427773b
commit d34785ae88
6 changed files with 53 additions and 39 deletions
+5 -11
View File
@@ -13,7 +13,7 @@
</p> </p>
<p align="center"> <p align="center">
<img alt="version 0.3.0" src="https://img.shields.io/badge/version-0.3.0-0D4B4E"> <img alt="version 0.3.1" src="https://img.shields.io/badge/version-0.3.1-0D4B4E">
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-F2B35A"> <img alt="license MIT" src="https://img.shields.io/badge/license-MIT-F2B35A">
</p> </p>
@@ -50,9 +50,9 @@ Codex, Cursor, Claude Code and scripts.
| **Hooks** | `onRequest`, `onProvider`, `onUsage` | | **Hooks** | `onRequest`, `onProvider`, `onUsage` |
| **Protocols** | OpenAI chat, OpenAI Responses, Anthropic Messages, Gemini | | **Protocols** | OpenAI chat, OpenAI Responses, Anthropic Messages, Gemini |
| **Default policy** | `binaryPolicy: fail` — binary part for a non-local model → **403** | | **Default policy** | `binaryPolicy: fail` — binary part for a non-local model → **403** |
| **Alternative** | `binaryPolicy: guide` — withholds bytes, injects type/size metadata + a neutral local-tool prompt | | **Alternative** | `binaryPolicy: guide` — withholds bytes, injects type/media/size metadata + a neutral local-tool prompt |
| **Text masking** | **Not here.** Use Magpie **Settings → Privacy** | | **Text masking** | **Not here.** Use Magpie **Settings → Privacy** |
| **Tests** | 62 cases through the repository's `scripts/middleware.mjs` | | **Tests** | 67 cases through the repository's `scripts/middleware.mjs` |
## Install ## Install
@@ -84,7 +84,7 @@ The request is rewritten into a text-only request:
```text ```text
[A binary attachment was withheld locally and was NOT sent to you [A binary attachment was withheld locally and was NOT sent to you
(type image; mime image/png; bytes 481094). Do not claim to have seen it. (type image; media image; bytes 481094). Do not claim to have seen it.
First ask how the user/agent can process or read it locally.] First ask how the user/agent can process or read it locally.]
``` ```
@@ -178,9 +178,6 @@ magpie plugin options privacy-gate '{
checks still fail closed. checks still fail closed.
- **A guide prompt is not extraction.** It names local tools but cannot execute - **A guide prompt is not extraction.** It names local tools but cannot execute
them. Agents without a configured local extractor will only get advice. them. Agents without a configured local extractor will only get advice.
- **Guide-mode MIME metadata has a known bug:** [issue #795](https://github.com/magpie-community/plugins/issues/795)
validates a clipped prefix rather than the whole `media_type`. It does not
forward binary bytes.
- **Magpie engine timeouts fail open.** Magpie stops a hook at 250 ms and sends - **Magpie engine timeouts fail open.** Magpie stops a hook at 250 ms and sends
the request unchanged. This plugin keeps its own work bounded, but cannot turn the request unchanged. This plugin keeps its own work bounded, but cannot turn
an engine timeout into a rejection. See an engine timeout into a rejection. See
@@ -297,7 +294,7 @@ magpie plugin # 确认中间件已列出并启用
```text ```text
[A binary attachment was withheld locally and was NOT sent to you [A binary attachment was withheld locally and was NOT sent to you
(type image; mime image/png; bytes 481094). Do not claim to have seen it. (type image; media image; bytes 481094). Do not claim to have seen it.
First ask how the user/agent can process or read it locally.] First ask how the user/agent can process or read it locally.]
``` ```
@@ -388,9 +385,6 @@ magpie plugin options privacy-gate '{
超过总预算的文本不会被检查。结构性检查仍然失败关闭。 超过总预算的文本不会被检查。结构性检查仍然失败关闭。
- **guide 提示不是提取。** 它会列出本地工具名,但不能执行工具。没有配置本地 - **guide 提示不是提取。** 它会列出本地工具名,但不能执行工具。没有配置本地
提取器的 agent 只会得到建议。 提取器的 agent 只会得到建议。
- **guide 模式的 MIME 元数据存在已知问题:**
[issue #795](https://github.com/magpie-community/plugins/issues/795)
检查的是截断后的前缀,而不是完整的 `media_type`。它不会转发二进制字节。
- **Magpie 引擎超时会失败开放。** Magpie 在 250 ms 时停止钩子并原样发送请求。 - **Magpie 引擎超时会失败开放。** Magpie 在 250 ms 时停止钩子并原样发送请求。
本插件会限制自身工作耗时,但无法把引擎超时变成拒绝。参见 本插件会限制自身工作耗时,但无法把引擎超时变成拒绝。参见
[magpie#5305](https://github.com/yetone/magpie/issues/5305)。 [magpie#5305](https://github.com/yetone/magpie/issues/5305)。
File diff suppressed because one or more lines are too long
+8 -7
View File
@@ -157,17 +157,18 @@ measurements, not a guarantee that the engine's 250 ms hook limit is never hit.
is the engine's fail-open behavior, tracked in is the engine's fail-open behavior, tracked in
[magpie#5305](https://github.com/yetone/magpie/issues/5305). [magpie#5305](https://github.com/yetone/magpie/issues/5305).
## Known issue ## Guide metadata
[Issue #795](https://github.com/magpie-community/plugins/issues/795): guide-mode Guide mode never copies a raw MIME value into the prompt. It maps the detected
`guideMime` validates a 100-character clip rather than the whole `media_type`. type to one fixed coarse class: `image`, `pdf`, `office`, `audio`, `video` or
A long base64-like value with a slash in the first 100 characters can contribute `unknown`. Exact subtypes and parameters are deliberately omitted; the model
a fake MIME prefix to the guide prompt. This is a metadata-validation bug, not a only needs enough information to choose a local extractor. MIME parameters are
binary egress bypass; the part's bytes are still withheld. stripped before classification, and oversized candidates are ignored before
lowercasing so untrusted metadata cannot add unbounded normalization work.
## Verification ## Verification
- 62 cases in `cases.json`, run through `scripts/middleware.mjs` using the same - 67 cases in `cases.json`, run through `scripts/middleware.mjs` using the same
hook calls Magpie performs. hook calls Magpie performs.
- The package has no network, timers, Node APIs or imports; the middleware file - The package has no network, timers, Node APIs or imports; the middleware file
is one plain JavaScript module. is one plain JavaScript module.
-7
View File
@@ -89,12 +89,6 @@ A long hash, minified line or base64-ish text can exceed the 400-character
threshold. Raise `blobBase64Chars` / `blobHexChars` deliberately and understand threshold. Raise `blobBase64Chars` / `blobHexChars` deliberately and understand
that a larger document can then pass. Do not disable the gate. that a larger document can then pass. Do not disable the gate.
### Guide mode returns a MIME-looking string that is not a MIME
Known open issue [#795](https://github.com/magpie-community/plugins/issues/795):
the guide prompt may carry a clipped long `media_type` prefix. The binary bytes
are still withheld; this is metadata-only.
### The request went through even though the gate is on ### The request went through even though the gate is on
Check the Magpie log for `onRequest: took too long`. Magpie drops a hook after Check the Magpie log for `onRequest: took too long`. Magpie drops a hook after
@@ -125,4 +119,3 @@ the same gateway.
- [Design](./design.md) - [Design](./design.md)
- [Brand assets](../BRAND.md) - [Brand assets](../BRAND.md)
- Magpie engine timeout: [magpie#5305](https://github.com/yetone/magpie/issues/5305) - Magpie engine timeout: [magpie#5305](https://github.com/yetone/magpie/issues/5305)
- Guide MIME issue: [#795](https://github.com/magpie-community/plugins/issues/795)
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@magpie-community/middleware-privacy-gate", "name": "@magpie-community/middleware-privacy-gate",
"version": "0.3.0", "version": "0.3.1",
"description": "Structural egress gate for Magpie: withholds or rejects image/file/audio parts, encoded blobs and disallowed models, and guides agents toward local extraction when Magpie cannot mask text. Companion to ShyPie for DeepSeek Harness.", "description": "Structural egress gate for Magpie: withholds or rejects image/file/audio parts, encoded blobs and disallowed models, and guides agents toward local extraction when Magpie cannot mask text. Companion to ShyPie for DeepSeek Harness.",
"type": "module", "type": "module",
"files": [ "files": [
@@ -802,7 +802,7 @@ function rejectOrBlank(body, ctx, block, opts) {
// Guide policy: withhold bytes, keep enough metadata for the model to choose a tool // Guide policy: withhold bytes, keep enough metadata for the model to choose a tool
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function guideMime(node) { function guideMimeCandidate(node) {
var candidates = [ var candidates = [
node.media_type, node.mediaType, node.mime_type, node.mimeType, node.media_type, node.mediaType, node.mime_type, node.mimeType,
node.source && (node.source.media_type || node.source.mediaType), node.source && (node.source.media_type || node.source.mediaType),
@@ -813,8 +813,7 @@ function guideMime(node) {
]; ];
for (var i = 0; i < candidates.length; i++) { for (var i = 0; i < candidates.length; i++) {
if (typeof candidates[i] === 'string' && candidates[i].length > 0) { if (typeof candidates[i] === 'string' && candidates[i].length > 0) {
var candidate = candidates[i].slice(0, 100); return candidates[i];
if (/^[A-Za-z0-9][A-Za-z0-9.+-]*\/[A-Za-z0-9][A-Za-z0-9.+-]*$/.test(candidate)) return candidate;
} }
} }
var url = typeof node.image_url === 'string' ? node.image_url : node.image_url && node.image_url.url; var url = typeof node.image_url === 'string' ? node.image_url : node.image_url && node.image_url.url;
@@ -822,13 +821,35 @@ function guideMime(node) {
if (typeof url === 'string' && url.indexOf('data:') === 0) { if (typeof url === 'string' && url.indexOf('data:') === 0) {
var comma = url.indexOf(','); var comma = url.indexOf(',');
if (comma > 5) { if (comma > 5) {
var mime = url.slice(5, comma).split(';')[0].slice(0, 100); var mime = url.slice(5, comma).split(';')[0];
if (/^[A-Za-z0-9][A-Za-z0-9.+-]*\/[A-Za-z0-9][A-Za-z0-9.+-]*$/.test(mime)) return mime; if (mime.length > 0) return mime;
} }
} }
return ''; return '';
} }
// The guide prompt only needs a coarse class. Never echo raw media_type: it is
// untrusted metadata and exact subtypes/parameters are not useful for tool choice.
function guideMediaClass(node, kind) {
var candidate = guideMimeCandidate(node);
// Do not normalize an unbounded untrusted string outside the scan budget.
// 256 is above the longest usable type/subtype+parameters prefix and keeps
// this step O(1) for oversized metadata.
if (candidate.length > 256) candidate = '';
candidate = candidate.split(';')[0].trim().toLowerCase();
if (candidate.indexOf('image/') === 0) return 'image';
if (candidate === 'application/pdf' || candidate === 'application/x-pdf') return 'pdf';
if (candidate === 'application/msword' || candidate === 'application/vnd.ms-excel' ||
candidate === 'application/vnd.ms-powerpoint' ||
candidate.indexOf('application/vnd.openxmlformats-officedocument.') === 0) return 'office';
if (candidate.indexOf('audio/') === 0) return 'audio';
if (candidate.indexOf('video/') === 0) return 'video';
if (kind === 'image' || kind === 'image_url' || kind === 'input_image') return 'image';
if (kind === 'audio' || kind === 'input_audio') return 'audio';
if (kind === 'video' || kind === 'input_video') return 'video';
return 'unknown';
}
function guideBytes(node) { function guideBytes(node) {
var candidates = [node.bytes, node.size, node.file_size, node.fileSize, node.sizeBytes]; var candidates = [node.bytes, node.size, node.file_size, node.fileSize, node.sizeBytes];
for (var i = 0; i < candidates.length; i++) { for (var i = 0; i < candidates.length; i++) {
@@ -848,10 +869,10 @@ function guideBytes(node) {
} }
function guideText(node, kind, opts) { function guideText(node, kind, opts) {
var mime = guideMime(node); var media = guideMediaClass(node, kind);
var bytes = guideBytes(node); var bytes = guideBytes(node);
var bits = ['type ' + kind]; var bits = ['type ' + kind];
if (mime !== '') bits.push('mime ' + mime); bits.push('media ' + media);
if (bytes !== null) bits.push('bytes ' + bytes); if (bytes !== null) bits.push('bytes ' + bytes);
var text = '[A binary attachment was withheld locally and was NOT sent to you (' + bits.join('; ') + var text = '[A binary attachment was withheld locally and was NOT sent to you (' + bits.join('; ') +
'). Do not claim to have seen it. First ask how the user/agent can process or read it locally'; '). Do not claim to have seen it. First ask how the user/agent can process or read it locally';