Running liuska serve securely
liuska serve renders whatever templates and data it receives. Templates can name files (fonts, images, snippets, sample data), so treat the API like any other service that reads from disk. This page covers the protections built in and how to set them up for a public demo or a shared deployment.
Quick start
Local use needs no setup. By default the server:
- listens on
127.0.0.1only, - accepts
Hostheaders for localhost names only (DNS-rebinding protection), - blocks cross-site browser requests that change state (CSRF),
- has CORS turned off,
- lets templates read files only inside
--templates(and any--asset-dir).
To expose the server:
# keys.txt: one key per line, "#" comments allowed. Generate keys with e.g.
# openssl rand -base64 32
liuska serve --addr 0.0.0.0:8080 --templates ./templates \
--api-key-file /run/secrets/liuska-keys --read-only
The server refuses to listen on a non-loopback address without API keys. Pass --no-auth to override this, but only when the port is reachable from trusted clients alone, such as a sidecar on a private network.
Flags
| Flag | Default | Description |
|---|---|---|
--addr |
127.0.0.1:8080 |
Listen address. Use 0.0.0.0:PORT (or a specific interface) explicitly to accept remote connections. |
--api-key KEY |
– | Require this key. Repeatable. Flags show up in process lists, so prefer one of the next two. |
--api-key-file FILE |
– | Keys, one per line. Blank lines and # comments are ignored. Repeatable. |
LIUSKA_API_KEYS / LIUSKA_API_KEY |
– | Environment variables with comma-separated keys. |
--read-only |
off | Reject PUT, PATCH and DELETE under /api/, for example saving templates. Rendering (POST) still works. |
--cors-origin ORIGIN |
– (CORS off) | Allow browser calls from this exact origin, e.g. https://app.example.com. Repeatable. * is not accepted. |
--allowed-host HOST |
localhost names on a loopback address; any otherwise | Accepted Host header values (name or name:port). Repeatable. Set this behind a reverse proxy. |
--asset-dir DIR |
– | Extra directory templates may read images, snippets and sample data from. Repeatable. |
--font-dir DIR |
– | Font directory. It is also an allowed root for font files. |
--max-body SIZE |
32MB |
Maximum request body size. Bigger bodies get 413. |
--timeout DUR |
30s |
Deadline per API request, including time spent waiting for a render slot. On timeout the server answers 503. 0 disables it. |
--max-concurrent N |
2 × CPUs | Number of POST /api/… requests (render, preview, validate, layout) that run at the same time. Others wait until --timeout, then get 503 with Retry-After. |
--max-pages N |
5000 |
Fail a render with 422 once the layout passes N pages. |
--max-rows N |
200000 |
Fail a render with 422 when one table has more than N data rows. |
--no-auth |
off | Allow a non-loopback address without keys. |
PNG previews are also capped at 64 megapixels per page (an A4 page at 600 dpi is about 35 MP), and images whose header claims more than 100 megapixels are refused before they are decoded.
API keys
Send the key as Authorization: Bearer <key>. X-API-Key: <key> also works. Keys must be at least 16 characters with no whitespace.
- Every route under
/api/needs a key, including routes added later. The only exception isGET /api/healthz. - The designer's static files (
/,/assets/…) are public so the browser can load the app. The app does not contain anything secret. - The server compares keys in constant time. Both sides are hashed with SHA-256 first, and every configured key is checked with no early exit.
- To rotate keys, add the new key, restart, move the clients over, remove the old key and restart again.
GET /healthz and GET /api/healthz answer {"ok":true,"authRequired":true} without a key. With a valid key, or when no keys are configured, they also return version, templatesDir and readOnly.
Designer
When the server needs a key, the designer asks for it the first time an API call returns 401. It keeps the key in sessionStorage, so the key is forgotten when the tab is closed. The key is never saved in templates or localStorage. If you cancel the prompt, the designer stops asking until you reload the page. If the key is wrong, the server rejects it and the designer asks again.
Template file access
Templates can reference files in fonts[].src, image src (including paths that come from data bindings and file:// URLs), snippetPath and sampleDataFile. In serve, every such path has to resolve to a file inside an allowed root:
- the
--templatesdirectory, - each
--asset-dir, - each
--font-dir, for fonts only.
Rules:
- Relative paths resolve against the templates directory for inline templates, and against the template file's own directory for named templates and snippets.
../is allowed as long as the result stays inside a root. - Absolute paths are allowed only when they point inside a root.
- Symlinks are followed before the check. A link inside a root that points outside is refused.
- UNC paths (
\\host\share,//host/share), Windows device paths (\\.\,\\?\), drive-relative paths (C:x), device names (CON,NUL,COM1,aux.json, …) andfile://URLs with a remote host are always refused. Paths are checked as text first, so a UNC path never reaches the network (no NTLM leak). - If no templates directory or asset dir is configured, inline templates cannot read any files.
data:URIs still work. - A refused file is treated like a missing one: the render goes on without it, with a render warning. Validation reports
file not found, so it does not reveal whether files exist outside the roots. API responses show the path as written in the template, never the server's absolute path.
The CLI commands (render, validate, batch) do not restrict file access. The person running them already controls the files.
Go callers that embed the renderer can use the same policy:
tmpl.Files = renderer.NewFileAccess("/srv/templates", "/srv/assets") // nil = unrestricted
eng, _ := renderer.NewEngine(renderer.Options{MaxPages: 5000, MaxTableRows: 200000, MaxRasterPixels: 64_000_000})
Browser-facing protections
- Host check. On a loopback address, requests whose
Hostis notlocalhost,*.localhost,127.0.0.1or[::1]get421. This stops DNS-rebinding attacks, where an attacker's domain resolves to127.0.0.1so that a web page can read your local API. Behind a proxy or with a real hostname, list the names with--allowed-host. - Cross-origin protection. Go's
http.CrossOriginProtectionrejects unsafe methods (POST,PUT, …) when browser headers (Sec-Fetch-Site,Origin) show the request comes from another site and that origin is not in--cors-origin. A malicious page therefore cannot trigger renders or template writes, even with atext/plainform post. Clients that are not browsers (curl, backends) are not affected. - CORS is off by default. The Vite dev server proxies
/api, so the designer in development is same-origin and does not need CORS.--cors-originallows exact origins and theAuthorizationheader. - Headers on every response:
X-Content-Type-Options: nosniff,Referrer-Policy: no-referrer,X-Frame-Options: DENY,Cross-Origin-Opener-Policy: same-origin. - Designer CSP:
default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; frame-src 'self' blob:; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none', plus a restrictivePermissions-Policy. - API responses get
Content-Security-Policy: default-src 'none'; frame-ancestors 'none'andCache-Control: no-store. - Static files. No directory listings. Dotfiles (
.env,.git/…) are never served...cannot escape the designer directory.
Other limits
- The HTTP server sets
ReadHeaderTimeout10 s,ReadTimeout60 s,IdleTimeout120 s,WriteTimeoutmax(120 s,--timeout+ 30 s) andMaxHeaderBytes64 KiB. - Template names in
/api/templates/{name}andtemplateNameare plain file names: letters, digits,_,-and., with no separators, no.., no leading dot, no Windows device names and at most 128 characters. batchoutput file names built from data are sanitized. Separators and invalid characters are replaced, and Windows device names are prefixed with_.
Recommended production setup
- Run behind a TLS-terminating reverse proxy.
liuska servespeaks plain HTTP only. Set--allowed-hostto your public hostname. - Use
--api-key-filewith one key per client, and--read-onlyunless people really edit templates on that server. - Keep templates and assets in a dedicated directory, not your home directory or repository root.
- Run as an unprivileged user with read-only access to the templates directory, plus write access only if you allow saving. In a container, use a read-only root filesystem.
- Put rate limiting in the proxy.
--max-concurrentand--timeoutprotect the process itself, not the network.
For developers: adding endpoints
All routes are registered on the server's single http.ServeMux in generator/, and newServer wraps that mux in secure() (generator/security.go). A new route under /api/ therefore gets auth, read-only mode, body limits, the timeout, the concurrency limit and the headers automatically. Rules:
- Register with
s.mux.HandleFunc("POST /api/…", …)insidenewServer. Don't create a second mux or listener. - Endpoints that change server state must use
PUT,PATCHorDELETE.--read-onlyletsGET/POSTthrough. - Use
r.Context()for rendering so timeouts apply. - Read files that a request names (for example a template's
sampleDataFile) through the file policy: the template'sFilesfield (s.sandbox(t)), orrenderer.FileAccess. - Map
renderer.ErrLimitto422andcontext.DeadlineExceededto503, asrenderFailuredoes.
Known limits
- The check runs before the file is opened, so a local user who can create symlinks inside an allowed root could swap in a link between the check and the read (TOCTOU). Don't give untrusted users write access to the roots.
--editor-diris served withos.DirFS, which follows symlinks inside that directory. Point it at a build output you control.- One very large table is laid out in a single step. The timeout is checked between elements and pages, so one huge table can overrun
--timeout.--max-rowsand--max-bodybound how long that can take. - There is no built-in TLS, rate limiting or per-key permissions. Every key has the same rights.