diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 80c85ae..a557848 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -93,11 +93,10 @@ jobs: ./dist/llmgw-${version}-linux-amd64 --version cat dist/checksums.txt - # Signed when the repository has a signing key, so a machine that trusts - # it installs nothing the server alone could have put there. Without the - # secret nothing is signed and releases are checked on their checksums, - # as before; a build that trusts a key refuses those, so the secret has - # to be in place before trusted.pub is filled in. + # Signed, so a machine that trusts the key built into it installs + # nothing the server alone could have put there. `release sign` checks + # the signature against that key before writing it, so a secret that + # does not match trusted.pub stops the release too. - name: Sign the checksums env: RELEASE_SIGNING_KEY: ${{ secrets.RELEASE_SIGNING_KEY }} @@ -105,7 +104,15 @@ jobs: set -euo pipefail version="${GITHUB_REF_NAME:-$(git describe --tags --always)}" if [ -z "${RELEASE_SIGNING_KEY:-}" ]; then - echo "no RELEASE_SIGNING_KEY secret; releases are not signed" + # Builds that carry a trusted key install only signed releases: + # publishing an unsigned one would be a release no upgraded + # machine accepts, so it stops here instead. + if [ -s internal/release/trusted.pub ]; then + echo "internal/release/trusted.pub holds a key, and the RELEASE_SIGNING_KEY secret is not set:" + echo "add it under Settings -> Secrets, or this release could not be installed anywhere." + exit 1 + fi + echo "no RELEASE_SIGNING_KEY secret and no trusted key; releases are not signed" exit 0 fi ./dist/llmgw-${version}-linux-amd64 release sign dist/checksums.txt diff --git a/BACKLOG.md b/BACKLOG.md index f76f633..3453e2b 100644 --- a/BACKLOG.md +++ b/BACKLOG.md @@ -19,6 +19,14 @@ Conventions this codebase keeps, which every item here must keep too: ## A. Security — do first +**Status (2026-10-04): done and merged.** A1–A9, A6b included — etcd and +Patroni's API on TLS, the PoC moved in maintenance mode without a failover. +Deliberately narrower than written: the admin API is a token on loopback +rather than a unix socket (A5); thread ids are bounded and documented as +project-wide rather than scoped per user (A8); Python engines resolve with an +explicit PyTorch index and a PEP 751 lock rather than locks shipped per engine +version (A8). All of A is live on the PoC. + ### A1. Toolchain (S, hours) `govulncheck` finds 11 standard-library vulnerabilities reachable from this code at Go 1.26.3 (net/http, crypto/x509, os.Root, net/textproto). Raise the diff --git a/CHANGELOG.md b/CHANGELOG.md index 45d6e08..2d19c90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,221 @@ APIs change between minors without a deprecation period. ## Unreleased +### Security + +- **Built with Go 1.26.6.** Eleven standard-library vulnerabilities reachable + from this code at 1.26.3 (net/http, crypto/x509, os.Root, net/textproto) + are fixed by it. `make vuln` runs govulncheck; `make check` runs it with the + tests and vet. +- **Releases are signed, and only signed releases are installed.** The build + now carries the release signing key (`internal/release/trusted.pub`); the + control plane upgrading itself, an agent told to upgrade and a node being + added all refuse a release whose checksums are not signed with it. The CI + release job stops rather than publish an unsigned release. Adding a node + no longer has the node fetch the release and compare it with a checksum + from the same server: the control plane fetches it for the node's + architecture, checks the signature and the checksum, and hands it over. + Releases are fetched over https only (loopback excepted), and + `LLMGW_RELEASE_KEY` replaces the trusted key in development builds only. +- **The agent port is not opened without TLS by accident.** A control plane + with no `agent_tls` refused nothing: its agent port was plaintext, on every + interface, and any machine reaching it could register as any node — be + given replicas and so be sent prompts. It now refuses to start unless + `agent_insecure: true` is set, and then listens on loopback unless + `agent_listen` names an address. `server init` writes `agent_tls`, as + before. +- **A model definition cannot run code on its nodes.** A profile's `env` and + `args` reach a process the agent runs as root. The environment was checked + against a short denylist that missed NCCL's and CUDA's plugin and + injection libraries; arguments were not checked at all, so + `--trust-remote-code`, llama.cpp's `--log-file` or `--slot-save-path`, or + vLLM's `--tool-parser-plugin` were taken. `env` is now an allowlist of the + families engines are tuned with (CUDA_, NCCL_, VLLM_, OMP_, HF_…) less any + name for a path, plugin, library, address or credential, and llama.cpp's + `LLAMA_ARG_*` are checked as the flags they are; `args` refuse what reads, + writes or loads files, loads code, or moves what the agent sets (host, + port, key, model). Both are refused where a model is defined and again on + the node before the engine starts. +- **Engines no longer run as root.** The agent starts them as the system + user `llmgw-engine` (in the video and render groups for the GPUs), with a + home and caches of its own under the agent's work directory. `llmgw node + add` makes the user; a node without it keeps running engines as root and + the agent says so in its log. `supervisor.run_as` in agent.yaml names + another user, or `root`. +- **The admin API wants a token.** It listened on loopback with no + authentication, so any process or user on the control plane could add a + model, upgrade the fleet, read the engines' keys or dump the heap through + pprof. Its `/api` and `/debug` paths now demand the token the server + writes, 0600, to its state directory (`/var/lib/llmgw/admin-token`) on + first start; `llmgw ctl`, the console and `node add` read it, so they run + with sudo, and `LLMGW_ADMIN_TOKEN` or `ctl --token-file` give it from + elsewhere. pprof is off unless `debug_pprof: true`. `admin_listen` off + loopback is refused unless `admin_remote: true`. The read-only dashboard + pages, `/metrics` and `/health` are unchanged. +- **The replicated store asks for a password and speaks TLS.** Patroni's + REST API took changes from anyone who reached port 8008 — rewriting the + access rules to `trust`, or moving the leader — and PostgreSQL traffic + between the machines was plaintext (`sslmode=disable`). Patroni now asks + for a password before a change (kept with the cluster's others in + `/root/llmgw-pg`, and on the control plane in + `/etc/llmgw/secrets/patroni-rest` for `server add`); each member has a + certificate from the cluster's authority, PostgreSQL has `ssl` on, every + remote access rule is `hostssl`, and the control planes connect with + `sslmode=verify-full` against the authority's certificate. A new cluster is + set up this way; an existing one is changed in place, by reload, the + cluster serving throughout, by running **Replicate the store** in the + console again — or `sudo --preserve-env=SSH_AUTH_SOCK llmgw server init + --patroni @ --patroni @ --no-mirror --no-node + --no-model` — on the control plane that holds the authority, then on each + other control plane so it reconnects by TLS. etcd is not yet given TLS: + keep ports 2379 and 2380 to the cluster's machines. +- **A capacity fallback respects the key's allowed models, and is charged + at its own price.** When the model asked for had no capacity, the next in + its `fallbacks` served the request even if the key was not allowed it, and + the request was priced as the model asked for. Fallbacks a key may not use + are now skipped, and a request is charged what the model that answered + costs. +- **Suspending an account stops its keys.** Suspension closed the person's + sessions and left every API key they had made working. A key whose owner's + account is not active — suspended, pending, or gone — is now refused as a + revoked key is, within `auth.cache_ttl`, and works again when the account + is reinstated. +- **A join code is a credential only for its hour, and only on its + address.** The setup checked a code's expiry on the joining side alone, so + a code stayed good for as long as the setup ran; it now refuses an expired + one itself. Its listener binds the address the codes name rather than + every interface, and a joined machine is known by the address it connected + from, not the one it claimed. +- **The setup's sudo grant cannot outlive it.** Given a sudo password, the + setup writes a temporary `NOPASSWD` grant and removes it when it + finishes; a setup that crashed or was interrupted left it for good. The + grant now schedules its own removal within the hour as it is written, and + the password is dropped once the grant stands in for it. An address given + to the system ssh can no longer be read as one of its options. +- **Made-up keys cannot fill memory or the database.** Every well-formed key + presented, real or not, was cached for good and cost a database read the + first time. The cache is now bounded, and keys not in it are looked up at + most 20 a second per client address (50 at once); beyond that the request + is answered 429 without a read. +- **Slow or idle connections cannot pile up.** The servers bounded only how + long headers took. Every listener now also bounds how long a connection + may sit idle and how large its headers may be, and reading a request + (5 minutes on the client port, a minute or less elsewhere — never on the + agents' long-lived streams); the client port holds at most + `max_connections` (10,000) at once, and a message from an agent at most + 16 MB. +- **The sign-in and signup forms resist scripts.** Ten wrong passwords from + anywhere locked an address's owner out for the window; attempts are now + counted per address and client, so somebody guessing elsewhere cannot lock + the owner out, with a looser count per address alone so guesses spread + over many clients are still bounded. The sign-in form carries a token its + cookie matches, so another site cannot sign a visitor into an account of + its choosing. Posting either form is rationed per client address (sign-in + 30 a minute, signup 5), and a password hash that cannot get a turn within + ten seconds answers "busy" instead of holding the request. `server init` + now writes `signup_open: false`. +- **Forwarding headers are believed only from the proxies named.** + `X-Forwarded-Host` and `-Proto` were taken from anyone, and decide the + gateway address written into the `/setup` scripts clients pipe to a + shell; behind a caching proxy one request could poison them for others. + They — and `X-Forwarded-For`, now used for the per-address limits — are + believed only from `router.trusted_proxies` (addresses or CIDR prefixes). + **Behind a reverse proxy, set it**, or the pages and scripts name the + address the proxy reached and every client counts as the proxy. `/setup` + scripts are served `no-store`. +- **Removing a node revokes its certificate.** `ctl node remove` only + dropped the node from the inventory; its certificate, valid for a year, + still connected and was given work. Now the removal is recorded in the + store and the agent port refuses any certificate issued for that node + before it — onboarding the node again issues one that works. A mirror + serving its management API with `--tls-ca` now requires `--api-client` + (the control plane's name), since the same authority signs every node. +- **Engines listen on the management address only**, the one the control + plane reaches them on, instead of every address the node has — a public + one included. (When that address is a name, or not one of the node's, + they still listen on all.) **SGLang's key is off its command line**, + where every user on the node could read it: it reaches the engine in its + environment, and a short launcher hands it to SGLang inside the process. + Both take effect as engines restart under an upgraded agent. +- **Backups can be encrypted.** `backup_recipients` in server.yaml (age + recipients or SSH public keys) has `llmgw server backup` encrypt each + archive to them with age, as `.tar.gz.age`: an archive holds the CA's key + and every secret in the store, and is meant to be copied off the machine. + Open one with `age -d -i `. +- **PyTorch's index gives torch and nothing else.** vLLM's CUDA 12.9 build + was resolved with PyTorch's index beside PyPI and `unsafe-best-match`, so + either index could supply any package — how torchtriton was hijacked in + 2022. Now that index is explicit, named only for torch, torchvision and + torchaudio; every other package comes from PyPI alone, and the lock is + PEP 751's (`pylock.llmgw.toml`), pinning each file's address as well as + its hash. Checked against vLLM 0.30.0: the same 195 packages resolve. +- **The control plane's own fetches stay off link-local addresses.** + Checking a model's source and reading a GGUF's header went wherever the + URL said, cloud instance metadata (169.254.169.254) included. They now + refuse link-local, multicast and unspecified addresses, while still + reaching the cluster's network, where the mirror is. The mirror's fetches, + which refuse private addresses too, now also see through IPv6 addresses + carrying an IPv4 one (NAT64, 6to4, IPv4-compatible), and through a proxy: + the target's addresses are checked before the request is handed to it. +- **A CPU-only node reporting a problem no longer panics the agent + handler**, and **thread ids are bounded** to 128 letters, digits and + `- _ . :`. A thread is readable by every key of its project that knows + the id; the docs now say to use unguessable ids. +- **`/v1/models` lists only what the key may use**, rather than every + model the gateway serves to anyone. +- **Portal sessions end after a day unused** (still at most 14 days in + all), and expired or idle sessions and used or expired key claims are + deleted hourly — they were refused, but never removed. +- **Every page carries a Content-Security-Policy**: scripts only from the + gateway itself and the page's own inline ones, each named by its hash — + no inline handlers, no eval, nothing a model's reply or a model card + could slip into a page. The few `onchange`/`onsubmit` attributes moved + to `/_static/pages.js`, htmx runs with `allowEval: false`, and the live + pages' "only while visible" refresh no longer needs an evaluated filter. + Pages now carry their headers even when answered with an error status. +- **KaTeX 0.16.47** (from 0.16.11), past the advisories fixed in 0.16.21. +- **Smaller hardening.** The mirror no longer serves its bookkeeping files + (a file's `.source` URL and `.sha256`) on `:8081`. A GGUF header nesting + arrays more than 4 deep is refused rather than recursed into. Serve and + update jobs keep the last 100 finished, and the mirror forgets finished + downloads after a week, instead of remembering every one until restart. +- **A firewall for llmgw's internal ports.** `server init` (and the store + members it sets up with `--patroni`), `node add`, `server add` and + `mirror init` now keep the machine's internal ports — the agents' stream, + engines, the mirror, PostgreSQL, Patroni, etcd, node exporter — to the + cluster's own networks, in an nftables table of llmgw's own loaded by + `llmgw-firewall.service`. Only those ports are touched: the table accepts + everything else, so other services on the machine and any firewall it + already has are left as they were. Extra networks go in + `firewall_networks`; `--no-firewall` skips it; `llmgw doctor` says when a + node lacks it. +- **etcd and Patroni's API speak TLS.** etcd's peers and clients use + certificates from the cluster's authority, and a client must present one; + Patroni reaches it with its member's certificate and serves its API over + TLS. `server init --patroni`, run again, moves an existing cluster: with + Patroni in maintenance mode, so PostgreSQL serves on and nothing fails + over, etcd is rebuilt on TLS and Patroni's keys carried into it. The + control plane asks the API over HTTPS, verified, and over HTTP only where a + member has not moved yet. +- **A cluster keeping its access rules per member got no `hostssl`.** The + TLS step looked only at the shared configuration, and on a cluster set up + before the rules moved there said the rules were `hostssl` while + plaintext was still accepted. Each member's own rules are now made + `hostssl` too. + + +### Fixed + +- **An engine that died after an agent restart went unnoticed.** The watcher + that reports a ready engine dying was tied to the stream it was started + on, so after a reconnect nothing reported it, and an engine adopted from + the previous agent had no watcher at all: its replica stayed "ready" and + was routed to. Every connection now watches every ready shard, started or + adopted. Found on the PoC: a killed engine on node06 stayed ready for + minutes. +- `server init` rewriting the store's address no longer leaves + `/etc/llmgw/secrets` unreadable to the service when it was made 0700 for + the service user, which kept the gateway from starting. ### Everything that can be changed on a running cluster, from the console `sudo llmgw` only watched the cluster and edited the configuration files; diff --git a/Makefile b/Makefile index 890fc11..ac7040b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ MODULE := gitea.mynodes.xyz/ilker/llmgw VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev) LDFLAGS := -X '$(MODULE)/internal/version.Version=$(VERSION)' -.PHONY: build test race lint fmt clean rename proto proto-tools +.PHONY: build test race lint fmt vuln check clean rename proto proto-tools build: go build -ldflags "$(LDFLAGS)" -o bin/llmgw ./cmd/llmgw @@ -20,6 +20,17 @@ fmt: go fmt ./... go vet ./... +# Known vulnerabilities reachable from this code, in the standard library and +# in dependencies. A toolchain left behind is the commonest cause: the go +# directive in go.mod is what fixes it. +vuln: + go run golang.org/x/vuln/cmd/govulncheck@latest ./... + +# What has to pass before a change is merged. +check: test + go vet ./... + $(MAKE) vuln + # Regenerate the agent protocol. Generated code is committed, so this only # needs running when api/proto changes. proto: proto-tools diff --git a/deploy/examples/server.yaml b/deploy/examples/server.yaml index a24e4e7..5f6edf4 100644 --- a/deploy/examples/server.yaml +++ b/deploy/examples/server.yaml @@ -17,8 +17,10 @@ agent_listen: ":7443" # Mutual TLS on the agent port (plan §5.6). With it, a node's identity is taken # from its certificate's common name and an agent claiming a different node id -# is refused. Without it the port is plaintext HTTP/2 and anyone who can reach -# it can claim to be any node, so it must stay on a trusted network. +# is refused. Without it the port would be plaintext HTTP/2 and anyone who can +# reach it could claim to be any node, so the control plane refuses to start +# unless `agent_insecure: true` is set — and then listens on loopback unless +# agent_listen names an address. That is for one machine or a test. # # These certificates come from llmgw's own authority, not a public one: public # authorities stopped issuing the clientAuth usage, so no Let's Encrypt diff --git a/deploy/mirror/README.md b/deploy/mirror/README.md index 93286b7..44d3bf6 100644 --- a/deploy/mirror/README.md +++ b/deploy/mirror/README.md @@ -65,9 +65,13 @@ hand it certificates: llmgw mirror serve --dir /srv/models --base http://models.internal:8081 \ --tls-ca /etc/llmgw/ca.pem \ --tls-cert /etc/llmgw/mirror.pem \ - --tls-key /etc/llmgw/mirror-key.pem + --tls-key /etc/llmgw/mirror-key.pem \ + --api-client control-plane ``` +`--api-client` is required with `--tls-ca`: it names the one certificate the +API answers, because the CA signs every node's certificate as well. + That is the same CA the agents already use, so the certificates come from the authority you have: diff --git a/docs/install.md b/docs/install.md index b31945c..64aa014 100644 --- a/docs/install.md +++ b/docs/install.md @@ -78,7 +78,7 @@ llmgw --version `CGO_ENABLED=0` deliberately, so a binary built today runs on a node whose libc is older than the builder's. -To build instead of download, you need Go 1.26 or later and nothing else: +To build instead of download, you need Go 1.26.6 or later and nothing else (an older Go fetches it itself): ``` git clone https://gitea.mynodes.xyz/ilker/llmgw && cd llmgw @@ -152,6 +152,15 @@ promoted within about thirty seconds of the primary being lost, and the control plane follows it by itself. `llmgw ctl status` says who leads; see [deploy/ha](../deploy/ha/README.md) for what it sets up. +Everything between the machines is TLS, with certificates from the cluster's +own authority: PostgreSQL (every remote rule is `hostssl`, and the control +plane verifies the member it reaches), etcd (peers and clients, and a client +must present a certificate), and Patroni's API, which also asks for a +password before a change. A cluster set up by an earlier llmgw is moved to +this by running the same command again, on the control plane that holds the +authority: etcd is rebuilt on TLS with Patroni in maintenance mode, so +PostgreSQL serves throughout and no failover happens. + Every step looks before it acts. Running it again finishes an install that stopped halfway and changes nothing on one that did not; it never overwrites `server.yaml`, the authority, or an existing account's password. @@ -184,7 +193,7 @@ forwarded headers right; both are attached to every release. ``` curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/health # 200 -llmgw ctl status +sudo llmgw ctl status ``` `ctl status` will report no nodes and no models, which is correct — nothing has @@ -196,9 +205,15 @@ password, change it, and issue yourself a key from the profile page. `/etc/llmgw/server.yaml` is yours from here on. Three things in it are worth knowing before you edit it: -- `admin_listen` is on loopback on purpose. It serves pprof, which can dump the - process heap, and API keys sit in that heap in plaintext. To see the dashboard - from elsewhere use `/admin` on the client port — do not move this listener. +- `admin_listen` is on loopback on purpose, and the server refuses any other + address unless `admin_remote: true`. Its API under `/api` adds models and + upgrades nodes, so it also wants a token: the server writes one, 0600, to + `/var/lib/llmgw/admin-token` the first time it starts, and `llmgw ctl` + reads it from there — which is why `llmgw ctl` runs with sudo. + `LLMGW_ADMIN_TOKEN` gives it from elsewhere. pprof, whose heap holds the API + keys requests presented, is off unless `debug_pprof: true`, and then behind + the token too. To see the dashboard from elsewhere use `/admin` on the + client port — do not move this listener. - `default_rpm`, `default_tpm` and `default_price` are what make limits and budgets mean anything. Without a price every request costs zero and a budget never binds; without the defaults, a project created without limits is @@ -263,17 +278,20 @@ it can start downloads and delete models, and only the control plane needs it. The split is what lets you lock the second one down without re-certifying every agent that fetches from the first. -Give the management port the cluster's own CA and it takes client certificates: +Give the management port the cluster's own CA and it takes client certificates — +the control plane's, named with `--api-client`, and no other. The same CA signs +every node's certificate, so the name is required: without it any node, a +removed one's included, could delete models. ``` llmgw mirror serve --dir /srv/models --base http://models.internal:8081 \ --tls-ca /etc/llmgw/ca.pem \ - --tls-cert /etc/llmgw/mirror.pem --tls-key /etc/llmgw/mirror-key.pem + --tls-cert /etc/llmgw/mirror.pem --tls-key /etc/llmgw/mirror-key.pem \ + --api-client control-plane ``` -Without those three flags the management port is open to anyone who can reach -it, and the mirror says so in its own log at every start rather than letting you -find out later. +Without those flags the management port answers this machine only, and the +addresses given with `--api-allow`. ### The layout @@ -995,6 +1013,10 @@ curl $BASE/v1/chat/completions \ -d '{"model": "", "messages": [{"role":"user","content":"and then?"}]}' ``` +A thread belongs to the project, not to the key: **any key of the project that +knows the id reads the conversation.** Use ids nobody can guess — a UUID, not +`my-conversation` — of at most 128 letters, digits and `- _ . :`. + llmgw puts the stored history in front of that turn and sends it to whichever model is serving — **including a different one from the one the conversation started on**. That is the point: a thread begun on a large model can continue @@ -1058,6 +1080,39 @@ thing to get right is the health check: point it at `/ready`, not `/health`. so a load balancer using it would send half the traffic to something that refuses every request. +## The firewall + +`server init`, `node add`, `server add` and `mirror init` each keep that +machine's **internal** llmgw ports to the cluster's own networks, with an +nftables table of llmgw's own (`inet llmgw`, in `/etc/llmgw/firewall.nft`, +loaded at boot by `llmgw-firewall.service`): + +| Machine | Ports, for the cluster only | +| --- | --- | +| control plane | 7443 (agents), metrics when not on loopback, 8081 (its mirror), engines and 9100 when it is a node, 2379/2380 when it holds the store's third etcd vote | +| node | engine ports (`control.port_range`, 20000–20999), 9100 | +| store member | 5432, 8008, 2379, 2380, 9100 | +| mirror | 8081, 8082, 9100 | + +"The cluster's networks" are, on each machine, the network of the interface +it reaches the control planes (or the other store members) on — or just +their address, when the route goes through a gateway. Anything else that +must reach those ports, a Prometheus on another subnet or nodes behind a +router, goes in `server.yaml` before the machines are set up: + +```yaml +firewall_networks: [10.0.5.0/24, 192.168.1.20] +``` + +Only those ports are touched. The table's policy is to accept, so whatever +else the machine serves — and whatever firewall it already has — is left as +it was; the client API (`:8080`) is not closed, being meant to be reached. +Running a command again adds to what is there rather than replacing it, so a +machine that is both a store member and a node keeps both sets. `llmgw +doctor` on a node says when the table is missing. `--no-firewall` on any of +the four commands leaves the firewall alone; `sudo systemctl disable --now +llmgw-firewall` removes it. + ## Upgrading On the control plane: @@ -1178,6 +1233,20 @@ The control plane publishes when the newest archive was written as `llmgw_backup_last_success_timestamp_seconds`, and **BackupTooOld** fires when it is more than 26 hours old — including when there has never been one. +**Encrypt them.** Name who may open them in `/etc/llmgw/server.yaml`, and every +archive is encrypted to those keys with [age](https://age-encryption.org) and +named `.tar.gz.age`: + +```yaml +backup_recipients: + - age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p # age-keygen's public key + - ssh-ed25519 AAAAC3Nza... ops@example # or an SSH public key +``` + +Keep the matching private keys **off** this machine — an archive whose key +sits beside it is not protected by it. Encrypted, a copy is safe wherever it +lands. + **Copy the archives off the machine.** They sit on the disk they back up and do not survive losing it. Anything that copies files will do — rsync from the mirror, a NAS mount, an offsite bucket — as long as it keeps the archives @@ -1197,6 +1266,8 @@ them: ``` sudo systemctl stop llmgw-server mkdir /tmp/restore && sudo tar -xzf /var/backups/llmgw-server/llmgw-backup--
+ diff --git a/internal/web/templates/admin_mirror.html b/internal/web/templates/admin_mirror.html index 7ebb49e..be0bacc 100644 --- a/internal/web/templates/admin_mirror.html +++ b/internal/web/templates/admin_mirror.html @@ -4,6 +4,7 @@ {{/* Only while something is downloading. A reload at any other time would take with it whatever the operator was typing into the form below. */}} {{if .RefreshSec}}{{end}} + Mirror · llmgw
@@ -77,7 +78,7 @@ reachable from this machine.

