Files
plugins/packages/fingerprint/integration.md
T
yetone e7dfe88218 fingerprint 0.2.1: the page shows in magpie's Linux and Windows windows
nb5p (Discord #feedback, 10-31): the 模型指纹 page stays blank in the
desktop app while `magpie web` (Chromium) shows it.

Cause, Linux: magpie frames a plugin page in <iframe sandbox="allow-scripts">,
so the service document has an opaque origin. WebKitGTK (2.52.6, magpie's
Linux window) then matches nothing to CSP 'self', and the service's policy
was 'self' alone for scripts, styles and images: the bundle was never
requested. Chromium and macOS WebKit match 'self' there, so web mode and the
Mac app worked. The service now names its own validated listener
(http://127.0.0.1:<port> or localhost:<port>) and, when proxied, the
validated launcher origin, in every fetch directive. Nothing else is
allowed: script-src keeps its nonce and no 'unsafe-inline', and
object-src/base-uri/form-action are 'none'.

Two Windows faults on the same path:
- boot.js took the window's http://wails.localhost for a remote web page
  and showed "upgrade magpie" instead of going to the local service.
  Every *.localhost, localhost, ::1 and 127.x now count as this computer
  (the service still checks Host and Origin and its bearer token).
- server.ts kept assets inside ui/ with startsWith(resolve(uiDir) + "/"),
  which never holds for backslash paths, so every asset was a 404 on
  Windows, in web mode too. It now uses path.relative, and still refuses
  .., encoded .. and a backslash escape.

integration.md says the same about the CSP. dist rebuilt with Bun 1.4.2 on
mote (the old source rebuilt to the committed dist byte for byte, so the
dist diff is only this change).

Run:
- mote (macOS 27): bun run build, typecheck, bun test: 33 pass.
- win-run (Windows 11): bun test: 33 pass.
- Linux, the real app: a dev magpie (-tags dev,gtk3) in WSL magpie-desktop
  under a sandbox HOME, with this plugin installed. Old plugin: the frame
  reached the service and no script ran, blank. New: the service page
  rendered and connected to magpie.
- macOS, a WKWebView with a wails:// scheme over a real dev backend on
  mote, sandboxed frame: renders with the old and the new policy.

Broken by hand:
- the new CSP test is red on the old server.ts (no own origin in
  script-src etc.);
- the boot test for wails.localhost is red on the old boot.js;
- the asset test is red on Windows with the old containment check.

Not tried: the real Windows Wails app (WebView2) end to end, and the
reporter's own machine; their platform isn't stated, and Linux is the one
where this reproduces.
2026-11-01 00:08:40 +08:00

123 lines
6.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Magpie integration
## Plugin contract
The package is a Magpie page plugin. Its manifest declares `page.file` as
`page/index.html`, a local service on port `3488` (configurable through the
`port` option), and one provider-menu action named `fingerprint`. Magpie sends
the action as `{ id, provider: { id, name } }` to the page frame. The page boot
document captures this event before navigating to the service, so the selected
provider is preserved across the service navigation.
A current Magpie release with `page.service` support is required. The service is bound to loopback. In a
desktop window it is reached at `http://127.0.0.1:<port>`; in `magpie web`,
Magpie supplies `magpie.service`, a tokenized reverse-proxy URL. The service
does not expose the gateway listener. Its authenticated API accepts its local
page and the opaque `Origin: null` used by Magpie's sandbox, and rejects unrelated origins.
The page CSP names the validated local listener and, when proxied, the HTTP(S)
origin supplied by the page launcher in every fetch directive (default, script,
style, image and connect). Opaque sandbox frames cannot reliably match `'self'`:
WebKitGTK, magpie's Linux window, matches nothing to it there, so a policy of
`'self'` alone loads no script and the page stays blank. The launcher origin changes
only the document policy; the API bearer token and Host/Origin checks still apply.
## UI integration
The service page reconnects to Magpie using the upstream page bridge, vendored
with its MIT notice. Magpie supplies live theme tokens and language through
`hello` and `theme` messages. The interface uses the upstream base control
styles and a small selection of Magpie's list, group and segmented-control CSS.
It adds no sidebar or duplicate application header inside the plugin frame.
Standalone mode keeps its own theme and language controls.
The iframe remains isolated: the bridge exchanges messages with its parent;
it does not access the parent DOM or load private host scripts. The pinned
upstream revision is documented in `THIRD_PARTY_NOTICES.md`.
## Gateway reads
The service reads `GET /v1/models` for the text model catalog and
`GET /v1/magpie/quotas?kept=1` for cached subscription account labels. The
`kept=1` form does not ask vendors for a fresh quota reading. Account rows are
projected to provider, user id, and display name only; upstream credentials,
balances, and raw quota records are never sent to the browser.
Gateway authentication uses `Authorization: Bearer <key>` when an API key is
configured. Loopback Magpie normally needs no key. A remote gateway must be
shared and requires its enabled gateway key. `X-Magpie-Account` can pin a
gateway request to one account id or signed-in user, but is only meaningful to
Magpie and is never forwarded upstream.
## Local development
```sh
bun run rebuild
bun start
```
The default gateway is `http://127.0.0.1:3425`. Set
`MAGPIE_FINGERPRINT_GATEWAY` (or `MAGPIE_ADDR`) when Magpie listens elsewhere.
Set `MAGPIE_FINGERPRINT_API_KEY` only when that gateway requires a key. The
service port is controlled by `MAGPIE_FINGERPRINT_PORT`; data defaults to
`~/.config/magpie/fingerprint`.
The standalone illustrative fixture is opt-in:
```sh
bun src/cli.ts --demo
```
Demo mode never calls a real gateway or model. Numeric first-release detection
uses the pinned `lmfpd-sdk` 0.2.0 request/analyze flow; the SDK has no public
tokenizer API. Each job retains its loaded reference snapshot and records
`builtAt`; remote data is not version-pinned or verified against a locally
pinned hash. Jobs use three common challenges per batch, bounded concurrency,
and explicit cancellation/limits.
The detector reports model-family evidence, not identity authentication.
## Options and persistence
Plugin options accept `port`, `gateway`, `apiKey`, `dataDir`, and `bankSource`.
Keep the API key in the service environment when possible. `MAGPIE_FINGERPRINT_DATA_DIR`
overrides persistence; `MAGPIE_FINGERPRINT_BANK_SOURCE` can point to a local bank
directory for fully offline scoring. Each batch retains its loaded detector instance.
Opening/refreshing the page reads only the configured gateway and local history.
`POST /api/bank/download` is the explicit download/refresh action; a valid
`POST /api/jobs` also loads data if none is usable. Cached snapshots have no
background refresh, including after the SDK default 24-hour TTL. Downloads use
`https://lm.ikale.io/data/`, falling back to
`https://cdn.jsdmirror.com/npm/lmfpd-data@latest/`. These hosts see the client IP
but receive no provider credentials, model ids, prompts or replies. A custom
`bankSource` replaces these defaults.
Jobs are stored as atomic JSON files with owner-only permissions. The current limit
is 200 batches, 24 targets per batch, three concurrent targets and three concurrent
samples per target. The batch deadline is 10–900 seconds. Restarted unfinished jobs
become `interrupted`; already received raw replies remain available.
The SDK's `requestSample` does not accept a transport or external abort signal.
A second loopback-only listener provides one-shot, authenticated, job-bound sample
URLs. It forwards only the prescribed model/protocol to the configured gateway,
adds session/account headers, records provenance, and binds upstream fetches to
the job controller. It cannot be reached through the page service proxy.
The page's relative asset/API URLs also work under Magpie's remote proxy prefix.
Exports provide JSON text and clipboard/manual-copy fallback because Magpie's
iframe sandbox can block downloads. Standalone browser users can download a file.
## Multiple Magpie hosts
Desktop and `magpie web` can share a listener when their service protocol and
configuration match. The remaining host takes over if the owner exits. Different
configurations or unrelated applications on the port are rejected. The public
health digest uses HMAC-SHA256 with an owner-only, random per-install key; the
gateway API key still participates in matching. If the install cannot persist
that key, processes refuse to share a listener. After upgrading, turn Fingerprint
off and back on once to release an older listener.
For OpenAI-compatible chat streams, a clean EOF after an explicit `stop` is
normalized with `[DONE]` when the gateway omitted that sentinel. The raw response
text is unchanged, and provenance records `streamNormalization: chat-done-after-stop`.
Incomplete finishes, errors, and disconnected streams are never repaired.