Expand description
Access control and response hardening for the local HTTP server: a
per-process bearer token, a Host-header guard, an exact-origin CORS
allowlist, and the security headers (CSP included) every response carries.
§Threat model — stated honestly
Ledgeline serves a complete financial journal, readable and writable, over
plain HTTP on loopback. Until this module existed the only thing between that
journal and every web page the user had open was the port number, and
CorsLayer::permissive() gave even that away: any origin could read
/transactions and POST/DELETE /api/transactions (SEC-1).
§What the controls here genuinely stop
- A web page sweeping loopback ports. With no CORS layer on the default
path a page on
https://evil.example(orfile://, or anotherhttp://localhost:*app) still gets its request sent, but cannot read any reply — and because every wire and/apiroute now demands the token, it cannot land a blind write either. The token is a 256-bit random value it has no way to guess and, absent CORS, no way to read. - A page on an ALLOWLISTED origin reading the token.
--allow-originopens the API to a named dev origin, and for a while it opened the SPA shell too, because the CORS layer was installed over the whole router including the fallback. The shell then carried the token in its body, so onefetch("http://127.0.0.1:<port>/")from any page on that origin — another Vite project, a malicious dev dependency — read it and had full read/write on the journal with no interaction (SEC-12). The layer is now aroute_layerover the explicit routes only; seeprotect. - DNS rebinding.
host_guardrejects any request whoseHostis not a loopback name on the port we actually bound, so re-pointingattacker.example.comat127.0.0.1buys nothing. - Silent exfiltration if an XSS sink ever appears. The
connect-src 'self'CSP means injected script cannot post the journal anywhere.
§What they do NOT stop — do not claim otherwise
- A malicious process running as the SAME user. It can read (and rewrite) the journal file directly; no HTTP control is relevant.
- Anyone who can read the
--serverterminal. Headless mode prints the launch URL, token included, because something else has to drive it. - Anything at all once
--hostis pointed off loopback. That path is gated on an explicitly configured token ($LEDGELINE_TOKENfor Ledgeline,AppIdentity::token_envin general) and turns theHostguard off, because the legitimateHostvalues are then unknowable.
§Closed: another local process scraping the token from GET /
The SPA shell is served without a token (the browser has nothing to present
before it has loaded the page), and HTTP gives us no way to tell the user’s
own WebView from someone else’s curl. The shell used to carry the token in
its body for the browser to bootstrap, so any process that could reach
loopback — another local user’s included — could GET / and read it. The
token now appears in no response body at all:
- the GUI hands it to its WebView out of band, with wry’s initialization
script, guarded to our exact origin (
token_init_script); --serverprints a launch URL with the token in its#token=fragment, which a browser never puts on the wire; the SPA moves it intosessionStorageand strips it from the address bar.
That exposure is what made the following worth writing down. This threat
model used to bound the worst case by “the journal file’s own permissions”,
which was wrong — wrong enough to be a finding in its own right, because
it is not bounded at all. Whoever holds the token holds PUT /api/prefs, and
hledgerPath there names the binary spawned on every import. So a token
scraped once bought arbitrary CODE EXECUTION as the user, surviving restarts,
from which the journal’s permissions follow trivially (SEC-14).
Ledgeline’s prefs::check_storable refuses a group- or world-writable binary and runs
the candidate before persisting it. Read that narrowly:
- it removes the PERSISTENT half for anything that is not plausibly hledger — a script that prints nothing recognisable is refused and never written down, so it does not run on every import forever after;
- it does not remove execution. The probe is an
exec, soPUT /api/prefsstill runs whatever the caller names, exactly once (it used to be twice), before refusing it. Bounding that would mean not probing at all, and then a bad path is discovered at import time instead — which is the failure modeprefs.rsexists to prevent; - and an attacker who owns a binary that DOES print an hledger version banner walks through all of it unchanged.
So it is defence in depth for whoever already holds the token, and keeping
the token out of GET / is what keeps everyone else from holding it.
The token is per-process and never persisted: restarting the app invalidates it, and no other process is told what it is.
§Shared by every app on the shell
Nothing here is Ledgeline’s alone. An app names its token variable and its
SPA’s globals in an AppIdentity, adds any request headers its uploads
carry to the CORS allowlist (Security::cors_layer), and puts the whole
posture on its router with protect (the explicit routes) and harden
(everything).
Structs§
- Access
Token - A per-process bearer token. Never persisted, never logged except by explicit
request in
--servermode, and compared in (best-effort) constant time. - Process
Token - The process’s token together with where it came from.
from_envis what SEC-9 keys on: a non-loopback bind is only allowed when the operator chose the token deliberately. - Security
- The access-control posture a router enforces. Cheaply cloneable (the lists are shared), because both the middleware state and the SPA handler hold one.
- Security
Plan - The access-control decisions taken at startup, before the socket is bound.
Enums§
- Security
Error - Failures building the security configuration.
Constants§
- HANDOFF_
PATH - The same-origin stand-in for a link a sandboxed frame may not follow
itself:
<origin>/.ledgeline/open?url=<the link, percent-encoded>.
Functions§
- base_
csp - The Content-Security-Policy for every response that carries no inline script (JSON, static assets, errors).
- bearer_
token - The credential from an
Authorization: Bearer <token>value (scheme matched case-insensitively, per RFC 7235). - csp_
sha256 - The
sha256-<base64>source expression for one inline script’s exact text. - harden
- The layers every response passes through, applied to the WHOLE router (the
SPA shell included): the
Hostguard (anti-DNS-rebinding), a handler panic as a500rather than a dropped connection (SEC-2),nosniff,no-referrer, and the base CSP for any response without its own (SEC-7) —if_not_present, so the SPA shell’s policy, which additionally hashes the inline scripts it just rendered, is left alone. - host_
guard - Reject any request whose
Hostis not one this server answers to. - in_spa
- Is
candidatea document of the app’s own SPA: onoriginexactly (http://127.0.0.1:<port>, no trailing slash) and underspa_base(AppIdentity::spa_base)? - is_
loopback_ host - Is
hosta loopback address (or thelocalhostname)? Anything else — a LAN address, or the0.0.0.0/::wildcards — publishes the app’s data beyond this machine. - no_
store Cache-Control: no-store— “do not write this anywhere”.- no_
store_ json - A JSON response marked
no_store, with noETag— the one helper every Ledgeline*_apimodule uses for reads that are not derived from the journal snapshot (import, rules, aliases, budget, prices, projections, accounts, QB-journal, profiles, benchmarks). - plain_
https rawre-serialized when it is a plain https URL: no port, no credentials. The first test an app’s “open this in the system browser” allowlist applies, before its own hosts. The one judge of such URLs, for the window’s navigation and an app’s links alike (notgui-only, so a server-only build vets them the same way).- plain_
mailto rawre-serialized when it is amailto:URL with an address, such asmailto:ap@example.com?subject=Invoice%20108. Headers (subject,body, …) are fine; a baremailto:or one with only headers is refused. The sibling ofplain_httpsfor an app’s “open this in the mail app” links.- protect
- Put the token guard and the CORS allowlist on every route registered on
routerso far — call it after the last API route and BEFORE the SPA fallback, which must stay reachable without a token. - shell_
csp - The policy for one rendering of the SPA shell: the base policy plus a
'sha256-…'source for each inline<script>the shell actually contains, and the app’sframe-srcwhen it has one (AppIdentity::spa_frame_src). - token_
from_ env_ or_ random $<var>when set and non-empty, otherwise a fresh random token.varis the app’sAppIdentity::token_env: set it when a cross-origin client (vite dev, the Playwright e2e harness) has to know the token before the server starts.- token_
guard - Require
Authorization: Bearer <token>on every route this wraps. - token_
init_ script - The initialization script that hands the WebView its access token, as the
windowglobal the app’s SPA reads (AppIdentity::token_global).