Network
Two surfaces under one plugin name, and they answer different questions. Connectivity — status and onChange — reports whether this machine has a path to the network. Interception — the rules and the log — reports what happened to a request under the crust-net scheme. Neither one implies the other: an app can be online with no rules installed, and a rule set can block a request on a machine with no network at all.
Connectivity
| command | arguments | answer |
|---|---|---|
status | none | { ok: true, online, interfaceKinds } |
onChange | none | { ok: true, event: "network:change" } |
const { online, interfaceKinds } = await window.crust.invoke("network", "status");
if (!online) showOfflineBanner();
await window.crust.invoke("network", "onChange");
window.crust.listen("network:change", ({ online, interfaceKinds }) => {
banner.hidden = online;
});
online comes from SCNetworkReachability, not from a probe request: it answers whether a path exists, so a captive portal or a refused DNS lookup still reads online, and a machine whose radio is off reads offline. interfaceKinds is the set of interface types the machine has — wifi, wired, cellular, other — from SCNetworkInterfaceCopyAll, so a page can say "on cellular, do not fetch" without guessing from what is missing.
The observer is scheduled on the main run loop, which is the same loop the app pumps events through. onChange installs it once; a second call replaces the listener rather than stacking a duplicate network:change per call.
Not on a platform without the call: status and onChange answer not-implemented naming SCNetworkReachability in SystemConfiguration (macOS), INetworkListManager in NetworkListManager (Windows) or GNetworkMonitor in GIO (libgio-2.0.so.0, loaded through libgtk-3) (Linux). A status with no adapter is a refusal, never an empty { online: false } that reads as a real reading.
The interception half is a WKURLSchemeHandler the darwin adapter registers, so its decision runs in the process that owns the webview. It is registered by the macOS runtime with darwinNetworkAdapter(), so window.crust.invoke("network", …) works in an app that names no plugin. What it does not do is intercept a scheme it did not register — see what it does not intercept.
Interception
An allow rule serves the bytes it carries. It is never a pass-through and never a fetch: the handler answers didReceiveResponse: / didReceiveData: / didFinish: itself, with an NSHTTPURLResponse carrying Content-Type, Content-Length and Access-Control-Allow-Origin.
Commands
| command | arguments | answer |
|---|---|---|
rules | none | { ok: true, scheme, rules, registered, reason } |
setRules | { rules } | same |
log | none | { ok: true, entries } |
clearLog | none | { ok: true, cleared } |
scheme is "crust-net", and registered says whether the handler landed at all — a rule set that matches nothing is indistinguishable from a handler that was never installed, so both are reported.
The handler lands only on a WKWebViewConfiguration no webview has used yet, so an app must supply one before its window builds the webview. registered: false means the rules and the log still work and nothing is intercepted: darwinNetworkAdapter() without { configuration } refuses rather than guessing.
Rules
await window.crust.invoke("network", "setRules", {
rules: [
{ pattern: "crust-net://api.example.com/*", action: "block" },
{
pattern: "crust-net://fixture.example.com/hello.json",
action: "allow",
body: '{"hello":"crust"}',
type: "application/json",
},
],
});
Each rule needs a non-empty pattern and an action of "allow" or "block". The first match wins, and no match allows — with an empty body and the default type text/plain; charset=utf-8, which is an answered 200, not a pass-through. A rule set replaces the previous one whole.
body and type are for allow rules only. A block rule that carries either is refused, because nothing would ever serve it. Both are bounded by the native buffers: body at 262144 bytes and type at 127, and a body over the buffer is refused rather than truncated, so a page never reads a silently cut response.
A pattern has one wildcard, *, standing for any run of characters — not a regular expression. That is deliberate: the handler asks once per task and cannot wait, so a page must not be able to hand the main thread a pattern that backtracks catastrophically.
| pattern | matches |
|---|---|
crust-net://a.example/x | exactly that URL |
crust-net://a.example/* | anything under that host |
*.png | any URL ending in .png |
What WebKit allows
Measured on macOS 27 against a real WKWebView, with the handler registered on its configuration:
| the page does | result |
|---|---|
<script src="crust-net://…"> | served and executed |
fetch("crust-net://…") | served, read as text, status 200 and the rule's Content-Type |
<img src="crust-net://…"> | refused — the bytes are delivered, the element fires error, width 0 |
| navigating the webview to the URL | no document — the handler runs and the load completes, nothing renders |
So a custom scheme is for subresources: scripts, styles, JSON and anything else a page fetches. It cannot be the page itself, and it cannot be an image source. The Access-Control-Allow-Origin: * header is what makes a fetch from the bridge's http://127.0.0.1:<port> origin readable; without it the page gets TypeError: Load failed with status 0.
The proof is one run of bun run src/platform/darwin/network-probe.ts, which serves a page over real http://127.0.0.1:<port> and has it read a rule's bytes through both a <script src> and a fetch:
network served=152 body=served-bytes-42 tasks=4
network fetch="served-bytes-42" status=200 type="text/plain; charset=utf-8"
network img=error width=0 inline=9px
The log
const { entries } = await window.crust.invoke("network", "log");
Entries are { url, action, status, bytes }, oldest first, at most 100 — the oldest are dropped. status is "served" when the handler answered with bytes, "blocked" when it answered an error, and bytes is how many it delivered. A URL at the native buffer's bound is blocked with reason: "url-too-long" and is never matched, because a truncated prefix would let a tail-anchored rule be dodged.
window.crust.listen("network:request", ({ url, action, status, bytes }) => {
console.log(action, status, bytes, url);
});
One event per intercepted task, emitted in the same call that answers WebKit. clearLog reports how many entries it removed.
What it does not intercept
Only requests under the crust-net scheme reach decide. A page loading ordinary https:// URLs is not observed by this plugin; a rule list is not a proxy, and setRules cannot block a request the handler never sees. status answers that other question — whether a path exists at all — and a rule list never changes its answer.
Reaching an external HTTPS/WSS service
A built app's document comes from the loopback bridge, so the page's origin is http://127.0.0.1:<port>. 'self' in the policy therefore means that origin and never a provider: DEFAULT_CSP carries connect-src 'self', which by spec refuses a fetch, WebSocket, EventSource or sendBeacon to https://api.… or wss://api.…. That is the whole of the block; crust does not narrow it further and nothing widens it.
security.csp is the key that opens it. It replaces the whole header — the default string and its per-response nonce are gone — so write out every directive the page needs, including connect-src 'self' for the bridge's own invoke and event stream:
import type { CrustConfig } from "crust";
export default {
security: {
csp:
"default-src 'self'; script-src 'self' 'unsafe-inline'; " +
"style-src 'self' 'unsafe-inline'; img-src 'self' data:; media-src 'self' blob:; " +
"connect-src 'self' https://api.example.com wss://api.example.com; " +
"base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
},
} satisfies CrustConfig;
Two constraints come with that power. The policy is served byte for byte, so a directive you omit falls back to default-src and a loosening you forget is a silent block — the console names the directive. And a custom policy cannot carry the per-response nonce, so script-src must keep 'unsafe-inline' or the inline bootstrap never installs and every invoke fails silently; config validation refuses a script-src without it and says why.
The policy is compiled into a built binary, so changing it in a shipped app is a rebuild. Opening connect-src is also a real cost: with 'unsafe-inline' in script-src, any script that reaches the page can read the provider key, which is the honest argument for keeping the key in an extension and relaying over the bridge (Extensions). Either way the provider's own CORS answer still governs whether the page may read the reply, exactly as in the crust-net case above.
A page that records with MediaRecorder and plays the take back from URL.createObjectURL needs no entry of its own: media-src 'self' blob: is in DEFAULT_CSP, because media-src falls back to default-src and 'self' does not match blob:. Add media-src back if you replace the policy.
A single file has no crust.config.ts, so it declares the same section on one line of its own source, in the comment form that syntax has:
// crust-security: {"csp":"default-src 'self'; script-src 'self' 'unsafe-inline'; connect-src 'self' https://api.example.com","microphone":"Example records voice memos."}
<!-- crust-security: {"microphone":"Example records voice memos."} -->
It must be a JSON literal rather than an export: a single-file config is embedded as a literal by the dev runner and the built wrapper alike, and detection runs before the page is bundled, so an import or a computed value would build in dev and vanish from a build. The object is validated by the same validator a project config uses — a purpose string that is not a sentence and a script-src with no 'unsafe-inline' are refused with the same message.
Microphone
security.microphone is the whole capture opt-in, and it is not a CSP directive — getUserMedia is a permission decision, and crust sends no Permissions-Policy header:
security: { microphone: "Example records voice memos." },
The sentence is copied verbatim into Info.plist as NSMicrophoneUsageDescription, which macOS shows in its prompt, so it is checked as a sentence; a hardened-runtime signature seals com.apple.security.device.audio-input with it. Only a microphone-only request is granted — camera and mixed requests are denied. macOS is the only platform that grants today; Windows and Linux deny. crust dev denies too, because an unbundled run has no Info.plist for macOS to read, so the prompt is something you meet in a built app.
What dev does differently
With dev.url set, the page is served by that dev server — Vite, Next — and carries no crust policy at all: crust cannot set another server's headers, and serving a matching policy would mean proxying that server, which changes its HMR and origin. So an external call can work all through crust dev and be refused the moment the same page is served by the built app. To make that impossible to miss, crust dev prints it on every run with a dev server:
security no CSP in dev — http://localhost:5173 serves this page, so the built app enforces the default policy (connect-src 'self'); an external provider that works here can fail there
Without dev.url the bridge serves the document in dev too, and the policy applies exactly as it will after a build. Either way, a provider call is proven against crust build output, not against the dev run.
Failure shape
status and onChange without an adapter answer not-implemented naming their platform call; the interceptor's four answer not-implemented with requires: "WKURLSchemeHandler". A malformed rule list is a rejected invoke carrying bad-request, naming the rule index and field.