From cddb939db6e88ae7d42ed3780bd9458775aa7ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 28 Oct 2026 19:49:48 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20deploy:=20trusted=20proxies=20be?= =?UTF-8?q?hind=20Ferron,=20tokens=20out=20of=20access=20logs,=20tighter?= =?UTF-8?q?=20systemd=20unit,=20example=20port=20matches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++---- config/im.example.toml | 7 ++++--- deploy/ferron.kdl | 5 ++++- deploy/im.service | 10 ++++++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6c2ff75..dc8609b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The settings that matter first: | `data_dir` | Holds the instance key `im.key`, the photos, and the backups. Created with mode 0700. | | `listen` | The loopback address `im` binds to, for example `127.0.0.1:7650`. | | `base_url` | The public origin, with no trailing slash. This is the OpenID issuer and the origin of every link in mail. | -| `trusted_proxies` | The proxies whose `X-Forwarded-For` is believed. Leave empty when nothing is in front of `im`. | +| `trusted_proxies` | The proxies whose `X-Forwarded-For` is believed. Behind Ferron on the same host, `["127.0.0.1/32"]`; empty only when nothing is in front of `im`, otherwise every visitor shares the proxy's address and one visitor's failed sign-ins throttle everyone. The proxy must append the client address to `X-Forwarded-For`, never pass a client's own header through unchanged. | Secrets never go in the file. Client secrets are shown once and stored as hashes. The SMTP password is read from a file named in the configuration, and the instance key is created on first run. @@ -176,15 +176,20 @@ only to `/var/lib/im`. Create the user and the directories, copy the binary and ```sh useradd --system --home /var/lib/im --shell /usr/sbin/nologin im install -d -o im -g im -m 0700 /var/lib/im -install -d -m 0750 /etc/im -install -m 0640 config/im.toml /etc/im/im.toml +install -d -o root -g im -m 0750 /etc/im +install -o root -g im -m 0640 config/im.toml /etc/im/im.toml install -m 0644 deploy/im.service /etc/systemd/system/im.service systemctl daemon-reload systemctl enable --now im ``` Put a TLS-terminating reverse proxy in front of it. `deploy/ferron.kdl` is an example Ferron site that -forwards to `127.0.0.1:7650`; `base_url` must be the same public origin. +forwards to `127.0.0.1:7650`; `base_url` must be the same public origin. Three things matter in the proxy: + +- Redirect plain HTTP to HTTPS. `im` sends HSTS itself when `base_url` is `https://`. +- Do not buffer responses: the live update streams depend on it. +- Keep secrets out of the access log. Reset, invitation and email-change links carry a one-time token in + the path (`/reset/…`, `/invite/…`, `/email/…`), so log without the path for those prefixes, or not at all. ## Layout diff --git a/config/im.example.toml b/config/im.example.toml index d8052d9..5cb8eed 100644 --- a/config/im.example.toml +++ b/config/im.example.toml @@ -9,13 +9,14 @@ database = "data/im.db" data_dir = "data" # Address the HTTP server binds to. Put a TLS-terminating reverse proxy in front of it in production. -listen = "127.0.0.1:8080" +listen = "127.0.0.1:7650" # Public origin, with no trailing slash. This is the OpenID issuer and the origin of links in mail. -base_url = "http://localhost:8080" +base_url = "http://localhost:7650" # Proxies whose X-Forwarded-For header is believed, as addresses or CIDR networks. Empty means the -# TCP peer is always the client address. +# TCP peer is always the client address. Behind a reverse proxy on the same host, set ["127.0.0.1/32"]: +# left empty there, every visitor counts as 127.0.0.1, and one visitor's failed sign-ins throttle all. trusted_proxies = [] [mail] diff --git a/deploy/ferron.kdl b/deploy/ferron.kdl index 11044d9..6485726 100644 --- a/deploy/ferron.kdl +++ b/deploy/ferron.kdl @@ -1,6 +1,9 @@ // Example Ferron site for `im`. Ferron terminates TLS and forwards to the loopback listener of `im`. // Check the directive names against the Ferron version you run; this file shows the shape, it has not been -// run against a live Ferron here. `base_url` in im.toml must be this same origin. +// run against a live Ferron here. `base_url` in im.toml must be this same origin, and `trusted_proxies` +// must be ["127.0.0.1/32"]. In the real site also: redirect HTTP to HTTPS, do not buffer responses (the +// live streams need it), append the client to X-Forwarded-For, and keep /reset/, /invite/ and /email/ paths +// out of the access log: they carry one-time tokens. im.example.com { proxy http://127.0.0.1:7650 } diff --git a/deploy/im.service b/deploy/im.service index b1e314a..0b22d41 100644 --- a/deploy/im.service +++ b/deploy/im.service @@ -12,6 +12,8 @@ Group=im Environment=IM_CONFIG=/etc/im/im.toml ExecStart=/usr/local/bin/im serve WorkingDirectory=/var/lib/im +StateDirectory=im +StateDirectoryMode=0700 Restart=on-failure RestartSec=2 LimitNOFILE=65536 @@ -39,6 +41,14 @@ LockPersonality=yes MemoryDenyWriteExecute=yes SystemCallArchitectures=native SystemCallFilter=@system-service +SystemCallFilter=~@privileged +ProtectProc=invisible +ProcSubset=pid +RemoveIPC=yes +PrivateMounts=yes +KeyringMode=private +# A photo decode may use up to 96 MiB and two run at once; this leaves room for that and the rest. +MemoryMax=1G CapabilityBoundingSet= AmbientCapabilities= UMask=0077