- @@ -114,7 +115,7 @@ reachable from this machine.

{{if and $.HasCatalog (not .UsedBy)}}{{if .Serve}}
close {{else}}serve{{end}}{{end}} + class="inline" data-confirm="Remove {{.Model}}/{{.Version}} and every file in it?{{if .UsedBy}} It is in use by the catalog.{{end}}"> @@ -288,7 +289,7 @@ document.addEventListener('click', function (e) { {{if .Of}}{{end}} {{end}} - + {{end}} @@ -579,8 +580,7 @@ computes once it has it.

- diff --git a/internal/web/templates/admin_people.html b/internal/web/templates/admin_people.html index 51357b6..af55a6b 100644 --- a/internal/web/templates/admin_people.html +++ b/internal/web/templates/admin_people.html @@ -6,7 +6,7 @@ People · llmgw - +
diff --git a/internal/web/templates/chat.html b/internal/web/templates/chat.html index c957beb..1a00111 100644 --- a/internal/web/templates/chat.html +++ b/internal/web/templates/chat.html @@ -5,6 +5,7 @@ +
@@ -19,7 +20,7 @@ message rather than trusting the page. */}}
diff --git a/internal/web/templates/dashboard.html b/internal/web/templates/dashboard.html index 37b2f28..f15ccb8 100644 --- a/internal/web/templates/dashboard.html +++ b/internal/web/templates/dashboard.html @@ -2,7 +2,9 @@ + + llmgw — cluster
@@ -19,7 +21,7 @@ every few seconds threw away the scroll position of anyone reading the bottom of it. Paused while the tab is hidden. Without script, the meta refresh in the head does the old thing. */}} -
+
{{if .HasCharts}}
{{.Requests24h}}
requests · 24h
{{.RequestSpark}}
diff --git a/internal/web/templates/node.html b/internal/web/templates/node.html index 5a60a1a..9fd7d84 100644 --- a/internal/web/templates/node.html +++ b/internal/web/templates/node.html @@ -2,7 +2,9 @@ + + llmgw — {{.Node.ID}}
@@ -14,7 +16,7 @@
{{/* Refreshed in place, like the cluster page. */}} -
+
{{with .Node}}
{{with .Load}} diff --git a/internal/web/web.go b/internal/web/web.go index e870014..fb7434f 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -14,6 +14,7 @@ package web import ( + "bytes" "crypto/sha256" "embed" "encoding/hex" @@ -260,12 +261,23 @@ func (n noDirs) Open(name string) (fs.File, error) { } func render(w http.ResponseWriter, name string, data any) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - if err := tmpl.ExecuteTemplate(w, name, data); err != nil { - // The status is already written by then, so there is nothing honest to - // send; the page simply ends short. + renderStatus(w, http.StatusOK, name, data) +} + +// renderStatus is render with a status other than 200. +func renderStatus(w http.ResponseWriter, code int, name string, data any) { + // Rendered whole before anything is sent: the policy names the page's + // inline scripts by their hashes, and a template that fails halfway is + // an error page rather than half a page. + var page bytes.Buffer + if err := tmpl.ExecuteTemplate(&page, name, data); err != nil { http.Error(w, "template error", http.StatusInternalServerError) + return } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + securePage(w.Header(), &page) + w.WriteHeader(code) + _, _ = page.WriteTo(w) } // humanLatency writes a mean latency the way a person reads one: 5 ms, 8.8 s, diff --git a/internal/web/web_test.go b/internal/web/web_test.go index 3ef165a..f8d3b8b 100644 --- a/internal/web/web_test.go +++ b/internal/web/web_test.go @@ -12,6 +12,7 @@ import ( "testing" "time" + "gitea.mynodes.xyz/ilker/llmgw/internal/clientip" "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" "gitea.mynodes.xyz/ilker/llmgw/internal/control/fabrics" "gitea.mynodes.xyz/ilker/llmgw/internal/store" @@ -65,6 +66,7 @@ func TestLandingPageWarnsWhenTheGatewayIsOpen(t *testing.T) { } func TestTheExampleURLSurvivesAReverseProxy(t *testing.T) { + trustTestProxy(t) // The process sees http and an internal host; the client used https and a // public name. An example naming the internal address is one nobody can // paste anywhere. @@ -81,6 +83,7 @@ func TestTheExampleURLSurvivesAReverseProxy(t *testing.T) { } func TestForwardedHeaderChainsTakeTheFirstEntry(t *testing.T) { + trustTestProxy(t) // A chain of proxies appends rather than replaces. _, body := get(t, Landing("v1", true, false, "", ""), "http://internal/", map[string]string{ "X-Forwarded-Proto": "https, http", @@ -582,6 +585,7 @@ func TestEveryClientExampleIsInThePageItself(t *testing.T) { } func TestAHostileForwardedHostCannotWriteMarkup(t *testing.T) { + trustTestProxy(t) // Nothing overwrites X-Forwarded-Host when the gateway is reached directly, // so its value is whatever the caller sent, and it is now reflected into // several places on the page rather than one. Escaping is the template's @@ -687,3 +691,22 @@ type emptyFabrics struct{ fakeSource } func (emptyFabrics) Summary() adminapi.Summary { return adminapi.Summary{} } func (emptyFabrics) Fabrics() fabrics.Report { return fabrics.Report{} } + +// trustTestProxy believes forwarding headers from where httptest's requests +// come from, as a deployment behind a proxy it names would. +func trustTestProxy(t *testing.T) { + t.Helper() + clientip.SetTrusted([]string{"192.0.2.1"}) + t.Cleanup(func() { clientip.SetTrusted(nil) }) +} + +func TestForwardingHeadersFromAnUntrustedClientAreIgnored(t *testing.T) { + clientip.SetTrusted(nil) + _, body := get(t, Landing("v1", true, false, "", ""), "http://llmgw.example/", map[string]string{ + "X-Forwarded-Proto": "https", + "X-Forwarded-Host": "attacker.example", + }) + if strings.Contains(body, "attacker.example") || !strings.Contains(body, "http://llmgw.example/v1") { + t.Error("a client's own X-Forwarded-Host decided the address on the page") + } +} diff --git a/test/e2e/signup_test.go b/test/e2e/signup_test.go index b57aa3c..31b0a7c 100644 --- a/test/e2e/signup_test.go +++ b/test/e2e/signup_test.go @@ -54,6 +54,12 @@ func (b *browser) get(path string) (int, string) { func (b *browser) post(path string, form url.Values) (int, string, string) { b.t.Helper() + // The sign-in form carries a token its page set, as a browser's would. + if path == "/login" && form.Get("csrf") == "" { + _, page := b.get("/login") + form = cloneValues(form) + form.Set("csrf", csrf(b.t, page)) + } req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, b.base+path, strings.NewReader(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") @@ -613,3 +619,11 @@ func TestChatSendNeedsItsToken(t *testing.T) { } } } + +func cloneValues(v url.Values) url.Values { + out := url.Values{} + for k, vs := range v { + out[k] = append([]string{}, vs...) + } + return out +}