neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Client addresses behind a reverse proxy, recorded for staff with retention
a4db355 · gmorell · 2026-12-03 03:02
Message
{commit_body(@commit)}
Files changed
modified
README.md
+47
−0
| 101 | 101 | - **Log:** every staff action, from the web or the CLI, is in |
| 102 | 102 | `/admin/log` (also per user). |
| 103 | 103 | |
| 104 | +### Client addresses | |
| 105 | + | |
| 106 | +**What is collected:** the address an account used when it: | |
| 107 | + | |
| 108 | +- signed in (password or single sign-on); | |
| 109 | +- registered (redeeming an invite); | |
| 110 | +- started a topic or replied; | |
| 111 | +- flagged a post; | |
| 112 | +- used the app (at most one update per address every ten minutes). | |
| 113 | + | |
| 114 | +That gives one row per account and address (first and last seen, a count, the | |
| 115 | +last action), plus the address on each flag. | |
| 116 | + | |
| 117 | +**Why:** spotting a sock-puppet or a returning suspended member. A flag queue | |
| 118 | +item says when the author shares an address with other accounts. | |
| 119 | + | |
| 120 | +**Who sees it:** staff only, and every look is logged. | |
| 121 | + | |
| 122 | +- The address list on `/admin/users/{id}` sits behind a button that writes | |
| 123 | + to the moderation log. It shows the other accounts at each address and | |
| 124 | + whether they are read-only or suspended. | |
| 125 | +- `/admin/ips/{ip}` also lists the same /24 (IPv4) or /64 (IPv6), as a lead, | |
| 126 | + not proof. It is logged too. | |
| 127 | +- Addresses never appear on members' pages or in `/api/v1`. | |
| 128 | + | |
| 129 | +**How long:** rows not seen for `IP_RETENTION_DAYS` (default 180) are deleted, | |
| 130 | +and older flags lose their address. The server purges at start and hourly; | |
| 131 | +`manage ips purge [--days N]` does it on demand. 180 days covers several | |
| 132 | +cooldown or suspension cycles (a cooldown is at most 90 days), which is when | |
| 133 | +a returning account matters, without keeping a history of where everyone has | |
| 134 | +been. | |
| 135 | + | |
| 136 | +**Behind a proxy:** the server reads the TCP peer. Only when that peer is in | |
| 137 | +`TRUSTED_PROXIES` does it take the client from `X-Forwarded-For`: the | |
| 138 | +rightmost entry that isn't itself a trusted proxy, since everything to its | |
| 139 | +left is whatever the client sent. With no `X-Forwarded-For` it falls back to | |
| 140 | +`X-Real-IP`. An untrusted peer's headers are ignored. This is angler's | |
| 141 | +`TRUSTED_PROXIES` and gitgud's rule. | |
| 142 | + | |
| 143 | +The default here is to trust nothing, unlike angler and gitgud, whose | |
| 144 | +defaults are loopback and the private ranges. Behind nginx on the same host | |
| 145 | +set `127.0.0.1/32`; behind a cluster ingress, its pod range (often | |
| 146 | +`10.0.0.0/8`). Otherwise every client looks like the proxy. | |
| 147 | + | |
| 104 | 148 | ## Configuration (environment) |
| 105 | 149 | |
| 106 | 150 | | Var | Default | Meaning | |
| 118 | 162 | | `FLAG_HIDE_RATIO` | `0.1` | Auto-hide a post when distinct counted flaggers ÷ readers of it reaches this (0–1]; see Moderation | |
| 119 | 163 | | `FLAG_HIDE_MIN_FLAGS` | `2` | …and at least this many flaggers (one person never hides a post) | |
| 120 | 164 | | `FLAG_HIDE_ENOUGH_FLAGS` | `10` | …or this many, whatever the readership | |
| 165 | +| `TRUSTED_PROXIES` | _(none)_ | Comma-separated CIDRs of reverse proxies whose `X-Forwarded-For` / `X-Real-IP` is believed, e.g. `127.0.0.1/32` (nginx on the host), `10.0.0.0/8` (a cluster ingress). See Client addresses | | |
| 166 | +| `IP_RETENTION_DAYS` | `180` | How long client addresses are kept | | |
| 121 | 167 | | `MAIL_PRISM_URL` / `MAIL_PRISM_API_KEY` | _(unset)_ | Send mail through mail-prism. Unset (or blank): mail is only logged | |
| 122 | 168 | | `MAIL_FROM_ADDRESS` | _(the key's application address)_ | Must equal the mail-prism application address if set | |
| 123 | 169 | | `MAIL_FROM_NAME` | `APP_NAME` | From display name | |
| 252 | 298 | - `manage user list | set-staff <name> [--revoke] | set-trust <name> <0-4> | activate <name> | deactivate <name> | delete <name> --yes` |
| 253 | 299 | - `manage user verify-email <name> | resend-verification <name>` |
| 254 | 300 | - `manage user cooldown <name> <12h|5d|2w> --reason R [--by STAFF]` (≤ 90 days), `suspend <name> [--reason R] [--hours N | --days N] [--by STAFF]`, `end-cooldown|unsuspend <name>`, `flag-shadowban|unflag-shadowban <name> [--reason R]` — logged as `cli` (and the `--by` account) in the moderation log |
| 301 | + - `manage ips purge [--days N]` — delete client addresses older than `IP_RETENTION_DAYS` | |
| 255 | 302 | - `manage invite create [--email E] [--days N] | list | show <token>` |
| 256 | 303 | - `manage email test <to>` — send a test message through mail-prism (or report it isn't configured) |
| 257 | 304 | - `manage seed [--demo [--allow-public]]` — baseline categories/reactions/tags; `--demo` adds demo users and content |
modified
compose.yml
+7
−0
| 33 | 33 | # Also the base of email-verification links and the OIDC redirect URI. |
| 34 | 34 | # PUBLIC_URL: https://recourse.example.com |
| 35 | 35 | # |
| 36 | + # Behind nginx proxy_pass or a cluster ingress: the proxies whose | |
| 37 | + # X-Forwarded-For is believed (comma-separated CIDRs). Unset: none, and | |
| 38 | + # every request is from the proxy's address. Client addresses are kept | |
| 39 | + # IP_RETENTION_DAYS (staff only; README "Client addresses"). | |
| 40 | + # TRUSTED_PROXIES: 127.0.0.1/32,10.0.0.0/8 | |
| 41 | + # IP_RETENTION_DAYS: "180" | |
| 42 | + # | |
| 36 | 43 | # Email verification mail through mail-prism. Unset: links are only |
| 37 | 44 | # written to this container's log (development). |
| 38 | 45 | # MAIL_PRISM_URL: http://mail-prism-svc.mail-prism |
modified
migration/src/lib.rs
+2
−0
| 19 | 19 | mod m20240101_000017_layout_names; |
| 20 | 20 | mod m20240101_000018_api_token_hash; |
| 21 | 21 | mod m20240101_000019_moderation; |
| 22 | +mod m20240101_000020_user_ips; | |
| 22 | 23 | |
| 23 | 24 | pub struct Migrator; |
| 24 | 25 |
| 49 | 50 | Box::new(m20240101_000017_layout_names::Migration), |
| 50 | 51 | Box::new(m20240101_000018_api_token_hash::Migration), |
| 51 | 52 | Box::new(m20240101_000019_moderation::Migration), |
| 53 | + Box::new(m20240101_000020_user_ips::Migration), | |
| 52 | 54 | ] |
| 53 | 55 | } |
| 54 | 56 | } |
added
migration/src/m20240101_000020_user_ips.rs
+42
−0
| 1 | +//! Client addresses, for staff only: where each account has been seen | |
| 2 | +//! (sign-in, registration, posting, flagging, app use), and the address a | |
| 3 | +//! flag came from. Kept `IP_RETENTION_DAYS` (src/ips.rs purges). | |
| 4 | +//! | |
| 5 | +//! `ip` is the text form of the address (`inet` casts in queries look up | |
| 6 | +//! subnets); one row per (user, address). | |
| 7 | +use sea_orm_migration::prelude::*; | |
| 8 | + | |
| 9 | +#[derive(DeriveMigrationName)] | |
| 10 | +pub struct Migration; | |
| 11 | + | |
| 12 | +const UP: &str = r#" | |
| 13 | +CREATE TABLE user_ips ( | |
| 14 | + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, | |
| 15 | + ip VARCHAR(45) NOT NULL, | |
| 16 | + first_seen TIMESTAMPTZ NOT NULL, | |
| 17 | + last_seen TIMESTAMPTZ NOT NULL, | |
| 18 | + count INTEGER NOT NULL DEFAULT 1, | |
| 19 | + last_action VARCHAR(16) NOT NULL, | |
| 20 | + PRIMARY KEY (user_id, ip) | |
| 21 | +); | |
| 22 | +CREATE INDEX idx_user_ips_ip ON user_ips (ip); | |
| 23 | +CREATE INDEX idx_user_ips_last_seen ON user_ips (last_seen); | |
| 24 | + | |
| 25 | +ALTER TABLE post_flags ADD COLUMN ip VARCHAR(45) NULL; | |
| 26 | +"#; | |
| 27 | + | |
| 28 | +const DOWN: &str = r#" | |
| 29 | +ALTER TABLE post_flags DROP COLUMN IF EXISTS ip; | |
| 30 | +DROP TABLE IF EXISTS user_ips; | |
| 31 | +"#; | |
| 32 | + | |
| 33 | +#[async_trait::async_trait] | |
| 34 | +impl MigrationTrait for Migration { | |
| 35 | + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | |
| 36 | + manager.get_connection().execute_unprepared(UP).await.map(|_| ()) | |
| 37 | + } | |
| 38 | + | |
| 39 | + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | |
| 40 | + manager.get_connection().execute_unprepared(DOWN).await.map(|_| ()) | |
| 41 | + } | |
| 42 | +} |
modified
src/api/forum.rs
+6
−1
| 307 | 307 | pub async fn create_topic( |
| 308 | 308 | State(state): State<AppState>, |
| 309 | 309 | uc: UserContext, |
| 310 | + ip: crate::client_ip::ClientIp, | |
| 310 | 311 | ApiPath(slug): ApiPath<String>, |
| 311 | 312 | ApiJson(body): ApiJson<NewTopic>, |
| 312 | 313 | ) -> ApiResult<(StatusCode, Json<TopicPage>)> { |
| 313 | 314 | let topic = |
| 314 | 315 | actions::create_topic(&state, &uc.user, &slug, &body.title, &body.body, &body.tags.join(",")).await?; |
| 316 | + crate::ips::record(state.db.as_ref(), uc.user.id, ip.0, crate::ips::Seen::Post).await; | |
| 315 | 317 | let tree = CategoryTree::load(state.db.as_ref()).await?; |
| 316 | 318 | Ok((StatusCode::CREATED, Json(topic_page(&state, &uc, topic, &tree).await?))) |
| 317 | 319 | } |
| 325 | 327 | pub async fn reply( |
| 326 | 328 | State(state): State<AppState>, |
| 327 | 329 | uc: UserContext, |
| 330 | + ip: crate::client_ip::ClientIp, | |
| 328 | 331 | ApiPath(id): ApiPath<Uuid>, |
| 329 | 332 | ApiJson(body): ApiJson<NewPost>, |
| 330 | 333 | ) -> ApiResult<(StatusCode, Json<Post>)> { |
| 331 | 334 | let post = actions::reply(&state, &uc.user, id, &body.body).await?; |
| 335 | + crate::ips::record(state.db.as_ref(), uc.user.id, ip.0, crate::ips::Seen::Post).await; | |
| 332 | 336 | let (topic, tree) = actions::visible_topic(&state, viewer(&uc.user), post.topic_id).await?; |
| 333 | 337 | let caps = actions::topic_caps(&state, Some(&uc.user), tree.get(topic.category_id), &topic); |
| 334 | 338 | let row = web::build_post_rows(&state, vec![(post, Some(uc.user.clone()))], Some(&uc.user), &topic, &caps) |
| 444 | 448 | pub async fn flag( |
| 445 | 449 | State(state): State<AppState>, |
| 446 | 450 | uc: UserContext, |
| 451 | + ip: crate::client_ip::ClientIp, | |
| 447 | 452 | ApiPath(id): ApiPath<Uuid>, |
| 448 | 453 | ApiJson(body): ApiJson<FlagBody>, |
| 449 | 454 | ) -> ApiResult<(StatusCode, Json<FlagResponse>)> { |
| 450 | − crate::moderation::flag_post(&state, &uc.user, id, &body.reason, body.note.as_deref()).await?; | |
| 455 | + crate::moderation::flag_post(&state, &uc.user, id, &body.reason, body.note.as_deref(), ip.0).await?; | |
| 451 | 456 | Ok((StatusCode::CREATED, Json(FlagResponse { flagged: true }))) |
| 452 | 457 | } |
modified
src/auth/oidc.rs
+4
−0
Click to load diff…
modified
src/bin/manage.rs
+22
−0
Click to load diff…
added
src/client_ip.rs
+250
−0
Click to load diff…
modified
src/config.rs
+15
−0
Click to load diff…
modified
src/entities/mod.rs
+1
−0
Click to load diff…
modified
src/entities/post_flags.rs
+2
−0
Click to load diff…
added
src/entities/user_ips.rs
+37
−0
Click to load diff…
modified
src/handlers/auth.rs
+5
−0
Click to load diff…
modified
src/handlers/forum.rs
+4
−0
Click to load diff…
modified
src/handlers/moderation.rs
+127
−6
Click to load diff…
modified
src/handlers/posts.rs
+2
−1
Click to load diff…
added
src/ips.rs
+159
−0
Click to load diff…
modified
src/lib.rs
+2
−0
Click to load diff…
modified
src/main.rs
+14
−2
Click to load diff…
modified
src/middleware/auth.rs
+5
−0
Click to load diff…
modified
src/moderation.rs
+3
−0
Click to load diff…
modified
src/seed.rs
+1
−0
Click to load diff…
modified
src/web.rs
+2
−0
Click to load diff…
modified
templates/admin/_author_glance.html
+1
−0
Click to load diff…
added
templates/admin/_sightings.html
+17
−0
Click to load diff…
added
templates/admin/_user_ips.html
+20
−0
Click to load diff…
added
templates/admin/ip.html
+14
−0
Click to load diff…
modified
templates/admin/user.html
+10
−0
Click to load diff…
modified
tests/moderation_db.rs
+153
−2
Click to load diff…
Parents: e0979f3