From c18ccadf9953c838bac312a7f2c572184ae487ef Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Sun, 1 Nov 2026 15:31:20 +0100 Subject: [PATCH] chore(server): checkpoint deployed services and unfinished forums User requested a checkpoint and stopped all agents. Deployed artifacts remain the last verified releases; unfinished settings/categories/forums are preserved as WIP. See TODO/roadmap for verification and remaining release/updater work. --- CHANGELOG.md | 7 + Cargo.lock | 25 + README.md | 19 +- deploy/central/README.md | 30 + deploy/central/VERIFIED.md | 59 ++ dev/README.md | 31 +- dev/config.toml | 12 +- docs/00-status.md | 26 +- docs/02-protocol/README.md | 4 +- docs/02-protocol/account-presence.md | 54 ++ docs/02-protocol/central-account.md | 48 ++ docs/02-protocol/chat-attachments.md | 9 + docs/02-protocol/node-features.md | 31 + docs/02-protocol/security.md | 8 +- docs/02-protocol/tls.md | 44 +- docs/03-server/README.md | 12 +- docs/03-server/configuration.md | 19 + docs/05-features/hub-server-directory.md | 25 + docs/06-roadmap.md | 149 ++++- docs/design/accounts-and-avatars.md | 2 +- docs/design/ownership-and-admin-audit.md | 74 +++ gateway/Cargo.toml | 1 + gateway/src/admin/mod.rs | 2 +- gateway/src/domain/account.rs | 213 ++++++ gateway/src/domain/channels/mod.rs | 12 +- gateway/src/domain/channels/ops.rs | 18 + gateway/src/domain/config.rs | 86 ++- gateway/src/domain/mod.rs | 1 + gateway/src/domain/rate_limit/mod.rs | 41 +- gateway/src/domain/session.rs | 10 +- gateway/src/domain/storage/attachments.rs | 116 ++++ gateway/src/domain/storage/mod.rs | 86 ++- gateway/src/domain/storage/node_features.rs | 469 +++++++++++++ .../src/domain/storage/node_features/tests.rs | 354 ++++++++++ gateway/src/domain/storage/webhooks/mod.rs | 30 + gateway/src/handler/channel/admin_tests.rs | 489 ++++++++++++++ gateway/src/handler/channel/create.rs | 252 +++++-- gateway/src/handler/channel/edit.rs | 134 +++- gateway/src/handler/channel/join.rs | 27 +- gateway/src/handler/channel/join/tests.rs | 68 ++ gateway/src/handler/channel/layout.rs | 33 + gateway/src/handler/channel/leave.rs | 1 + gateway/src/handler/channel/mod.rs | 91 ++- gateway/src/handler/content/attachments.rs | 413 ++++++++++++ gateway/src/handler/content/chat.rs | 8 + gateway/src/handler/content/message_edit.rs | 20 + gateway/src/handler/content/mod.rs | 1 + gateway/src/handler/content/reaction.rs | 22 + gateway/src/handler/deliver.rs | 22 + gateway/src/handler/dispatch.rs | 16 + gateway/src/handler/forum.rs | 251 +++++++ gateway/src/handler/guild/crud.rs | 12 + gateway/src/handler/mod.rs | 3 + gateway/src/handler/node_settings.rs | 133 ++++ gateway/src/handler/profile.rs | 164 +++++ gateway/src/handler/profile/tests.rs | 321 +++++++++ gateway/src/lib.rs | 16 +- gateway/src/net/handshake.rs | 129 +++- gateway/src/net/io.rs | 2 +- gateway/src/net/state.rs | 5 + gateway/src/proto/packet/id.rs | 22 + gateway/src/tls.rs | 12 +- gateway/tests/tls_listener.rs | 58 +- hub/AUTH.md | 62 +- hub/README.md | 4 + hub/assets/nicknames/ATTRIBUTION.md | 9 + hub/assets/nicknames/LICENSE-CC-BY-SA-4.0.txt | 427 ++++++++++++ hub/assets/nicknames/friendly-ru.json | 387 +++++++++++ hub/examples/monster_preview.rs | 22 + hub/migrations/0006_account_profile.sql | 4 + hub/migrations/0007_community_directory.sql | 10 + hub/migrations/0008_desktop_auth.sql | 17 + hub/migrations/0009_account_onboarding.sql | 8 + hub/src/auth/mod.rs | 49 +- hub/src/auth/nicknames.rs | 41 ++ hub/src/auth/routes.rs | 64 +- hub/src/auth/routes/avatar.rs | 72 +- hub/src/auth/routes/desktop.rs | 287 ++++++++ hub/src/auth/routes/desktop_avatar.rs | 177 +++++ hub/src/auth/routes/directory.rs | 241 +++++++ hub/src/auth/routes/onboarding.rs | 93 +++ hub/src/auth/routes/profile.rs | 106 +++ hub/src/auth/store.rs | 54 +- hub/src/avatar.rs | 90 ++- hub/src/avatar/monsters.rs | 157 +++++ hub/src/identity.rs | 11 +- hub/src/web.rs | 2 + hub/tests/auth.rs | 17 + hub/tests/avatar.rs | 78 ++- hub/tests/desktop_auth.rs | 614 ++++++++++++++++++ hub/tests/directory.rs | 230 +++++++ hub/tests/email_auth.rs | 7 +- hub/tests/foundation.rs | 8 +- hub/tests/onboarding.rs | 265 ++++++++ hub/tests/profile.rs | 412 ++++++++++++ protocol/lnex.proto | 64 +- 96 files changed, 8665 insertions(+), 276 deletions(-) create mode 100644 docs/02-protocol/account-presence.md create mode 100644 docs/02-protocol/central-account.md create mode 100644 docs/02-protocol/chat-attachments.md create mode 100644 docs/02-protocol/node-features.md create mode 100644 docs/05-features/hub-server-directory.md create mode 100644 docs/design/ownership-and-admin-audit.md create mode 100644 gateway/src/domain/account.rs create mode 100644 gateway/src/domain/storage/attachments.rs create mode 100644 gateway/src/domain/storage/node_features.rs create mode 100644 gateway/src/domain/storage/node_features/tests.rs create mode 100644 gateway/src/handler/channel/admin_tests.rs create mode 100644 gateway/src/handler/channel/layout.rs create mode 100644 gateway/src/handler/content/attachments.rs create mode 100644 gateway/src/handler/forum.rs create mode 100644 gateway/src/handler/node_settings.rs create mode 100644 gateway/src/handler/profile.rs create mode 100644 gateway/src/handler/profile/tests.rs create mode 100644 hub/assets/nicknames/ATTRIBUTION.md create mode 100644 hub/assets/nicknames/LICENSE-CC-BY-SA-4.0.txt create mode 100644 hub/assets/nicknames/friendly-ru.json create mode 100644 hub/examples/monster_preview.rs create mode 100644 hub/migrations/0006_account_profile.sql create mode 100644 hub/migrations/0007_community_directory.sql create mode 100644 hub/migrations/0008_desktop_auth.sql create mode 100644 hub/migrations/0009_account_onboarding.sql create mode 100644 hub/src/auth/nicknames.rs create mode 100644 hub/src/auth/routes/desktop.rs create mode 100644 hub/src/auth/routes/desktop_avatar.rs create mode 100644 hub/src/auth/routes/directory.rs create mode 100644 hub/src/auth/routes/onboarding.rs create mode 100644 hub/src/auth/routes/profile.rs create mode 100644 hub/src/avatar/monsters.rs create mode 100644 hub/tests/desktop_auth.rs create mode 100644 hub/tests/directory.rs create mode 100644 hub/tests/onboarding.rs create mode 100644 hub/tests/profile.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1490b71..e5b7bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Unreleased] +### Added: Hub profile and registration completion + +- Authenticated `PUT /v1/account/profile`: choose a case-insensitive unique handle once, edit the display name, and expose `profile_complete` in login/session responses. +- Profile writes require exact Origin and session CSRF, have a 1 KiB body limit and independent account/source hourly budgets. Concurrent handle claims and selections from multiple sessions are atomic. +- Migration `0006_account_profile.sql` preserves existing identities and marks generated profiles incomplete. The new unique index rejects conflicting case variants in manually modified legacy databases. +- Added profile API integration tests and refreshed server status, next milestones and standalone voice deployment limitations. Desktop authorization and gateway account enforcement remain separate work. + ### Changed (breaking): LNEx v3, TLS 1.3 on the TCP control plane - The custom X25519 + HKDF + ChaCha20-Poly1305 layer on TCP is removed. The gateway listens with TLS 1.3 only (rustls, ALPN `vnox-lnex/3`); there is no plaintext, TLS 1.2 or LNEx v2 mode. - Protocol version bumped `v2` to `v3`. `Hello.server_eph_pubkey` and `Auth.client_eph_pubkey` are reserved. Old clients cannot connect and old gateways are refused by new clients with an explicit error. diff --git a/Cargo.lock b/Cargo.lock index 9fc0d7c..fae99d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,6 +360,12 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "combine" version = "4.6.9" @@ -875,6 +881,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gif" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8cfcc411d9adbbaba82fb72661cc1bcca13e8bba98b364e62b2dba8f960159" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "hashbrown" version = "0.15.5" @@ -1189,6 +1205,8 @@ checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104" dependencies = [ "bytemuck", "byteorder-lite", + "color_quant", + "gif", "image-webp", "moxcms", "num-traits", @@ -3087,6 +3105,7 @@ dependencies = [ "ed25519-dalek", "hex", "hmac", + "image", "prost", "prost-build", "rand 0.10.3", @@ -3298,6 +3317,12 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "weezl" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88" + [[package]] name = "whoami" version = "2.1.3" diff --git a/README.md b/README.md index ab27f3c..26bfae7 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Both are written in Rust on top of Tokio. The client is a separate native applic │ VNOX Client │ │ (Rust + Slint UI) │ └──────────────────────┬───────────────────────────────┘ - │ LNEx v1 + │ LNEx v3 ┌──────────┴──────────┐ │ TCP │ UDP ▼ ▼ @@ -89,7 +89,6 @@ Full architecture details: [docs/01-architecture.md](docs/01-architecture.md) - **Keyfile export/import:** Encrypted or plain JSON keyfile with passphrase ### Planned -- Protobuf wire format (Phase 2) - PostgreSQL backend (Phase 2) - Federation protocol (Phase 3) - Plugin runtime (Phase 3) @@ -105,7 +104,7 @@ Phase 1 is implemented. Not production ready. | Gateway | TCP listener, LNEx handshake, channels, chat, SQLite | | Voice node | UDP relay, voice packet routing, jitter buffer | | Desktop client | Slint UI, net layer, audio pipeline (partial) | -| LNEx protocol | Specified and implemented (JSON in Phase 1) | +| LNEx protocol | v3: Protobuf over TLS 1.3 | | Encryption | TLS 1.3 (TCP) + ChaCha20-Poly1305 (UDP voice) - DONE | | Federation | Planned (Phase 3) | | Mobile client | Planned (Phase 3) | @@ -119,13 +118,14 @@ See [docs/00-status.md](docs/00-status.md) for a full breakdown. **Requirements:** Rust 1.94+, a running [VNOX Client](../VNOX-Client/) ```bash -# Terminal 1 — gateway -cargo run -p vnox-gateway -- --config dev/config.toml - -# Terminal 2 — voice node -cargo run -p vnox-voice-node -- --config dev/config.toml +# Run from VNOX-Server/ +cargo run -p vnox-serverd -- --config dev/config.toml ``` +The combined daemon connects gateway authorization to the voice relay through +an in-process bridge. Separate gateway and voice-node processes do not yet +exchange voice credentials; the standalone relay rejects voice traffic. + The client connects to `127.0.0.1:7600` by default. Config reference: [dev/README.md](dev/README.md). ### Docker @@ -145,7 +145,8 @@ The voice relay forwards encoded packets and does not require libopus; the codec LNEx is a custom application-layer protocol for low-latency federated communication. Sits above TCP and UDP, defines packet framing, encryption, and routing. -Phase 1 uses JSON framing; Phase 2 will migrate to Protobuf. +LNEx v3 uses Protobuf payloads over TLS 1.3 for the TCP control plane and +authenticated, encrypted UDP packets for voice. The specification is **CC0** (public domain) — anyone can implement a compatible client or server. diff --git a/deploy/central/README.md b/deploy/central/README.md index 4a1dd41..65abf5e 100644 --- a/deploy/central/README.md +++ b/deploy/central/README.md @@ -6,6 +6,36 @@ public email-send/registry/friends/DM endpoints. Sign-in endpoints are GitHub an is configured, email one-time codes (`hub/AUTH.md`). See `hub/README.md` for exact capability status. SMTP sender display name is Vnox, address `noreply@loki-code.dev`. +The unverified community directory is now available through `GET/POST /v1/servers` +and owner-only `DELETE /v1/servers/{id}`. It is separate from cryptographic node +registration; see [HTTP contract](../../docs/05-features/hub-server-directory.md). + +## Directory deployment: 2026-11-01 + +- Hub: `d6d406b-directory-7d4ee740d4d9`; static musl binary SHA256 + `7d4ee740d4d93bcc738563838f37e81385d7fe9ca47933924639ce691621a7e0`. +- Web: `directory-c885e4689579`, served by the existing Nginx `/servers` fallback. +- Hub source is committed base `d6d406bbf5a50d73d1a5069b1cb57f728ad3e4c5` plus + directory-only changes; `source.tar.gz` and `release.json` are retained beside + the installed binary. Pending account-profile changes were excluded. +- Production migrations: 1, 2, 3, 4, 5, 7. Migration 6 is the unpublished profile + feature; the directory has no dependency on it. Do not renumber applied migrations. +- Private paired backup, taken while Hub was stopped: + `/var/backups/vnox-central/deploy-directory-20261101T112022Z/`. + Issuer identity and email-auth key were preserved. This local snapshot does not + complete the roadmap's off-host backup/retention milestone. +- Verified real public readiness, anonymous session, directory JSON, authenticated + mutation gate (anonymous POST returns 401), desktop/mobile page rendering and + absence of JavaScript errors. Browser mutation QA uses local fixtures; Hub + integration tests verify the persisted authorization and ownership flow. +- `vnox-server.service` remained running throughout the update. + +Rollback to the preceding Hub binary must account for SQLx migration 7: the old +binary rejects unknown applied migration versions. Stop Hub and preserve a current +paired snapshot before restoring a compatible database and switching releases. +Restoring the pre-deployment database discards subsequent writes; do not do this +as a routine binary-only rollback. + ## Layout - `/opt/vnox-central/releases/-/vnox-hub`: immutable release. diff --git a/deploy/central/VERIFIED.md b/deploy/central/VERIFIED.md index 65173b0..0f66880 100644 --- a/deploy/central/VERIFIED.md +++ b/deploy/central/VERIFIED.md @@ -1,5 +1,64 @@ # Vnox Central: deployment verification +## Live profile, onboarding and node administration — 2026-11-01 + +- Current Hub `20261101-live-admin-91595d45652a`; current gateway/voice daemon + `20261101-live-capability-219376accf98`; website + `20261101-names-monsters-ff473f423565`. +- Signed live profile updates are negotiated with `Session.live_profiles`. + Device and account binding are retained; channel visibility is checked at delivery. +- Node administrator UUID is explicitly configured for account `loki`. Creation, + rename and deletion of global channels require the verified account of the exact + session. Guild permissions remain separate. Channel persistence/reload tested. +- New accounts receive a generated `слово_0000` and a one-time optional name prompt; + migration 9 is independent of pending migration 6. Actual versions: 1–5, 7, 8, 9. + Existing account `loki` has no onboarding row and keeps its chosen name. +- Pixel-monster defaults use new immutable `/v1/avatars/default-v2/` URLs. Old v1 + URLs remain stable; own uploaded photographs remain authoritative. Public v2 PNG + verified over HTTPS using the desktop User-Agent. +- Final server workspace: 181 tests, Clippy and formatting passed. Website: + 28 tests, typecheck/build, formatting and isolated onboarding browser QA passed. +- Paired backup `/var/backups/vnox-central/live-profile-admin-20261101T134328Z`; + capability-only gateway backup + `/var/backups/vnox-central/live-capability-20261101T135637Z`. + Issuer, node identity, TLS and administrator configuration preserved. + +## Desktop profile update — 2026-11-01 + +- Hub release `20261101-desktop-profile-0bc942369c89`, binary SHA256 + `0bc942369c8943177814c4495dfac1b6413d9b7b79a74766f724415ca3acb82a`. +- Added enrollment/device-signature authorized avatar upload/delete and display + name mutation. Existing browser account avatar storage is reused. +- 167 workspace tests, 6 desktop-auth tests, formatting and Clippy passed in the + isolated profile-free release workspace. Hub migrations remain 1–5, 7, 8. +- Both public desktop mutation endpoints return 401 without authentication. + Hub readiness and active status verified; gateway stayed running unchanged. +- Private backup `/var/backups/vnox-central/desktop-avatar-20261101T130504Z`; + issuer/email keys preserved. + +## Current update — 2026-11-01 + +- Hub release `20261101-media-presence-75ff65dc3e7b`; SHA256 + `75ff65dc3e7b045b617b6214cdeb676506584d7d61a1f898f4bfa8f6e7c14dab`. +- Combined gateway/voice release `20261101-media-presence-d4d98bbc2932`; SHA256 + `d4d98bbc2932cb7ef8692e67b11067162e42299451e25e7caed7551f38f502fb`. +- Both services active; gateway health and LNEx v4 version verified. Public + Central `/v1/info` responds over HTTPS. Issuer and node identities preserved. +- Email/GitHub and desktop account authentication, uploaded/generated avatars, + and the public community directory are enabled. Friends, DM and marketplace + remain disabled in Central. Earlier entries below describe historical releases. +- Added signed account/profile metadata, multi-session presence lifecycle and + authenticated channel image attachments. Existing desktop proofs remain valid. +- Paired private SQLite backup: + `/var/backups/vnox-central/media-presence-20261101T123503Z`. + Hub migrations remain 1–5, 7, 8; pending profile migration 6 was not deployed. +- Isolated release workspace: 164 tests passed, formatting and all-target Clippy + passed; both binaries built with the locked musl release configuration. +- Website remains the previously deployed account/directory release. No real + account was created or mail sent during this update. + +## Initial deployment (historical) + - Endpoint: https://central.loki-code.dev - Service: vnox-central (systemd enabled/active, dedicated user). - Internal listener: 127.0.0.1:37623; external access goes through Nginx443. diff --git a/dev/README.md b/dev/README.md index 1fd72cf..50e8234 100644 --- a/dev/README.md +++ b/dev/README.md @@ -48,25 +48,44 @@ sqlite_path = "./dev/data/vnox.db" ## First run +Run from `VNOX-Server/`: + ```sh mkdir -p dev/data -cargo run -p vnox-gateway -- --config dev/config.toml -cargo run -p vnox-voice-node -- --config dev/config.toml -cargo run -p vnox-client +cargo run -p vnox-serverd -- --config dev/config.toml ``` +`vnox-serverd` runs the TCP gateway and UDP relay together and passes voice +credentials through their in-process bridge. Separate gateway and voice-node +processes do not yet exchange credentials; the standalone relay rejects voice +traffic. + +In another terminal, run from `VNOX-Client/`: + +```sh +cargo run --bin vnox-client +``` + +Connect to `127.0.0.1:7600`. The gateway generates a self-signed TLS certificate +for local development and prints its SPKI SHA-256 fingerprint. Compare it with +the client's first-use trust prompt before accepting. + On first gateway start, `dev/data/server_identity.json` is created automatically. Back it up if you care about a stable server identity across reinstalls. ## E2E voice test -With gateway and voice-node running: +With `vnox-serverd` running, execute from `VNOX-Client/` using the fingerprint +printed by the gateway (replace `HEX`): ```sh -cargo run -p vnox-client --bin vnox-e2e-voice +cargo run --bin vnox-e2e-voice -- --fingerprint HEX ``` +For a disposable loopback test, `--trust-first-use` can replace `--fingerprint HEX`. +The headless test keeps certificate trust in memory only. + This headless test connects two TCP clients, joins the `voice` channel, sends UDP packets, and verifies the voice-node relays between them. Exit code 0 means pass. ## Logs @@ -74,7 +93,7 @@ This headless test connects two TCP clients, joins the `voice` channel, sends UD Set log level via environment variable: ```sh -VNOX_LOG=debug cargo run -p vnox-gateway -- --config dev/config.toml +VNOX_LOG=debug cargo run -p vnox-serverd -- --config dev/config.toml ``` ## Full reference diff --git a/dev/config.toml b/dev/config.toml index e2be742..0f5e055 100644 --- a/dev/config.toml +++ b/dev/config.toml @@ -3,6 +3,10 @@ name = "dev-node" address = "127.0.0.1" [gateway] +# Central account UUIDs explicitly authorized for global channel management. +# Device public keys and nicknames are not accepted here. +admin_account_ids = [] + bind = "127.0.0.1:7600" max_connections = 100 session_timeout = 600 @@ -12,13 +16,19 @@ admin_bind = "127.0.0.1:7601" message_rate_per_sec = 5 # Burst size for the token bucket. message_rate_burst = 10 -# TLS 1.3 is always on (LNEx v3). With neither option below the gateway generates +# TLS 1.3 is always on (LNEx v4). With neither option below the gateway generates # a self-signed certificate in /tls/ (key = server identity) and prints its # SPKI SHA-256 fingerprint at startup. Clients pin it on first use. # For a public CA certificate (for example Let's Encrypt) set both paths and restart on renewal: # tls_cert_path = "/etc/letsencrypt/live/chat.example.org/fullchain.pem" # tls_key_path = "/etc/letsencrypt/live/chat.example.org/privkey.pem" +# Required for account admission: copy the issuer UUID/public key from your trusted +# Central operator. No configured issuer means every client admission is rejected. +# [gateway.central_account] +# issuer = "" +# public_key = "<64-character Ed25519 public key hex>" + [voice] bind = "127.0.0.1:7700" diff --git a/docs/00-status.md b/docs/00-status.md index 571bdad..16c797b 100644 --- a/docs/00-status.md +++ b/docs/00-status.md @@ -3,10 +3,18 @@ This file describes what the repository actually does today. Protocol and security docs may describe future phases. When in doubt, trust this file. -Last updated: 2026-10-24. +Last updated: 2026-10-25. --- +## Update 2026-10-25 (Hub profile API) + +- `PUT /v1/account/profile`: authenticated, CSRF-protected handle selection and display-name editing. Handles are case-insensitive, unique and permanent once selected. +- `user.profile_complete` is returned in session and login responses. Existing generated handles remain incomplete until explicitly selected by their owners. +- Migration `0006_account_profile.sql` preserves account IDs, provider bindings, sessions and avatars. It adds a case-insensitive unique handle index; conflicting manually inserted legacy handles cause migration failure rather than reassignment. +- This is server API support. Browser onboarding, desktop authorization/device enrollment, avatar synchronization to community members and mandatory Hub identity at gateways remain unfinished. +- Code availability does not imply production deployment. See [Hub API contract](../hub/AUTH.md). + ## Update 2026-10 (LNEx v3, TLS 1.3) - TCP control plane is TLS 1.3 only (rustls); the custom X25519/HKDF/ChaCha20 TCP layer was removed (**breaking**, protocol `v3`). See [02-protocol/tls.md](02-protocol/tls.md). @@ -15,11 +23,11 @@ Last updated: 2026-10-24. ## Update 2026-10 (protocol v2) -- Signed v2 handshake and protobuf payloads between client and gateway (verified by `VNOX-Client/tests/gateway_interop.rs`). +- Protobuf payloads between client and gateway (the historical v2 handshake has since been replaced by v3) (verified by `VNOX-Client/tests/gateway_interop.rs`). - Authenticated, encrypted UDP voice relay in voice-node. - Voice access is revoked when a channel is deleted, a member is kicked or permissions change. - Senders get acknowledgements for their own chat messages. -- Hub email sign-in by one-time code (HMAC-protected, rate-limited; enabled only when SMTP is configured; code-ready, not yet enabled in production). Handle selection after first sign-in is not implemented. +- Hub email sign-in by one-time code (HMAC-protected, rate-limited; enabled only when SMTP is configured; code-ready, not yet enabled in production). Handle selection is now available through the profile API; onboarding UI is not implemented. - Hub own avatar (web): upload, remove and public serving of a re-encoded 256x256 PNG; code-ready, not deployed. Backup rotation to 30 days for deleted data is decided but not implemented. - Channel webhooks, GitHub sign-in, hub service, screen sharing and media broadcast. - `cargo audit`: no known vulnerabilities in both workspaces (2026-10-24). @@ -30,7 +38,7 @@ Last updated: 2026-10-24. - **Gateway (TCP):** Ed25519 client auth, sessions, channel join/leave, text chat, SQLite history, guilds, roles, permissions, invites, audit log, rate limiting. - **Gateway (HTTP admin):** `GET /health`, `GET /version`, `GET /metrics` (Prometheus exposition). Default bind 0.0.0.0:7601. -- **Voice node (UDP):** relay between clients in the same channel; members expire after 30 s idle; jitter buffer wired into relay path. +- **Voice node (UDP):** relay between clients in the same channel; members expire after 30 s idle (clients maintain registration with encrypted keepalives); jitter buffer wired into relay path. - **Client:** connect UI, channel list, text chat, net layer, voice capture/playback wired to UI, friends/DM panel, guild bar with create + invite-accept popups, right-side member list panel, message context menu (react/edit/delete/reply/copy), custom status + activity status, block list, channel create UI, encrypted identity vault. - **Server identity:** gateway generates or loads `server_identity.json` in the configured data directory. - **UI refactoring:** modular structure (`app/`, `chat/`, `connect/`, `sidebar/`, `members/`, `state/`, `settings/`). @@ -71,24 +79,24 @@ Last updated: 2026-10-24. | Federation crate | Stub only (`// TODO`). | | RNNoise | Feature-gated, no-op stub by default. Enable with `--features rnnoise`. | | Slint migration | Done. The client UI is Slint (RU/EN localization, themes, settings). | -| Server-side channel creation | Client-side channels work locally; gateway still needs `ChannelCreate` packet + storage (Phase 2). | +| Server-side channel management | Gateway create/edit/delete handlers exist, with guild authorization and storage. See `gateway/src/handler/channel/`. | | Identity export/import UI | Vault API is ready (`identity::save(identity, Some(passphrase))`), but no UI modal yet. | --- -## Specified in docs, not implemented yet +## Implementation status of planned features | Feature | Target phase | |---------|--------------| | Seed phrase / encrypted keyfile export UI | Phase 2 | -| Gateway to voice-node membership signaling | Phase 2 | -| Protobuf payloads (replacing JSON) | Phase 2 | +| Gateway to voice-node membership signaling | In-process bridge implemented in `serverd`; separate-process signaling remains unfinished. Standalone voice-node is fail-closed. | +| Protobuf payloads (replacing JSON) | Implemented; LNEx v3 uses protobuf over TLS 1.3. | | `/health` HTTP endpoint on gateway | ✅ DONE | | Rate limiting | ✅ DONE | | Prometheus metrics | ✅ DONE | | Identity keypair encryption at rest (Argon2id passphrase) | ✅ DONE | | Published Docker images (`ghcr.io/vnox/...`) | Not published yet | -| Server-side channel create/edit/delete | Phase 2 | +| Server-side channel create/edit/delete | Implemented; further permission changes need regression tests. | | Federation protocol spec | Phase 3 | --- diff --git a/docs/02-protocol/README.md b/docs/02-protocol/README.md index 5f5d60f..ed5e1aa 100644 --- a/docs/02-protocol/README.md +++ b/docs/02-protocol/README.md @@ -29,7 +29,7 @@ encryption, routing, identity, and federation. ## Version -Current version: `LNEx v3` (TCP runs inside TLS 1.3, see [tls.md](tls.md)). There is no negotiation with older versions. +Current version: `LNEx v4` (TCP runs inside TLS 1.3 and requires a Central account proof; see [tls.md](tls.md) and [central-account.md](central-account.md)). There is no negotiation with older versions. Version is negotiated during handshake. Clients and servers advertise their supported versions. If no common version exists, the connection @@ -83,4 +83,4 @@ Full state machine: see individual spec files. - [federation/README.md](federation/README.md) — node discovery, cross-node routing - [identity.md](identity.md) — keypair model, auth flow, permissions - [security.md](security.md) — encryption, threat model -- [tls.md](tls.md) — TLS 1.3 control plane, trust model, LNEx v3 handshake +- [tls.md](tls.md) — TLS 1.3 control plane, trust model, LNEx v4 handshake diff --git a/docs/02-protocol/account-presence.md b/docs/02-protocol/account-presence.md new file mode 100644 index 0000000..0296300 --- /dev/null +++ b/docs/02-protocol/account-presence.md @@ -0,0 +1,54 @@ +# Account presence and profiles + +LNEx v4 additive metadata: MemberInfo fields 4 account_id and 5 avatar_url; UserJoin fields 4 account_id and 5 avatar_url. user_id remains the authenticated Ed25519 device key, never an account UUID. Channel memberships and UDP routes retain their session IDs. Snapshots include each device once; repeated sessions on the same device emit UserLeave only after the last subscription leaves. Presence OFFLINE is emitted only after the last session of that device closes. Clients group member rows by nonempty account_id while retaining all device IDs for voice attribution, and remove only the leaving device. + +Central v1 signed proof claims optionally include display_name and avatar_url. Names are bounded to 64 characters; avatar URLs are relative /v1/avatars/ paths, resolved only against https://central.loki-code.dev. Gateway ignores unsigned client nickname when a signed profile exists. Missing optional fields retain compatibility with existing proofs. Avatar HTTP requests have no auth/cookies, reject redirects, bound response and dimensions, and execute outside the UI thread. Default avatar paths use the authenticated account UUID. No database migration, ownership reassignment or protocol version change is required. + +## Desktop avatar mutation and live summary refresh + +`POST /v1/auth/desktop/avatar` uploads a cropped raw PNG (image/png, at most 2 MiB); `DELETE` on the same path uses an empty body. Both use Authorization: Bearer , X-Vnox-Public-Key (lowercase Ed25519 hex), X-Vnox-Timestamp (Unix seconds), and X-Vnox-Signature (hex Ed25519). Signature bytes cover `VNOX-CENTRAL-PROFILE-v1\0` followed by method ASCII, NUL, request path ASCII, NUL, decimal timestamp ASCII, NUL, then SHA-256(raw body). Timestamp skew is bounded to 60 seconds. Hub verifies the active unexpired enrollment matches the supplied key and account; cookies are never used. Replays within that window repeat the identical idempotent image/delete mutation. Existing avatar normalization, per-account/per-source budgets, bounded decoding and immutable content IDs apply. Success returns `{user: AccountSummary}` with effective same-origin avatar path. No schema migration is required. + +Desktop refresh responses already return `user`; clients now validate subject consistency, atomically persist it only if the same enrollment remains active, and publish a non-secret profile update to UI. Admission, startup, periodic refresh and opening profile settings obtain current Central data. Mutation results use the same update path. Late network results cannot restore credentials or avatars after sign-out. Public PNG requests remain separate and never carry enrollment headers. + +`POST /v1/auth/desktop/profile` uses the same enrollment headers and signature (including its distinct path) with JSON `{display_name: string}` limited to 1024 bytes. The trimmed name must contain 1–64 characters and no control/bidi override characters. Only existing accounts.display_name changes; handle, account UUID, device ID, roles and profile_complete remain unchanged. Account/source profile rate limits apply. Success returns `{user: AccountSummary}` and clients persist/publish it exactly like avatar changes. Signed proofs carry the new display name on admission and through the additive live update described below. No protocol version change is introduced. + +## Live signed profile updates + +LNEx v4 additive packets: `0x0042 ProfileUpdate { bytes account_proof = 1; }` (client to gateway), `0x0043 ProfileUpdated { string user_id = 1; string account_id = 2; string nickname = 3; string avatar_url = 4; int64 issued_at = 5; }` (gateway to client). Gateway verifies the Central issuer signature, lifetime, audience and device binding exactly as admission, and requires the subject to equal the current session account. Display name and avatar must be valid signed profile claims. No client-supplied unsigned nickname/avatar is accepted. Existing Ed25519 device keys, channel/session membership, ownership, permissions and UDP credentials stay unchanged. + +The newest accepted issued_at among active sessions of an account prevents older proofs downgrading metadata. Equal-second proofs are accepted (Central proof timestamp resolution is one second); same-second cosmetic ordering is last-arrival. This is transient display metadata, not authorization. Gateway updates profile fields on account sessions and emits each affected device's ProfileUpdated only to its own sessions or peers currently sharing its text/voice channel. Delivery rechecks that visibility; no node-wide profile broadcast or join/leave event is generated. + +Desktop forwards the proof obtained by its existing Central refresh; profile-only mutation notifications trigger one coalesced background refresh, not a refresh loop. Before chat/image send or channel/voice join, a proof older than five seconds causes those actions to wait for one bounded refresh; audio/UDP, incoming packets and keepalives continue. ProfileUpdate is written before the actions on the same TCP stream. A failed Central refresh leaves the gateway/voice session active, reports failure, and sends the action with its last verified profile. Opening settings and periodic refresh also propagate signed updates live. No reconnect is required. + +## Registration display names and web onboarding + +New accounts receive one locally generated friendly Russian word plus `_` and four +cryptographically random decimal digits. Neither email local parts nor GitHub names +become public defaults. Existing account names and immutable handle selection are +unchanged. Word data and its license ship with Hub; no network lookup occurs during +registration. + +Migration 0009 adds an independent account_onboarding table. Only newly created +accounts receive pending rows; existing accounts do not acquire a prompt. Session +responses add optional top-level onboarding_pending. POST /v1/account/onboarding +accepts {decision:"rename"|"later"}, records an explicit choice once, and never +changes handle/name. POST /v1/account/display-name accepts {display_name:string}, +updates only the existing display_name column, resolves a pending prompt as an +explicit rename, and returns {display_name,onboarding_pending:false}. Both routes +require the same own-origin cookie session and CSRF protection as avatar changes, +small JSON bodies, and per-account/source profile budgets. They do not depend on +pending migration 0006 or require LNEx changes. + +## Explicit node administrator capability + +Node operators may set `[gateway] admin_account_ids = [""]`. The default empty list grants nobody node administration. Startup rejects invalid/nil UUIDs, duplicates and more than 256 entries. Only the verified Central subject on the authenticated Session is compared with this list; nickname and device public key never confer this role. This is a static operator setting, applied on daemon restart/admission, not first-visitor ownership. + +Session gains additive `bool node_admin = 4`, advertising global channel management capability to clients. A listed account may create, rename and delete global (guild_id absent) channels; its other admitted devices receive the same capability. `general` and `voice` remain undeletable bootstrap IDs. Guild ownership/membership/role checks remain unchanged: node administrator status cannot create/edit/delete another guild's channels or bypass voice access. Server checks actual stored channel scope for edit/delete, never a client claim. Clients expose global management controls only for advertised capability and still handle gateway denial. + +Global channel metadata changes are visible to all authenticated node sessions; guild metadata remains restricted to persisted guild members. Created/renamed channels are persisted through the existing storage bootstrap path. No database/protocol version migration is required. + +Deferred activity is scoped to one TCP session, limited to 32 actions and 4 MiB total contents. Duplicate joins coalesce. Explicit leave cancels matching deferred activity; disconnect/server switch clears it, and session scope drop aborts the HTTP refresh task. Nothing is replayed into another node or session. + +### Gateway capability negotiation + +Session field 5 `bool live_profiles` is true on gateways supporting packets 0x0042/0x0043. Absent/false on older LNEx v4 gateways: desktop must not send ProfileUpdate, start activity-triggered profile HTTP refresh, or defer chat/image/join for it. Normal existing activity packets continue directly. The local Central profile UI can still refresh independently. This explicit opt-in preserves additive compatibility with older v4 gateways that reject unknown packet IDs. diff --git a/docs/02-protocol/central-account.md b/docs/02-protocol/central-account.md new file mode 100644 index 0000000..cff6409 --- /dev/null +++ b/docs/02-protocol/central-account.md @@ -0,0 +1,48 @@ +# LNEx v4: mandatory Central account admission + +LNEx v4 requires a Central account proof in AUTH (field 6, `bytes account_proof`). +There is no anonymous or private-mode exemption. v3 clients and gateways must be +upgraded together. TLS 1.3 and existing channel-bound Ed25519 signatures remain; +v4 changes their domain separator from `VNOX-LNEx-v3-` to `VNOX-LNEx-v4-`. + +The proof is UTF-8 JSON `{ "claims": "", "signature": "" }`. +Claims are the exact UTF-8 JSON bytes (hex encoded), containing `version` (1), +`issuer` (Hub issuer UUID), `subject` (Central account UUID), `public_key` (lowercase +hex desktop Ed25519 key), `issued_at` and `expires_at` (Unix seconds), and +`audience` (`vnox-gateway`). The Hub Ed25519 signature covers the bytes +`VNOX-CENTRAL-ACCOUNT-v1\0` followed by the decoded claims. Gateways use explicitly +configured trusted issuer UUID/public key, never keys supplied by clients or +fetched from arbitrary URLs. They reject missing, malformed, expired, future, +overlong-lived (>600 seconds), wrong-audience, unknown-issuer and incorrectly +signed proofs, and require the subject key to equal AUTH's client public key. +AUTH's channel-bound signature includes the proof. Gateways verify before storage +or session creation. Proofs authorize new admission for ten minutes; revoking a +device stops refresh immediately, while already issued proofs expire naturally. +Existing gateway session timeout remains independent of the admission proof. + +## Browser device authorization + +Desktop POSTs `/v1/auth/desktop/start` with only its public key. The Hub returns a +256-bit device secret, separate random user code, expiry (600 seconds), and a +same-origin browser URL `/desktop?code=...`. Only the user code enters the browser; +no private identity key, device secret or account proof appears in URLs. The +browser uses the existing real email/GitHub Central login. It displays the key +fingerprint and requires explicit approval with the normal session cookie, +exact-Origin and session-CSRF checks. An authenticated user's approval binds the +request to that account; approval never creates a synthetic account. Desktop +polls `/v1/auth/desktop/poll` with its device secret; pending is HTTP 202, expired +or canceled requests fail closed. Approval yields the signed proof and a long +random refresh secret. Secrets are stored only as SHA-256 hashes on Hub. Polling +completion is single-use, so a captured browser code cannot retrieve credentials. +Desktop cancellation deletes the pending request. Device enrollment expires after +30 days; refresh checks that enrollment is still live and issues a new short proof. +Desktop logout disables local admission immediately by atomically moving credentials +to a private revocation tombstone; online revocation removes it. Network failures +retain the admission-disabled tombstone, retried on later sign-in/admission. HTTPS is required +for all desktop Central calls; redirects are disabled and request timeouts bounded. +Desktop local credential storage uses a private directory and 0600 file, bound to +the local identity public key. The local Ed25519 private key never leaves desktop. + +Rate limits and bounded request bodies apply. Expiry is checked on every operation; +cleanup does not replace authorization checks. HTTP responses use no-store. All +browser approval actions are explicit POSTs; GET requests never approve devices. diff --git a/docs/02-protocol/chat-attachments.md b/docs/02-protocol/chat-attachments.md new file mode 100644 index 0000000..982420a --- /dev/null +++ b/docs/02-protocol/chat-attachments.md @@ -0,0 +1,9 @@ +# Channel image attachments (additive LNEx v4 extension) + +Written before implementation. Existing packets and protobuf field numbers remain unchanged; no protocol version bump is needed. Unknown packet IDs remain ignorable. Voice messages and DM attachments are outside this extension. + +Authenticated TLS sessions may send AttachmentUpload (0x0220): request_id=1 string, channel_id=2 string, data=3 bytes. The gateway validates PNG/JPEG/GIF content (maximum 3 MiB encoded; dimensions <=4096 each; <=4 million pixels and <=120 frames and <=8,388,608 aggregate frame pixels (32 MiB RGBA)), enforces upload rate and durable per-sender 64 MiB / global 512 MiB quotas (also 10,000 attachments per sender and 100,000 overall to bound row metadata), and requires the session to be currently joined to the channel. Success durably stores bytes in the existing authenticated database and posts an ordinary chat message `vnox-attachment:`. Such a marker identifies an attachment only within its message channel, never a filesystem path or public URL. + +AttachmentFetch (0x0221): request_id=1, channel_id=2, attachment_id=3 strings. The current session must be joined to that exact channel; lookup is scoped by channel and ID. AttachmentResult (0x0222): request_id=1, channel_id=2, attachment_id=3, mime=4 strings, data=5 bytes, error=6 string. Upload responses omit bytes; fetch responses carry validated bytes. Errors carry a bounded human-readable error and no bytes. All traffic uses the existing TLS frame transport; 3 MiB binary protobuf remains below its 4 MiB frame limit. No HTTP listener, bearer URL, cookies, or externally accessible storage is introduced. + +Images are validated off the async runtime with at most two concurrent validations per gateway process. Quota check, byte insertion, chat history and outgoing webhook events commit in one database transaction, serialized using the existing SQLite write lock / PostgreSQL advisory transaction lock across gateway processes. Current channel existence, text kind and guild membership are revalidated under the access lock; uploads additionally require MESSAGE_SEND (owner/administrator override). Uploads use the existing chat-send rate limiter. Fetches have a separate per-session budget (50 initial requests, refilling at four per second), stored in the same session entry and removed on disconnect, so opening history cannot consume chat-send tokens. Existing channel history preserves the marker and stored bytes survive restarts. Attachments currently have no automatic eviction; quota exhaustion returns an error. Deleting a message does not free its attachment immediately. Clients must bound decoding/cache and explicitly confirm clipboard previews before uploading. Direct HTTPS image URLs are a separate client capability and must not bypass private-network or redirect protections. diff --git a/docs/02-protocol/node-features.md b/docs/02-protocol/node-features.md new file mode 100644 index 0000000..117642e --- /dev/null +++ b/docs/02-protocol/node-features.md @@ -0,0 +1,31 @@ +# Node settings, structured channels and forums (LNEx v4 additive) + +## Plan and compatibility + +Implement persisted public node name/description, one-level channel categories and actual forum topics/replies. Authorization uses the verified Central account/session and existing guild permissions. Existing channels/messages/ownership/voice keys remain intact. Storage extends gateway SQLite/PostgreSQL idempotently, independently of Hub migrations. + +Session advertises `node_settings` (6), `channel_structure` (7), `forums` (8). A capable desktop sends `0x0232 ClientFeatures {node_settings=1,channel_structure=2,forums=3}` after Session, before channel listing/actions, only when the corresponding server flag is true. No AUTH fields are added: older v4 signature verification re-encodes AUTH and would drop unknown fields. Older clients never report features: new category/forum kinds are hidden from their channel lists and metadata delivery; existing text/voice remain usable. New clients talking to old gateways disable unsupported controls/packets. + +## Settings + +`0x0230 NodeSettingsRequest {request_id=1,update=2,display_name=3,description=4}`. `0x0231 NodeSettingsResult {request_id=1,display_name=2,description=3,error_code=4,error=5}`. Reads return public data to authenticated clients; updates require the exact session's configured node-admin Central UUID. Name is trimmed, 1–100 characters, no control/bidi override characters. Description is trimmed, at most 2000 characters, permits newline/tab but no other controls/bidi overrides. Success persists before ACK and updates HELLO's node name; future restarts use the stored name/description, config name is only an initial fallback. Changes broadcast Result with empty request_id to feature-capable authenticated clients. Errors are correlated Results, never unrelated join errors. + +## Categories and layout + +Channel kinds are `text`, `voice`, `category`, `forum`. Add parent_id/position to ChannelCreate (5/6), ChannelState (7/8), ChannelListItem (5/6); ChannelEdit optionally changes parent_id (3) and position (4), absent means unchanged, present empty parent means detach to root. Position is 0–1000000, stable ties use channel ID. Categories always live at root; a child's parent must be an existing category in the exact same global/guild scope. No nested categories, cycles, cross-guild parents or implicit channel replacement. + +Node administrators manage global structure; guild management still requires the existing owner/MANAGE_CHANNELS policy. Category JoinChannel is rejected before changing current text/voice subscriptions. Forum JoinChannel is view-only (no generic chat history, audio or UDP credentials). Deleting a category atomically reparents children to root and deletes only the grouping metadata. Bootstrap general/voice IDs remain undeletable. A forum containing any topic cannot be deleted; guild deletion containing forum topics is also refused, preserving discoverability and all replies. + +## Forums + +`0x0240 ForumRequest`: request_id (1), ForumOperation operation (2), channel_id (3), topic_id (4), title (5), content (6), offset (7), limit (8), message_id (9). Enum UNSPECIFIED=0, LIST=1, CREATE=2, OPEN=3, REPLY=4. + +`0x0241 ForumResult`: request_id (1), operation (2), channel_id (3), topic_id (4), topics (5), posts (6), has_more (7), error_code (8), error (9). Every response/error is correlated. `0x0242 ForumChanged`: channel_id (1), topic (2), post (3), broadcast only to current authorized forum subscribers and rechecked at delivery. + +ForumTopic: topic_id (1), channel_id (2), title (3), MemberInfo author (4), created_at (5), updated_at (6), reply_count (7). ForumPost: ChatMessage message (1), MemberInfo author (2), topic_id (3), uint64 ordinal (4). Author IDs/account/name/avatar originate solely from the authenticated verified session. Snapshot names/avatar allow offline authors; clients retain newer live profile cache entries when displaying snapshots. + +LIST defaults to 20, maximum 50 topics; OPEN defaults to 50, maximum 100 posts; offsets at most 100000. Handler fetches limit+1 to determine has_more. Titles are trimmed, 1–160 characters, without control/bidi overrides. Bodies are trimmed, 1–16000 characters and at most 64 KiB, newline/tab permitted. CREATE requires stable client UUID topic_id and message_id; REPLY requires stable message_id. Retries reuse IDs and must match channel/topic/author device+account/title/body to be idempotent; conflicts fail without linking unrelated/private messages. Topic and first message commit atomically. Per-topic ordinal guarantees deterministic reply order even within one millisecond. + +Posts reuse existing messages under the forum channel ID plus an explicit topic mapping. Reads always constrain both channel and topic. Generic chat sends/history, message edits/deletes/reactions and attachment uploads do not mutate forum posts; dedicated ForumRequest is the only posting API in this scope. No topic/post deletion is exposed. Forum access requires node-global visibility or guild membership; guild posting additionally requires owner/MESSAGE_SEND, node administration does not bypass guild policy. Rate limits and shared-access guards protect posting versus concurrent channel/access changes. CREATE/REPLY clients refresh signed profile through the existing bounded activity queue, without interrupting voice. + +All deferred administrative or forum actions are fenced to the authenticated client session so queued operations cannot execute on a different node after switching. diff --git a/docs/02-protocol/security.md b/docs/02-protocol/security.md index 9b032a3..455f2f0 100644 --- a/docs/02-protocol/security.md +++ b/docs/02-protocol/security.md @@ -1,6 +1,6 @@ # Security -> **LNEx v3 status.** TCP traffic runs over TLS 1.3. +> **LNEx v4 status.** TCP traffic runs over TLS 1.3. > UDP voice path is still plaintext (Phase 2). See > [features/encryption.md](../05-features/encryption.md) for details. @@ -31,7 +31,7 @@ The threat model covers: ## Encryption -TCP control traffic is protected by **TLS 1.3** (rustls, ALPN `vnox-lnex/3`). The earlier +TCP control traffic is protected by **TLS 1.3** (rustls, ALPN `vnox-lnex/4`). The earlier LNEx v2 per-frame ChaCha20-Poly1305 layer is removed. Trust model, certificate handling, channel binding and version bump: see [tls.md](tls.md). @@ -116,3 +116,7 @@ temporary ban depending on node configuration. - rotate node keypair on suspected compromise See `03-server/operations.md` for hardening checklist. + +LNEx v4 additionally requires a short-lived, issuer-signed Central account proof +bound to the client Ed25519 key, before any session or user write. Private mode +has the same requirement. See [Central account admission](central-account.md). diff --git a/docs/02-protocol/tls.md b/docs/02-protocol/tls.md index 9357ccc..5d033f2 100644 --- a/docs/02-protocol/tls.md +++ b/docs/02-protocol/tls.md @@ -1,8 +1,10 @@ -# TLS 1.3 for the TCP control plane (LNEx v3) +# TLS 1.3 for the TCP control plane (LNEx v4) -> Status: design accepted and implemented together with this document. +> Current version: LNEx v4. TLS was introduced in v3; v4 adds mandatory +> [Central account admission](central-account.md), preserving the TLS security model. +> This document describes the current v4 transport parameters. > This is a **breaking change**: LNEx v2 (custom X25519 + HKDF + ChaCha20-Poly1305 -> layer on TCP) is removed. The protocol version is bumped to `v3`. +> layer on TCP) is removed. The protocol version is bumped to `v4`. > UDP voice is **not** affected (see [Voice](#voice-udp-is-unchanged)). ## Goals @@ -27,7 +29,7 @@ | # | Decision | |---|----------| -| D1 | `rustls` 0.23 with the `aws_lc_rs` provider, **TLS 1.3 only** on both sides. ALPN `vnox-lnex/3` is required. | +| D1 | `rustls` 0.23 with the `aws_lc_rs` provider, **TLS 1.3 only** on both sides. ALPN `vnox-lnex/4` is required. | | D2 | The server certificate is either user supplied (`tls_cert_path` / `tls_key_path`, for example Let's Encrypt) or **auto generated** on first start and stored in `data_dir/tls/self-signed.pem`. | | D3 | The auto generated certificate uses the **server identity Ed25519 key as its TLS key**. Therefore the certificate fingerprint is a pure function of the identity key, survives certificate regeneration, and no second private key is stored. | | D4 | The **pin** is `SHA-256(SubjectPublicKeyInfo DER)` of the leaf certificate (the "SPKI fingerprint"). It is the trust anchor for self-signed servers. | @@ -35,7 +37,7 @@ | D6 | A changed fingerprint is a **hard block** with a warning. There is no "connect anyway". The only way forward is an explicit "forget this server" followed by a new trust decision. | | D7 | The Ed25519 client authentication stays, but its signature covers a **TLS exporter value (channel binding)**, so it cannot be relayed over another TLS connection. The server HELLO is signed with its identity key over the same binding. | | D8 | The X25519 + HKDF + ChaCha20-Poly1305 layer on TCP, the `ENCRYPTED` packet flag on TCP and the ephemeral key fields in `Hello` and `Auth` are removed. | -| D9 | LNEx version is bumped from `v2` to `v3`. Old clients and old servers fail with an explicit error. Downgrade is impossible by construction. | +| D9 | LNEx version is bumped from `v2` to `v4`. Old clients and old servers fail with an explicit error. Downgrade is impossible by construction. | ## Threat model @@ -45,7 +47,7 @@ | Active MITM terminating TLS with its own certificate | Pin mismatch (known server) or TOFU prompt showing a different fingerprint (new server). Even if the user accepted a wrong fingerprint, the MITM cannot produce the server's Ed25519 signature over this connection's exporter, and the client rejects a server identity that differs from the stored one. | | MITM relaying the server's HELLO to the client | HELLO signature covers the exporter of the TLS session between the client and the relay, which differs from the relay to server session. Verification fails. | | Malicious server relaying a client's AUTH to another server | AUTH signature covers the exporter of the connection to the malicious server. Another server computes a different value and rejects it. | -| Downgrade to plaintext, TLS 1.2 or LNEx v2 | The client never opens a plaintext socket, the rustls configs list only TLS 1.3, the server requires ALPN `vnox-lnex/3`, and the version string `v3` is inside the signed transcripts. | +| Downgrade to plaintext, TLS 1.2 or LNEx v2 | The client never opens a plaintext socket, the rustls configs list only TLS 1.3, the server requires ALPN `vnox-lnex/4`, and the version string `v4` is inside the signed transcripts. | | First connection MITM (TOFU weakness) | Unavoidable without a CA or out of band data. Mitigation: the server prints its fingerprint at startup and writes it to `data_dir/tls/fingerprint.txt`; owners should publish it with the invitation, users compare before confirming. Public CA certificates skip TOFU. | | Compromised or replaced server key | Fingerprint changes, clients block (D6). | @@ -82,7 +84,7 @@ At startup the gateway logs `TLS SPKI SHA-256 fingerprint: ` and writes it - Protocol versions: TLS 1.3 only (`rustls::version::TLS13`). - Cipher suites: the rustls defaults for TLS 1.3 (AES-GCM, ChaCha20-Poly1305). -- ALPN: `vnox-lnex/3`. A connection without the negotiated ALPN is dropped before any +- ALPN: `vnox-lnex/4`. A connection without the negotiated ALPN is dropped before any LNEx byte is read. - No client certificates. - Handshake timeouts: 15 seconds for the TLS handshake and 15 seconds for the LNEx HELLO/AUTH/SESSION exchange. @@ -99,7 +101,7 @@ The server peeks at the first byte of a new connection (up to 2 seconds). A TLS client sends a handshake record (`0x16`) immediately. Anything else (a v2 client waits for the server HELLO and sends nothing, a plaintext client sends LNEx bytes) receives one plaintext LNEx `Error` frame, `ErrorCode::VersionMismatch`, with the message -`TLS 1.3 required (LNEx v3): update your client`, and the connection is closed. This +`TLS 1.3 required (LNEx v4): update your client`, and the connection is closed. This frame is the only plaintext the gateway ever writes and it contains no secrets. ## Client @@ -150,7 +152,7 @@ TLS 1.3 handshake with a custom ServerCertVerifier: UI on UntrustedServer: show host, fingerprint (grouped hex), "Trust" / "Cancel". Trust -> trust_store[host:port] = { pin, fingerprint }, reconnect. -LNEx v3 handshake inside the tunnel (see below) +LNEx v4 handshake inside the tunnel (see below) after a valid server HELLO: rec.identity set and != server_pubkey -> ERROR IdentityChanged (hard block) otherwise store identity (TOFU of the identity inside an already trusted channel) @@ -175,7 +177,7 @@ When a pinned server presents another SPKI the client: server migration (for example moving from a self-signed certificate to Let's Encrypt or restoring a server without its `server_identity.json`). -## LNEx v3 handshake inside TLS +## LNEx v4 handshake inside TLS The 12 byte packet header and protobuf payloads are unchanged. Frames are written directly into the TLS stream. The `flags` field must be `0` on the TCP control plane @@ -184,10 +186,10 @@ directly into the TLS stream. The `flags` field must be `0` on the TCP control p ``` Client Gateway | -- TCP + TLS 1.3 handshake (ALPN) ---> | - | both sides: binding = Export("EXPORTER-VNOX-LNEx-v3-channel-binding", no context, 32) - | <------ HELLO (seq 0) ---------------- | lnex_version="v3", server_pubkey, challenge_nonce, + | both sides: binding = Export("EXPORTER-VNOX-LNEx-v4-channel-binding", no context, 32) + | <------ HELLO (seq 0) ---------------- | lnex_version="v4", server_pubkey, challenge_nonce, | | node_name, private_mode, server_signature - | ------- AUTH (seq 0) ----------------> | client_pubkey, nickname, lnex_version="v3", signature + | ------- AUTH (seq 0) ----------------> | client_pubkey, nickname, lnex_version="v4", signature, account_proof | <------ SESSION (seq 1) -------------- | session_id, token, expires_at ``` @@ -195,10 +197,10 @@ Signatures (Ed25519, strict verification): ``` server_signature = Sign(server_key, - "VNOX-LNEx-v3-server-hello\0" || binding || protobuf(Hello with server_signature empty)) + "VNOX-LNEx-v4-server-hello\0" || binding || protobuf(Hello with server_signature empty)) client signature = Sign(client_key, - "VNOX-LNEx-v3-client-auth\0" || binding + "VNOX-LNEx-v4-client-auth\0" || binding || u32be(len(signed_hello)) || signed_hello || protobuf(Auth with signature empty)) ``` @@ -238,18 +240,18 @@ needs the client to trust the gateway certificate (`--trust-first-use` or `--fin | Client | Server | Result | |--------|--------|--------| -| v3 | v3 | works | -| v2 (old) | v3 | server sends a plaintext `Error(VersionMismatch)` "TLS 1.3 required (LNEx v3): update your client"; an old client shows a handshake error or times out. Nothing is negotiated. | -| v3 | v2 (old) | the server answers the TLS ClientHello with LNEx bytes. The client maps the rustls decode failure to "The server does not speak TLS 1.3 (LNEx v3). It is probably an outdated server, ask the owner to update." No retry in plaintext. | -| v3, TLS 1.2 only peer | v3 | handshake failure, protocol version alert, same explicit message. | -| v3 | v3 with `lnex_version` other than `v3` in HELLO/AUTH | `VersionMismatch`, connection closed. | +| v4 | v4 | works | +| v2 (old) | v4 | server sends a plaintext `Error(VersionMismatch)` "TLS 1.3 required (LNEx v4): update your client"; an old client shows a handshake error or times out. Nothing is negotiated. | +| v4 | v2 (old) | the server answers the TLS ClientHello with LNEx bytes. The client maps the rustls decode failure to "The server does not speak TLS 1.3 (LNEx v4). It is probably an outdated server, ask the owner to update." No retry in plaintext. | +| v4, TLS 1.2 only peer | v4 | handshake failure, protocol version alert, same explicit message. | +| v4 | v4 with `lnex_version` other than `v4` in HELLO/AUTH | `VersionMismatch`, connection closed. | There is no version negotiation and no compatibility window: the old handshake is not implemented in the new binaries. ## Operations: enabling it on a server -1. Update the gateway (and `serverd`) to the LNEx v3 build. Remove `tls_enabled = false` +1. Update the gateway (and `serverd`) to the LNEx v4 build. Remove `tls_enabled = false` if present. 2. Self-signed: nothing to configure. Start the gateway, read the fingerprint from the log or `data_dir/tls/fingerprint.txt`, publish it to your users. diff --git a/docs/03-server/README.md b/docs/03-server/README.md index a3ce217..3b54b6b 100644 --- a/docs/03-server/README.md +++ b/docs/03-server/README.md @@ -1,14 +1,20 @@ # Server / Gateway -A VNOX node consists of two processes: +A VNOX node contains two components: ``` gateway — TCP, handles auth / chat / channels / permissions voice-node — UDP, handles voice relay ``` -Both are written in Rust. They can run on the same machine or separately. -For most self-hosted setups, running both on one machine is fine. +Both are written in Rust. Run `vnox-serverd` for working voice: it connects the +gateway to voice-node through an in-process authenticated credential bridge. +Standalone gateway and voice-node binaries exist, but separate-process signaling +is not implemented; standalone voice-node fails closed without credentials. + +The optional central account service is `vnox-hub`. It provides browser sign-in, +profile/handle selection and own-avatar APIs; it does not relay community messages +or voice. See [Hub setup](../../hub/README.md) and [account API](../../hub/AUTH.md). --- diff --git a/docs/03-server/configuration.md b/docs/03-server/configuration.md index d2dddd2..7151c4a 100644 --- a/docs/03-server/configuration.md +++ b/docs/03-server/configuration.md @@ -219,3 +219,22 @@ vnox-gateway --config /etc/vnox/config.toml --check ``` Exits 0 if config is valid, prints errors otherwise. + +## Mandatory Central account admission (LNEx v4) + +Every gateway connection needs a short-lived Central account proof, signed by the +explicitly configured issuer and bound to the client Ed25519 identity. Private mode +uses the same requirement. There is no anonymous admission switch. Configure the +public issuer identity obtained through your trusted Central operator: + +```toml +[gateway.central_account] +issuer = "" +public_key = "<64-character Ed25519 public key hex>" +``` + +Missing or invalid trust rejects all account admissions. The gateway never accepts +a trust key from a client and never automatically discovers issuer keys over HTTP. +Central device logout/revocation prevents further proof refresh; previously issued +proofs remain valid for at most ten minutes. Active session lifetime follows the +existing gateway session policy. See [proof format](../02-protocol/central-account.md). diff --git a/docs/05-features/hub-server-directory.md b/docs/05-features/hub-server-directory.md new file mode 100644 index 0000000..83e37f7 --- /dev/null +++ b/docs/05-features/hub-server-directory.md @@ -0,0 +1,25 @@ +# Hub community server directory HTTP contract + +Listings are unverified user submissions, not endorsements or evidence of server ownership, +reachability, protocol compatibility or safety. Hub performs no DNS resolution or network probes. +This is a Hub HTTP API and does not change LNEx. + +- `GET /v1/servers` is public and returns `{ "servers": [...] }`, at most 100 + rows ordered by ID ascending. `?after=` continues after the final ID. +- `POST /v1/servers` requires the existing session cookie, exact Origin and CSRF token. + JSON fields are `name` (1–80 characters), `address` (public DNS/IP and explicit nonzero + port), and `description` (0–500 characters). Strings are trimmed; controls and bidi + formatting are rejected. DNS names become lowercase; IPs and ports are canonicalized. + Success is 201 `{ "server": {...} }`; duplicate normalized addresses return 409 + `address_taken`. A user can hold at most 10 listings (409 `listing_limit`). +- `DELETE /v1/servers/{id}` requires the same session, Origin and CSRF protections. + Owners receive 204; missing and other users' listings receive 404 `server_not_found`. + +Each record has `id`, `name`, `address`, `description`, `owner_id`, `created_at` +(Unix seconds). Addresses are globally unique; account deletion cascades to listings. +Mutation bodies are capped at 4096 bytes; JSON errors return 400 `invalid_request` +(or 413 when oversized), invalid field values return 422 `invalid_server`. +Mutation attempts share an independent per-account 10/hour and source 30/hour budget; +public reads use an independent 20/source and 300/global minute budget. Exhaustion returns 429 +`try_later`. Storage errors return 503 `try_later`. The proxy must overwrite +X-Forwarded-For as required by the existing Hub authentication contract. diff --git a/docs/06-roadmap.md b/docs/06-roadmap.md index 542ba8a..6e851e6 100644 --- a/docs/06-roadmap.md +++ b/docs/06-roadmap.md @@ -1,5 +1,91 @@ # Roadmap +## Active request 2026-11-01: account presence and profiles + +- [ ] Correct multi-session account presence: one visible member per account, + preserve membership/presence until its last relevant session leaves, and test + two sessions closing in either order. +- [ ] Define profile/avatar propagation to desktop before protocol additions; + browser login must not leave the desktop showing a disconnected local profile. +- [ ] Client remembers successful server selection and reconnects after account + restoration; visual QA must run only on an isolated virtual display/profile. + +## Active request 2026-11-01: image/GIF attachments + +- [ ] Specify durable image/GIF upload, download, ownership, access checks, + storage quotas and bounded decoding before implementation/protocol changes. +- [ ] Implement authenticated attachment persistence and client clipboard image + uploads with preview/cancel; receivers must actually obtain the uploaded media. +- [ ] Add bounded direct-HTTPS image/GIF display in chat, with animation and + failures handled without blocking the UI. +- [ ] Deferred: voice messages, explicitly excluded from the current request. + +## Active request 2026-11-01: desktop account enforcement + +- [ ] Specify desktop authorization/device enrollment, proof binding to the + client's Ed25519 identity, gateway issuer trust, expiry/revocation and outage + behavior before implementation. +- [ ] Require an authenticated Central account in both the desktop connection + flow and gateway admission; a client-only gate is insufficient. +- [ ] Bump LNEx version for breaking admission/handshake changes and update tests. +- [ ] Preserve browser sessions and show own/generated VNOX avatars in the site + header; uploads remain bounded, re-encoded and served from Hub storage. +- [ ] Validate deployment and new desktop installation before declaring complete. + +Updated: 2026-10-25. Historical phase checklists below are not release acceptance +criteria. For actual behavior and limitations, see [current status](00-status.md). + +## Audit 2026-11-01: ownership and client administration + +See [ownership/admin audit](design/ownership-and-admin-audit.md) for verified code +paths and missing flows. Guild creators already become owners; a VPS operator +does not automatically receive account-level node administration. Ownership +transfer is not implemented. The Slint management UI is incomplete despite the +historical feature checklists below. + +- [ ] Specify node-admin enrollment and ownership transfer before LNEx changes. +- [ ] Replace the misleading full-permission `@everyone` owner role with a safe, + explicitly migrated default-role model; do not auto-assign the existing role. +- [ ] Preserve `owner_id` and server-confirmed role/member data in the client and + implement guild, invite, channel and role management screens. +- [ ] Correct `/version` reporting `lnex_version: "1"` for the LNEx v3 gateway. + +## Delivered 2026-11-01: public community directory + +- [x] Public website `/servers` with RU/EN browsing, search of loaded entries, + address copying and pagination. +- [x] Durable Hub HTTP directory with authenticated submissions and owner-only + removal, bounded fields, unique normalized addresses and independent rate limits. +- [x] Published and checked at `https://central.loki-code.dev/servers`. + +Listings are unverified announcements. This does not implement cryptographic +node registration, mandatory Hub login at gateways, ownership transfer or +federation. See [HTTP contract](05-features/hub-server-directory.md). + +## Next server milestones + +1. Hub account/profile foundation: email and GitHub browser sign-in, own avatar API, + and unique handle selection API are implemented. Add browser onboarding and a + separately specified desktop authorization/device-enrollment flow next. +2. Define how community gateways verify Hub account/device proofs, including + revocation, migration from local identities and Hub outages. Mandatory Hub login + is not enforced today. Document the LNEx contract before implementation. +3. Define profile/avatar distribution to other clients, cache invalidation and + visibility policy. Current Hub own-avatar API does not synchronize desktop avatars. +4. Implement authenticated separate-process gateway/voice-node signaling. The + combined `serverd` already has an in-process credential bridge. +5. Implement and verify off-host paired issuer/database backups and 30-day rotation + for deleted account/avatar data. Current deployment notes describe a foundation + snapshot, not ongoing backup assurance. + +### Completed in this server batch + +- [x] Authenticated `PUT /v1/account/profile` with Origin/CSRF checks and bounded body. +- [x] Case-insensitive unique permanent handle selection and editable display name. +- [x] Registration-completion flag in session/login responses and migration for existing accounts. +- [x] Validation, rate limits and tests for concurrent claims and stale sessions. + + --- ## Phase 1 — MVP @@ -9,7 +95,7 @@ Goal: a working voice + chat system you can actually use. ### Server - [x] LNEx v1 protocol spec - [x] Gateway: auth, sessions, channels, text chat -- [x] Voice node: UDP relay (jitter buffer code exists, not wired into relay yet) +- [x] Voice node: authenticated encrypted UDP relay with per-sender jitter buffers - [x] SQLite storage: message history, user records - [x] Docker + systemd deployment ([Dockerfile](../Dockerfile), [docker-compose.yml](../docker-compose.yml)) - [x] config.toml reference ([configuration.md](../03-server/configuration.md), [dev/README.md](../../dev/README.md)) @@ -157,11 +243,11 @@ Goal: scalability, federation preparation, advanced security. - [ ] Rate limiting refinement - [ ] PostgreSQL backend (sqlx support ready, config not exposed yet) - [ ] Prometheus metrics on gateway and voice-node -- [ ] Gateway admin API (HTTP endpoint) -- [ ] Identity keypair encryption at rest (Argon2id passphrase) +- [x] Gateway HTTP health/version/metrics foundation (full management API remains separate work) +- [x] Identity keypair encryption at rest (Argon2id passphrase) ### Protocol -- [ ] Protobuf schemas (replacing JSON) +- [x] Protobuf schemas (replacing JSON) - [ ] E2EE for direct messages (optional) - [ ] E2EE for private channels (opt-in) - [ ] Voice regions: Warsaw, Frankfurt, Amsterdam, London, New York, Singapore @@ -212,3 +298,58 @@ Goal: federation ecosystem and mobile clients. Spatial audio for games, adaptive bitrate relay, distributed mesh, VNOX SDK (Rust → TypeScript → Python), offline message queue. + +## Account presence and Central member profiles (2026-11-01) + +- [ ] Keep authenticated Central account IDs separate from Ed25519 device IDs. Channel subscriptions and voice routes remain session/device scoped; account member rows aggregate devices. Closing either of two sessions must retain the other session, member and online presence. +- [ ] Distribute Central-signed display names and same-origin avatar paths in additive member metadata; fetch bounded PNGs without forwarding credentials, cache successful results, and retain initials on errors. Preserve guild ownership and historical device IDs. +- [ ] Verify two-device and repeated-device lifecycle, profile propagation and rejected avatar origins with regression tests. + +## Desktop Central avatar correction (2026-11-01) + +- [x] Refresh and persist the Central summary at admission and bounded background/profile-view refresh; deliver updates to UI without reading credentials every tick. Drop late results after logout or enrollment replacement. +- [x] Restore desktop photo choose/crop/upload/delete against Central enrollment-authenticated endpoints. Reuse normalized PNG storage/default avatar and account/IP budgets; no browser cookie or profile migration 6. +- [x] Verify website-avatar refresh, authenticated PNG upload/delete, device binding, expired/revoked secrets, invalid image limits, and summary/UI propagation. + +- [x] Add desktop editable Central display_name with signed enrollment-authorized profile mutation, existing 64-character/control validation, persisted summary/watch update and name-adjacent Save/status controls. Unique handle and ownership IDs remain unchanged. + +Desktop name/photo mutations update the own account UI and persisted enrollment immediately. Live signed profile propagation below supersedes the original admission-only peer refresh limitation; no forced reconnect is used. + +## Live signed Central profile propagation (2026-11-01) + +- [x] Implement additive ProfileUpdate/ProfileUpdated after the documented contract: verify issuer/device/account/lifetime, guard monotonic issued_at, update only profile metadata and target currently authorized shared-channel peers. +- [x] Verify chat/voice refresh with two clients and no disconnect or membership changes; preserve ownership, device IDs, roles and UDP routes. + +## Explicit node administrator capability (2026-11-01) + +- [x] Validate gateway.admin_account_ids UUID allowlist at startup; Session.node_admin advertises only global channel CRUD. Match verified account_id, preserve guild permissions and bootstrap channel IDs. +- [x] Regression checks authorized account/second device, nonadmin and cross-guild denial; metadata fanout and persistent global channel reload. + +- [x] Negotiate Session.live_profiles (field 5): legacy v4 gateways receive no ProfileUpdate packets and no profile-based activity delay; capable gateways retain proof-before-action ordering. + +## Node settings, categories and real forums (2026-11-01) + +- [ ] Follow [node feature contract](02-protocol/node-features.md) before implementation: negotiated capabilities/client feature report, persisted name/description and same-scope one-level layout. +- [ ] Add category grouping and real topics/replies with correlated errors, idempotent stable mutation IDs, bounded reads and preserved guild permissions. +- [ ] Reject category joins/unsupported forum chat mutations; dissolve category grouping and refuse destructive nonempty forum/guild deletion. +- [ ] Verify authenticated admin controls, persistence/restart, cross-scope denial, legacy visibility, no voice capture, topic/post isolation and concurrent/idempotent requests. + +## Контрольная точка: остановлено по просьбе пользователя — 2026-11-01 + +Все три агента остановлены. Работающие версии оставлены установленными: +- Client: `20261101-f16a49f9c678` (218 тестов, 2 ignored, Clippy проходил). +- Gateway VPS: `20261101-live-capability-219376accf98`. +- Central: `20261101-live-admin-91595d45652a`; Web: `20261101-names-monsters-ff473f423565`. +- Аккаунт `loki` имеет права управления глобальными каналами узла. + +В коммитах сохранён НЕЗАВЕРШЁННЫЙ следующий этап, не опубликованный в production: +- меню заголовка/ПКМ, настройки имени/описания узла, категории и форумы; +- серверное хранилище уже имеет 6 проходивших focused-регрессий, но общий + финальный client/server build и интеграционная проверка ещё не завершены. +- версии/автообновление/релиз Linux+Windows пока только запланированы. + +Продолжение: сначала завершить сборку и общие тесты текущего черновика, затем +проверить voice relay/чат/форумы в изолированной среде, собрать и развернуть; +после этого реализовать updater с подписанным manifest и выпустить бинарники +в https://git.wihuk.pro/boba/VNOX-Client.git. Cargo.repository уже исправлен. +Не запускать тестовые приложения на рабочем столе пользователя. diff --git a/docs/design/accounts-and-avatars.md b/docs/design/accounts-and-avatars.md index dea760b..375e3ed 100644 --- a/docs/design/accounts-and-avatars.md +++ b/docs/design/accounts-and-avatars.md @@ -1,6 +1,6 @@ # Vnox: регистрация, устройства и аватары -Статус: продуктовая и техническая спецификация. Регистрация Hub пока не реализована. Клиентский редактор аватара реализуется отдельно; до появления API сохраняет реальное изображение локально. +Статус на 2026-10-25: сервер Hub поддерживает браузерный вход через GitHub и почтовый код, API выбора уникального handle и отображаемого имени, а также собственные аватары. Контракт: `hub/AUTH.md`. Экран завершения регистрации, авторизация desktop, привязка устройств и проверка аккаунта на gateway ещё не реализованы. Редактор аватара desktop сохраняет изображение локально; синхронизации с Hub и другими участниками пока нет. Последующие разделы описывают целевую модель и не означают готовность всех функций. ## Вход и создание аккаунта diff --git a/docs/design/ownership-and-admin-audit.md b/docs/design/ownership-and-admin-audit.md new file mode 100644 index 0000000..d1714f6 --- /dev/null +++ b/docs/design/ownership-and-admin-audit.md @@ -0,0 +1,74 @@ +# Владение узлом и сообществом: состояние реализации + +Обновлено 2026-11-01 после проверки и установки LNEx v4. Администраторы узла +задаются оператором в `[gateway] admin_account_ids = ["Central-account-UUID"]`. +Проверяется аккаунт конкретной аутентифицированной сессии; имя и ключ другого +устройства сами по себе прав не дают. Администратор управляет глобальными +текстовыми/голосовыми каналами; права внутри сообществ остаются отдельными. +Клиент получает capability `Session.node_admin` и показывает создание, +переименование и подтверждение удаления каналов. Решение всегда принимает сервер. + +Это аудит поведения и список следующих шагов, а не замена документации протокола. + +## Что уже есть + +- При `GuildCreate` gateway берёт владельца из аутентифицированной сессии, + а не из пользовательского запроса. Создатель записывается в `guilds.owner_id` + и становится участником своего сообщества. +- Владелец проходит проверки прав своего сообщества независимо от ролей. + Удаление сообщества разрешено только владельцу; владельца нельзя исключить. +- Роли содержат битовую маску, включая `MANAGE_ROLES`, `MANAGE_CHANNELS`, + `KICK_MEMBERS` и `ADMINISTRATOR`. Назначение роли проверяет принадлежность + роли и участника тому же сообществу. Ограниченный управляющий ролями не может + выдать права, которых у него нет. +- Это права сообщества. Для глобальных каналов действует отдельный список + `gateway.admin_account_ids`. Базовые `general` и `voice` нельзя удалить. + Адрес `gateway.admin_bind` предоставляет health/version/metrics и не выдаёт админку. + +Источники: `gateway/src/handler/guild/`, +`gateway/src/domain/storage/guilds/`, [правила авторизации](channel-authorization.md). + +## Чего нет и что требует исправления + +1. Установка сервера на VPS не привязывает автоматически аккаунт оператора. + Настройка `admin_account_ids` уже реализована; root/оператор явно указывает + Central UUID. Одноразовый claim/bootstrap через клиент пока не реализован. + Нельзя выдавать админку первому подключившемуся. +2. Передача `owner_id` другому участнику не реализована. В спецификации + [Community Model](../07-community-model.md) поле пока обозначено immutable. + Нужны сначала контракт и политика восстановления, затем атомарная операция + с проверкой текущего владельца, целевого участника, подтверждением и аудитом. +3. Создаваемая роль `@everyone` содержит `u64::MAX` и назначается только + создателю. Новые участники не получают её автоматически. Поэтому сейчас это + фактически административная роль с неверным названием, а не базовые права + всех участников. Нельзя начать автоматически назначать её всем: это выдаст + административные права. Нужны безопасные базовые права и явная миграция. +4. В текущем Slint-клиенте есть сетевые команды создания сообщества, ролей, + приглашений и назначения ролей, но нет законченного интерфейса управления. + `GuildState` теряет `owner_id` при обработке списка и создания сообщества; + результаты создания ролей и приглашений пока не используются интерфейсом. +5. LNEx v4 связывает Central-аккаунт с ключом устройства подписанным proof. + Владение существующим сообществом по-прежнему определяется device identity; + простой вход в Central не переносит это владение. Важно сохранять её ключ. + +## Следующие шаги клиента + +1. Сохранить `owner_id`, участников, роли и подтверждённые сервером права + в состоянии клиента; показывать владельца и доступные действия. +2. Доделать создание сообщества, приглашения, управление каналами, ролями, + назначениями и исключением участников. Обрабатывать отказ сервера и + обновление прав во время открытого окна управления. +3. Добавить получение админки узла и передачу владения только после согласования + соответствующих серверных контрактов; интерфейс не может сам выдать права. +4. Добавить каталог серверов/переход с сайта отдельно от получения прав. + Автор объявления в каталоге не считается владельцем VPS или сообщества. + +## Проверка существующего хостинга + +На 2026-11-01 узел `185.176.94.38:7600` работает через `vnox-server.service` +(`vnox-serverd`, автозапуск включён), голосовой UDP-порт `7700` используется +без изменения протокола. Health отвечает `ok`, gateway сообщает LNEx v4. +Оператором настроены права управления глобальными каналами аккаунта `loki`. +Проверены подпись аккаунта, второе устройство, отказ другому аккаунту с тем же +ключом, отсутствие обхода guild-прав и сохранение каналов после загрузки БД. +Это не подтверждает слышимость реального микрофона и воспроизведения. diff --git a/gateway/Cargo.toml b/gateway/Cargo.toml index 0120eed..f22fa5d 100644 --- a/gateway/Cargo.toml +++ b/gateway/Cargo.toml @@ -17,6 +17,7 @@ name = "vnox-gateway" path = "src/main.rs" [dependencies] +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif"] } tokio.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/gateway/src/admin/mod.rs b/gateway/src/admin/mod.rs index 4b82387..3a71ca4 100644 --- a/gateway/src/admin/mod.rs +++ b/gateway/src/admin/mod.rs @@ -63,7 +63,7 @@ async fn version(State(_s): State) -> impl IntoResponse { let body = serde_json::json!({ "name": "vnox-gateway", "version": env!("CARGO_PKG_VERSION"), - "lnex_version": "1", + "lnex_version": crate::net::handshake::LNEX_VERSION, }); (StatusCode::OK, axum::Json(body)) } diff --git a/gateway/src/domain/account.rs b/gateway/src/domain/account.rs new file mode 100644 index 0000000..d6dbdc7 --- /dev/null +++ b/gateway/src/domain/account.rs @@ -0,0 +1,213 @@ +//! Central-issued, identity-bound account proofs required for LNEx admission. +use anyhow::{Context, Result, ensure}; +use ed25519_dalek::{Signature, VerifyingKey}; +use serde::Deserialize; + +use super::config::CentralAccountConfig; + +const CONTEXT: &[u8] = b"VNOX-CENTRAL-ACCOUNT-v1\0"; +const MAX_PROOF_BYTES: usize = 8192; +const MAX_LIFETIME: i64 = 600; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Envelope { + claims: String, + signature: String, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Claims { + #[serde(default)] + display_name: Option, + #[serde(default)] + avatar_url: Option, + version: u32, + issuer: String, + subject: String, + public_key: String, + issued_at: i64, + expires_at: i64, + audience: String, +} + +#[derive(Debug, Clone)] +pub struct AccountClaims { + pub issued_at: i64, + pub account_id: String, + pub display_name: Option, + pub avatar_url: Option, +} + +pub fn safe_avatar_path(path: &str) -> bool { + path.len() <= 256 + && path.starts_with("/v1/avatars/") + && path + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"/-".contains(&b)) +} + +/// Verify an account proof against configured issuer trust and the authenticated device key. +/// Returns the Central account UUID; no input key can establish its own trust. +pub fn verify( + proof: &[u8], + device_public_key: &[u8; 32], + trust: Option<&CentralAccountConfig>, + now: i64, +) -> Result { + let trust = trust.context("Central account issuer is not configured")?; + ensure!( + !proof.is_empty() && proof.len() <= MAX_PROOF_BYTES, + "missing or oversized account proof" + ); + let envelope: Envelope = serde_json::from_slice(proof).context("invalid account proof")?; + let claims_bytes = hex::decode(&envelope.claims).context("invalid account claims encoding")?; + let signature = Signature::from_slice(&hex::decode(&envelope.signature)?)?; + let public_key: [u8; 32] = hex::decode(&trust.public_key)? + .try_into() + .map_err(|_| anyhow::anyhow!("invalid Central issuer key"))?; + let key = VerifyingKey::from_bytes(&public_key)?; + let mut signed = CONTEXT.to_vec(); + signed.extend_from_slice(&claims_bytes); + key.verify_strict(&signed, &signature) + .context("invalid account proof signature")?; + let claims: Claims = serde_json::from_slice(&claims_bytes).context("invalid account claims")?; + ensure!( + claims.version == 1 && claims.audience == "vnox-gateway", + "unsupported account proof" + ); + let issuer = uuid::Uuid::parse_str(&claims.issuer).context("invalid account issuer")?; + ensure!( + issuer == uuid::Uuid::parse_str(&trust.issuer)?, + "untrusted account issuer" + ); + let subject = uuid::Uuid::parse_str(&claims.subject).context("invalid account subject")?; + ensure!(!subject.is_nil(), "invalid account subject"); + ensure!( + claims.public_key == hex::encode(device_public_key), + "account proof belongs to another device" + ); + ensure!( + claims.issued_at > 0 && claims.issued_at <= now, + "account proof issued in the future" + ); + ensure!(claims.expires_at > now, "account proof expired"); + ensure!( + claims + .expires_at + .checked_sub(claims.issued_at) + .is_some_and(|ttl| ttl > 0 && ttl <= MAX_LIFETIME), + "invalid account proof lifetime" + ); + let display_name = claims + .display_name + .filter(|name| !name.trim().is_empty() && name.chars().count() <= 64); + let avatar_url = claims.avatar_url.filter(|path| safe_avatar_path(path)); + Ok(AccountClaims { + issued_at: claims.issued_at, + account_id: subject.to_string(), + display_name, + avatar_url, + }) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use ed25519_dalek::{Signer, SigningKey}; + use serde_json::{Value, json}; + + pub(crate) fn issuer_key() -> SigningKey { + SigningKey::from_bytes(&[73; 32]) + } + pub(crate) fn trust() -> CentralAccountConfig { + CentralAccountConfig { + issuer: "8a1495ae-7174-4d22-94a5-080f61aa1691".into(), + public_key: hex::encode(issuer_key().verifying_key().to_bytes()), + } + } + pub(crate) fn claims(public_key: &[u8; 32], now: i64) -> Value { + json!({"version":1,"issuer":trust().issuer,"subject":"c3ebd30f-6bb4-4c31-9016-bec57e986c93","public_key":hex::encode(public_key),"issued_at":now,"expires_at":now+600,"audience":"vnox-gateway"}) + } + pub(crate) fn sign(claims: Value) -> Vec { + let raw = serde_json::to_vec(&claims).unwrap(); + let mut signed = CONTEXT.to_vec(); + signed.extend_from_slice(&raw); + serde_json::to_vec(&json!({"claims":hex::encode(raw),"signature":hex::encode(issuer_key().sign(&signed).to_bytes())})).unwrap() + } + pub(crate) fn proof(public_key: &[u8; 32], now: i64) -> Vec { + sign(claims(public_key, now)) + } + + #[test] + fn signed_profile_is_bound_to_account_and_unsafe_avatar_is_dropped() { + let mut value = claims(&[9; 32], 1000); + value["display_name"] = json!("Central Name"); + value["avatar_url"] = json!("/v1/avatars/default/c3ebd30f-6bb4-4c31-9016-bec57e986c93"); + let verified = verify(&sign(value.clone()), &[9; 32], Some(&trust()), 1001).unwrap(); + assert_eq!(verified.display_name.as_deref(), Some("Central Name")); + assert!(verified.avatar_url.is_some()); + value["avatar_url"] = json!("https://github.com/avatar"); + assert!( + verify(&sign(value), &[9; 32], Some(&trust()), 1001) + .unwrap() + .avatar_url + .is_none() + ); + } + #[test] + fn trusted_identity_bound_proof_is_accepted() { + assert!(verify(&proof(&[9; 32], 1000), &[9; 32], Some(&trust()), 1001).is_ok()); + } + #[test] + fn missing_proof_and_missing_trust_fail_closed() { + assert!(verify(&[], &[9; 32], Some(&trust()), 1001).is_err()); + assert!(verify(&proof(&[9; 32], 1000), &[9; 32], None, 1001).is_err()); + } + #[test] + fn expiration_future_and_excessive_lifetime_are_rejected() { + for (issued, expires) in [ + (400, 1000), + (1001, 1600), + (1000, 1601), + (1000, 999), + (i64::MIN, i64::MAX), + ] { + let mut value = claims(&[9; 32], 1000); + value["issued_at"] = json!(issued); + value["expires_at"] = json!(expires); + assert!(verify(&sign(value), &[9; 32], Some(&trust()), 1000).is_err()); + } + } + #[test] + fn wrong_identity_issuer_audience_and_signature_are_rejected() { + assert!(verify(&proof(&[9; 32], 1000), &[8; 32], Some(&trust()), 1001).is_err()); + for (field, value) in [ + ("issuer", "45cb4c36-7846-41f3-adf6-39e2e208cd50"), + ("audience", "other"), + ("subject", "not-a-uuid"), + ] { + let mut claims = claims(&[9; 32], 1000); + claims[field] = json!(value); + assert!(verify(&sign(claims), &[9; 32], Some(&trust()), 1001).is_err()); + } + let mut envelope: Value = serde_json::from_slice(&proof(&[9; 32], 1000)).unwrap(); + envelope["signature"] = json!("00".repeat(64)); + assert!( + verify( + &serde_json::to_vec(&envelope).unwrap(), + &[9; 32], + Some(&trust()), + 1001 + ) + .is_err() + ); + } + #[test] + fn malformed_and_oversized_envelopes_are_rejected() { + for proof in [b"{}".to_vec(), b"not-json".to_vec(), vec![b' '; 8193]] { + assert!(verify(&proof, &[9; 32], Some(&trust()), 1001).is_err()); + } + } +} diff --git a/gateway/src/domain/channels/mod.rs b/gateway/src/domain/channels/mod.rs index 9c94289..c10670f 100644 --- a/gateway/src/domain/channels/mod.rs +++ b/gateway/src/domain/channels/mod.rs @@ -1,6 +1,6 @@ pub mod ops; -pub use ops::{create, delete, get_channel, join, leave, list, members, rename}; +pub use ops::{create, delete, get_channel, join, leave, list, members, rename, set_layout}; use std::collections::{HashMap, HashSet}; use std::sync::Arc; @@ -10,6 +10,8 @@ use tokio::sync::RwLock; pub enum ChannelKind { Text, Voice, + Category, + Forum, } impl ChannelKind { @@ -17,6 +19,8 @@ impl ChannelKind { match self { Self::Text => "text", Self::Voice => "voice", + Self::Category => "category", + Self::Forum => "forum", } } } @@ -27,6 +31,8 @@ pub struct Channel { pub name: String, pub kind: ChannelKind, pub guild_id: Option, + pub parent_id: Option, + pub position: i32, pub members: HashSet, } @@ -41,6 +47,8 @@ pub fn new_store() -> ChannelStore { name: "general".into(), kind: ChannelKind::Text, guild_id: None, + parent_id: None, + position: 0, members: HashSet::new(), }, ); @@ -51,6 +59,8 @@ pub fn new_store() -> ChannelStore { name: "voice".into(), kind: ChannelKind::Voice, guild_id: None, + parent_id: None, + position: 0, members: HashSet::new(), }, ); diff --git a/gateway/src/domain/channels/ops.rs b/gateway/src/domain/channels/ops.rs index 2830d34..8aac7cd 100644 --- a/gateway/src/domain/channels/ops.rs +++ b/gateway/src/domain/channels/ops.rs @@ -49,6 +49,8 @@ pub async fn create( name: channel_name.to_string(), kind, guild_id, + parent_id: None, + position: 0, members: std::collections::HashSet::new(), }, ); @@ -75,3 +77,19 @@ pub async fn rename(store: &ChannelStore, channel_id: &str, new_name: &str) -> b pub async fn list(store: &ChannelStore) -> Vec { store.read().await.values().cloned().collect() } + +/// Set persisted one-level parent/order metadata without changing subscriptions. +pub async fn set_layout( + store: &ChannelStore, + id: &str, + parent: Option, + position: i32, +) -> bool { + if let Some(channel) = store.write().await.get_mut(id) { + channel.parent_id = parent; + channel.position = position; + true + } else { + false + } +} diff --git a/gateway/src/domain/config.rs b/gateway/src/domain/config.rs index 0e9cf9b..c965950 100644 --- a/gateway/src/domain/config.rs +++ b/gateway/src/domain/config.rs @@ -75,6 +75,12 @@ pub struct NodeConfig { #[derive(Debug, Deserialize)] pub struct GatewayConfig { + /// Operator-selected Central account UUIDs allowed to manage global node channels. + #[serde(default)] + pub admin_account_ids: Vec, + /// Trusted Central issuer. Missing trust fails closed for every account admission. + #[serde(default)] + pub central_account: Option, pub bind: String, pub max_connections: Option, pub session_timeout: Option, @@ -90,7 +96,7 @@ pub struct GatewayConfig { /// Defaults to 10. #[serde(default)] pub message_rate_burst: Option, - /// Deprecated. TLS 1.3 is always on since LNEx v3. `true` is accepted with a warning, + /// Deprecated. TLS 1.3 is always on since LNEx v4. `true` is accepted with a warning, /// `false` is a startup error because plaintext is no longer supported. #[serde(default)] pub tls_enabled: Option, @@ -103,12 +109,54 @@ pub struct GatewayConfig { pub tls_key_path: Option, } +/// Explicit account proof trust anchor; never accepted from a client or discovered over HTTP. +#[derive(Debug, Deserialize, Clone)] +#[serde(deny_unknown_fields)] +pub struct CentralAccountConfig { + pub issuer: String, + pub public_key: String, +} + impl GatewayConfig { + /// Validate the explicit account allowlist before opening the gateway listener. + pub fn validate_admin_accounts(&self) -> Result<()> { + anyhow::ensure!( + self.admin_account_ids.len() <= 256, + "gateway.admin_account_ids has more than 256 entries" + ); + let mut seen = std::collections::HashSet::new(); + for account in &self.admin_account_ids { + let id = uuid::Uuid::parse_str(account).map_err(|_| { + anyhow::anyhow!("gateway.admin_account_ids must contain Central account UUIDs") + })?; + anyhow::ensure!( + !id.is_nil(), + "gateway.admin_account_ids cannot contain the nil UUID" + ); + anyhow::ensure!( + seen.insert(id), + "gateway.admin_account_ids contains a duplicate account UUID" + ); + } + Ok(()) + } + /// This capability is tied to the admitted Central subject, never a name or device key. + pub fn is_node_admin(&self, account_id: &str) -> bool { + let Ok(account_id) = uuid::Uuid::parse_str(account_id) else { + return false; + }; + !account_id.is_nil() + && self + .admin_account_ids + .iter() + .any(|id| uuid::Uuid::parse_str(id).ok() == Some(account_id)) + } + /// Reject configurations that would weaken or break the mandatory TLS 1.3 listener. pub fn validate_tls(&self) -> Result<()> { if self.tls_enabled == Some(false) { anyhow::bail!( - "gateway.tls_enabled = false is no longer supported: LNEx v3 requires TLS 1.3; remove the option" + "gateway.tls_enabled = false is no longer supported: LNEx v4 requires TLS 1.3; remove the option" ); } if self.tls_cert_path.is_some() != self.tls_key_path.is_some() { @@ -140,7 +188,10 @@ pub fn load() -> Result { let text = std::fs::read_to_string(&path) .map_err(|e| anyhow::anyhow!("cannot read config {path}: {e}"))?; - toml::from_str(&text).map_err(|e| anyhow::anyhow!("invalid config: {e}")) + let config: Config = + toml::from_str(&text).map_err(|e| anyhow::anyhow!("invalid config: {e}"))?; + config.gateway.validate_admin_accounts()?; + Ok(config) } #[cfg(test)] @@ -151,6 +202,35 @@ mod tests { toml::from_str(&format!("bind = \"127.0.0.1:0\"\n{extra}")).unwrap() } + #[test] + fn node_admin_allowlist_accepts_only_explicit_non_nil_account_uuids() { + let none = gateway(""); + none.validate_admin_accounts().unwrap(); + assert!(!none.is_node_admin("nick")); + for invalid in [ + "nickname".to_owned(), + "a".repeat(64), + uuid::Uuid::nil().to_string(), + ] { + assert!( + gateway(&format!("admin_account_ids = [\"{invalid}\"]")) + .validate_admin_accounts() + .is_err() + ); + } + let account = uuid::Uuid::new_v4(); + let valid = gateway(&format!("admin_account_ids = [\"{account}\"]")); + valid.validate_admin_accounts().unwrap(); + assert!(valid.is_node_admin(&account.to_string())); + assert!(!valid.is_node_admin(&uuid::Uuid::new_v4().to_string())); + assert!( + gateway(&format!( + "admin_account_ids = [\"{account}\",\"{account}\"]" + )) + .validate_admin_accounts() + .is_err() + ); + } #[test] fn tls_defaults_to_generated_certificate() { gateway("").validate_tls().unwrap(); diff --git a/gateway/src/domain/mod.rs b/gateway/src/domain/mod.rs index 2e62cdb..8e641b7 100644 --- a/gateway/src/domain/mod.rs +++ b/gateway/src/domain/mod.rs @@ -1,3 +1,4 @@ +pub mod account; pub mod auth; pub mod channels; pub mod config; diff --git a/gateway/src/domain/rate_limit/mod.rs b/gateway/src/domain/rate_limit/mod.rs index d33d024..9a5642b 100644 --- a/gateway/src/domain/rate_limit/mod.rs +++ b/gateway/src/domain/rate_limit/mod.rs @@ -19,6 +19,7 @@ pub struct RateLimiter { struct Bucket { tokens: f32, + fetch_tokens: f32, last_refill: Instant, } @@ -34,7 +35,17 @@ impl RateLimiter { /// Try to consume one token. Returns `true` if allowed, `false` if rate-limited. /// Lazily creates a bucket for new sessions. pub fn try_consume(&self, session_id: &str) -> bool { - if self.rate_per_sec <= 0.0 { + self.consume(session_id, false) + } + + /// Separate image-download budget within the same live session entry. + /// Fifty initial tokens cover one history page without spending chat tokens. + pub fn try_consume_attachment_fetch(&self, session_id: &str) -> bool { + self.consume(session_id, true) + } + + fn consume(&self, session_id: &str, attachment_fetch: bool) -> bool { + if !attachment_fetch && self.rate_per_sec <= 0.0 { return true; } let mut guard = self.inner.lock().unwrap(); @@ -43,14 +54,21 @@ impl RateLimiter { .entry(session_id.to_string()) .or_insert_with(|| Bucket { tokens: self.burst as f32, + fetch_tokens: 50.0, last_refill: now, }); // Refill let elapsed = now.duration_since(bucket.last_refill).as_secs_f32(); bucket.tokens = (bucket.tokens + elapsed * self.rate_per_sec).min(self.burst as f32); + bucket.fetch_tokens = (bucket.fetch_tokens + elapsed * 4.0).min(50.0); bucket.last_refill = now; - if bucket.tokens >= 1.0 { - bucket.tokens -= 1.0; + let tokens = if attachment_fetch { + &mut bucket.fetch_tokens + } else { + &mut bucket.tokens + }; + if *tokens >= 1.0 { + *tokens -= 1.0; true } else { false @@ -105,6 +123,23 @@ mod tests { } } + #[test] + fn image_history_budget_is_separate_and_removed_with_session() { + let r = RateLimiter::new(0.1, 1); + for _ in 0..50 { + assert!(r.try_consume_attachment_fetch("s1")); + } + assert!(!r.try_consume_attachment_fetch("s1")); + assert!( + r.try_consume("s1"), + "downloads must not consume chat tokens" + ); + assert!(!r.try_consume("s1")); + assert!(r.try_consume_attachment_fetch("s2")); + r.remove("s1"); + assert!(r.try_consume_attachment_fetch("s1")); + } + #[test] fn remove_clears_bucket() { let r = RateLimiter::new(0.1, 1); diff --git a/gateway/src/domain/session.rs b/gateway/src/domain/session.rs index 6ff65e1..3d7fe6b 100644 --- a/gateway/src/domain/session.rs +++ b/gateway/src/domain/session.rs @@ -9,6 +9,10 @@ pub struct Session { pub token: String, pub user_id: String, pub nickname: String, + pub account_id: String, + pub avatar_url: String, + pub profile_issued_at: i64, + pub client_features: crate::proto::ClientFeatures, /// Viewed text channel; independent of the active voice room. pub channel_id: Option, pub voice_channel_id: Option, @@ -24,8 +28,12 @@ pub async fn create(store: &SessionStore, user_id: String, nickname: String) -> let s = Session { session_id: Uuid::new_v4().to_string(), token: Uuid::new_v4().to_string(), - user_id, + user_id: user_id.clone(), nickname, + account_id: user_id.clone(), + avatar_url: String::new(), + profile_issued_at: 0, + client_features: Default::default(), channel_id: None, voice_channel_id: None, }; diff --git a/gateway/src/domain/storage/attachments.rs b/gateway/src/domain/storage/attachments.rs new file mode 100644 index 0000000..d5f3d1d --- /dev/null +++ b/gateway/src/domain/storage/attachments.rs @@ -0,0 +1,116 @@ +use super::{Pool, Storage}; +use anyhow::Result; + +impl Storage { + pub async fn migrate_attachments(&self) -> Result<()> { + let sql = "CREATE TABLE IF NOT EXISTS chat_attachments (id TEXT PRIMARY KEY, channel_id TEXT NOT NULL, sender_id TEXT NOT NULL, mime TEXT NOT NULL, data BYTEA NOT NULL)"; + match &self.pool { + Pool::Sqlite(p) => { + sqlx::query(sql).execute(p).await?; + } + Pool::Postgres(p) => { + sqlx::query(sql).execute(p).await?; + } + } + Ok(()) + } + + pub async fn get_attachment( + &self, + id: &str, + channel: &str, + ) -> Result)>> { + Ok(match &self.pool { + Pool::Sqlite(p) => { + sqlx::query_as("SELECT mime,data FROM chat_attachments WHERE id=? AND channel_id=?") + .bind(id) + .bind(channel) + .fetch_optional(p) + .await? + } + Pool::Postgres(p) => { + sqlx::query_as( + "SELECT mime,data FROM chat_attachments WHERE id=$1 AND channel_id=$2", + ) + .bind(id) + .bind(channel) + .fetch_optional(p) + .await? + } + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn concurrent_uploads_cannot_exceed_sender_quota() -> Result<()> { + let path = std::env::temp_dir().join(format!("vnox-quota-{}.db", uuid::Uuid::new_v4())); + let storage = Storage::connect_sqlite(&path.to_string_lossy()).await?; + if let Pool::Sqlite(pool) = &storage.pool { + sqlx::query("INSERT INTO chat_attachments(id,channel_id,sender_id,mime,data) VALUES ('existing','one','sender','image/png',zeroblob(66060288))").execute(pool).await?; + } + let one = crate::proto::ChatMessage { + message_id: "one".into(), + channel_id: "one".into(), + sender_id: "sender".into(), + ..Default::default() + }; + let two = crate::proto::ChatMessage { + message_id: "two".into(), + ..one.clone() + }; + let data = vec![0; 1024 * 1024]; + let (a, b) = tokio::join!( + storage.save_attachment_message(&one, "a", "image/png", &data), + storage.save_attachment_message(&two, "b", "image/png", &data) + ); + assert_eq!(usize::from(a.is_ok()) + usize::from(b.is_ok()), 1); + let count = usize::from(storage.get_attachment("a", "one").await?.is_some()) + + usize::from(storage.get_attachment("b", "one").await?.is_some()); + assert_eq!(count, 1); + drop(storage); + let _ = tokio::fs::remove_file(path).await; + Ok(()) + } + + #[tokio::test] + async fn attachment_bytes_survive_reopen_and_are_channel_scoped() -> Result<()> { + let path = + std::env::temp_dir().join(format!("vnox-attachment-{}.db", uuid::Uuid::new_v4())); + let path_text = path.to_string_lossy(); + let storage = Storage::connect_sqlite(&path_text).await?; + let msg = crate::proto::ChatMessage { + message_id: "message".into(), + channel_id: "one".into(), + sender_id: "sender".into(), + content: "vnox-attachment:id".into(), + ..Default::default() + }; + assert!( + storage + .save_attachment_message(&msg, "id", "image/png", b"test bytes") + .await? + ); + assert!( + storage + .save_attachment_message(&msg, "orphan", "image/png", b"test bytes") + .await + .is_err() + ); + assert!(storage.get_attachment("orphan", "one").await?.is_none()); + assert!(storage.get_attachment("id", "two").await?.is_none()); + assert!(storage.get_attachment("missing", "one").await?.is_none()); + drop(storage); + let storage = Storage::connect_sqlite(&path_text).await?; + assert_eq!( + storage.get_attachment("id", "one").await?, + Some(("image/png".into(), b"test bytes".to_vec())) + ); + drop(storage); + let _ = tokio::fs::remove_file(path).await; + Ok(()) + } +} diff --git a/gateway/src/domain/storage/mod.rs b/gateway/src/domain/storage/mod.rs index 64be5b4..74d4c05 100644 --- a/gateway/src/domain/storage/mod.rs +++ b/gateway/src/domain/storage/mod.rs @@ -1,8 +1,10 @@ +pub mod attachments; pub mod dm_unread; pub mod dms; pub mod e2ee_dms; pub mod guilds; pub mod messages; +pub mod node_features; pub mod presence; pub mod social; pub mod webhooks; @@ -31,6 +33,8 @@ impl Storage { }; s.migrate().await?; s.migrate_webhooks().await?; + s.migrate_attachments().await?; + s.migrate_node_features().await?; info!("storage (sqlite): {path}"); Ok(s) } @@ -42,6 +46,8 @@ impl Storage { }; s.migrate().await?; s.migrate_webhooks().await?; + s.migrate_attachments().await?; + s.migrate_node_features().await?; info!("storage (postgres): connected"); Ok(s) } @@ -445,6 +451,8 @@ pub struct ChannelRecord { pub kind: String, pub guild_id: Option, pub created_at: i64, + pub parent_id: Option, + pub position: i32, } impl Storage { @@ -507,36 +515,47 @@ impl Storage { } pub async fn delete_channel(&self, id: &str) -> Result { + macro_rules! delete { + ($pool:expr) => {{ + let mut tx = $pool.begin().await?; + // A category is organizational: removing it never removes child channels. + sqlx::query("UPDATE channel_layout SET parent_id=NULL WHERE parent_id=$1") + .bind(id) + .execute(&mut *tx) + .await?; + sqlx::query("DELETE FROM channel_layout WHERE channel_id=$1") + .bind(id) + .execute(&mut *tx) + .await?; + // Forum FK RESTRICT refuses data loss if a caller omitted the topic guard. + let changed = sqlx::query("DELETE FROM channels WHERE id=$1") + .bind(id) + .execute(&mut *tx) + .await? + .rows_affected() + > 0; + tx.commit().await?; + Ok(changed) + }}; + } match &self.pool { - Pool::Sqlite(p) => { - let result = sqlx::query("DELETE FROM channels WHERE id=?") - .bind(id) - .execute(p) - .await?; - Ok(result.rows_affected() > 0) - } - Pool::Postgres(p) => { - let result = sqlx::query("DELETE FROM channels WHERE id=$1") - .bind(id) - .execute(p) - .await?; - Ok(result.rows_affected() > 0) - } + Pool::Sqlite(pool) => delete!(pool), + Pool::Postgres(pool) => delete!(pool), } } pub async fn list_channels(&self) -> Result> { let rows = match &self.pool { Pool::Sqlite(p) => { - sqlx::query_as::<_, (String, String, String, Option, i64)>( - "SELECT id, name, kind, guild_id, created_at FROM channels", + sqlx::query_as::<_, (String, String, String, Option, i64, Option, i32)>( + "SELECT c.id,c.name,c.kind,c.guild_id,c.created_at,l.parent_id,COALESCE(l.position,0) FROM channels c LEFT JOIN channel_layout l ON l.channel_id=c.id", ) .fetch_all(p) .await? } Pool::Postgres(p) => { - sqlx::query_as::<_, (String, String, String, Option, i64)>( - "SELECT id, name, kind, guild_id, created_at FROM channels", + sqlx::query_as::<_, (String, String, String, Option, i64, Option, i32)>( + "SELECT c.id,c.name,c.kind,c.guild_id,c.created_at,l.parent_id,COALESCE(l.position,0) FROM channels c LEFT JOIN channel_layout l ON l.channel_id=c.id", ) .fetch_all(p) .await? @@ -544,13 +563,17 @@ impl Storage { }; Ok(rows .into_iter() - .map(|(id, name, kind, guild_id, created_at)| ChannelRecord { - id, - name, - kind, - guild_id, - created_at, - }) + .map( + |(id, name, kind, guild_id, created_at, parent_id, position)| ChannelRecord { + id, + name, + kind, + guild_id, + created_at, + parent_id, + position, + }, + ) .collect()) } @@ -559,9 +582,18 @@ impl Storage { for ch in channels { let kind = match ch.kind.as_str() { "voice" => ChannelKind::Voice, - _ => ChannelKind::Text, + "text" => ChannelKind::Text, + "category" => ChannelKind::Category, + "forum" => ChannelKind::Forum, + _ => anyhow::bail!("unsupported stored channel kind"), }; - crate::domain::channels::create(channel_store, &ch.id, &ch.name, kind, ch.guild_id) + if !crate::domain::channels::create(channel_store, &ch.id, &ch.name, kind, ch.guild_id) + .await + { + // Bootstrap IDs already exist in the fresh cache; retain their stored names. + crate::domain::channels::rename(channel_store, &ch.id, &ch.name).await; + } + crate::domain::channels::set_layout(channel_store, &ch.id, ch.parent_id, ch.position) .await; } Ok(()) diff --git a/gateway/src/domain/storage/node_features.rs b/gateway/src/domain/storage/node_features.rs new file mode 100644 index 0000000..fae2440 --- /dev/null +++ b/gateway/src/domain/storage/node_features.rs @@ -0,0 +1,469 @@ +//! Durable node presentation, category layout and transactional forum posts. +use super::{Pool, Storage, now_ms}; +use crate::proto::{ChatMessagePayload, ForumPost, ForumTopic, MemberInfo}; +use anyhow::{Context, Result, ensure}; + +macro_rules! db { + ($storage:expr, $pool:ident, $body:block) => { + match &$storage.pool { + Pool::Sqlite($pool) => $body, + Pool::Postgres($pool) => $body, + } + }; +} + +macro_rules! validate_layout { + ($tx:ident,$id:expr,$parent:expr) => {{ + let (kind,scope):(String,Option)=sqlx::query_as("SELECT kind,guild_id FROM channels WHERE id=$1").bind($id).fetch_one(&mut *$tx).await?; + if let Some(parent)=$parent { + ensure!(kind!="category", "categories cannot be nested"); + let target:Option<(String,Option)>=sqlx::query_as("SELECT kind,guild_id FROM channels WHERE id=$1").bind(parent).fetch_optional(&mut *$tx).await?; + ensure!(target.is_some_and(|(kind,parent_scope)|kind=="category" && parent_scope==scope), "category must exist in the same scope"); + } + }}; +} + +#[derive(sqlx::FromRow)] +struct TopicRow { + topic_id: String, + channel_id: String, + title: String, + author_user_id: String, + author_account_id: String, + author_nickname: String, + author_avatar_url: String, + created_at: i64, + updated_at: i64, + reply_count: i64, +} +impl TopicRow { + fn wire(self) -> ForumTopic { + ForumTopic { + topic_id: self.topic_id, + channel_id: self.channel_id, + title: self.title, + author: Some(MemberInfo { + user_id: self.author_user_id, + account_id: self.author_account_id, + nickname: self.author_nickname, + avatar_url: self.author_avatar_url, + in_voice: false, + }), + created_at: self.created_at, + updated_at: self.updated_at, + reply_count: u32::try_from(self.reply_count).unwrap_or(u32::MAX), + } + } +} +#[derive(sqlx::FromRow)] +struct PostRow { + id: String, + channel_id: String, + sender_id: String, + content: String, + timestamp: i64, + reply_to: Option, + topic_id: String, + post_index: i64, + author_account_id: String, + author_nickname: String, + author_avatar_url: String, +} +impl PostRow { + fn same_request(&self, channel: &str, topic: &str, body: &str, author: &MemberInfo) -> bool { + self.channel_id == channel + && self.topic_id == topic + && self.content == body + && self.sender_id == author.user_id + && self.author_account_id == author.account_id + } + fn wire(self) -> ForumPost { + ForumPost { + topic_id: self.topic_id, + ordinal: u64::try_from(self.post_index).unwrap_or_default(), + author: Some(MemberInfo { + user_id: self.sender_id.clone(), + account_id: self.author_account_id, + nickname: self.author_nickname, + avatar_url: self.author_avatar_url, + in_voice: false, + }), + message: Some(ChatMessagePayload { + message_id: self.id, + channel_id: self.channel_id, + sender_id: self.sender_id, + content: self.content, + timestamp: self.timestamp, + reply_to: self.reply_to, + edited: false, + }), + } + } +} +macro_rules! topic_sql { ($tail:literal) => { concat!("SELECT topic_id,channel_id,title,author_user_id,author_account_id,author_nickname,author_avatar_url,created_at,updated_at,reply_count FROM forum_topics", $tail) }; } +macro_rules! post_sql { ($tail:literal) => { concat!("SELECT m.id,m.channel_id,m.sender_id,m.content,m.timestamp,m.reply_to,p.topic_id,p.post_index,p.author_account_id,p.author_nickname,p.author_avatar_url FROM forum_posts p JOIN messages m ON m.id=p.message_id JOIN forum_topics t ON t.topic_id=p.topic_id AND t.channel_id=m.channel_id", $tail) }; } +fn validate_post( + topic: &str, + message: &str, + channel: &str, + body: &str, + author: &MemberInfo, +) -> Result<()> { + for id in [topic, message] { + ensure!( + uuid::Uuid::parse_str(id).is_ok_and(|v| !v.is_nil() && v.to_string() == id), + "forum identifiers must be canonical UUIDs" + ); + } + ensure!( + !channel.is_empty() && channel.len() <= 128, + "invalid forum channel" + ); + ensure!( + !body.trim().is_empty() && body.len() <= 64 * 1024 && body.chars().count() <= 16_000, + "forum post must contain 1–16000 characters within 64 KiB" + ); + ensure!( + !author.user_id.is_empty() + && author.user_id.len() <= 128 + && !author.account_id.is_empty() + && author.account_id.len() <= 128, + "authenticated forum author required" + ); + Ok(()) +} +impl Storage { + pub async fn migrate_node_features(&self) -> Result<()> { + let schema = "CREATE TABLE IF NOT EXISTS node_settings (singleton INTEGER PRIMARY KEY CHECK(singleton=1),display_name TEXT NOT NULL,description TEXT NOT NULL); + CREATE TABLE IF NOT EXISTS channel_layout (channel_id TEXT PRIMARY KEY REFERENCES channels(id) ON DELETE CASCADE,parent_id TEXT REFERENCES channels(id) ON DELETE SET NULL,position INTEGER NOT NULL DEFAULT 0); + CREATE INDEX IF NOT EXISTS idx_channel_layout_parent ON channel_layout(parent_id,position); + CREATE TABLE IF NOT EXISTS forum_topics (topic_id TEXT PRIMARY KEY,channel_id TEXT NOT NULL REFERENCES channels(id) ON DELETE RESTRICT,title TEXT NOT NULL,author_user_id TEXT NOT NULL,author_account_id TEXT NOT NULL,author_nickname TEXT NOT NULL,author_avatar_url TEXT NOT NULL,created_at BIGINT NOT NULL,updated_at BIGINT NOT NULL,reply_count BIGINT NOT NULL DEFAULT 0 CHECK(reply_count>=0)); + CREATE INDEX IF NOT EXISTS idx_forum_topics_channel ON forum_topics(channel_id,updated_at,topic_id); + CREATE TABLE IF NOT EXISTS forum_posts (message_id TEXT PRIMARY KEY REFERENCES messages(id) ON DELETE RESTRICT,topic_id TEXT NOT NULL REFERENCES forum_topics(topic_id) ON DELETE RESTRICT,post_index BIGINT NOT NULL CHECK(post_index>=0),author_account_id TEXT NOT NULL,author_nickname TEXT NOT NULL,author_avatar_url TEXT NOT NULL,UNIQUE(topic_id,post_index)); + CREATE INDEX IF NOT EXISTS idx_forum_posts_topic ON forum_posts(topic_id,post_index);"; + db!(self, pool, { + sqlx::raw_sql(schema).execute(pool).await?; + }); + Ok(()) + } + pub async fn load_node_settings(&self) -> Result> { + db!(self, pool, { + Ok(sqlx::query_as( + "SELECT display_name,description FROM node_settings WHERE singleton=1", + ) + .fetch_optional(pool) + .await?) + }) + } + pub async fn save_node_settings(&self, name: &str, description: &str) -> Result<()> { + ensure!( + !name.trim().is_empty() + && name.chars().count() <= 100 + && !name.chars().any(char::is_control), + "invalid node name" + ); + ensure!( + description.chars().count() <= 2000, + "node description is too long" + ); + db!(self, pool, { + sqlx::query("INSERT INTO node_settings(singleton,display_name,description) VALUES(1,$1,$2) ON CONFLICT(singleton) DO UPDATE SET display_name=excluded.display_name,description=excluded.description").bind(name).bind(description).execute(pool).await?; + }); + Ok(()) + } + pub async fn set_channel_layout( + &self, + id: &str, + parent: Option<&str>, + position: i32, + ) -> Result<()> { + ensure!( + position >= 0 && parent != Some(id), + "invalid channel layout" + ); + db!(self, pool, { + let mut tx = pool.begin().await?; + // Built-in channels predate persistent channel rows; materialize only these IDs. + if ["general", "voice"].contains(&id) { + sqlx::query("INSERT INTO channels(id,name,kind,guild_id,created_at) VALUES($1,$1,$2,NULL,$3) ON CONFLICT(id) DO NOTHING") + .bind(id).bind(if id=="voice" {"voice"} else {"text"}).bind(now_ms()).execute(&mut *tx).await?; + } + let (kind, scope): (String, Option) = + sqlx::query_as("SELECT kind,guild_id FROM channels WHERE id=$1") + .bind(id) + .fetch_one(&mut *tx) + .await?; + if let Some(parent) = parent { + ensure!(kind != "category", "categories cannot be nested"); + let target: Option<(String, Option)> = + sqlx::query_as("SELECT kind,guild_id FROM channels WHERE id=$1") + .bind(parent) + .fetch_optional(&mut *tx) + .await?; + ensure!( + target.is_some_and( + |(kind, parent_scope)| kind == "category" && parent_scope == scope + ), + "category must exist in the same scope" + ); + } + sqlx::query("INSERT INTO channel_layout(channel_id,parent_id,position) VALUES($1,$2,$3) ON CONFLICT(channel_id) DO UPDATE SET parent_id=excluded.parent_id,position=excluded.position") + .bind(id).bind(parent).bind(position).execute(&mut *tx).await?; + tx.commit().await?; + }); + Ok(()) + } + /// Commit a channel and its layout together; a rejected parent cannot leave an orphan. + pub async fn create_channel_with_layout( + &self, + id: &str, + name: &str, + kind: &str, + guild: Option<&str>, + parent: Option<&str>, + position: i32, + ) -> Result { + ensure!( + position >= 0 && parent != Some(id), + "invalid channel layout" + ); + db!(self, pool, { + let mut tx = pool.begin().await?; + let inserted=sqlx::query("INSERT INTO channels(id,name,kind,guild_id,created_at) VALUES($1,$2,$3,$4,$5) ON CONFLICT(id) DO NOTHING") + .bind(id).bind(name).bind(kind).bind(guild).bind(now_ms()).execute(&mut *tx).await?.rows_affected()>0; + if !inserted { + tx.commit().await?; + return Ok(false); + } + validate_layout!(tx, id, parent); + sqlx::query( + "INSERT INTO channel_layout(channel_id,parent_id,position) VALUES($1,$2,$3)", + ) + .bind(id) + .bind(parent) + .bind(position) + .execute(&mut *tx) + .await?; + tx.commit().await?; + Ok(true) + }) + } + /// Rename/move is one transaction, including legacy bootstrap-row materialization. + pub async fn update_channel_with_layout( + &self, + id: &str, + name: &str, + parent: Option<&str>, + position: i32, + ) -> Result { + ensure!( + position >= 0 && parent != Some(id), + "invalid channel layout" + ); + db!(self, pool, { + let mut tx = pool.begin().await?; + if ["general", "voice"].contains(&id) { + sqlx::query("INSERT INTO channels(id,name,kind,guild_id,created_at) VALUES($1,$2,$3,NULL,$4) ON CONFLICT(id) DO NOTHING") + .bind(id).bind(name).bind(if id=="voice" {"voice"} else {"text"}).bind(now_ms()).execute(&mut *tx).await?; + } + let changed = sqlx::query("UPDATE channels SET name=$1 WHERE id=$2") + .bind(name) + .bind(id) + .execute(&mut *tx) + .await? + .rows_affected() + > 0; + if !changed { + tx.commit().await?; + return Ok(false); + } + validate_layout!(tx, id, parent); + sqlx::query("INSERT INTO channel_layout(channel_id,parent_id,position) VALUES($1,$2,$3) ON CONFLICT(channel_id) DO UPDATE SET parent_id=excluded.parent_id,position=excluded.position") + .bind(id).bind(parent).bind(position).execute(&mut *tx).await?; + tx.commit().await?; + Ok(true) + }) + } + pub async fn forum_has_topics(&self, channel: &str) -> Result { + db!(self, pool, { + Ok(sqlx::query_scalar::<_, bool>( + "SELECT EXISTS(SELECT 1 FROM forum_topics WHERE channel_id=$1)", + ) + .bind(channel) + .fetch_one(pool) + .await?) + }) + } + pub async fn guild_has_forum_topics(&self, guild: &str) -> Result { + db!(self, pool, { + Ok(sqlx::query_scalar::<_,bool>("SELECT EXISTS(SELECT 1 FROM forum_topics t JOIN channels c ON c.id=t.channel_id WHERE c.guild_id=$1)").bind(guild).fetch_one(pool).await?) + }) + } + pub async fn forum_list( + &self, + channel: &str, + offset: u32, + limit: u32, + ) -> Result> { + db!(self, pool, { + let query = topic_sql!( + " WHERE channel_id=$1 ORDER BY updated_at DESC,topic_id LIMIT $2 OFFSET $3" + ); + Ok(sqlx::query_as::<_, TopicRow>(query) + .bind(channel) + .bind(i64::from(limit.min(101))) + .bind(i64::from(offset)) + .fetch_all(pool) + .await? + .into_iter() + .map(TopicRow::wire) + .collect()) + }) + } + pub async fn forum_topic(&self, channel: &str, topic: &str) -> Result> { + db!(self, pool, { + let query = topic_sql!(" WHERE channel_id=$1 AND topic_id=$2"); + Ok(sqlx::query_as::<_, TopicRow>(query) + .bind(channel) + .bind(topic) + .fetch_optional(pool) + .await? + .map(TopicRow::wire)) + }) + } + pub async fn forum_posts( + &self, + channel: &str, + topic: &str, + offset: u32, + limit: u32, + ) -> Result> { + db!(self, pool, { + let query = post_sql!( + " WHERE m.channel_id=$1 AND p.topic_id=$2 ORDER BY p.post_index LIMIT $3 OFFSET $4" + ); + Ok(sqlx::query_as::<_, PostRow>(query) + .bind(channel) + .bind(topic) + .bind(i64::from(limit.min(101))) + .bind(i64::from(offset)) + .fetch_all(pool) + .await? + .into_iter() + .map(PostRow::wire) + .collect()) + }) + } + pub async fn forum_create( + &self, + topic_id: &str, + message_id: &str, + channel: &str, + title: &str, + content: &str, + author: &MemberInfo, + ) -> Result<(ForumTopic, ForumPost)> { + validate_post(topic_id, message_id, channel, content, author)?; + ensure!( + !title.trim().is_empty() + && title.chars().count() <= 160 + && !title.chars().any(char::is_control), + "invalid forum topic title" + ); + db!(self, pool, { + let mut tx = pool.begin().await?; + let time = now_ms(); + let inserted=sqlx::query("INSERT INTO forum_topics(topic_id,channel_id,title,author_user_id,author_account_id,author_nickname,author_avatar_url,created_at,updated_at,reply_count) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$8,0) ON CONFLICT(topic_id) DO NOTHING") + .bind(topic_id).bind(channel).bind(title).bind(&author.user_id).bind(&author.account_id).bind(&author.nickname).bind(&author.avatar_url).bind(time).execute(&mut *tx).await?.rows_affected()>0; + if !inserted { + let tq = topic_sql!(" WHERE topic_id=$1"); + let topic = sqlx::query_as::<_, TopicRow>(tq) + .bind(topic_id) + .fetch_one(&mut *tx) + .await?; + let pq = post_sql!(" WHERE p.topic_id=$1 AND p.post_index=0"); + let post = sqlx::query_as::<_, PostRow>(pq) + .bind(topic_id) + .fetch_one(&mut *tx) + .await?; + ensure!( + topic.channel_id == channel + && topic.title == title + && post.id == message_id + && post.same_request(channel, topic_id, content, author), + "forum topic identifier conflict" + ); + tx.commit().await?; + return Ok((topic.wire(), post.wire())); + } + // Never adopt an existing chat message ID into a forum: conflict rolls back topic too. + sqlx::query("INSERT INTO messages(id,channel_id,sender_id,content,timestamp,reply_to) VALUES($1,$2,$3,$4,$5,NULL)").bind(message_id).bind(channel).bind(&author.user_id).bind(content).bind(time).execute(&mut *tx).await?; + sqlx::query("INSERT INTO forum_posts(message_id,topic_id,post_index,author_account_id,author_nickname,author_avatar_url) VALUES($1,$2,0,$3,$4,$5)").bind(message_id).bind(topic_id).bind(&author.account_id).bind(&author.nickname).bind(&author.avatar_url).execute(&mut *tx).await?; + let tq = topic_sql!(" WHERE topic_id=$1"); + let pq = post_sql!(" WHERE p.message_id=$1"); + let topic = sqlx::query_as::<_, TopicRow>(tq) + .bind(topic_id) + .fetch_one(&mut *tx) + .await?; + let post = sqlx::query_as::<_, PostRow>(pq) + .bind(message_id) + .fetch_one(&mut *tx) + .await?; + tx.commit().await?; + Ok((topic.wire(), post.wire())) + }) + } + pub async fn forum_reply( + &self, + message_id: &str, + channel: &str, + topic_id: &str, + content: &str, + author: &MemberInfo, + ) -> Result<(ForumTopic, ForumPost)> { + validate_post(topic_id, message_id, channel, content, author)?; + db!(self, pool, { + let mut tx = pool.begin().await?; + // Acquire the writer/row lock before reading idempotency state; increments cannot race. + let query = "UPDATE forum_topics SET updated_at=updated_at WHERE channel_id=$1 AND topic_id=$2 RETURNING topic_id,channel_id,title,author_user_id,author_account_id,author_nickname,author_avatar_url,created_at,updated_at,reply_count"; + let topic = sqlx::query_as::<_, TopicRow>(query) + .bind(channel) + .bind(topic_id) + .fetch_optional(&mut *tx) + .await? + .context("forum topic not found")?; + let pq = post_sql!(" WHERE p.message_id=$1"); + if let Some(post) = sqlx::query_as::<_, PostRow>(pq) + .bind(message_id) + .fetch_optional(&mut *tx) + .await? + { + ensure!( + post.same_request(channel, topic_id, content, author), + "forum post identifier conflict" + ); + tx.commit().await?; + return Ok((topic.wire(), post.wire())); + } + let time = now_ms(); + sqlx::query("INSERT INTO messages(id,channel_id,sender_id,content,timestamp,reply_to) VALUES($1,$2,$3,$4,$5,NULL)").bind(message_id).bind(channel).bind(&author.user_id).bind(content).bind(time).execute(&mut *tx).await?; + let query = "UPDATE forum_topics SET reply_count=reply_count+1,updated_at=CASE WHEN updated_at>$1 THEN updated_at ELSE $1 END WHERE channel_id=$2 AND topic_id=$3 RETURNING topic_id,channel_id,title,author_user_id,author_account_id,author_nickname,author_avatar_url,created_at,updated_at,reply_count"; + let topic = sqlx::query_as::<_, TopicRow>(query) + .bind(time) + .bind(channel) + .bind(topic_id) + .fetch_one(&mut *tx) + .await?; + sqlx::query("INSERT INTO forum_posts(message_id,topic_id,post_index,author_account_id,author_nickname,author_avatar_url) VALUES($1,$2,$3,$4,$5,$6)").bind(message_id).bind(topic_id).bind(topic.reply_count).bind(&author.account_id).bind(&author.nickname).bind(&author.avatar_url).execute(&mut *tx).await?; + let post = sqlx::query_as::<_, PostRow>(pq) + .bind(message_id) + .fetch_one(&mut *tx) + .await?; + tx.commit().await?; + Ok((topic.wire(), post.wire())) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/gateway/src/domain/storage/node_features/tests.rs b/gateway/src/domain/storage/node_features/tests.rs new file mode 100644 index 0000000..8514bdc --- /dev/null +++ b/gateway/src/domain/storage/node_features/tests.rs @@ -0,0 +1,354 @@ +use super::*; +use std::sync::Arc; +fn id() -> String { + uuid::Uuid::new_v4().to_string() +} +fn author() -> MemberInfo { + MemberInfo { + user_id: "device-a".into(), + account_id: "account-a".into(), + nickname: "Alice".into(), + avatar_url: "/v1/avatars/default-v2/11111111-1111-4111-8111-111111111111".into(), + in_voice: false, + } +} +async fn fixture() -> Result { + let store = Storage::connect_sqlite(":memory:").await?; + store + .create_channel("forum", "Forum", "forum", None) + .await?; + Ok(store) +} +#[tokio::test] +async fn forum_creation_reply_and_retries_are_atomic_and_ordered() -> Result<()> { + let store = fixture().await?; + let topic = id(); + let first = id(); + let reply = id(); + let (t, p) = store + .forum_create(&topic, &first, "forum", "Topic", "first", &author()) + .await?; + assert_eq!(t.reply_count, 0); + assert_eq!(p.ordinal, 0); + let retry = store + .forum_create(&topic, &first, "forum", "Topic", "first", &author()) + .await?; + assert_eq!(retry.1.message.as_ref().unwrap().message_id, first); + let (t, p) = store + .forum_reply(&reply, "forum", &topic, "reply", &author()) + .await?; + assert_eq!(t.reply_count, 1); + assert_eq!(p.ordinal, 1); + let retry = store + .forum_reply(&reply, "forum", &topic, "reply", &author()) + .await?; + assert_eq!(retry.0.reply_count, 1); + let posts = store.forum_posts("forum", &topic, 0, 101).await?; + assert_eq!(posts.len(), 2); + assert_eq!(posts.iter().map(|p| p.ordinal).collect::>(), [0, 1]); + assert_eq!( + store.forum_posts("forum", &topic, 1, 1).await?[0] + .message + .as_ref() + .unwrap() + .content, + "reply" + ); + assert_eq!(store.forum_list("forum", 0, 51).await?.len(), 1); + assert!( + store + .forum_posts("another-forum", &topic, 0, 100) + .await? + .is_empty() + ); + assert!(store.forum_topic("another-forum", &topic).await?.is_none()); + let mut other = author(); + other.account_id = "another-account".into(); + assert!( + store + .forum_create(&topic, &first, "forum", "Topic", "first", &other) + .await + .is_err() + ); + assert!( + store + .forum_reply(&reply, "forum", &topic, "changed", &author()) + .await + .is_err() + ); + assert_eq!( + store + .forum_topic("forum", &topic) + .await? + .unwrap() + .reply_count, + 1 + ); + Ok(()) +} +#[tokio::test] +async fn an_existing_private_chat_message_cannot_be_adopted_or_leave_a_partial_topic() -> Result<()> +{ + let store = fixture().await?; + let message = id(); + let bad_topic = id(); + store + .save_message(&ChatMessagePayload { + message_id: message.clone(), + channel_id: "private-room".into(), + sender_id: author().user_id, + content: "private existing body".into(), + timestamp: 1, + edited: false, + reply_to: None, + }) + .await?; + assert!( + store + .forum_create(&bad_topic, &message, "forum", "Topic", "first", &author()) + .await + .is_err() + ); + assert!(store.forum_topic("forum", &bad_topic).await?.is_none()); + assert!(store.forum_list("forum", 0, 50).await?.is_empty()); + let topic = id(); + store + .forum_create(&topic, &id(), "forum", "Valid", "first", &author()) + .await?; + assert!( + store + .forum_reply( + &message, + "forum", + &topic, + "private existing body", + &author() + ) + .await + .is_err() + ); + assert_eq!( + store + .forum_topic("forum", &topic) + .await? + .unwrap() + .reply_count, + 0 + ); + assert_eq!(store.forum_posts("forum", &topic, 0, 100).await?.len(), 1); + assert_eq!( + store.get_history("private-room", 10).await?[0].content, + "private existing body" + ); + Ok(()) +} +#[tokio::test] +async fn category_removal_keeps_child_channels_and_forum_posts_and_invalid_scope_rolls_back() +-> Result<()> { + let store = fixture().await?; + store + .create_channel("category", "Group", "category", None) + .await?; + store + .create_channel("foreign", "Private", "category", Some("guild")) + .await?; + store + .set_channel_layout("forum", Some("category"), 4) + .await?; + store + .set_channel_layout("general", Some("category"), 5) + .await?; + assert!( + store + .set_channel_layout("forum", Some("foreign"), 2) + .await + .is_err() + ); + assert!( + store + .set_channel_layout("category", Some("foreign"), 2) + .await + .is_err() + ); + assert!( + store + .set_channel_layout("forum", Some("general"), 2) + .await + .is_err() + ); + assert_eq!( + store + .list_channels() + .await? + .into_iter() + .find(|c| c.id == "forum") + .unwrap() + .parent_id + .as_deref(), + Some("category") + ); + let topic = id(); + store + .forum_create(&topic, &id(), "forum", "Topic", "first", &author()) + .await?; + assert!(store.delete_channel("forum").await.is_err()); + assert_eq!( + store + .list_channels() + .await? + .into_iter() + .find(|c| c.id == "forum") + .unwrap() + .parent_id + .as_deref(), + Some("category") + ); + assert!(store.delete_channel("category").await?); + let rows = store.list_channels().await?; + assert!( + rows.iter() + .any(|c| c.id == "forum" && c.parent_id.is_none()) + ); + assert!( + rows.iter() + .any(|c| c.id == "general" && c.parent_id.is_none()) + ); + assert_eq!(store.forum_posts("forum", &topic, 0, 100).await?.len(), 1); + Ok(()) +} +#[tokio::test] +async fn concurrent_replies_and_duplicate_creates_have_one_total_order() -> Result<()> { + let store = Arc::new(fixture().await?); + let topic = id(); + let first = id(); + let a = author(); + let (left, right) = tokio::join!( + store.forum_create(&topic, &first, "forum", "Topic", "first", &a), + store.forum_create(&topic, &first, "forum", "Topic", "first", &a) + ); + left?; + right?; + let mut workers = Vec::new(); + for index in 0..12 { + let store = store.clone(); + let topic = topic.clone(); + workers.push(tokio::spawn(async move { + store + .forum_reply(&id(), "forum", &topic, &format!("reply {index}"), &author()) + .await + })); + } + for worker in workers { + worker.await??; + } + let posts = store.forum_posts("forum", &topic, 0, 100).await?; + assert_eq!(posts.len(), 13); + assert_eq!( + posts.iter().map(|post| post.ordinal).collect::>(), + (0..=12).collect::>() + ); + assert_eq!( + store + .forum_topic("forum", &topic) + .await? + .unwrap() + .reply_count, + 12 + ); + Ok(()) +} +#[tokio::test] +async fn node_settings_layout_and_topics_survive_reopen_and_cache_load() -> Result<()> { + let folder = std::env::temp_dir().join(format!("vnox-node-features-{}", id())); + std::fs::create_dir_all(&folder)?; + let path = folder.join("data.sqlite"); + let path = path.to_str().unwrap(); + let store = Storage::connect_sqlite(path).await?; + store + .save_node_settings("Наш сервер", "Описание\nсервера") + .await?; + assert!(store.save_node_settings("\ninvalid", "x").await.is_err()); + store + .create_channel("category", "Group", "category", None) + .await?; + store + .create_channel("forum", "Forum", "forum", None) + .await?; + store + .set_channel_layout("forum", Some("category"), 3) + .await?; + let topic = id(); + store + .forum_create(&topic, &id(), "forum", "Topic", "first", &author()) + .await?; + if let Pool::Sqlite(pool) = &store.pool { + pool.close().await; + } + let reopened = Storage::connect_sqlite(path).await?; + assert_eq!( + reopened.load_node_settings().await?, + Some(("Наш сервер".into(), "Описание\nсервера".into())) + ); + let cache = crate::domain::channels::new_store(); + reopened.load_channels_to_cache(&cache).await?; + let forum = crate::domain::channels::get_channel(&cache, "forum") + .await + .unwrap(); + assert_eq!(forum.kind, crate::domain::channels::ChannelKind::Forum); + assert_eq!(forum.parent_id.as_deref(), Some("category")); + assert_eq!(forum.position, 3); + assert_eq!( + reopened.forum_posts("forum", &topic, 0, 100).await?.len(), + 1 + ); + assert!(reopened.forum_has_topics("forum").await?); + assert!(!reopened.guild_has_forum_topics("absent").await?); + if let Pool::Sqlite(pool) = &reopened.pool { + pool.close().await; + } + std::fs::remove_dir_all(folder)?; + Ok(()) +} + +#[tokio::test] +async fn invalid_layout_cannot_partially_create_rename_or_materialize_a_bootstrap_channel() +-> Result<()> { + let store = fixture().await?; + assert!( + store + .create_channel_with_layout("new", "New", "text", None, Some("missing"), 0) + .await + .is_err() + ); + assert!(!store.list_channels().await?.iter().any(|c| c.id == "new")); + assert!( + store + .update_channel_with_layout("forum", "Changed", Some("missing"), 1) + .await + .is_err() + ); + assert_eq!( + store + .list_channels() + .await? + .iter() + .find(|c| c.id == "forum") + .unwrap() + .name, + "Forum" + ); + assert!( + store + .update_channel_with_layout("general", "Changed", Some("missing"), 1) + .await + .is_err() + ); + assert!( + !store + .list_channels() + .await? + .iter() + .any(|c| c.id == "general") + ); + Ok(()) +} diff --git a/gateway/src/domain/storage/webhooks/mod.rs b/gateway/src/domain/storage/webhooks/mod.rs index 4fbf4fb..586bf6d 100644 --- a/gateway/src/domain/storage/webhooks/mod.rs +++ b/gateway/src/domain/storage/webhooks/mod.rs @@ -126,6 +126,27 @@ impl Storage { &self, msg: &ChatMessagePayload, receipt: Option<(&str, &str, &str, &str)>, + ) -> Result { + self.save_message_with_attachment(msg, receipt, None).await + } + + /// Commit validated image bytes, quotas, history and outgoing events atomically. + pub async fn save_attachment_message( + &self, + msg: &ChatMessagePayload, + id: &str, + mime: &str, + data: &[u8], + ) -> Result { + self.save_message_with_attachment(msg, None, Some((id, mime, data))) + .await + } + + async fn save_message_with_attachment( + &self, + msg: &ChatMessagePayload, + receipt: Option<(&str, &str, &str, &str)>, + attachment: Option<(&str, &str, &[u8])>, ) -> Result { let event_id = uuid::Uuid::new_v4().to_string(); let event=serde_json::json!({"type":"message.created","id":event_id,"occurred_at":crate::webhooks::now_ms(),"message":{"id":msg.message_id.chars().take(128).collect::(),"id_truncated":msg.message_id.chars().count()>128,"channel_id":msg.channel_id,"sender_id":msg.sender_id,"content":msg.content.chars().take(8000).collect::(),"content_truncated":msg.content.chars().count()>8000,"timestamp":msg.timestamp}}).to_string(); @@ -133,6 +154,14 @@ impl Storage { let mut tx = $p.begin().await?; // Serialize capacity checks and receipt inserts across concurrent publishers. sqlx::query($lock).execute(&mut *tx).await?; + if let Some((id, mime, data)) = attachment { + anyhow::ensure!(!data.is_empty() && data.len() <= 3 * 1024 * 1024, "image exceeds byte limits"); + let (global_count, personal_count): (i64,i64) = sqlx::query_as("SELECT COUNT(*), COALESCE(SUM(CASE WHEN sender_id=$1 THEN 1 ELSE 0 END),0) FROM chat_attachments").bind(&msg.sender_id).fetch_one(&mut *tx).await?; + anyhow::ensure!(global_count < 100_000 && personal_count < 10_000, "image attachment count quota reached"); + let (global, personal): (i64, i64) = sqlx::query_as("SELECT COALESCE(SUM(length(data)),0), COALESCE(SUM(CASE WHEN sender_id=$1 THEN length(data) ELSE 0 END),0) FROM chat_attachments").bind(&msg.sender_id).fetch_one(&mut *tx).await?; + anyhow::ensure!(global + data.len() as i64 <= 512 * 1024 * 1024 && personal + data.len() as i64 <= 64 * 1024 * 1024, "image storage quota reached"); + sqlx::query("INSERT INTO chat_attachments (id,channel_id,sender_id,mime,data) VALUES ($1,$2,$3,$4,$5)").bind(id).bind(&msg.channel_id).bind(&msg.sender_id).bind(mime).bind(data).execute(&mut *tx).await?; + } if let Some((hook, delivery, hash, token_hash)) = receipt { // Lock and revalidate the credential immediately before persistence, so rotation/revocation cannot race a waiting delivery. let active=sqlx::query("UPDATE webhooks SET created_at=created_at WHERE id=$1 AND token_hash=$2 AND channel_id=$3 AND revoked_at IS NULL AND direction='incoming'").bind(hook).bind(token_hash).bind(&msg.channel_id).execute(&mut *tx).await?.rows_affected(); @@ -163,6 +192,7 @@ impl Storage { sqlx::query("INSERT INTO webhook_receipts(hook_id,delivery_id,body_hash,message_id,created_at) VALUES($1,$2,$3,$4,$5)").bind(hook).bind(delivery).bind(hash).bind(&msg.message_id).bind(crate::webhooks::now_ms()).execute(&mut *tx).await?; } let inserted=sqlx::query("INSERT INTO messages(id,channel_id,sender_id,content,timestamp,reply_to) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(id) DO NOTHING").bind(&msg.message_id).bind(&msg.channel_id).bind(&msg.sender_id).bind(&msg.content).bind(msg.timestamp).bind(&msg.reply_to).execute(&mut *tx).await?.rows_affected()>0; + anyhow::ensure!(attachment.is_none() || inserted, "image message ID already exists"); if inserted { let hooks=sqlx::query_as::<_,(String,)>("SELECT id FROM webhooks WHERE channel_id=$1 AND direction='outgoing' AND revoked_at IS NULL AND suspended_reason IS NULL LIMIT 32").bind(&msg.channel_id).fetch_all(&mut *tx).await?; for (hook,) in hooks { diff --git a/gateway/src/handler/channel/admin_tests.rs b/gateway/src/handler/channel/admin_tests.rs new file mode 100644 index 0000000..740fed0 --- /dev/null +++ b/gateway/src/handler/channel/admin_tests.rs @@ -0,0 +1,489 @@ +use super::*; +use crate::{ + domain::{channels, config, session, storage::Storage}, + handler::{Ctx, dispatch}, + proto::{self, PacketId, to_payload}, +}; +use prost::Message; +use std::sync::{Arc, atomic::AtomicUsize}; + +pub(super) async fn fixture() -> (State, String, String, String, std::path::PathBuf) { + let directory = std::env::temp_dir().join(format!("vnox-node-admin-{}", uuid::Uuid::new_v4())); + std::fs::create_dir(&directory).unwrap(); + let storage = Storage::connect_sqlite(directory.join("node.sqlite").to_str().unwrap()) + .await + .unwrap(); + let mut config: config::Config = + toml::from_str(include_str!("../../../../dev/config.toml")).unwrap(); + let account = uuid::Uuid::new_v4().to_string(); + config.gateway.admin_account_ids = vec![account.clone()]; + config.gateway.validate_admin_accounts().unwrap(); + let identity = crate::bootstrap::server_identity::ServerIdentity::load_or_generate( + &directory.join("identity"), + ) + .unwrap(); + let (broadcast, _) = tokio::sync::broadcast::channel(64); + let state = State::new( + session::new_store(), + channels::new_store(), + Arc::new(storage), + Arc::new(config), + Arc::new(identity), + broadcast, + crate::admin::metrics::Metrics::new(), + Arc::new(AtomicUsize::new(0)), + Arc::new(AtomicUsize::new(0)), + None, + ); + let admin = session::create(&state.sessions, hex::encode([9; 32]), "Admin".into()).await; + let second = session::create( + &state.sessions, + hex::encode([10; 32]), + "Other device".into(), + ) + .await; + // Exactly the same device key admitted under a DIFFERENT account must not inherit role. + let other = session::create(&state.sessions, admin.user_id.clone(), "Admin".into()).await; + state + .sessions + .write() + .await + .get_mut(&admin.session_id) + .unwrap() + .account_id = account.clone(); + state + .sessions + .write() + .await + .get_mut(&second.session_id) + .unwrap() + .account_id = account; + state + .sessions + .write() + .await + .get_mut(&other.session_id) + .unwrap() + .account_id = uuid::Uuid::new_v4().to_string(); + ( + state, + admin.session_id, + second.session_id, + other.session_id, + directory, + ) +} +pub(super) async fn request( + state: &State, + sid: &str, + packet: PacketId, + payload: Vec, +) -> Option<(proto::PacketHeader, Vec)> { + let (mut stream, mut client) = tokio::io::duplex(65536); + let mut seq = 0; + dispatch( + &mut Ctx { + stream: &mut stream, + seq: &mut seq, + state, + }, + sid, + packet, + &payload, + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + drop(stream); + crate::net::io::read_packet(&mut client).await.ok() +} +fn create(id: &str, guild: Option) -> Vec { + to_payload(&proto::ChannelCreate { + channel_id: id.into(), + channel_name: "New channel".into(), + kind: "text".into(), + guild_id: guild, + ..Default::default() + }) +} +fn deny(response: Option<(proto::PacketHeader, Vec)>) { + let (header, payload) = response.expect("permission error response"); + assert_eq!(header.packet_id, PacketId::Error as u16); + assert_eq!( + proto::ErrorPayload::decode(payload.as_slice()) + .unwrap() + .code, + proto::ErrorCode::PermissionDenied as u32 + ); +} + +#[tokio::test] +async fn listed_account_and_second_device_can_manage_global_channels_but_same_key_other_account_cannot() + { + let (state, admin, second, other, dir) = fixture().await; + let mut events = state.broadcast.subscribe(); + deny( + request( + &state, + &other, + PacketId::ChannelCreate, + create("blocked", None), + ) + .await, + ); + assert!( + channels::get_channel(&state.channels, "blocked") + .await + .is_none() + ); + let created = request( + &state, + &admin, + PacketId::ChannelCreate, + create("global", None), + ) + .await + .unwrap(); + assert_eq!(created.0.packet_id, PacketId::ChannelState as u16); + let notification = events.try_recv().unwrap(); + assert_eq!(notification.channel_id, None); + assert_eq!(notification.target_session_id, None); + assert_eq!( + notification.exclude_session.as_deref(), + Some(admin.as_str()) + ); + assert!( + channels::get_channel(&state.channels, "global") + .await + .is_some() + ); + deny( + request( + &state, + &other, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "global".into(), + channel_name: "Stolen".into(), + ..Default::default() + }), + ) + .await, + ); + request( + &state, + &second, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "global".into(), + channel_name: "Renamed by same account".into(), + ..Default::default() + }), + ) + .await; + assert_eq!( + channels::get_channel(&state.channels, "global") + .await + .unwrap() + .name, + "Renamed by same account" + ); + deny( + request( + &state, + &other, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: "global".into(), + }), + ) + .await, + ); + request( + &state, + &second, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: "global".into(), + }), + ) + .await; + assert!( + channels::get_channel(&state.channels, "global") + .await + .is_none() + ); + for id in ["general", "voice"] { + deny( + request( + &state, + &admin, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: id.into(), + }), + ) + .await, + ); + assert!(channels::get_channel(&state.channels, id).await.is_some()); + } + drop(state); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[tokio::test] +async fn node_administration_never_bypasses_another_guilds_channel_permissions() { + let (state, admin, _second, _other, dir) = fixture().await; + let guild = state + .storage + .create_guild("guild-owner-device", "Private guild") + .await + .unwrap(); + channels::create( + &state.channels, + "private", + "Private", + channels::ChannelKind::Text, + Some(guild.clone()), + ) + .await; + state + .storage + .create_channel("private", "Private", "text", Some(&guild)) + .await + .unwrap(); + deny( + request( + &state, + &admin, + PacketId::ChannelCreate, + create("private-new", Some(guild.clone())), + ) + .await, + ); + let collision = request( + &state, + &admin, + PacketId::ChannelCreate, + create("private", None), + ) + .await + .unwrap(); + assert_eq!(collision.0.packet_id, PacketId::Error as u16); + assert_eq!( + channels::get_channel(&state.channels, "private") + .await + .unwrap() + .guild_id, + Some(guild.clone()) + ); + assert_eq!( + state + .storage + .list_channels() + .await + .unwrap() + .into_iter() + .find(|channel| channel.id == "private") + .unwrap() + .guild_id, + Some(guild.clone()) + ); + + deny( + request( + &state, + &admin, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "private".into(), + channel_name: "Stolen".into(), + ..Default::default() + }), + ) + .await, + ); + deny( + request( + &state, + &admin, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: "private".into(), + }), + ) + .await, + ); + assert_eq!( + channels::get_channel(&state.channels, "private") + .await + .unwrap() + .name, + "Private" + ); + let owner = session::create(&state.sessions, "guild-owner-device".into(), "Owner".into()).await; + request( + &state, + &owner.session_id, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "private".into(), + channel_name: "Owner edit".into(), + ..Default::default() + }), + ) + .await; + assert_eq!( + channels::get_channel(&state.channels, "private") + .await + .unwrap() + .name, + "Owner edit" + ); + drop(state); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[tokio::test] +async fn global_created_renamed_and_deleted_channels_survive_fresh_storage_bootstrap() { + let (state, admin, _second, other, dir) = fixture().await; + request( + &state, + &admin, + PacketId::ChannelCreate, + create("persistent-global", None), + ) + .await; + request( + &state, + &admin, + PacketId::ChannelCreate, + create("deleted-global", None), + ) + .await; + request( + &state, + &admin, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: "deleted-global".into(), + }), + ) + .await; + request( + &state, + &admin, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "persistent-global".into(), + channel_name: "Saved name".into(), + ..Default::default() + }), + ) + .await; + request( + &state, + &admin, + PacketId::ChannelEdit, + to_payload(&proto::ChannelEdit { + channel_id: "general".into(), + channel_name: "Saved bootstrap name".into(), + ..Default::default() + }), + ) + .await; + let (mut stream, _peer) = tokio::io::duplex(65536); + super::join( + &mut stream, + &mut 0, + &other, + "persistent-global", + &state, + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + assert_eq!( + state.sessions.read().await[&other].channel_id.as_deref(), + Some("persistent-global") + ); + drop(state); + let restarted = Storage::connect_sqlite(dir.join("node.sqlite").to_str().unwrap()) + .await + .unwrap(); + let cache = channels::new_store(); + restarted.load_channels_to_cache(&cache).await.unwrap(); + let global = channels::get_channel(&cache, "persistent-global") + .await + .unwrap(); + assert_eq!(global.name, "Saved name"); + assert_eq!(global.kind, channels::ChannelKind::Text); + assert_eq!(global.guild_id, None); + assert!(global.members.is_empty()); + assert!( + channels::get_channel(&cache, "deleted-global") + .await + .is_none() + ); + assert_eq!( + channels::get_channel(&cache, "general").await.unwrap().name, + "Saved bootstrap name" + ); + assert!(channels::get_channel(&cache, "voice").await.is_some()); + drop(restarted); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[tokio::test] +async fn global_create_notification_precedes_delete_even_when_creator_socket_is_blocked() { + let (state, admin, _second, _other, dir) = fixture().await; + let mut events = state.broadcast.subscribe(); + let (mut socket, mut client) = tokio::io::duplex(1); + let shared = state.clone(); + let actor = admin.clone(); + let task = tokio::spawn(async move { + super::handle_channel_create( + &mut socket, + &mut 0, + &actor, + &create("ordered-global", None), + &shared, + ) + .await + }); + let created = tokio::time::timeout(std::time::Duration::from_secs(1), events.recv()) + .await + .unwrap() + .unwrap(); + let header = proto::PacketHeader::from_bytes(created.data[..12].try_into().unwrap()); + assert_eq!(header.packet_id, PacketId::ChannelCreate as u16); + assert!( + !task.is_finished(), + "creator socket intentionally blocked before its response" + ); + request( + &state, + &admin, + PacketId::ChannelDelete, + to_payload(&proto::ChannelDelete { + channel_id: "ordered-global".into(), + }), + ) + .await; + let deleted = events.try_recv().unwrap(); + let header = proto::PacketHeader::from_bytes(deleted.data[..12].try_into().unwrap()); + assert_eq!(header.packet_id, PacketId::ChannelDelete as u16); + crate::net::io::read_packet(&mut client).await.unwrap(); + task.await.unwrap().unwrap(); + assert!( + events.try_recv().is_err(), + "no late create event can recreate a deleted sidebar row" + ); + assert!( + channels::get_channel(&state.channels, "ordered-global") + .await + .is_none() + ); + drop(state); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/gateway/src/handler/channel/create.rs b/gateway/src/handler/channel/create.rs index d622aa5..5664cc9 100644 --- a/gateway/src/handler/channel/create.rs +++ b/gateway/src/handler/channel/create.rs @@ -31,6 +31,8 @@ pub async fn handle_channel_create( let kind = match req.kind.as_str() { "text" => channels::ChannelKind::Text, "voice" => channels::ChannelKind::Voice, + "category" => channels::ChannelKind::Category, + "forum" => channels::ChannelKind::Forum, other => { warn!( "channel_create from {}: invalid kind '{other}'", @@ -50,6 +52,24 @@ pub async fn handle_channel_create( } }; + if !super::visible_kind(&kind, &sess) { + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::InvalidPacket as u32, + message: "channel kind capability was not negotiated".into(), + }), + ) + .await; + } + let parent_id = req + .parent_id + .as_deref() + .map(str::trim) + .filter(|id| !id.is_empty()) + .map(str::to_owned); let channel_id = req.channel_id.trim().to_string(); let channel_name = if req.channel_name.trim().is_empty() { channel_id.clone() @@ -95,38 +115,89 @@ pub async fn handle_channel_create( } let access = state.access.write().await; - if !super::may_manage(state, &sess.user_id, req.guild_id.as_deref()).await? { + if !super::may_manage_session(state, &sess, req.guild_id.as_deref()).await? { drop(access); super::denied(stream, seq).await?; return Ok(()); } let guild_id = req.guild_id.clone(); - - let created = channels::create( - &state.channels, + if let Err(message) = super::layout::validate( + state, &channel_id, - &channel_name, - kind.clone(), - guild_id.clone(), + &kind, + guild_id.as_deref(), + parent_id.as_deref(), + req.position, ) - .await; + .await + { + drop(access); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::InvalidPacket as u32, + message: message.into(), + }), + ) + .await; + } - if created - && let Err(error) = state + let created = if channels::get_channel(&state.channels, &channel_id) + .await + .is_some() + { + false + } else { + state .storage - .create_channel( + .create_channel_with_layout( &channel_id, &channel_name, kind.as_str(), guild_id.as_deref(), + parent_id.as_deref(), + req.position, ) - .await - { - channels::delete(&state.channels, &channel_id).await; - return Err(error); + .await? + }; + if created { + channels::create( + &state.channels, + &channel_id, + &channel_name, + kind.clone(), + guild_id.clone(), + ) + .await; + channels::set_layout( + &state.channels, + &channel_id, + parent_id.clone(), + req.position, + ) + .await; + } + if created && guild_id.is_none() { + super::notify_global( + state, + Some(session_id), + encode_packet( + PacketId::ChannelCreate, + 0, + &to_payload(&ChannelCreatePayload { + channel_id: channel_id.clone(), + channel_name: channel_name.clone(), + kind: kind.as_str().into(), + guild_id: None, + parent_id: parent_id.clone(), + position: req.position, + }), + ), + ); } - drop(access); if !created { io::send_frame( @@ -158,25 +229,31 @@ pub async fn handle_channel_create( members: Vec::new(), voice_endpoint: state.config.voice.bind.clone(), guild_id: req.guild_id.clone(), + parent_id: parent_id.clone(), + position: req.position, }; io::send_frame(stream, PacketId::ChannelState, seq, &to_payload(&sp)).await?; - super::notify_guild( - state, - guild_id.as_deref(), - Some(session_id), - encode_packet( - PacketId::ChannelCreate, - 0, - &to_payload(&ChannelCreatePayload { - channel_id: channel_id.clone(), - channel_name: channel_name.clone(), - kind: kind.as_str().into(), - guild_id: guild_id.clone(), - }), - ), - ) - .await?; + if guild_id.is_some() { + super::notify_guild( + state, + guild_id.as_deref(), + Some(session_id), + encode_packet( + PacketId::ChannelCreate, + 0, + &to_payload(&ChannelCreatePayload { + channel_id: channel_id.clone(), + channel_name: channel_name.clone(), + kind: kind.as_str().into(), + guild_id: guild_id.clone(), + parent_id: parent_id.clone(), + position: req.position, + }), + ), + ) + .await?; + } Ok(()) } @@ -213,9 +290,9 @@ pub async fn handle_channel_delete( let access = state.access.write().await; let channel = channels::get_channel(&state.channels, &req.channel_id).await; - if !super::may_manage( + if !super::may_manage_session( state, - &sess.user_id, + &sess, channel.as_ref().and_then(|c| c.guild_id.as_deref()), ) .await? @@ -224,9 +301,75 @@ pub async fn handle_channel_delete( super::denied(stream, seq).await?; return Ok(()); } + if channel + .as_ref() + .is_some_and(|channel| channel.kind == channels::ChannelKind::Forum) + && state.storage.forum_has_topics(&req.channel_id).await? + { + drop(access); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::PermissionDenied as u32, + message: "forum contains topics; deletion is disabled to preserve its posts".into(), + }), + ) + .await; + } + let children = if channel + .as_ref() + .is_some_and(|channel| channel.kind == channels::ChannelKind::Category) + { + channels::list(&state.channels) + .await + .into_iter() + .filter(|child| child.parent_id.as_deref() == Some(req.channel_id.as_str())) + .collect::>() + } else { + Vec::new() + }; state.storage.delete_channel(&req.channel_id).await?; super::revoke_channel(state, &req.channel_id).await; let existed = channels::delete(&state.channels, &req.channel_id).await; + for child in children { + channels::set_layout(&state.channels, &child.id, None, child.position).await; + super::notify_scope_locked( + state, + child.guild_id.as_deref(), + None, + encode_packet( + PacketId::ChannelEdit, + 0, + &to_payload(&crate::proto::ChannelEdit { + channel_id: child.id, + channel_name: child.name, + parent_id: Some(String::new()), + position: Some(child.position), + }), + ), + ) + .await?; + } + + if existed + && channel + .as_ref() + .is_some_and(|channel| channel.guild_id.is_none()) + { + super::notify_global( + state, + None, + encode_packet( + PacketId::ChannelDelete, + 0, + &to_payload(&ChannelDeletePayload { + channel_id: req.channel_id.clone(), + }), + ), + ); + } drop(access); if !existed { @@ -248,21 +391,26 @@ pub async fn handle_channel_delete( req.channel_id, sess.nickname ); - super::notify_guild( - state, - channel - .as_ref() - .and_then(|channel| channel.guild_id.as_deref()), - None, - encode_packet( - PacketId::ChannelDelete, - 0, - &to_payload(&ChannelDeletePayload { - channel_id: req.channel_id.clone(), - }), - ), - ) - .await?; + if channel + .as_ref() + .is_some_and(|channel| channel.guild_id.is_some()) + { + super::notify_guild( + state, + channel + .as_ref() + .and_then(|channel| channel.guild_id.as_deref()), + None, + encode_packet( + PacketId::ChannelDelete, + 0, + &to_payload(&ChannelDeletePayload { + channel_id: req.channel_id.clone(), + }), + ), + ) + .await?; + } Ok(()) } @@ -288,7 +436,8 @@ pub async fn handle_channel_list( None }; - let channels = channels::list(&state.channels).await; + let mut channels = channels::list(&state.channels).await; + channels.sort_by(|a, b| a.position.cmp(&b.position).then_with(|| a.id.cmp(&b.id))); let items: Vec = channels .iter() .filter(|c| { @@ -302,11 +451,14 @@ pub async fn handle_channel_list( .map(|gid| c.guild_id.as_deref() == Some(gid.as_str())) .unwrap_or(true) }) + .filter(|c| super::visible_kind(&c.kind, &sess)) .map(|c| ChannelListItem { channel_id: c.id.clone(), channel_name: c.name.clone(), kind: c.kind.as_str().into(), guild_id: c.guild_id.clone(), + parent_id: c.parent_id.clone(), + position: c.position, }) .collect(); let p = ChannelListPayload { diff --git a/gateway/src/handler/channel/edit.rs b/gateway/src/handler/channel/edit.rs index a75fe00..d28b6df 100644 --- a/gateway/src/handler/channel/edit.rs +++ b/gateway/src/handler/channel/edit.rs @@ -46,9 +46,9 @@ pub async fn handle_channel_edit( let access = state.access.write().await; let channel = channels::get_channel(&state.channels, &channel_id).await; - if !super::may_manage( + if !super::may_manage_session( state, - &sess.user_id, + &sess, channel.as_ref().and_then(|c| c.guild_id.as_deref()), ) .await? @@ -57,12 +57,97 @@ pub async fn handle_channel_edit( super::denied(stream, seq).await?; return Ok(()); } - state + let Some(existing) = channel.as_ref() else { + drop(access); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::ChannelNotFound as u32, + message: "channel not found".into(), + }), + ) + .await; + }; + let parent_id = match req.parent_id.as_deref() { + Some(parent) => { + let parent = parent.trim(); + if parent.is_empty() { + None + } else { + Some(parent.to_owned()) + } + } + None => existing.parent_id.clone(), + }; + let position = req.position.unwrap_or(existing.position); + if !super::visible_kind(&existing.kind, &sess) { + drop(access); + return super::denied(stream, seq).await; + } + if let Err(message) = super::layout::validate( + state, + &channel_id, + &existing.kind, + existing.guild_id.as_deref(), + parent_id.as_deref(), + position, + ) + .await + { + drop(access); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::InvalidPacket as u32, + message: message.into(), + }), + ) + .await; + } + if !state .storage - .update_channel(&channel_id, &channel_name) - .await?; + .update_channel_with_layout(&channel_id, &channel_name, parent_id.as_deref(), position) + .await? + { + drop(access); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&crate::proto::ErrorPayload { + code: crate::proto::ErrorCode::ChannelNotFound as u32, + message: "channel not found".into(), + }), + ) + .await; + } + channels::set_layout(&state.channels, &channel_id, parent_id.clone(), position).await; let renamed = channels::rename(&state.channels, &channel_id, &channel_name).await; + if renamed + && channel + .as_ref() + .is_some_and(|channel| channel.guild_id.is_none()) + { + super::notify_global( + state, + None, + encode_packet( + PacketId::ChannelEdit, + 0, + &to_payload(&ChannelEditPayload { + channel_id: channel_id.clone(), + channel_name: channel_name.clone(), + parent_id: Some(parent_id.clone().unwrap_or_default()), + position: Some(position), + }), + ), + ); + } drop(access); if !renamed { io::send_frame( @@ -83,22 +168,29 @@ pub async fn handle_channel_edit( channel_id, channel_name, sess.nickname ); - super::notify_guild( - state, - channel - .as_ref() - .and_then(|channel| channel.guild_id.as_deref()), - None, - encode_packet( - PacketId::ChannelEdit, - 0, - &to_payload(&ChannelEditPayload { - channel_id: channel_id.clone(), - channel_name: channel_name.clone(), - }), - ), - ) - .await?; + if channel + .as_ref() + .is_some_and(|channel| channel.guild_id.is_some()) + { + super::notify_guild( + state, + channel + .as_ref() + .and_then(|channel| channel.guild_id.as_deref()), + None, + encode_packet( + PacketId::ChannelEdit, + 0, + &to_payload(&ChannelEditPayload { + channel_id: channel_id.clone(), + channel_name: channel_name.clone(), + parent_id: Some(parent_id.clone().unwrap_or_default()), + position: Some(position), + }), + ), + ) + .await?; + } Ok(()) } diff --git a/gateway/src/handler/channel/join.rs b/gateway/src/handler/channel/join.rs index 717c75d..2742e50 100644 --- a/gateway/src/handler/channel/join.rs +++ b/gateway/src/handler/channel/join.rs @@ -60,6 +60,21 @@ pub async fn join( ) .await; } + if ch.kind == channels::ChannelKind::Category || !super::visible_kind(&ch.kind, &sess) { + drop(guard); + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&proto::ErrorPayload { + code: proto::ErrorCode::InvalidPacket as u32, + message: + "categories cannot be joined and optional channel kinds require negotiation" + .into(), + }), + ) + .await; + } let is_voice = ch.kind == channels::ChannelKind::Voice; if is_voice && state.voice_member_tx.is_none() { drop(guard); @@ -168,9 +183,15 @@ pub async fn join( None }; let mut members = Vec::new(); + let mut devices = std::collections::HashSet::new(); for sid in channels::members(&state.channels, channel_id).await { if let Some(s) = session::get(&state.sessions, &sid).await { + if !devices.insert(s.user_id.clone()) { + continue; + } members.push(MemberInfo { + account_id: s.account_id, + avatar_url: s.avatar_url, user_id: s.user_id, nickname: s.nickname, in_voice: is_voice, @@ -181,6 +202,8 @@ pub async fn join( channel_id: channel_id.into(), user_id: sess.user_id, nickname: sess.nickname, + account_id: sess.account_id, + avatar_url: sess.avatar_url, }; let _ = state.broadcast.send(BroadcastMsg { channel_id: Some(channel_id.into()), @@ -196,6 +219,8 @@ pub async fn join( members, voice_endpoint: state.config.voice.bind.clone(), guild_id: ch.guild_id, + parent_id: ch.parent_id, + position: ch.position, }; io::send_frame(stream, PacketId::ChannelState, seq, &to_payload(&payload)).await?; if let Some(credentials) = credentials { @@ -207,7 +232,7 @@ pub async fn join( ) .await?; } - if !is_voice { + if ch.kind == channels::ChannelKind::Text { let history = state.storage.get_history(channel_id, HISTORY_LIMIT).await?; if !history.is_empty() { let _ = state.broadcast.send(BroadcastMsg { diff --git a/gateway/src/handler/channel/join/tests.rs b/gateway/src/handler/channel/join/tests.rs index bf69259..08d5f84 100644 --- a/gateway/src/handler/channel/join/tests.rs +++ b/gateway/src/handler/channel/join/tests.rs @@ -212,6 +212,7 @@ async fn channel_mutations_require_guild_authority_and_deletion_revokes_voice_cr &to_payload(&ChannelEditPayload { channel_id: "managed".into(), channel_name: "stolen".into(), + ..Default::default() }), &state, ) @@ -497,6 +498,7 @@ async fn private_channel_notifications_target_members_and_acknowledge_editor() { channel_name: "Private name".into(), kind: "text".into(), guild_id: Some(guild.clone()), + ..Default::default() }; crate::handler::channel::handle_channel_create( &mut stream, @@ -514,6 +516,7 @@ async fn private_channel_notifications_target_members_and_acknowledge_editor() { &to_payload(&ChannelEditPayload { channel_id: creation.channel_id.clone(), channel_name: "Renamed privately".into(), + ..Default::default() }), &state, ) @@ -676,3 +679,68 @@ async fn queued_targeted_history_is_rechecked_after_leave_or_guild_revoke() { "history queued before a guild kick must not be delivered afterwards" ); } + +#[tokio::test] +async fn repeated_device_sessions_leave_only_after_last_subscription_in_either_order() { + for reverse in [false, true] { + let (state, first) = fixture().await; + let second = session::create(&state.sessions, "visitor".into(), "Visitor".into()).await; + channels::join(&state.channels, "general", &second.session_id).await; + let mut events = state.broadcast.subscribe(); + let order = if reverse { + [second.session_id, first] + } else { + [first, second.session_id] + }; + channels::leave(&state.channels, "general", &order[0]).await; + broadcast_leave(&state, "general", &order[0]).await; + assert!( + events.try_recv().is_err(), + "remaining session must retain member" + ); + assert_eq!(channels::members(&state.channels, "general").await.len(), 1); + channels::leave(&state.channels, "general", &order[1]).await; + broadcast_leave(&state, "general", &order[1]).await; + assert!(events.try_recv().is_ok(), "last session must emit leave"); + } +} + +#[tokio::test] +async fn snapshot_has_one_member_per_device_and_carries_central_profile() { + use prost::Message; + let (state, first) = fixture().await; + let account = "c3ebd30f-6bb4-4c31-9016-bec57e986c93"; + { + let mut sessions = state.sessions.write().await; + let first = sessions.get_mut(&first).unwrap(); + first.account_id = account.into(); + first.avatar_url = format!("/v1/avatars/default/{account}"); + } + let second = session::create(&state.sessions, "visitor".into(), "Visitor".into()).await; + { + let mut sessions = state.sessions.write().await; + let member = sessions.get_mut(&second.session_id).unwrap(); + member.account_id = account.into(); + member.avatar_url = format!("/v1/avatars/default/{account}"); + } + let (mut stream, mut peer) = tokio::io::duplex(65536); + join( + &mut stream, + &mut 0, + &second.session_id, + "general", + &state, + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + let (_, payload) = io::read_packet(&mut peer).await.unwrap(); + let snapshot = ChannelStatePayload::decode(payload.as_slice()).unwrap(); + assert_eq!(snapshot.members.len(), 1); + assert_eq!(snapshot.members[0].account_id, account); + assert_eq!( + snapshot.members[0].avatar_url, + format!("/v1/avatars/default/{account}") + ); + assert_eq!(snapshot.members[0].user_id, "visitor"); +} diff --git a/gateway/src/handler/channel/layout.rs b/gateway/src/handler/channel/layout.rs new file mode 100644 index 0000000..719c0e5 --- /dev/null +++ b/gateway/src/handler/channel/layout.rs @@ -0,0 +1,33 @@ +//! One-level category layout. Call validation while holding the access mutation guard. +use crate::{ + domain::channels::{self, ChannelKind}, + net::state::State, +}; + +pub(super) async fn validate( + state: &State, + id: &str, + kind: &ChannelKind, + guild: Option<&str>, + parent: Option<&str>, + position: i32, +) -> Result<(), &'static str> { + if !(0..=1_000_000).contains(&position) { + return Err("position must be between 0 and 1000000"); + } + if kind == &ChannelKind::Category && parent.is_some() { + return Err("categories must be at root"); + } + if let Some(parent) = parent { + if parent == id { + return Err("channel cannot be its own parent"); + } + let Some(category) = channels::get_channel(&state.channels, parent).await else { + return Err("parent category not found"); + }; + if category.kind != ChannelKind::Category || category.guild_id.as_deref() != guild { + return Err("parent must be a category in the same server or guild scope"); + } + } + Ok(()) +} diff --git a/gateway/src/handler/channel/leave.rs b/gateway/src/handler/channel/leave.rs index 262f065..fe6200d 100644 --- a/gateway/src/handler/channel/leave.rs +++ b/gateway/src/handler/channel/leave.rs @@ -16,6 +16,7 @@ pub async fn leave( channel_id: &str, state: &State, ) -> Result<()> { + let _guard = state.access.write().await; let Some(sess) = session::get(&state.sessions, session_id).await else { return Ok(()); }; diff --git a/gateway/src/handler/channel/mod.rs b/gateway/src/handler/channel/mod.rs index 11ebf57..f57fa32 100644 --- a/gateway/src/handler/channel/mod.rs +++ b/gateway/src/handler/channel/mod.rs @@ -1,6 +1,7 @@ pub mod create; pub mod edit; pub mod join; +mod layout; pub mod leave; pub use create::{handle_channel_create, handle_channel_delete, handle_channel_list}; @@ -15,6 +16,14 @@ use crate::{ pub async fn broadcast_leave(state: &State, channel_id: &str, session_id: &str) { if let Some(sess) = crate::domain::session::get(&state.sessions, session_id).await { + // Membership is session-scoped; a device remains until its last subscription leaves. + for sid in crate::domain::channels::members(&state.channels, channel_id).await { + if let Some(other) = crate::domain::session::get(&state.sessions, &sid).await + && other.user_id == sess.user_id + { + return; + } + } let p = UserLeavePayload { channel_id: channel_id.into(), user_id: sess.user_id.clone(), @@ -85,7 +94,33 @@ pub async fn revoke_guild_access(state: &State, guild: &str, user: Option<&str>) } } -/// Global channels have no modeled server administrator: authenticated users cannot mutate them. +/// Unknown structural kinds stay hidden from legacy clients that never opted in. +pub(super) fn visible_kind( + kind: &crate::domain::channels::ChannelKind, + session: &crate::domain::session::Session, +) -> bool { + use crate::domain::channels::ChannelKind; + match kind { + ChannelKind::Category => session.client_features.channel_structure, + ChannelKind::Forum => session.client_features.forums, + ChannelKind::Text | ChannelKind::Voice => true, + } +} + +/// Node capability is scoped to this exact authenticated account session. Device-key +/// lookup could accidentally inherit a different account's concurrently admitted role. +pub(super) async fn may_manage_session( + state: &State, + session: &crate::domain::session::Session, + guild: Option<&str>, +) -> anyhow::Result { + if guild.is_none() { + return Ok(state.config.gateway.is_node_admin(&session.account_id)); + } + may_manage(state, &session.user_id, guild).await +} + +/// Guild authorization retains its existing device ownership/role model. pub(super) async fn may_manage( state: &State, user: &str, @@ -124,7 +159,7 @@ pub(super) async fn denied( seq, &to_payload(&crate::proto::ErrorPayload { code: crate::proto::ErrorCode::PermissionDenied as u32, - message: "guild MANAGE_CHANNELS permission required".into(), + message: "node administrator or guild MANAGE_CHANNELS permission required".into(), }), ) .await @@ -232,15 +267,39 @@ pub async fn recheck_voice_permissions( Ok(()) } -/// Metadata is addressed only to currently persisted guild members, including non-subscribers. +/// Queue global metadata while the caller still serializes its channel mutation. +/// This helper performs no socket I/O and never reacquires the access lock. +pub(super) fn notify_global(state: &State, exclude: Option<&str>, data: Vec) { + let _ = state.broadcast.send(BroadcastMsg { + channel_id: None, + exclude_session: exclude.map(str::to_owned), + target_session_id: None, + data, + }); +} + +/// Global metadata is public to authenticated sessions; guild metadata is restricted to persisted members. pub(super) async fn notify_guild( state: &State, guild: Option<&str>, exclude: Option<&str>, data: Vec, ) -> anyhow::Result<()> { - let Some(guild) = guild else { return Ok(()) }; let _access = state.access.read().await; + notify_scope_locked(state, guild, exclude, data).await +} + +/// Caller holds the access guard so a mutation and its metadata queue remain ordered. +pub(super) async fn notify_scope_locked( + state: &State, + guild: Option<&str>, + exclude: Option<&str>, + data: Vec, +) -> anyhow::Result<()> { + let Some(guild) = guild else { + notify_global(state, exclude, data); + return Ok(()); + }; let members = state.storage.list_guild_members(guild).await?; let sessions: Vec<_> = state .sessions @@ -274,9 +333,21 @@ pub async fn may_deliver_metadata( payload: &[u8], ) -> bool { use prost::Message; + let Some(recipient_session) = crate::domain::session::get(&state.sessions, recipient).await + else { + return false; + }; let guild = match packet { PacketId::ChannelCreate => match crate::proto::ChannelCreatePayload::decode(payload) { - Ok(message) => message.guild_id, + Ok(message) => { + if (message.kind == "category" + && !recipient_session.client_features.channel_structure) + || (message.kind == "forum" && !recipient_session.client_features.forums) + { + return false; + } + message.guild_id + } Err(_) => return false, }, PacketId::ChannelEdit => match crate::proto::ChannelEditPayload::decode(payload) { @@ -284,7 +355,12 @@ pub async fn may_deliver_metadata( match crate::domain::channels::get_channel(&state.channels, &message.channel_id) .await { - Some(channel) => channel.guild_id, + Some(channel) => { + if !visible_kind(&channel.kind, &recipient_session) { + return false; + } + channel.guild_id + } None => return false, } } @@ -302,3 +378,6 @@ pub async fn may_deliver_metadata( .await .is_ok_and(|guilds| guilds.iter().any(|g| g.id == guild)) } + +#[cfg(test)] +mod admin_tests; diff --git a/gateway/src/handler/content/attachments.rs b/gateway/src/handler/content/attachments.rs new file mode 100644 index 0000000..726e138 --- /dev/null +++ b/gateway/src/handler/content/attachments.rs @@ -0,0 +1,413 @@ +use crate::{ + handler::Ctx, + net::{io, state::BroadcastMsg}, + proto::{self, PacketId}, +}; +use anyhow::{Result, bail}; +use image::{AnimationDecoder, ImageDecoder}; +use prost::Message; +use std::io::Cursor; +use tokio::io::{AsyncRead, AsyncWrite}; + +const MAX_BYTES: usize = 3 * 1024 * 1024; + +fn validate(data: &[u8]) -> Result<&'static str> { + if data.is_empty() || data.len() > MAX_BYTES { + bail!("image must be between 1 byte and 3 MiB"); + } + let format = image::guess_format(data)?; + let mime = match format { + image::ImageFormat::Png => "image/png", + image::ImageFormat::Jpeg => "image/jpeg", + image::ImageFormat::Gif => "image/gif", + _ => bail!("only PNG, JPEG and GIF are supported"), + }; + let mut reader = image::ImageReader::with_format(Cursor::new(data), format); + let mut limits = image::Limits::default(); + limits.max_image_width = Some(4096); + limits.max_image_height = Some(4096); + limits.max_alloc = Some(32 * 1024 * 1024); + reader.limits(limits.clone()); + let (w, h) = image::ImageReader::with_format(Cursor::new(data), format).into_dimensions()?; + if w == 0 || h == 0 || w > 4096 || h > 4096 || u64::from(w) * u64::from(h) > 4_000_000 { + bail!("image dimensions exceed limits"); + } + if format == image::ImageFormat::Gif { + let mut decoder = image::codecs::gif::GifDecoder::new(Cursor::new(data))?; + decoder.set_limits(limits)?; + let mut frames = 0; + let mut pixels = 0u64; + for frame in decoder.into_frames() { + let frame = frame?; + frames += 1; + pixels += u64::from(frame.buffer().width()) * u64::from(frame.buffer().height()); + if frames > 120 || pixels > 8_388_608 { + bail!("GIF animation exceeds limits"); + } + } + if frames == 0 { + bail!("GIF has no frames"); + } + } else { + reader.decode()?; + } + Ok(mime) +} + +pub async fn handle( + ctx: &mut Ctx<'_, S>, + session_id: &str, + pid: PacketId, + payload: &[u8], +) -> Result<()> { + let (request_id, channel_id, data, attachment_id) = if pid == PacketId::AttachmentUpload { + let p = proto::AttachmentUpload::decode(payload)?; + (p.request_id, p.channel_id, Some(p.data), String::new()) + } else { + let p = proto::AttachmentFetch::decode(payload)?; + (p.request_id, p.channel_id, None, p.attachment_id) + }; + let mut response = proto::AttachmentResult { + request_id, + channel_id: channel_id.clone(), + attachment_id, + ..Default::default() + }; + let result: Result<()> = async { + if response.request_id.len() > 128 + || channel_id.len() > 128 + || response.attachment_id.len() > 128 + { + bail!("invalid attachment request"); + } + let _access = ctx.state.access.read().await; + let session = crate::domain::session::get(&ctx.state.sessions, session_id) + .await + .ok_or_else(|| anyhow::anyhow!("session expired"))?; + if session.channel_id.as_deref() != Some(channel_id.as_str()) { + bail!("join this channel before accessing attachments"); + } + let channel = crate::domain::channels::get_channel(&ctx.state.channels, &channel_id) + .await + .ok_or_else(|| anyhow::anyhow!("channel not found"))?; + if channel.kind != crate::domain::channels::ChannelKind::Text { + bail!("image attachments require a text channel"); + } + if let Some(guild_id) = &channel.guild_id { + if !ctx + .state + .storage + .list_user_guilds(&session.user_id) + .await? + .iter() + .any(|guild| &guild.id == guild_id) + { + bail!("guild membership required"); + } + if data.is_some() { + let owner = ctx + .state + .storage + .get_guild(guild_id) + .await? + .is_some_and(|guild| guild.owner_id == session.user_id); + let roles = ctx + .state + .storage + .get_user_role_perms(guild_id, &session.user_id) + .await?; + let permissions = crate::domain::permissions::Permissions::from_role_perms(&roles); + if !owner && !permissions.has(crate::domain::permissions::Permissions::MESSAGE_SEND) + { + bail!("MESSAGE_SEND permission required"); + } + } + } + let allowed = if data.is_some() { + ctx.state.rate_limiter.try_consume(session_id) + } else { + ctx.state + .rate_limiter + .try_consume_attachment_fetch(session_id) + }; + if !allowed { + bail!("image request rate limit reached"); + } + if let Some(data) = data { + if data.len() > MAX_BYTES { + bail!("image exceeds 3 MiB"); + } + static VALIDATIONS: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(2); + let _permit = VALIDATIONS + .try_acquire() + .map_err(|_| anyhow::anyhow!("image validation is busy; try again"))?; + let (data, mime) = tokio::task::spawn_blocking(move || -> Result<_> { + let mime = validate(&data)?; + Ok((data, mime)) + }) + .await??; + let id = uuid::Uuid::new_v4().to_string(); + let msg = proto::ChatMessage { + message_id: uuid::Uuid::new_v4().to_string(), + channel_id: channel_id.clone(), + sender_id: session.user_id, + content: format!("vnox-attachment:{id}"), + timestamp: crate::domain::storage::now_ms(), + edited: false, + reply_to: None, + }; + if !ctx + .state + .storage + .save_attachment_message(&msg, &id, mime, &data) + .await? + { + bail!("could not save image message"); + } + let _ = ctx.state.broadcast.send(BroadcastMsg { + channel_id: Some(channel_id.clone()), + exclude_session: None, + target_session_id: None, + data: proto::encode_packet(PacketId::ChatMessage, 0, &proto::to_payload(&msg)), + }); + response.attachment_id = id; + response.mime = mime.into(); + } else { + let (mime, data) = ctx + .state + .storage + .get_attachment(&response.attachment_id, &channel_id) + .await? + .ok_or_else(|| anyhow::anyhow!("image not found"))?; + response.mime = mime; + response.data = data; + } + Ok(()) + } + .await; + if let Err(error) = result { + tracing::debug!(%error, "attachment request rejected"); + response.error = error.to_string().chars().take(256).collect(); + } + response.request_id = response.request_id.chars().take(128).collect(); + response.channel_id = response.channel_id.chars().take(128).collect(); + response.attachment_id = response.attachment_id.chars().take(128).collect(); + io::send_frame( + ctx.stream, + PacketId::AttachmentResult, + ctx.seq, + &proto::to_payload(&response), + ) + .await?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[tokio::test] + async fn authenticated_upload_broadcast_fetch_and_revoked_membership() -> Result<()> { + use crate::{ + domain::{channels, session}, + net::state::State, + }; + use std::sync::{Arc, atomic::AtomicUsize}; + let storage = crate::domain::storage::Storage::connect_sqlite(":memory:").await?; + let config = toml::from_str(include_str!("../../../../dev/config.toml"))?; + let dir = + std::env::temp_dir().join(format!("vnox-attachment-auth-{}", uuid::Uuid::new_v4())); + let identity = crate::bootstrap::server_identity::ServerIdentity::load_or_generate(&dir)?; + std::fs::remove_file(dir.join("server_identity.json"))?; + std::fs::remove_dir(dir)?; + let (broadcast, _) = tokio::sync::broadcast::channel(16); + let state = State::new( + session::new_store(), + channels::new_store(), + Arc::new(storage), + Arc::new(config), + Arc::new(identity), + broadcast, + crate::admin::metrics::Metrics::new(), + Arc::new(AtomicUsize::new(0)), + Arc::new(AtomicUsize::new(0)), + None, + ); + let guild = state.storage.create_guild("owner", "private").await?; + channels::create( + &state.channels, + "private", + "Private", + channels::ChannelKind::Text, + Some(guild), + ) + .await; + let session = session::create(&state.sessions, "outsider".into(), "Outsider".into()).await; + // Model a stale channel field after permission revocation: current guild membership must still be checked. + state + .sessions + .write() + .await + .get_mut(&session.session_id) + .expect("test session exists") + .channel_id = Some("private".into()); + for (pid, payload) in [ + ( + PacketId::AttachmentUpload, + proto::AttachmentUpload { + request_id: "up".into(), + channel_id: "private".into(), + data: vec![1], + } + .encode_to_vec(), + ), + ( + PacketId::AttachmentFetch, + proto::AttachmentFetch { + request_id: "get".into(), + channel_id: "private".into(), + attachment_id: "hidden".into(), + } + .encode_to_vec(), + ), + ] { + let (mut stream, mut peer) = tokio::io::duplex(4096); + handle( + &mut Ctx { + stream: &mut stream, + seq: &mut 0, + state: &state, + }, + &session.session_id, + pid, + &payload, + ) + .await?; + let (_, bytes) = io::read_packet(&mut peer).await?; + let response = proto::AttachmentResult::decode(bytes.as_slice())?; + assert_eq!(response.error, "guild membership required"); + assert!(response.data.is_empty()); + } + // A valid public-channel upload is persisted, announced, and fetchable by another joined participant. + channels::create( + &state.channels, + "public", + "Public", + channels::ChannelKind::Text, + None, + ) + .await; + state + .sessions + .write() + .await + .get_mut(&session.session_id) + .expect("test session exists") + .channel_id = Some("public".into()); + let mut broadcasts = state.broadcast.subscribe(); + let mut png = Cursor::new(Vec::new()); + image::RgbaImage::new(2, 2).write_to(&mut png, image::ImageFormat::Png)?; + let png = png.into_inner(); + let (mut stream, mut peer) = tokio::io::duplex(8192); + let upload = proto::AttachmentUpload { + request_id: "valid".into(), + channel_id: "public".into(), + data: png.clone(), + }; + handle( + &mut Ctx { + stream: &mut stream, + seq: &mut 0, + state: &state, + }, + &session.session_id, + PacketId::AttachmentUpload, + &upload.encode_to_vec(), + ) + .await?; + let (_, bytes) = io::read_packet(&mut peer).await?; + let accepted = proto::AttachmentResult::decode(bytes.as_slice())?; + assert!(accepted.error.is_empty(), "{}", accepted.error); + assert!(!accepted.attachment_id.is_empty()); + let announced = broadcasts.recv().await?; + assert_eq!(announced.channel_id.as_deref(), Some("public")); + let message = proto::ChatMessage::decode(&announced.data[12..])?; + assert_eq!( + message.content, + format!("vnox-attachment:{}", accepted.attachment_id) + ); + assert_eq!(message.sender_id, "outsider"); + let recipient = + session::create(&state.sessions, "recipient".into(), "Recipient".into()).await; + state + .sessions + .write() + .await + .get_mut(&recipient.session_id) + .expect("recipient exists") + .channel_id = Some("public".into()); + let fetch = proto::AttachmentFetch { + request_id: "fetch".into(), + channel_id: "public".into(), + attachment_id: accepted.attachment_id, + }; + handle( + &mut Ctx { + stream: &mut stream, + seq: &mut 0, + state: &state, + }, + &recipient.session_id, + PacketId::AttachmentFetch, + &fetch.encode_to_vec(), + ) + .await?; + let (_, bytes) = io::read_packet(&mut peer).await?; + let fetched = proto::AttachmentResult::decode(bytes.as_slice())?; + assert!(fetched.error.is_empty(), "{}", fetched.error); + assert_eq!(fetched.mime, "image/png"); + assert_eq!(fetched.data, png); + Ok(()) + } + #[test] + fn rejects_empty_oversized_and_unknown_images() { + assert!(validate(&[]).is_err()); + assert!(validate(&vec![0; MAX_BYTES + 1]).is_err()); + assert!(validate(b"not an image").is_err()); + } + #[test] + fn bounds_dimensions_and_gif_frame_count() { + let image = image::RgbaImage::new(4097, 1); + let mut bytes = Cursor::new(Vec::new()); + image + .write_to(&mut bytes, image::ImageFormat::Png) + .expect("encode wide test image"); + assert!(validate(bytes.get_ref()).is_err()); + let make_gif = |count| { + let mut data = Vec::new(); + { + let mut encoder = image::codecs::gif::GifEncoder::new(&mut data); + for _ in 0..count { + encoder + .encode_frame(image::Frame::new(image::RgbaImage::new(1, 1))) + .expect("encode gif frame"); + } + } + data + }; + assert_eq!( + validate(&make_gif(2)).expect("valid animated gif"), + "image/gif" + ); + assert!(validate(&make_gif(121)).is_err()); + } + #[test] + fn validates_real_png_and_rejects_truncation() { + let image = image::RgbaImage::new(2, 2); + let mut bytes = Cursor::new(Vec::new()); + image + .write_to(&mut bytes, image::ImageFormat::Png) + .expect("encode test png"); + assert_eq!(validate(bytes.get_ref()).expect("valid png"), "image/png"); + assert!(validate(&bytes.get_ref()[..24]).is_err()); + } +} diff --git a/gateway/src/handler/content/chat.rs b/gateway/src/handler/content/chat.rs index 5e38b54..66c15c7 100644 --- a/gateway/src/handler/content/chat.rs +++ b/gateway/src/handler/content/chat.rs @@ -7,6 +7,7 @@ use crate::{ }; pub async fn handle(session_id: &str, mut msg: ChatMessagePayload, state: &State) -> Result<()> { + let _access = state.access.read().await; let sess = match crate::domain::session::get(&state.sessions, session_id).await { Some(s) => s, None => return Ok(()), @@ -16,6 +17,13 @@ pub async fn handle(session_id: &str, mut msg: ChatMessagePayload, state: &State return Ok(()); } + if !crate::domain::channels::get_channel(&state.channels, &msg.channel_id) + .await + .is_some_and(|channel| channel.kind == crate::domain::channels::ChannelKind::Text) + { + return Ok(()); + } + // Rate limit check (token bucket per session). if !state.rate_limiter.try_consume(session_id) { state.metrics.inc(&state.metrics.rate_limited_events); diff --git a/gateway/src/handler/content/message_edit.rs b/gateway/src/handler/content/message_edit.rs index d93b1f7..83a79a4 100644 --- a/gateway/src/handler/content/message_edit.rs +++ b/gateway/src/handler/content/message_edit.rs @@ -9,6 +9,7 @@ use crate::{ }; pub async fn handle_message_edit(session_id: &str, payload: &[u8], state: &State) -> Result<()> { + let _access = state.access.read().await; let edit = crate::proto::MessageEditPayload::decode(payload)?; let sess = match session::get(&state.sessions, session_id).await { Some(s) => s, @@ -31,6 +32,14 @@ pub async fn handle_message_edit(session_id: &str, payload: &[u8], state: &State None => return Ok(()), }; + if msg.channel_id != edit.channel_id + || !crate::domain::channels::get_channel(&state.channels, &msg.channel_id) + .await + .is_some_and(|channel| channel.kind == crate::domain::channels::ChannelKind::Text) + { + return Ok(()); + } + state .storage .edit_message(&edit.message_id, &edit.content) @@ -61,6 +70,7 @@ pub async fn handle_message_edit(session_id: &str, payload: &[u8], state: &State } pub async fn handle_message_delete(session_id: &str, payload: &[u8], state: &State) -> Result<()> { + let _access = state.access.read().await; let delete = MessageDeletePayload::decode(payload)?; let sess = match session::get(&state.sessions, session_id).await { Some(s) => s, @@ -77,6 +87,16 @@ pub async fn handle_message_delete(session_id: &str, payload: &[u8], state: &Sta return Ok(()); } + let Some(message) = state.storage.get_message(&delete.message_id).await? else { + return Ok(()); + }; + if message.channel_id != delete.channel_id + || !crate::domain::channels::get_channel(&state.channels, &message.channel_id) + .await + .is_some_and(|channel| channel.kind == crate::domain::channels::ChannelKind::Text) + { + return Ok(()); + } state.storage.delete_message(&delete.message_id).await?; let _ = state.broadcast.send(BroadcastMsg { diff --git a/gateway/src/handler/content/mod.rs b/gateway/src/handler/content/mod.rs index 9cdcbd9..c23630f 100644 --- a/gateway/src/handler/content/mod.rs +++ b/gateway/src/handler/content/mod.rs @@ -1,3 +1,4 @@ +pub mod attachments; pub mod chat; pub mod message_edit; pub mod presence; diff --git a/gateway/src/handler/content/reaction.rs b/gateway/src/handler/content/reaction.rs index 931aed8..3ee8453 100644 --- a/gateway/src/handler/content/reaction.rs +++ b/gateway/src/handler/content/reaction.rs @@ -12,6 +12,7 @@ pub async fn handle_reaction_add( mut payload: ReactionPayload, state: &State, ) -> Result<()> { + let _access = state.access.read().await; let sess = match session::get(&state.sessions, session_id).await { Some(s) => s, None => return Ok(()), @@ -27,6 +28,16 @@ pub async fn handle_reaction_add( return Ok(()); } + let Some(message) = state.storage.get_message(&payload.message_id).await? else { + return Ok(()); + }; + if message.channel_id != payload.channel_id + || !crate::domain::channels::get_channel(&state.channels, &message.channel_id) + .await + .is_some_and(|channel| channel.kind == crate::domain::channels::ChannelKind::Text) + { + return Ok(()); + } payload.user_id = sess.user_id.clone(); state .storage @@ -52,6 +63,7 @@ pub async fn handle_reaction_remove( mut payload: ReactionPayload, state: &State, ) -> Result<()> { + let _access = state.access.read().await; let sess = match session::get(&state.sessions, session_id).await { Some(s) => s, None => return Ok(()), @@ -61,6 +73,16 @@ pub async fn handle_reaction_remove( return Ok(()); } + let Some(message) = state.storage.get_message(&payload.message_id).await? else { + return Ok(()); + }; + if message.channel_id != payload.channel_id + || !crate::domain::channels::get_channel(&state.channels, &message.channel_id) + .await + .is_some_and(|channel| channel.kind == crate::domain::channels::ChannelKind::Text) + { + return Ok(()); + } payload.user_id = sess.user_id.clone(); // Check user owns the reaction diff --git a/gateway/src/handler/deliver.rs b/gateway/src/handler/deliver.rs index a74fb53..aafd131 100644 --- a/gateway/src/handler/deliver.rs +++ b/gateway/src/handler/deliver.rs @@ -42,6 +42,28 @@ pub async fn deliver_frame( { return Ok(()); } + if crate::proto::PacketId::from_u16(hdr.packet_id) + == Some(crate::proto::PacketId::ForumChanged) + && !crate::handler::forum::may_deliver( + state, + session_id, + &msg.data[crate::proto::PacketHeader::SIZE..], + ) + .await + { + return Ok(()); + } + if crate::proto::PacketId::from_u16(hdr.packet_id) + == Some(crate::proto::PacketId::ProfileUpdated) + && !crate::handler::profile::may_deliver( + state, + session_id, + &msg.data[crate::proto::PacketHeader::SIZE..], + ) + .await + { + return Ok(()); + } if crate::proto::PacketId::from_u16(hdr.packet_id) == Some(crate::proto::PacketId::MediaEvent) && !crate::media::may_deliver( diff --git a/gateway/src/handler/dispatch.rs b/gateway/src/handler/dispatch.rs index 69f52b8..0bf7ad0 100644 --- a/gateway/src/handler/dispatch.rs +++ b/gateway/src/handler/dispatch.rs @@ -21,6 +21,22 @@ pub async fn dispatch( addr: std::net::SocketAddr, ) -> Result<()> { match pid { + PacketId::ClientFeatures => { + super::node_settings::features(session_id, payload, ctx.state).await? + } + PacketId::NodeSettingsRequest => { + super::node_settings::handle(ctx.stream, ctx.seq, session_id, payload, ctx.state) + .await? + } + PacketId::ForumRequest => { + super::forum::handle(ctx.stream, ctx.seq, session_id, payload, ctx.state).await? + } + PacketId::ProfileUpdate => { + super::profile::handle(ctx.stream, ctx.seq, session_id, payload, ctx.state).await? + } + PacketId::AttachmentUpload | PacketId::AttachmentFetch => { + content::attachments::handle(ctx, session_id, pid, payload).await?; + } PacketId::MediaControl => crate::media::handle(ctx, session_id, payload).await?, PacketId::WebhookCreate | PacketId::WebhookList diff --git a/gateway/src/handler/forum.rs b/gateway/src/handler/forum.rs new file mode 100644 index 0000000..5f145b4 --- /dev/null +++ b/gateway/src/handler/forum.rs @@ -0,0 +1,251 @@ +//! Real topic/reply operations. Forum posts never pass through generic chat mutation APIs. +use crate::{ + domain::{ + channels::{self, Channel, ChannelKind}, + permissions::Permissions, + session::{self, Session}, + }, + net::{ + io, + state::{BroadcastMsg, State}, + }, + proto::{ + self, ForumChanged, ForumOperation, ForumRequest, ForumResult, MemberInfo, PacketId, + encode_packet, to_payload, + }, +}; +use anyhow::Result; +use prost::Message; +use tokio::io::{AsyncRead, AsyncWrite}; + +type Rejection = (proto::ErrorCode, &'static str); +fn invalid(message: &'static str) -> Rejection { + (proto::ErrorCode::InvalidPacket, message) +} +fn text(value: &str, max: usize, multiline: bool) -> bool { + value.chars().count()<=max && !value.chars().any(|c|(c.is_control() && !(multiline && matches!(c,'\n'|'\t'))) || matches!(c,'\u{061c}'|'\u{200e}'|'\u{200f}'|'\u{202a}'..='\u{202e}'|'\u{2066}'..='\u{2069}')) +} +fn uuid(value: &str) -> bool { + uuid::Uuid::parse_str(value).is_ok_and(|id| !id.is_nil() && id.to_string() == value) +} +fn author(session: &Session) -> MemberInfo { + MemberInfo { + user_id: session.user_id.clone(), + account_id: session.account_id.clone(), + nickname: session.nickname.clone(), + avatar_url: session.avatar_url.clone(), + in_voice: false, + } +} + +async fn authorize( + state: &State, + session: &Session, + channel: &Channel, + posting: bool, +) -> Result> { + if !session.client_features.forums { + return Ok(Err(invalid("forum support was not negotiated"))); + } + if channel.kind != ChannelKind::Forum + || session.channel_id.as_deref() != Some(channel.id.as_str()) + || !channel.members.contains(&session.session_id) + { + return Ok(Err(( + proto::ErrorCode::PermissionDenied, + "open this forum before reading or posting", + ))); + } + if let Some(guild) = &channel.guild_id { + if !state + .storage + .list_user_guilds(&session.user_id) + .await? + .iter() + .any(|membership| membership.id == *guild) + { + return Ok(Err(( + proto::ErrorCode::PermissionDenied, + "guild membership required", + ))); + } + if posting { + let owner = state + .storage + .get_guild(guild) + .await? + .is_some_and(|guild| guild.owner_id == session.user_id); + let permissions = state + .storage + .get_user_role_perms(guild, &session.user_id) + .await?; + if !owner && !Permissions::from_role_perms(&permissions).has(Permissions::MESSAGE_SEND) + { + return Ok(Err(( + proto::ErrorCode::PermissionDenied, + "MESSAGE_SEND permission required", + ))); + } + } + } + Ok(Ok(())) +} + +pub async fn handle( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + seq: &mut u32, + sid: &str, + payload: &[u8], + state: &State, +) -> Result<()> { + let request = if payload.len() <= 70 * 1024 { + ForumRequest::decode(payload).ok() + } else { + None + }; + let Some(request) = request else { + return send( + stream, + seq, + ForumResult { + error_code: proto::ErrorCode::InvalidPacket as u32, + error: "invalid forum request".into(), + ..Default::default() + }, + ) + .await; + }; + let mut result = ForumResult { + request_id: request.request_id.clone(), + operation: request.operation, + channel_id: request.channel_id.clone(), + topic_id: request.topic_id.clone(), + ..Default::default() + }; + let guard = state.access.read().await; + let operation = ForumOperation::try_from(request.operation).ok(); + let posting = matches!( + operation, + Some(ForumOperation::Create | ForumOperation::Reply) + ); + let session = session::get(&state.sessions, sid).await; + let channel = channels::get_channel(&state.channels, &request.channel_id).await; + let rejection = if request.request_id.is_empty() + || request.request_id.len() > 128 + || request.channel_id.is_empty() + || request.channel_id.len() > 128 + { + Some(invalid("request_id and channel_id are required")) + } else if request.offset > 100_000 { + Some(invalid("forum offset is too large")) + } else if operation.is_none() || operation == Some(ForumOperation::Unspecified) { + Some(invalid("unsupported forum operation")) + } else if channel.is_none() { + Some((proto::ErrorCode::ChannelNotFound, "forum not found")) + } else if session.is_none() { + Some((proto::ErrorCode::AuthFailed, "session not found")) + } else { + authorize( + state, + session.as_ref().expect("checked session"), + channel.as_ref().expect("checked forum"), + posting, + ) + .await? + .err() + }; + if let Some((code, error)) = rejection { + result.error_code = code as u32; + result.error = error.into(); + drop(guard); + return send(stream, seq, result).await; + } + let session = session.expect("checked session"); + if !state.rate_limiter.try_consume(sid) { + result.error_code = proto::ErrorCode::RateLimited as u32; + result.error = "forum requests are too frequent".into(); + drop(guard); + return send(stream, seq, result).await; + } + let outcome:Result,Rejection>>=async { + match operation.expect("checked operation") { + ForumOperation::List=>{ + let limit=if request.limit==0 {20} else {request.limit.min(50)}; + result.topics=state.storage.forum_list(&request.channel_id,request.offset,limit+1).await?; + result.has_more=result.topics.len()>limit as usize;result.topics.truncate(limit as usize);Ok(Ok(None)) + } + ForumOperation::Open=>{ + if !uuid(&request.topic_id) {return Ok(Err(invalid("valid topic_id is required")));} + let Some(topic)=state.storage.forum_topic(&request.channel_id,&request.topic_id).await? else {return Ok(Err((proto::ErrorCode::ChannelNotFound,"topic not found in this forum")))}; + let limit=if request.limit==0 {50} else {request.limit.min(100)}; + let posts=state.storage.forum_posts(&request.channel_id,&request.topic_id,request.offset,limit+1).await?; + let mut bytes=0;let mut count=0; + for post in &posts {if count>=limit as usize || bytes+post.encoded_len()>2*1024*1024 {break;}bytes+=post.encoded_len();count+=1;} + result.has_more=posts.len()>count;result.posts=posts.into_iter().take(count).collect();result.topics.push(topic);Ok(Ok(None)) + } + ForumOperation::Create=>{ + let title=request.title.trim();let content=request.content.trim(); + if !uuid(&request.topic_id) || !uuid(&request.message_id) || title.is_empty() || !text(title,160,false) || content.is_empty() || content.len()>64*1024 || !text(content,16000,true) {return Ok(Err(invalid("topic/message UUIDs, title (1–160) and body (1–16000 characters, 64 KiB maximum) are required")));} + let (topic,post)=state.storage.forum_create(&request.topic_id,&request.message_id,&request.channel_id,title,content,&author(&session)).await?; + result.topics.push(topic.clone());result.posts.push(post.clone());Ok(Ok(Some(ForumChanged {channel_id:request.channel_id.clone(),topic:Some(topic),post:Some(post)}))) + } + ForumOperation::Reply=>{ + let content=request.content.trim(); + if !uuid(&request.topic_id) || !uuid(&request.message_id) || content.is_empty() || content.len()>64*1024 || !text(content,16000,true) {return Ok(Err(invalid("topic/message UUIDs and body (1–16000 characters, 64 KiB maximum) are required")));} + if state.storage.forum_topic(&request.channel_id,&request.topic_id).await?.is_none() {return Ok(Err((proto::ErrorCode::ChannelNotFound,"topic not found in this forum")));} + let (topic,post)=state.storage.forum_reply(&request.message_id,&request.channel_id,&request.topic_id,content,&author(&session)).await?; + result.topics.push(topic.clone());result.posts.push(post.clone());Ok(Ok(Some(ForumChanged {channel_id:request.channel_id.clone(),topic:Some(topic),post:Some(post)}))) + } + ForumOperation::Unspecified=>Ok(Err(invalid("unsupported forum operation"))), + } + }.await; + match outcome { + Ok(Ok(Some(event))) => { + let _ = state.broadcast.send(BroadcastMsg { + channel_id: Some(request.channel_id), + exclude_session: Some(sid.into()), + target_session_id: None, + data: encode_packet(PacketId::ForumChanged, 0, &to_payload(&event)), + }); + } + Ok(Ok(None)) => {} + Ok(Err((code, error))) => { + result.error_code = code as u32; + result.error = error.into(); + } + Err(error) => { + tracing::warn!(%error,"forum operation failed"); + result.error_code = proto::ErrorCode::InvalidPacket as u32; + result.error = + "forum operation failed or mutation IDs conflict; no unrelated message was changed" + .into(); + } + } + drop(guard); + send(stream, seq, result).await +} +async fn send( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + seq: &mut u32, + result: ForumResult, +) -> Result<()> { + io::send_frame(stream, PacketId::ForumResult, seq, &to_payload(&result)).await +} + +/// Topic/reply content must remain hidden if access/subscription is revoked while queued. +pub async fn may_deliver(state: &State, sid: &str, payload: &[u8]) -> bool { + let Ok(event) = ForumChanged::decode(payload) else { + return false; + }; + let _guard = state.access.read().await; + let Some(session) = session::get(&state.sessions, sid).await else { + return false; + }; + let Some(channel) = channels::get_channel(&state.channels, &event.channel_id).await else { + return false; + }; + matches!( + authorize(state, &session, &channel, false).await, + Ok(Ok(())) + ) +} diff --git a/gateway/src/handler/guild/crud.rs b/gateway/src/handler/guild/crud.rs index 22e9f36..c6707df 100644 --- a/gateway/src/handler/guild/crud.rs +++ b/gateway/src/handler/guild/crud.rs @@ -88,6 +88,18 @@ pub async fn handle_guild_delete( return Ok(()); } + if state.storage.guild_has_forum_topics(&req.guild_id).await? { + drop(guard); + send_err( + stream, + seq, + crate::proto::ErrorCode::PermissionDenied, + "guild contains forum topics; deletion is disabled to preserve them", + ) + .await?; + return Ok(()); + } + crate::handler::channel::revoke_guild_access(state, &req.guild_id, None).await; state.storage.delete_guild(&req.guild_id).await?; state diff --git a/gateway/src/handler/mod.rs b/gateway/src/handler/mod.rs index 1a88920..b88b07e 100644 --- a/gateway/src/handler/mod.rs +++ b/gateway/src/handler/mod.rs @@ -4,8 +4,11 @@ pub mod deliver; pub mod direct_message; pub mod dispatch; pub mod e2ee; +pub mod forum; pub mod friends; pub mod guild; +pub mod node_settings; +pub mod profile; pub mod run; pub use dispatch::dispatch; diff --git a/gateway/src/handler/node_settings.rs b/gateway/src/handler/node_settings.rs new file mode 100644 index 0000000..e285b3c --- /dev/null +++ b/gateway/src/handler/node_settings.rs @@ -0,0 +1,133 @@ +//! Public node settings and post-SESSION feature negotiation. +use crate::{ + domain::session, + net::{ + io, + state::{BroadcastMsg, State}, + }, + proto::{ + self, ClientFeatures, NodeSettingsRequest, NodeSettingsResult, PacketId, encode_packet, + to_payload, + }, +}; +use anyhow::Result; +use prost::Message; +use tokio::io::{AsyncRead, AsyncWrite}; + +pub async fn features(sid: &str, payload: &[u8], state: &State) -> Result<()> { + anyhow::ensure!(payload.len() <= 128, "client feature report is too large"); + let features = ClientFeatures::decode(payload)?; + if let Some(session) = state.sessions.write().await.get_mut(sid) { + session.client_features = features; + } + Ok(()) +} + +pub async fn handle( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + seq: &mut u32, + sid: &str, + payload: &[u8], + state: &State, +) -> Result<()> { + let request = if payload.len() <= 12 * 1024 { + NodeSettingsRequest::decode(payload).ok() + } else { + None + }; + let Some(request) = request else { + return send( + stream, + seq, + NodeSettingsResult { + error_code: proto::ErrorCode::InvalidPacket as u32, + error: "invalid node settings request".into(), + ..Default::default() + }, + ) + .await; + }; + let Some(session) = session::get(&state.sessions, sid).await else { + return Ok(()); + }; + let mut result = NodeSettingsResult { + request_id: request.request_id.clone(), + ..Default::default() + }; + let reject = if request.request_id.is_empty() || request.request_id.len() > 128 { + Some((proto::ErrorCode::InvalidPacket, "request_id is required")) + } else if !session.client_features.node_settings { + Some(( + proto::ErrorCode::InvalidPacket, + "node settings capability was not negotiated", + )) + } else if request.update && !state.config.gateway.is_node_admin(&session.account_id) { + Some(( + proto::ErrorCode::PermissionDenied, + "node administrator account required", + )) + } else { + None + }; + if let Some((code, message)) = reject { + result.error_code = code as u32; + result.error = message.into(); + return send(stream, seq, result).await; + } + if request.update { + let name = request.display_name.trim(); + let description = request.description.trim(); + if !valid_text(name, 100, false) || name.is_empty() || !valid_text(description, 2000, true) + { + result.error_code = proto::ErrorCode::InvalidPacket as u32; + result.error="name must contain 1–100 characters; description maximum 2000 characters, without control characters".into(); + return send(stream, seq, result).await; + } + let guard = state.access.write().await; + state.storage.save_node_settings(name, description).await?; + *state.node_settings.write().await = (name.to_owned(), description.to_owned()); + let event = NodeSettingsResult { + display_name: name.into(), + description: description.into(), + ..Default::default() + }; + let recipients: Vec<_> = state + .sessions + .read() + .await + .values() + .filter(|session| session.session_id != sid && session.client_features.node_settings) + .map(|session| session.session_id.clone()) + .collect(); + for recipient in recipients { + let _ = state.broadcast.send(BroadcastMsg { + channel_id: None, + exclude_session: None, + target_session_id: Some(recipient), + data: encode_packet(PacketId::NodeSettingsResult, 0, &to_payload(&event)), + }); + } + drop(guard); + } + let current = state.node_settings.read().await; + result.display_name = current.0.clone(); + result.description = current.1.clone(); + drop(current); + send(stream, seq, result).await +} +fn valid_text(value: &str, max: usize, multiline: bool) -> bool { + value.chars().count()<=max && !value.chars().any(|c|(c.is_control() && !(multiline && matches!(c,'\n'|'\t'))) || matches!(c,'\u{061c}'|'\u{200e}'|'\u{200f}'|'\u{202a}'..='\u{202e}'|'\u{2066}'..='\u{2069}')) +} +async fn send( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + seq: &mut u32, + result: NodeSettingsResult, +) -> Result<()> { + io::send_frame( + stream, + PacketId::NodeSettingsResult, + seq, + &to_payload(&result), + ) + .await +} diff --git a/gateway/src/handler/profile.rs b/gateway/src/handler/profile.rs new file mode 100644 index 0000000..6192b3d --- /dev/null +++ b/gateway/src/handler/profile.rs @@ -0,0 +1,164 @@ +//! Account display metadata changes are authorized only by a fresh Central proof. +use crate::{ + domain::{account, session}, + net::{ + io, + state::{BroadcastMsg, State}, + }, + proto::{self, PacketId, ProfileUpdate, ProfileUpdated, encode_packet, to_payload}, +}; +use anyhow::{Context, Result, ensure}; +use prost::Message; +use std::collections::HashMap; +use tokio::io::{AsyncRead, AsyncWrite}; + +pub async fn handle( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + seq: &mut u32, + sid: &str, + payload: &[u8], + state: &State, +) -> Result<()> { + if !state.profile_rate_limiter.try_consume(sid) { + return io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&proto::ErrorPayload { + code: proto::ErrorCode::RateLimited as u32, + message: "profile updates are too frequent".into(), + }), + ) + .await; + } + let result: Result<()> = async { + ensure!(payload.len() <= 8300, "profile proof is too large"); + let update = ProfileUpdate::decode(payload)?; + apply(state, sid, &update.account_proof).await + } + .await; + if let Err(error) = result { + tracing::debug!(%error, "rejected signed profile update"); + io::send_frame( + stream, + PacketId::Error, + seq, + &to_payload(&proto::ErrorPayload { + code: proto::ErrorCode::AuthFailed as u32, + message: "Central profile proof is invalid or belongs to another account".into(), + }), + ) + .await?; + } + Ok(()) +} + +pub async fn apply(state: &State, sid: &str, proof: &[u8]) -> Result<()> { + let _access = state.access.write().await; + let current = session::get(&state.sessions, sid) + .await + .context("missing session")?; + let device: [u8; 32] = hex::decode(¤t.user_id)? + .try_into() + .map_err(|_| anyhow::anyhow!("invalid device key"))?; + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH)? + .as_secs() as i64; + let claims = account::verify( + proof, + &device, + state.config.gateway.central_account.as_ref(), + now, + )?; + ensure!( + claims.account_id == current.account_id, + "profile proof account does not match session" + ); + let nickname = claims + .display_name + .context("signed display name required")?; + let avatar_url = claims.avatar_url.context("signed avatar path required")?; + let mut sessions = state.sessions.write().await; + let newest = sessions + .values() + .filter(|s| s.account_id == current.account_id) + .map(|s| s.profile_issued_at) + .max() + .unwrap_or(0); + if claims.issued_at < newest { + return Ok(()); + } + let mut devices: HashMap)> = HashMap::new(); + for session in sessions + .values_mut() + .filter(|s| s.account_id == current.account_id) + { + let changed = session.nickname != nickname || session.avatar_url != avatar_url; + session.nickname = nickname.clone(); + session.avatar_url = avatar_url.clone(); + session.profile_issued_at = claims.issued_at; + let entry = devices.entry(session.user_id.clone()).or_default(); + entry.0 |= changed; + entry.1.extend( + [session.channel_id.clone(), session.voice_channel_id.clone()] + .into_iter() + .flatten(), + ); + } + drop(sessions); + for (device, (changed, mut channels)) in devices { + if !changed { + continue; + } + channels.sort(); + channels.dedup(); + let event = ProfileUpdated { + user_id: device, + account_id: current.account_id.clone(), + nickname: nickname.clone(), + avatar_url: avatar_url.clone(), + issued_at: claims.issued_at, + }; + for channel in channels { + let _ = state.broadcast.send(BroadcastMsg { + channel_id: Some(channel), + exclude_session: None, + target_session_id: None, + data: encode_packet(PacketId::ProfileUpdated, 0, &to_payload(&event)), + }); + } + } + Ok(()) +} + +/// Recheck current visibility when a queued update is delivered after access changes. +pub async fn may_deliver(state: &State, recipient: &str, payload: &[u8]) -> bool { + let Ok(event) = ProfileUpdated::decode(payload) else { + return false; + }; + let _access = state.access.read().await; + let sessions = state.sessions.read().await; + let Some(recipient) = sessions.get(recipient) else { + return false; + }; + if recipient.user_id == event.user_id && recipient.account_id == event.account_id { + return true; + } + sessions.values().any(|subject| { + subject.user_id == event.user_id + && subject.account_id == event.account_id + && [ + subject.channel_id.as_deref(), + subject.voice_channel_id.as_deref(), + ] + .into_iter() + .flatten() + .any(|channel| { + recipient.channel_id.as_deref() == Some(channel) + || recipient.voice_channel_id.as_deref() == Some(channel) + }) + }) +} + +#[cfg(test)] +mod tests; diff --git a/gateway/src/handler/profile/tests.rs b/gateway/src/handler/profile/tests.rs new file mode 100644 index 0000000..2672b0f --- /dev/null +++ b/gateway/src/handler/profile/tests.rs @@ -0,0 +1,321 @@ +use super::*; +use crate::domain::channels; +use crate::handler::{Ctx, deliver::deliver_frame, dispatch}; +use std::sync::{Arc, atomic::AtomicUsize}; + +async fn fixture() -> ( + State, + String, + String, + String, + tokio::sync::broadcast::Receiver, +) { + let storage = crate::domain::storage::Storage::connect_sqlite(":memory:") + .await + .unwrap(); + let mut config: crate::domain::config::Config = + toml::from_str(include_str!("../../../../dev/config.toml")).unwrap(); + config.gateway.central_account = Some(account::tests::trust()); + let dir = std::env::temp_dir().join(format!("vnox-profile-{}", uuid::Uuid::new_v4())); + let identity = + crate::bootstrap::server_identity::ServerIdentity::load_or_generate(&dir).unwrap(); + std::fs::remove_dir_all(dir).unwrap(); + let (broadcast, _) = tokio::sync::broadcast::channel(128); + let (voice, voice_rx) = tokio::sync::broadcast::channel(32); + let state = State::new( + session::new_store(), + channels::new_store(), + Arc::new(storage), + Arc::new(config), + Arc::new(identity), + broadcast, + crate::admin::metrics::Metrics::new(), + Arc::new(AtomicUsize::new(0)), + Arc::new(AtomicUsize::new(0)), + Some(voice), + ); + let a = session::create(&state.sessions, hex::encode([9; 32]), "Old Name".into()).await; + let b = session::create(&state.sessions, hex::encode([8; 32]), "Peer".into()).await; + let hidden = session::create(&state.sessions, hex::encode([7; 32]), "Hidden".into()).await; + for sid in [&a.session_id, &b.session_id] { + channels::join(&state.channels, "general", sid).await; + state + .sessions + .write() + .await + .get_mut(sid) + .unwrap() + .channel_id = Some("general".into()); + } + let now = now(); + let mut sessions = state.sessions.write().await; + let a = sessions.get_mut(&a.session_id).unwrap(); + a.account_id = "c3ebd30f-6bb4-4c31-9016-bec57e986c93".into(); + a.avatar_url = "/v1/avatars/old".into(); + a.profile_issued_at = now - 30; + let a = a.session_id.clone(); + drop(sessions); + (state, a, b.session_id, hidden.session_id, voice_rx) +} +fn now() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() as i64 +} +fn proof(key: &[u8; 32], issued: i64, name: &str, avatar: &str) -> Vec { + let mut claims = account::tests::claims(key, issued); + claims["display_name"] = serde_json::json!(name); + claims["avatar_url"] = serde_json::json!(avatar); + account::tests::sign(claims) +} +fn packet(msg: &BroadcastMsg) -> PacketId { + PacketId::from_u16( + crate::proto::PacketHeader::from_bytes(msg.data[..12].try_into().unwrap()).packet_id, + ) + .unwrap() +} + +#[tokio::test] +async fn two_clients_receive_signed_profile_before_chat_and_voice_rejoin_without_membership_changes() + { + let (state, a, b, hidden, mut voice_rx) = fixture().await; + let (mut a_socket, mut a_client) = tokio::io::duplex(65536); + let (mut b_socket, mut b_client) = tokio::io::duplex(65536); + let mut a_seq = 0; + let mut b_seq = 0; + crate::handler::channel::join( + &mut a_socket, + &mut a_seq, + &a, + "voice", + &state, + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + for _ in 0..2 { + io::read_packet(&mut a_client).await.unwrap(); + } + assert!(voice_rx.try_recv().unwrap().contains("join")); + channels::join(&state.channels, "voice", &b).await; + state + .sessions + .write() + .await + .get_mut(&b) + .unwrap() + .voice_channel_id = Some("voice".into()); + let before = channels::members(&state.channels, "general").await; + let mut events = state.broadcast.subscribe(); + let fresh = proof(&[9; 32], now() - 1, "New Name", "/v1/avatars/new"); + let mut ctx = Ctx { + stream: &mut a_socket, + seq: &mut a_seq, + state: &state, + }; + dispatch( + &mut ctx, + &a, + PacketId::ProfileUpdate, + &to_payload(&ProfileUpdate { + account_proof: fresh, + }), + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + dispatch( + &mut ctx, + &a, + PacketId::ChatMessage, + &to_payload(&proto::ChatMessage { + message_id: uuid::Uuid::new_v4().to_string(), + channel_id: "general".into(), + content: "fresh profile message".into(), + ..Default::default() + }), + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + assert_eq!(channels::members(&state.channels, "general").await, before); + assert_eq!( + state.sessions.read().await[&a].voice_channel_id.as_deref(), + Some("voice") + ); + assert!( + voice_rx.try_recv().is_err(), + "profile update must not issue/revoke voice credentials" + ); + let mut received = Vec::new(); + while let Ok(event) = events.try_recv() { + assert!(!matches!( + packet(&event), + PacketId::UserJoin | PacketId::UserLeave | PacketId::Disconnect + )); + assert!( + !may_deliver(&state, &hidden, &event.data[12..]).await + || packet(&event) != PacketId::ProfileUpdated + ); + deliver_frame(&mut b_socket, &mut b_seq, &b, &event, &state) + .await + .unwrap(); + received.push(packet(&event)); + } + assert_eq!(received.last(), Some(&PacketId::ChatMessage)); + assert_eq!( + received + .iter() + .filter(|packet| **packet == PacketId::ProfileUpdated) + .count(), + 2 + ); + for _ in 0..2 { + let (header, payload) = io::read_packet(&mut b_client).await.unwrap(); + assert_eq!(header.packet_id, PacketId::ProfileUpdated as u16); + let profile = ProfileUpdated::decode(payload.as_slice()).unwrap(); + assert_eq!(profile.nickname, "New Name"); + assert_eq!(profile.avatar_url, "/v1/avatars/new"); + assert_eq!(profile.user_id, hex::encode([9; 32])); + } + assert_eq!( + io::read_packet(&mut b_client).await.unwrap().0.packet_id, + PacketId::ChatMessage as u16 + ); + crate::handler::channel::leave(&mut a_socket, &mut a_seq, &a, "voice", &state) + .await + .unwrap(); + while events.try_recv().is_ok() {} + apply( + &state, + &a, + &proof(&[9; 32], now(), "Newest Name", "/v1/avatars/newest"), + ) + .await + .unwrap(); + crate::handler::channel::join( + &mut a_socket, + &mut a_seq, + &a, + "voice", + &state, + "127.0.0.1:1".parse().unwrap(), + ) + .await + .unwrap(); + let (_, snapshot) = io::read_packet(&mut a_client).await.unwrap(); + let snapshot = proto::ChannelState::decode(snapshot.as_slice()).unwrap(); + assert_eq!(snapshot.members.len(), 2); + assert_eq!( + snapshot + .members + .iter() + .find(|m| m.user_id == hex::encode([9; 32])) + .unwrap() + .nickname, + "Newest Name" + ); + assert_eq!( + io::read_packet(&mut a_client).await.unwrap().0.packet_id, + PacketId::VoiceSession as u16 + ); + let join = std::iter::from_fn(|| events.try_recv().ok()) + .find(|e| packet(e) == PacketId::UserJoin) + .unwrap(); + let join = proto::UserJoin::decode(&join.data[12..]).unwrap(); + assert_eq!(join.nickname, "Newest Name"); + assert_eq!(join.avatar_url, "/v1/avatars/newest"); + assert_eq!(state.sessions.read().await.len(), 3); + assert_eq!( + state.sessions.read().await[&a].user_id, + hex::encode([9; 32]) + ); +} + +#[tokio::test] +async fn forged_wrong_account_device_expired_and_older_profiles_cannot_downgrade_or_escape_visibility() + { + let (state, a, b, hidden, _voice) = fixture().await; + let mut events = state.broadcast.subscribe(); + let accepted = proof(&[9; 32], now() - 1, "New Name", "/v1/avatars/new"); + apply(&state, &a, &accepted).await.unwrap(); + let event = events.try_recv().unwrap(); + assert!(may_deliver(&state, &b, &event.data[12..]).await); + assert!(!may_deliver(&state, &hidden, &event.data[12..]).await); + state.sessions.write().await.get_mut(&b).unwrap().channel_id = None; + assert!( + !may_deliver(&state, &b, &event.data[12..]).await, + "queued event after access revocation is hidden" + ); + let mut claims = account::tests::claims(&[9; 32], now()); + claims["subject"] = serde_json::json!(uuid::Uuid::new_v4().to_string()); + claims["display_name"] = serde_json::json!("Other"); + claims["avatar_url"] = serde_json::json!("/v1/avatars/other"); + let mut forged = accepted.clone(); + *forged.last_mut().unwrap() = b'!'; + for invalid in [ + account::tests::sign(claims), + proof(&[8; 32], now(), "Other", "/v1/avatars/other"), + proof(&[9; 32], now() - 1000, "Expired", "/v1/avatars/old"), + forged, + ] { + assert!(apply(&state, &a, &invalid).await.is_err()); + } + apply( + &state, + &a, + &proof(&[9; 32], now() - 10, "Old Name", "/v1/avatars/old"), + ) + .await + .unwrap(); + assert_eq!(state.sessions.read().await[&a].nickname, "New Name"); + assert!(events.try_recv().is_err()); +} + +#[tokio::test] +async fn account_devices_share_only_metadata_and_older_other_device_proof_does_not_revert_it() { + let (state, a, _b, _hidden, _voice) = fixture().await; + let second = session::create(&state.sessions, hex::encode([10; 32]), "Old Name".into()).await; + let account_id = state.sessions.read().await[&a].account_id.clone(); + state + .sessions + .write() + .await + .get_mut(&second.session_id) + .unwrap() + .account_id = account_id; + let guild = state + .storage + .create_guild(&second.user_id, "Device-owned guild") + .await + .unwrap(); + apply( + &state, + &a, + &proof(&[9; 32], now() - 1, "New Name", "/v1/avatars/new"), + ) + .await + .unwrap(); + apply( + &state, + &second.session_id, + &proof(&[10; 32], now() - 10, "Old Name", "/v1/avatars/old"), + ) + .await + .unwrap(); + let second = state.sessions.read().await[&second.session_id].clone(); + assert_eq!(second.nickname, "New Name"); + assert_eq!(second.avatar_url, "/v1/avatars/new"); + assert_eq!(second.user_id, hex::encode([10; 32])); + assert_eq!( + state + .storage + .get_guild(&guild) + .await + .unwrap() + .unwrap() + .owner_id, + hex::encode([10; 32]) + ); +} diff --git a/gateway/src/lib.rs b/gateway/src/lib.rs index 7fe0a4a..1dc1bf4 100644 --- a/gateway/src/lib.rs +++ b/gateway/src/lib.rs @@ -30,6 +30,7 @@ impl Drop for ConnGuard { pub async fn run(cfg: Arc, voice_member_tx: Option) -> Result<()> { cfg.gateway.validate_tls()?; + cfg.gateway.validate_admin_accounts()?; if cfg.gateway.tls_enabled == Some(true) { warn!("gateway.tls_enabled is deprecated: TLS 1.3 is always on, remove the option"); } @@ -118,6 +119,9 @@ pub async fn run(cfg: Arc, voice_member_tx: Option( .await; } + let _cleanup_guard = state.access.write().await; let user_id = session::get(&state.sessions, &sid) .await .map(|s| s.user_id.clone()); - if let Some(ref uid) = user_id { + let another_device_session = state + .sessions + .read() + .await + .values() + .any(|other| other.session_id != sid && Some(&other.user_id) == user_id.as_ref()); + if let Some(ref uid) = user_id + && !another_device_session + { state.presences.write().await.insert( uid.clone(), PresenceInfo { @@ -316,6 +329,7 @@ async fn handle( .sessions_count .fetch_sub(1, std::sync::atomic::Ordering::Relaxed); state.rate_limiter.remove(&sid); + state.profile_rate_limiter.remove(&sid); debug!("{addr} cleaned up"); result } diff --git a/gateway/src/net/handshake.rs b/gateway/src/net/handshake.rs index 0adbd9a..b73694b 100644 --- a/gateway/src/net/handshake.rs +++ b/gateway/src/net/handshake.rs @@ -10,11 +10,11 @@ use crate::{ proto::{self, AuthPayload, HelloPayload, PacketId, SessionPayload, to_payload}, }; -/// LNEx protocol version. v3 runs inside TLS 1.3 (see `docs/02-protocol/tls.md`). -pub const LNEX_VERSION: &str = "v3"; +/// LNEx protocol version. v4 requires a Central account inside TLS 1.3 (see `docs/02-protocol/tls.md`). +pub const LNEX_VERSION: &str = "v4"; -const SERVER_HELLO_CONTEXT: &[u8] = b"VNOX-LNEx-v3-server-hello\0"; -const CLIENT_AUTH_CONTEXT: &[u8] = b"VNOX-LNEx-v3-client-auth\0"; +const SERVER_HELLO_CONTEXT: &[u8] = b"VNOX-LNEx-v4-server-hello\0"; +const CLIENT_AUTH_CONTEXT: &[u8] = b"VNOX-LNEx-v4-client-auth\0"; /// Bytes the server identity signs: context, TLS exporter, HELLO without its signature. pub fn server_hello_transcript(binding: &[u8], unsigned_hello: &[u8]) -> Vec { @@ -56,7 +56,7 @@ pub async fn run( lnex_version: LNEX_VERSION.into(), server_pubkey: hex::decode(state.server_identity.pubkey_hex())?, challenge_nonce: challenge.to_vec(), - node_name: state.config.node.name.clone(), + node_name: state.node_settings.read().await.0.clone(), private_mode, server_signature: Vec::new(), }; @@ -84,7 +84,7 @@ pub async fn run( stream, seq, proto::ErrorCode::VersionMismatch, - "unsupported version: LNEx v3 required", + "unsupported version: LNEx v4 required", ) .await?; return Err(anyhow::anyhow!("version mismatch")); @@ -118,6 +118,30 @@ pub async fn run( return Err(anyhow::anyhow!("auth failed")); } + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH)? + .as_secs() as i64; + let account = match crate::domain::account::verify( + &msg.account_proof, + &pubkey, + state.config.gateway.central_account.as_ref(), + now, + ) { + Ok(account) => account, + Err(error) => { + warn!("{addr} Central account admission failed: {error}"); + state.metrics.inc(&state.metrics.auth_failures); + io::send_error( + stream, + seq, + proto::ErrorCode::AuthFailed, + "Central account sign-in required; sign in again in the desktop client", + ) + .await?; + return Err(anyhow::anyhow!("Central account admission failed")); + } + }; + if state .storage .is_banned(&hex::encode(&msg.client_pubkey)) @@ -134,13 +158,35 @@ pub async fn run( .await?; // SESSION - let sess = session::create( + let mut sess = session::create( &state.sessions, hex::encode(&msg.client_pubkey), msg.nickname.clone(), ) .await; + sess.profile_issued_at = account.issued_at; + sess.account_id = account.account_id; + sess.nickname = account.display_name.unwrap_or(sess.nickname); + sess.avatar_url = account + .avatar_url + .unwrap_or_else(|| format!("/v1/avatars/default-v2/{}", sess.account_id)); + { + let mut sessions = state.sessions.write().await; + if let Some(latest) = sessions + .values() + .filter(|s| { + s.account_id == sess.account_id && s.profile_issued_at > sess.profile_issued_at + }) + .max_by_key(|s| s.profile_issued_at) + { + sess.nickname = latest.nickname.clone(); + sess.avatar_url = latest.avatar_url.clone(); + sess.profile_issued_at = latest.profile_issued_at; + } + sessions.insert(sess.session_id.clone(), sess.clone()); + } + let timeout_secs = state.config.gateway.session_timeout.unwrap_or(600); let expires_at = std::time::SystemTime::now() .checked_add(std::time::Duration::from_secs(timeout_secs)) @@ -148,6 +194,11 @@ pub async fn run( .map(|d| d.as_secs() as i64) .unwrap_or(0); let sp = SessionPayload { + live_profiles: true, + node_settings: true, + channel_structure: true, + forums: true, + node_admin: state.config.gateway.is_node_admin(&sess.account_id), session_id: sess.session_id.clone(), token: sess.token.clone(), expires_at, @@ -173,7 +224,9 @@ mod tests { let storage = crate::domain::storage::Storage::connect_sqlite(":memory:") .await .unwrap(); - let config = toml::from_str(include_str!("../../../dev/config.toml")).unwrap(); + let mut config: crate::domain::config::Config = + toml::from_str(include_str!("../../../dev/config.toml")).unwrap(); + config.gateway.central_account = Some(crate::domain::account::tests::trust()); let dir = std::env::temp_dir().join(format!("vnox-hs-test-{}", uuid::Uuid::new_v4())); let identity = crate::bootstrap::server_identity::ServerIdentity::load_or_generate(&dir).unwrap(); @@ -208,6 +261,13 @@ mod tests { nickname: "tester".into(), lnex_version: version.into(), signature: Vec::new(), + account_proof: crate::domain::account::tests::proof( + &key.verifying_key().to_bytes(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() as i64, + ), }; let transcript = client_auth_transcript(client_binding, &payload, &auth.encode_to_vec()); auth.signature = key.sign(&transcript).to_bytes().to_vec(); @@ -250,6 +310,57 @@ mod tests { (task.await.unwrap(), error) } + #[tokio::test] + async fn node_admin_session_capability_uses_verified_central_account_on_each_device() { + for listed in [false, true] { + let mut state = state().await; + Arc::get_mut(&mut state.config) + .unwrap() + .gateway + .admin_account_ids = vec![if listed { + "c3ebd30f-6bb4-4c31-9016-bec57e986c93".into() + } else { + uuid::Uuid::new_v4().to_string() + }]; + let mut devices = Vec::new(); + for _ in 0..2 { + let (mut server, mut peer) = tokio::io::duplex(65536); + let shared = state.clone(); + let task = tokio::spawn(async move { + run( + &mut server, + "127.0.0.1:1".parse().unwrap(), + &shared, + &mut 0, + &[9; 32], + ) + .await + .unwrap() + }); + client_auth(&mut peer, &[9; 32], LNEX_VERSION).await; + let (header, payload) = io::read_packet(&mut peer).await.unwrap(); + assert_eq!(header.packet_id, PacketId::Session as u16); + assert_eq!( + SessionPayload::decode(payload.as_slice()) + .unwrap() + .node_admin, + listed + ); + assert!( + SessionPayload::decode(payload.as_slice()) + .unwrap() + .live_profiles + ); + let admitted = task.await.unwrap(); + assert_eq!(admitted.account_id, "c3ebd30f-6bb4-4c31-9016-bec57e986c93"); + devices.push(admitted.user_id); + } + assert_ne!( + devices[0], devices[1], + "two different devices of the same admitted account" + ); + } + } #[tokio::test] async fn signature_for_the_same_channel_binding_is_accepted() { let (result, _) = handshake(&[9; 32], &[9; 32], LNEX_VERSION).await; @@ -269,7 +380,7 @@ mod tests { let (result, error) = handshake(&[3; 32], &[3; 32], "v2").await; assert!(result.is_err()); assert_eq!(error.code, proto::ErrorCode::VersionMismatch as u32); - assert!(error.message.contains("v3")); + assert!(error.message.contains("v4")); } #[tokio::test] diff --git a/gateway/src/net/io.rs b/gateway/src/net/io.rs index aef6256..9543f12 100644 --- a/gateway/src/net/io.rs +++ b/gateway/src/net/io.rs @@ -1,4 +1,4 @@ -//! LNEx v3 TCP framing. Frames are written directly into the TLS 1.3 stream, so there is +//! LNEx v4 TCP framing. Frames are written directly into the TLS 1.3 stream, so there is //! no per frame encryption: the 12 byte header is followed by the protobuf payload and //! `flags` must be zero on the control plane. use anyhow::Result; diff --git a/gateway/src/net/state.rs b/gateway/src/net/state.rs index 3420cf1..48efdc6 100644 --- a/gateway/src/net/state.rs +++ b/gateway/src/net/state.rs @@ -16,6 +16,7 @@ pub type VoiceMemberTx = tokio::sync::broadcast::Sender; #[derive(Clone)] pub struct State { pub access: Arc>, + pub node_settings: Arc>, pub media: Arc, pub webhooks: Arc, pub sessions: SessionStore, @@ -35,6 +36,7 @@ pub struct State { pub channels_count: Arc, /// Per-session rate limiter (token bucket). pub rate_limiter: Arc, + pub profile_rate_limiter: Arc, /// Gateway → voice-node membership bridge. pub voice_member_tx: Option, /// E2EE key store: user_id -> X25519 public key bytes @@ -58,7 +60,9 @@ impl State { let rate_per_sec = config.gateway.message_rate_per_sec.unwrap_or(5.0); let burst = config.gateway.message_rate_burst.unwrap_or(10); let rate_limiter = Arc::new(RateLimiter::new(rate_per_sec, burst)); + let node_settings = Arc::new(RwLock::new((config.node.name.clone(), String::new()))); Self { + node_settings, access: Arc::default(), media: Arc::default(), webhooks: Arc::default(), @@ -73,6 +77,7 @@ impl State { sessions_count, channels_count, rate_limiter, + profile_rate_limiter: Arc::new(RateLimiter::new(1.0, 4)), voice_member_tx, e2ee_keys: Arc::new(RwLock::new(HashMap::new())), } diff --git a/gateway/src/proto/packet/id.rs b/gateway/src/proto/packet/id.rs index dd613c8..7929f6c 100644 --- a/gateway/src/proto/packet/id.rs +++ b/gateway/src/proto/packet/id.rs @@ -1,7 +1,16 @@ #[repr(u16)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PacketId { + NodeSettingsRequest = 0x0230, + NodeSettingsResult = 0x0231, + ClientFeatures = 0x0232, + ForumRequest = 0x0240, + ForumResult = 0x0241, + ForumChanged = 0x0242, WebhookErrorResult = 0x0206, + AttachmentUpload = 0x0220, + AttachmentFetch = 0x0221, + AttachmentResult = 0x0222, MediaControl = 0x0180, MediaEvent = 0x0181, WebhookCreate = 0x0200, @@ -29,6 +38,8 @@ pub enum PacketId { ChannelEdit = 0x0036, UserJoin = 0x0040, UserLeave = 0x0041, + ProfileUpdate = 0x0042, + ProfileUpdated = 0x0043, PermissionCheck = 0x0050, PermissionDeny = 0x0051, DmStart = 0x0060, @@ -85,6 +96,15 @@ pub enum PacketId { impl PacketId { pub fn from_u16(v: u16) -> Option { match v { + 0x0230 => Some(Self::NodeSettingsRequest), + 0x0231 => Some(Self::NodeSettingsResult), + 0x0232 => Some(Self::ClientFeatures), + 0x0240 => Some(Self::ForumRequest), + 0x0241 => Some(Self::ForumResult), + 0x0242 => Some(Self::ForumChanged), + 0x0220 => Some(Self::AttachmentUpload), + 0x0221 => Some(Self::AttachmentFetch), + 0x0222 => Some(Self::AttachmentResult), 0x0206 => Some(Self::WebhookErrorResult), 0x0180 => Some(Self::MediaControl), 0x0181 => Some(Self::MediaEvent), @@ -113,6 +133,8 @@ impl PacketId { 0x0036 => Some(Self::ChannelEdit), 0x0040 => Some(Self::UserJoin), 0x0041 => Some(Self::UserLeave), + 0x0042 => Some(Self::ProfileUpdate), + 0x0043 => Some(Self::ProfileUpdated), 0x0050 => Some(Self::PermissionCheck), 0x0051 => Some(Self::PermissionDeny), 0x0060 => Some(Self::DmStart), diff --git a/gateway/src/tls.rs b/gateway/src/tls.rs index 66e5a36..772cef6 100644 --- a/gateway/src/tls.rs +++ b/gateway/src/tls.rs @@ -1,6 +1,6 @@ -//! TLS 1.3 for the TCP control plane (LNEx v3). See `docs/02-protocol/tls.md`. +//! TLS 1.3 for the TCP control plane (LNEx v4). See `docs/02-protocol/tls.md`. //! -//! * TLS 1.3 only, ALPN `vnox-lnex/3`, no client certificates, no plaintext fallback. +//! * TLS 1.3 only, ALPN `vnox-lnex/4`, no client certificates, no plaintext fallback. //! * Certificate: user supplied (PEM chain + key) or auto generated and stored in the data //! directory. The auto generated certificate is signed with the server identity Ed25519 //! key, so its SPKI fingerprint is a pure function of the identity. @@ -18,13 +18,13 @@ use crate::bootstrap::server_identity::ServerIdentity; use crate::domain::config::GatewayConfig; /// Application protocol negotiated through ALPN. A connection without it is dropped. -pub const ALPN: &[u8] = b"vnox-lnex/3"; +pub const ALPN: &[u8] = b"vnox-lnex/4"; /// TLS exporter label (RFC 5705 / RFC 8446 section 7.5) used for the channel binding. -pub const EXPORTER_LABEL: &[u8] = b"EXPORTER-VNOX-LNEx-v3-channel-binding"; +pub const EXPORTER_LABEL: &[u8] = b"EXPORTER-VNOX-LNEx-v4-channel-binding"; /// Size of the exported channel binding value. pub const BINDING_LEN: usize = 32; /// Message shown to peers that do not speak TLS 1.3 (old LNEx v2 clients, plaintext probes). -pub const LEGACY_MESSAGE: &str = "TLS 1.3 required (LNEx v3): update your client"; +pub const LEGACY_MESSAGE: &str = "TLS 1.3 required (LNEx v4): update your client"; /// Certificate chain, private key and the fingerprint of the leaf public key. pub struct TlsMaterial { @@ -246,7 +246,7 @@ pub fn channel_binding(connection: &rustls::ServerConnection) -> Result<[u8; BIN ); anyhow::ensure!( connection.alpn_protocol() == Some(ALPN), - "ALPN vnox-lnex/3 required" + "ALPN vnox-lnex/4 required" ); connection .export_keying_material([0u8; BINDING_LEN], EXPORTER_LABEL, None) diff --git a/gateway/tests/tls_listener.rs b/gateway/tests/tls_listener.rs index a0e4ec6..3387e54 100644 --- a/gateway/tests/tls_listener.rs +++ b/gateway/tests/tls_listener.rs @@ -1,5 +1,5 @@ //! Black box tests of the gateway TCP listener: TLS 1.3 only, ALPN, legacy rejection, -//! certificate fingerprint and the channel bound LNEx v3 handshake. +//! certificate fingerprint and the channel bound LNEx v4 handshake. use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -46,6 +46,9 @@ address = "127.0.0.1" [gateway] bind = "127.0.0.1:{port}" admin_bind = "127.0.0.1:0" +[gateway.central_account] +issuer = "8a1495ae-7174-4d22-94a5-080f61aa1691" +public_key = "{issuer_key}" [voice] bind = "127.0.0.1:0" [storage] @@ -55,7 +58,8 @@ sqlite_path = "{dir}/t.db" [server] mode = "private" "#, - dir = directory.display() + dir = directory.display(), + issuer_key = hex::encode(SigningKey::from_bytes(&[73; 32]).verifying_key().to_bytes()) ); let cfg: vnox_gateway::domain::config::Config = toml::from_str(&config).unwrap(); let task = tokio::spawn(async move { @@ -215,7 +219,7 @@ async fn client_without_alpn_is_rejected_before_any_lnex_byte() { let read = tokio::time::timeout(Duration::from_secs(3), tls.read(&mut byte)).await; assert!( !matches!(read, Ok(Ok(n)) if n > 0), - "no LNEx data may be sent without ALPN vnox-lnex/3" + "no LNEx data may be sent without ALPN vnox-lnex/4" ); } } @@ -255,12 +259,29 @@ async fn silent_v2_style_client_gets_explicit_version_error() { assert_eq!(id, PacketId::Error as u16); let error = ErrorPayload::decode(payload.as_slice()).unwrap(); assert_eq!(error.code, ErrorCode::VersionMismatch as u32); - assert!(error.message.contains("LNEx v3")); + assert!(error.message.contains("LNEx v4")); +} + +fn account_proof(public_key: &[u8; 32]) -> Vec { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs() as i64; + let claims = serde_json::to_vec(&serde_json::json!({ + "version":1,"issuer":"8a1495ae-7174-4d22-94a5-080f61aa1691", + "subject":"c3ebd30f-6bb4-4c31-9016-bec57e986c93","public_key":hex::encode(public_key), + "issued_at":now,"expires_at":now+600,"audience":"vnox-gateway" + })) + .unwrap(); + let mut signed = b"VNOX-CENTRAL-ACCOUNT-v1\0".to_vec(); + signed.extend_from_slice(&claims); + serde_json::to_vec(&serde_json::json!({"claims":hex::encode(claims),"signature":hex::encode(SigningKey::from_bytes(&[73;32]).sign(&signed).to_bytes())})).unwrap() } async fn authenticate( tls: &mut tokio_rustls::client::TlsStream, signing_binding: Option<[u8; 32]>, + include_account: bool, ) -> (u16, Vec) { let binding = tls .get_ref() @@ -289,6 +310,11 @@ async fn authenticate( nickname: "tls-test".into(), lnex_version: LNEX_VERSION.into(), signature: Vec::new(), + account_proof: if include_account { + account_proof(&key.verifying_key().to_bytes()) + } else { + Vec::new() + }, }; let signed_with = signing_binding.unwrap_or(binding); let transcript = client_auth_transcript(&signed_with, &payload, &auth.encode_to_vec()); @@ -305,7 +331,7 @@ async fn full_handshake_over_tls_creates_a_session() { let server = start().await; let (config, _) = client_config(&[&rustls::version::TLS13], true); let mut tls = connect_tls(&server, config).await.unwrap(); - let (id, _) = authenticate(&mut tls, None).await; + let (id, _) = authenticate(&mut tls, None, true).await; assert_eq!(id, PacketId::Session as u16); } @@ -314,8 +340,28 @@ async fn auth_signed_for_another_tls_connection_is_rejected() { let server = start().await; let (config, _) = client_config(&[&rustls::version::TLS13], true); let mut tls = connect_tls(&server, config).await.unwrap(); - let (id, payload) = authenticate(&mut tls, Some([0x42; 32])).await; + let (id, payload) = authenticate(&mut tls, Some([0x42; 32]), true).await; assert_eq!(id, PacketId::Error as u16); let error = ErrorPayload::decode(payload.as_slice()).unwrap(); assert_eq!(error.code, ErrorCode::AuthFailed as u32); } + +#[tokio::test] +async fn valid_device_signature_without_a_central_account_is_rejected() { + let server = start().await; + let (config, _) = client_config(&[&rustls::version::TLS13], true); + let mut tls = connect_tls(&server, config).await.unwrap(); + let (id, payload) = authenticate(&mut tls, None, false).await; + assert_eq!(id, PacketId::Error as u16); + let error = ErrorPayload::decode(payload.as_slice()).unwrap(); + assert_eq!(error.code, ErrorCode::AuthFailed as u32); + assert!(error.message.contains("Central account")); +} + +#[tokio::test] +async fn old_v3_alpn_is_rejected_before_any_lnex_byte() { + let server = start().await; + let (mut config, _) = client_config(&[&rustls::version::TLS13], true); + config.alpn_protocols = vec![b"vnox-lnex/3".to_vec()]; + assert!(connect_tls(&server, config).await.is_err()); +} diff --git a/hub/AUTH.md b/hub/AUTH.md index ad76911..92f8787 100644 --- a/hub/AUTH.md +++ b/hub/AUTH.md @@ -55,7 +55,7 @@ Per address 1 per 60 s and 5 per hour; per source (`X-Forwarded-For`, as GitHub Addresses are trimmed and lowercased; only ASCII, a local part of `[a-z0-9._%+-]` without leading/trailing/double dots, at most 64 characters, a multi-label hostname whose last label is alphabetic, at most 254 characters in total. `+tag` is not stripped and Gmail dots are not folded. Unusual legal addresses are rejected on purpose. -The identity is `external_identities(provider='email', subject=)`, reusing the existing table so one address maps to one account, and it can never collide with `github.com` subjects. A matching GitHub account or email is never merged automatically. A first successful verification creates an account with a generated `u_` handle and the neutral display name `Vnox user`; the local part of the address is never used. Choosing a handle and display name after registration is not implemented yet (an own avatar can be uploaded, see below) (see `design/accounts-and-avatars.md`). The address is kept in plain normalized form in `external_identities` because later features (security notices, adding a second sign-in method) need it; it is used for nothing except sending codes today. +The identity is `external_identities(provider='email', subject=)`, reusing the existing table so one address maps to one account, and it can never collide with `github.com` subjects. A matching GitHub account or email is never merged automatically. A first successful verification creates an account with a generated `u_` handle and the neutral display name `Vnox user`; the local part of the address is never used. The profile API below selects a handle and display name after registration; browser onboarding is still separate work. An own avatar can be uploaded, see below. The address is kept in plain normalized form in `external_identities` because later features (security notices, adding a second sign-in method) need it; it is used for nothing except sending codes today. ### Mail and logging @@ -104,3 +104,63 @@ The provider avatar URL stays on `accounts.avatar_url` and is the fallback after - Avatars are public to anyone who knows the id; ids are not secret but are not listed anywhere. Identical images from two accounts share an id. - Deleted photos can remain in database backups. The project owner decided that backups keep deleted data for up to 30 days; this needs backup rotation to be implemented and is not enforced by the Hub. - Decoding uses the `image` crate with only the `png`, `jpeg` and `webp` features. `cargo audit` reported no advisories when this was added. Decoder bugs in these crates are the main residual risk; decoding is bounded in size, memory and concurrency. + +## Account profile and registration completion + +`PUT /v1/account/profile` accepts JSON with exactly `handle` and `display_name`. +It requires the session cookie, exact Origin and X-CSRF-Token, like avatar changes. +The body limit is 1 KiB. Successful responses are `200 {"user": ...}`; session and +email verification responses also include `user.profile_complete`. Existing and +new accounts start with `profile_complete: false`; generated `u_` handles +are placeholders, never selected automatically from a provider identity. + +Handles are trimmed, ASCII-lowercased, 3 to 24 characters, start with a letter, +and contain only ASCII letters, digits and underscore. Names starting with `u_` +are reserved for generated accounts; `admin`, `administrator`, `root`, `system`, +`support`, `vnox`, `central`, `moderator`, `everyone` and `here` are reserved. +A unique database index enforces case-insensitive ownership, including concurrent +claims. Once selected, a handle cannot change; submitting the same normalized +handle permits display-name changes and makes retries safe. Display names are +trimmed, 1 to 64 Unicode scalar values, with control characters and Unicode +bidirectional formatting controls rejected. Names are plain text, not markup. + +Errors: `400 invalid_request`, `422 invalid_handle` / `invalid_display_name`, +`409 handle_taken` / `handle_locked`, standard session/Origin/CSRF errors, +`413` for bodies over the limit, `429 try_later`, `503 try_later`. Malformed JSON, +unknown fields and missing fields are rejected without changing the account. +`/v1/info` advertises `capabilities.account_profile: "enabled"` independently of +which sign-in providers are configured. Profile writes are limited to 10 attempts per account and 30 per source per hour, +with bounded in-memory counters (reset at restart). Authentication precedes body +reading; errors and success responses are no-store. Provider sign-in preserves +the selected profile. User ID and provider bindings never change. + +## Desktop browser approval (LNEx v4) + +Desktop enrollment uses the existing email/GitHub browser session and explicit +approval at `/desktop`. Profile completion is not required. The public pairing +code and full local Ed25519 public key appear in the browser; private keys and +bearer credentials never do. The full protocol and claim format are specified in +[central-account.md](../docs/02-protocol/central-account.md). + +- `POST /v1/auth/desktop/start`: `{public_key}` returns a private `device_secret`, + public `user_code`, `verification_uri`, `expires_at`, and five-second `interval`. +- `GET /v1/auth/desktop/request?code=...`: public key, expiry, approval state. +- `POST /v1/auth/desktop/approve`: `{code}`, authenticated session, exact Origin, + session CSRF header; requires explicit user action. +- `POST /v1/auth/desktop/poll`: `{device_secret}` returns 202 while pending; + approved requests are consumed once and return account proof, refresh secret, + account details, and proof expiry. +- `POST /v1/auth/desktop/cancel`: `{device_secret}` cancels either pending request + or consumed enrollment, covering cancellation and lost-response races. +- `POST /v1/auth/desktop/refresh`: `{refresh_secret}` issues a fresh proof only + for an unexpired, unrevoked enrollment. +- `POST /v1/auth/desktop/revoke`: `{refresh_secret}`, idempotently revokes it. + +Requests expire in ten minutes; enrollments expire in thirty days. Hub stores +only hashes of pairing, device, and refresh secrets. Proofs last at most ten +minutes and bind the Central account UUID to the local identity key. All LNEx v4 +gateways require a proof verified against explicit issuer UUID/public-key trust; +there is no anonymous or private-server exception. Revocation prevents new proof +issuance; issued proofs and existing gateway sessions retain their documented +lifetimes. Desktop logout disables local admission immediately and retains a +private revocation tombstone for retry if Central is unavailable. diff --git a/hub/README.md b/hub/README.md index 1c88a1a..4b7e2c7 100644 --- a/hub/README.md +++ b/hub/README.md @@ -12,6 +12,7 @@ Community messages and voice never pass through this process. - `/v1/info`: public issuer ID/key and honest feature availability. - `/`: deployment status page; `/assets/vnox-mark.svg`: approved V connection mark. - Durable SQLite schema and issuer identity with a private `0600` key file. +- Account profile: `PUT /v1/account/profile` selects a unique permanent handle, edits the display name and completes registration (`user.profile_complete`; see `AUTH.md`). - Own avatar for signed-in web sessions: `POST`/`DELETE /v1/account/avatar`, public `GET /v1/avatars/` (re-encoded 256x256 PNG in SQLite, see `AUTH.md`). - TLS-only SMTP transport and HTML/plain-text transactional email templates. @@ -80,3 +81,6 @@ Hub. An invalid/malformed issuer file likewise fails closed. Protect the entire data directory, not only the JSON file. Additional account/device revocation, registry authorization, mailbox lifecycle and storage policies require their own implementation and security review before those capabilities are enabled. + +Community server listings are unverified browser submissions. Public reads and authenticated +owner-managed publication are documented in [the HTTP contract](../docs/05-features/hub-server-directory.md). diff --git a/hub/assets/nicknames/ATTRIBUTION.md b/hub/assets/nicknames/ATTRIBUTION.md new file mode 100644 index 0000000..815e1c8 --- /dev/null +++ b/hub/assets/nicknames/ATTRIBUTION.md @@ -0,0 +1,9 @@ +# Friendly Russian nickname words + +Data source: [OpenRussian / Badestrand Russian Dictionary](https://github.com/Badestrand/russian-dictionary), maintained by OpenRussian and its contributors. + +Pinned revision: `50e210c4803237779cb562bc1abcea529066031c`; downloaded `nouns.csv` from https://raw.githubusercontent.com/Badestrand/russian-dictionary/50e210c4803237779cb562bc1abcea529066031c/nouns.csv . + +The source and this derived JSON word selection are licensed under **Creative Commons Attribution-ShareAlike 4.0 International**. The full license is adjacent in LICENSE-CC-BY-SA-4.0.txt. This data license is separate from the application's code license. No endorsement is implied. + +Modifications by VNOX contributors (2026): manually selected friendly nature, animal, food and object nouns; retained only upstream bare dictionary forms; sorted and deduplicated; converted TSV data to a JSON array. 385 words. No people's names, email identifiers, insults or political/military labels are intentionally included. Words are display names, not unique handles. diff --git a/hub/assets/nicknames/LICENSE-CC-BY-SA-4.0.txt b/hub/assets/nicknames/LICENSE-CC-BY-SA-4.0.txt new file mode 100644 index 0000000..3b7b82d --- /dev/null +++ b/hub/assets/nicknames/LICENSE-CC-BY-SA-4.0.txt @@ -0,0 +1,427 @@ +Attribution-ShareAlike 4.0 International + +======================================================================= + +Creative Commons Corporation ("Creative Commons") is not a law firm and +does not provide legal services or legal advice. Distribution of +Creative Commons public licenses does not create a lawyer-client or +other relationship. Creative Commons makes its licenses and related +information available on an "as-is" basis. Creative Commons gives no +warranties regarding its licenses, any material licensed under their +terms and conditions, or any related information. Creative Commons +disclaims all liability for damages resulting from their use to the +fullest extent possible. + +Using Creative Commons Public Licenses + +Creative Commons public licenses provide a standard set of terms and +conditions that creators and other rights holders may use to share +original works of authorship and other material subject to copyright +and certain other rights specified in the public license below. The +following considerations are for informational purposes only, are not +exhaustive, and do not form part of our licenses. + + Considerations for licensors: Our public licenses are + intended for use by those authorized to give the public + permission to use material in ways otherwise restricted by + copyright and certain other rights. Our licenses are + irrevocable. Licensors should read and understand the terms + and conditions of the license they choose before applying it. + Licensors should also secure all rights necessary before + applying our licenses so that the public can reuse the + material as expected. Licensors should clearly mark any + material not subject to the license. This includes other CC- + licensed material, or material used under an exception or + limitation to copyright. More considerations for licensors: + wiki.creativecommons.org/Considerations_for_licensors + + Considerations for the public: By using one of our public + licenses, a licensor grants the public permission to use the + licensed material under specified terms and conditions. If + the licensor's permission is not necessary for any reason--for + example, because of any applicable exception or limitation to + copyright--then that use is not regulated by the license. Our + licenses grant only permissions under copyright and certain + other rights that a licensor has authority to grant. Use of + the licensed material may still be restricted for other + reasons, including because others have copyright or other + rights in the material. A licensor may make special requests, + such as asking that all changes be marked or described. + Although not required by our licenses, you are encouraged to + respect those requests where reasonable. More_considerations + for the public: + wiki.creativecommons.org/Considerations_for_licensees + +======================================================================= + +Creative Commons Attribution-ShareAlike 4.0 International Public +License + +By exercising the Licensed Rights (defined below), You accept and agree +to be bound by the terms and conditions of this Creative Commons +Attribution-ShareAlike 4.0 International Public License ("Public +License"). To the extent this Public License may be interpreted as a +contract, You are granted the Licensed Rights in consideration of Your +acceptance of these terms and conditions, and the Licensor grants You +such rights in consideration of benefits the Licensor receives from +making the Licensed Material available under these terms and +conditions. + + +Section 1 -- Definitions. + + a. Adapted Material means material subject to Copyright and Similar + Rights that is derived from or based upon the Licensed Material + and in which the Licensed Material is translated, altered, + arranged, transformed, or otherwise modified in a manner requiring + permission under the Copyright and Similar Rights held by the + Licensor. For purposes of this Public License, where the Licensed + Material is a musical work, performance, or sound recording, + Adapted Material is always produced where the Licensed Material is + synched in timed relation with a moving image. + + b. Adapter's License means the license You apply to Your Copyright + and Similar Rights in Your contributions to Adapted Material in + accordance with the terms and conditions of this Public License. + + c. BY-SA Compatible License means a license listed at + creativecommons.org/compatiblelicenses, approved by Creative + Commons as essentially the equivalent of this Public License. + + d. Copyright and Similar Rights means copyright and/or similar rights + closely related to copyright including, without limitation, + performance, broadcast, sound recording, and Sui Generis Database + Rights, without regard to how the rights are labeled or + categorized. For purposes of this Public License, the rights + specified in Section 2(b)(1)-(2) are not Copyright and Similar + Rights. + + e. Effective Technological Measures means those measures that, in the + absence of proper authority, may not be circumvented under laws + fulfilling obligations under Article 11 of the WIPO Copyright + Treaty adopted on December 20, 1996, and/or similar international + agreements. + + f. Exceptions and Limitations means fair use, fair dealing, and/or + any other exception or limitation to Copyright and Similar Rights + that applies to Your use of the Licensed Material. + + g. License Elements means the license attributes listed in the name + of a Creative Commons Public License. The License Elements of this + Public License are Attribution and ShareAlike. + + h. Licensed Material means the artistic or literary work, database, + or other material to which the Licensor applied this Public + License. + + i. Licensed Rights means the rights granted to You subject to the + terms and conditions of this Public License, which are limited to + all Copyright and Similar Rights that apply to Your use of the + Licensed Material and that the Licensor has authority to license. + + j. Licensor means the individual(s) or entity(ies) granting rights + under this Public License. + + k. Share means to provide material to the public by any means or + process that requires permission under the Licensed Rights, such + as reproduction, public display, public performance, distribution, + dissemination, communication, or importation, and to make material + available to the public including in ways that members of the + public may access the material from a place and at a time + individually chosen by them. + + l. Sui Generis Database Rights means rights other than copyright + resulting from Directive 96/9/EC of the European Parliament and of + the Council of 11 March 1996 on the legal protection of databases, + as amended and/or succeeded, as well as other essentially + equivalent rights anywhere in the world. + + m. You means the individual or entity exercising the Licensed Rights + under this Public License. Your has a corresponding meaning. + + +Section 2 -- Scope. + + a. License grant. + + 1. Subject to the terms and conditions of this Public License, + the Licensor hereby grants You a worldwide, royalty-free, + non-sublicensable, non-exclusive, irrevocable license to + exercise the Licensed Rights in the Licensed Material to: + + a. reproduce and Share the Licensed Material, in whole or + in part; and + + b. produce, reproduce, and Share Adapted Material. + + 2. Exceptions and Limitations. For the avoidance of doubt, where + Exceptions and Limitations apply to Your use, this Public + License does not apply, and You do not need to comply with + its terms and conditions. + + 3. Term. The term of this Public License is specified in Section + 6(a). + + 4. Media and formats; technical modifications allowed. The + Licensor authorizes You to exercise the Licensed Rights in + all media and formats whether now known or hereafter created, + and to make technical modifications necessary to do so. The + Licensor waives and/or agrees not to assert any right or + authority to forbid You from making technical modifications + necessary to exercise the Licensed Rights, including + technical modifications necessary to circumvent Effective + Technological Measures. For purposes of this Public License, + simply making modifications authorized by this Section 2(a) + (4) never produces Adapted Material. + + 5. Downstream recipients. + + a. Offer from the Licensor -- Licensed Material. Every + recipient of the Licensed Material automatically + receives an offer from the Licensor to exercise the + Licensed Rights under the terms and conditions of this + Public License. + + b. Additional offer from the Licensor -- Adapted Material. + Every recipient of Adapted Material from You + automatically receives an offer from the Licensor to + exercise the Licensed Rights in the Adapted Material + under the conditions of the Adapter's License You apply. + + c. No downstream restrictions. You may not offer or impose + any additional or different terms or conditions on, or + apply any Effective Technological Measures to, the + Licensed Material if doing so restricts exercise of the + Licensed Rights by any recipient of the Licensed + Material. + + 6. No endorsement. Nothing in this Public License constitutes or + may be construed as permission to assert or imply that You + are, or that Your use of the Licensed Material is, connected + with, or sponsored, endorsed, or granted official status by, + the Licensor or others designated to receive attribution as + provided in Section 3(a)(1)(A)(i). + + b. Other rights. + + 1. Moral rights, such as the right of integrity, are not + licensed under this Public License, nor are publicity, + privacy, and/or other similar personality rights; however, to + the extent possible, the Licensor waives and/or agrees not to + assert any such rights held by the Licensor to the limited + extent necessary to allow You to exercise the Licensed + Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this + Public License. + + 3. To the extent possible, the Licensor waives any right to + collect royalties from You for the exercise of the Licensed + Rights, whether directly or through a collecting society + under any voluntary or waivable statutory or compulsory + licensing scheme. In all other cases the Licensor expressly + reserves any right to collect such royalties. + + +Section 3 -- License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the +following conditions. + + a. Attribution. + + 1. If You Share the Licensed Material (including in modified + form), You must: + + a. retain the following if it is supplied by the Licensor + with the Licensed Material: + + i. identification of the creator(s) of the Licensed + Material and any others designated to receive + attribution, in any reasonable manner requested by + the Licensor (including by pseudonym if + designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of + warranties; + + v. a URI or hyperlink to the Licensed Material to the + extent reasonably practicable; + + b. indicate if You modified the Licensed Material and + retain an indication of any previous modifications; and + + c. indicate the Licensed Material is licensed under this + Public License, and include the text of, or the URI or + hyperlink to, this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any + reasonable manner based on the medium, means, and context in + which You Share the Licensed Material. For example, it may be + reasonable to satisfy the conditions by providing a URI or + hyperlink to a resource that includes the required + information. + + 3. If requested by the Licensor, You must remove any of the + information required by Section 3(a)(1)(A) to the extent + reasonably practicable. + + b. ShareAlike. + + In addition to the conditions in Section 3(a), if You Share + Adapted Material You produce, the following conditions also apply. + + 1. The Adapter's License You apply must be a Creative Commons + license with the same License Elements, this version or + later, or a BY-SA Compatible License. + + 2. You must include the text of, or the URI or hyperlink to, the + Adapter's License You apply. You may satisfy this condition + in any reasonable manner based on the medium, means, and + context in which You Share Adapted Material. + + 3. You may not offer or impose any additional or different terms + or conditions on, or apply any Effective Technological + Measures to, Adapted Material that restrict exercise of the + rights granted under the Adapter's License You apply. + + +Section 4 -- Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that +apply to Your use of the Licensed Material: + + a. for the avoidance of doubt, Section 2(a)(1) grants You the right + to extract, reuse, reproduce, and Share all or a substantial + portion of the contents of the database; + + b. if You include all or a substantial portion of the database + contents in a database in which You have Sui Generis Database + Rights, then the database in which You have Sui Generis Database + Rights (but not its individual contents) is Adapted Material, + + including for purposes of Section 3(b); and + c. You must comply with the conditions in Section 3(a) if You Share + all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not +replace Your obligations under this Public License where the Licensed +Rights include other Copyright and Similar Rights. + + +Section 5 -- Disclaimer of Warranties and Limitation of Liability. + + a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE + EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS + AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF + ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, + IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, + WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, + ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT + KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT + ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. + + b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE + TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, + NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, + INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, + COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR + USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN + ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR + DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR + IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. + + c. The disclaimer of warranties and limitation of liability provided + above shall be interpreted in a manner that, to the extent + possible, most closely approximates an absolute disclaimer and + waiver of all liability. + + +Section 6 -- Term and Termination. + + a. This Public License applies for the term of the Copyright and + Similar Rights licensed here. However, if You fail to comply with + this Public License, then Your rights under this Public License + terminate automatically. + + b. Where Your right to use the Licensed Material has terminated under + Section 6(a), it reinstates: + + 1. automatically as of the date the violation is cured, provided + it is cured within 30 days of Your discovery of the + violation; or + + 2. upon express reinstatement by the Licensor. + + For the avoidance of doubt, this Section 6(b) does not affect any + right the Licensor may have to seek remedies for Your violations + of this Public License. + + c. For the avoidance of doubt, the Licensor may also offer the + Licensed Material under separate terms or conditions or stop + distributing the Licensed Material at any time; however, doing so + will not terminate this Public License. + + d. Sections 1, 5, 6, 7, and 8 survive termination of this Public + License. + + +Section 7 -- Other Terms and Conditions. + + a. The Licensor shall not be bound by any additional or different + terms or conditions communicated by You unless expressly agreed. + + b. Any arrangements, understandings, or agreements regarding the + Licensed Material not stated herein are separate from and + independent of the terms and conditions of this Public License. + + +Section 8 -- Interpretation. + + a. For the avoidance of doubt, this Public License does not, and + shall not be interpreted to, reduce, limit, restrict, or impose + conditions on any use of the Licensed Material that could lawfully + be made without permission under this Public License. + + b. To the extent possible, if any provision of this Public License is + deemed unenforceable, it shall be automatically reformed to the + minimum extent necessary to make it enforceable. If the provision + cannot be reformed, it shall be severed from this Public License + without affecting the enforceability of the remaining terms and + conditions. + + c. No term or condition of this Public License will be waived and no + failure to comply consented to unless expressly agreed to by the + Licensor. + + d. Nothing in this Public License constitutes or may be interpreted + as a limitation upon, or waiver of, any privileges and immunities + that apply to the Licensor or You, including from the legal + processes of any jurisdiction or authority. + + +======================================================================= + +Creative Commons is not a party to its public +licenses. Notwithstanding, Creative Commons may elect to apply one of +its public licenses to material it publishes and in those instances +will be considered the “Licensor.” The text of the Creative Commons +public licenses is dedicated to the public domain under the CC0 Public +Domain Dedication. Except for the limited purpose of indicating that +material is shared under a Creative Commons public license or as +otherwise permitted by the Creative Commons policies published at +creativecommons.org/policies, Creative Commons does not authorize the +use of the trademark "Creative Commons" or any other trademark or logo +of Creative Commons without its prior written consent including, +without limitation, in connection with any unauthorized modifications +to any of its public licenses or any other arrangements, +understandings, or agreements concerning use of licensed material. For +the avoidance of doubt, this paragraph does not form part of the +public licenses. + +Creative Commons may be contacted at creativecommons.org. diff --git a/hub/assets/nicknames/friendly-ru.json b/hub/assets/nicknames/friendly-ru.json new file mode 100644 index 0000000..f117923 --- /dev/null +++ b/hub/assets/nicknames/friendly-ru.json @@ -0,0 +1,387 @@ +[ + "абрикос", + "аист", + "айва", + "айсберг", + "акация", + "акварель", + "аккорд", + "акробат", + "альбатрос", + "альбом", + "ананас", + "анемон", + "антилопа", + "апельсин", + "арбуз", + "арка", + "арлекин", + "аромат", + "астра", + "атлас", + "афалина", + "бабочка", + "багет", + "бамбук", + "банан", + "бантик", + "барбарис", + "барс", + "барсук", + "бархат", + "бегония", + "белка", + "бергамот", + "берёза", + "бинокль", + "бисквит", + "блик", + "бобр", + "бокал", + "боровик", + "брусника", + "букет", + "булочка", + "бурундук", + "бусина", + "василёк", + "вафля", + "веер", + "велосипед", + "верба", + "верблюд", + "вереск", + "весна", + "ветер", + "ветка", + "веточка", + "вишня", + "волна", + "волчок", + "воробей", + "воск", + "восход", + "выдра", + "вьюнок", + "газель", + "галактика", + "гамак", + "гвоздика", + "гейзер", + "георгин", + "гепард", + "гиацинт", + "глобус", + "голубика", + "голубь", + "горизонт", + "горлица", + "горошек", + "гортензия", + "гранат", + "графит", + "гребешок", + "грейпфрут", + "гриб", + "груша", + "гусеница", + "гусь", + "дельфин", + "джем", + "дождик", + "дождь", + "долька", + "домик", + "дракон", + "дрозд", + "дуб", + "дудочка", + "дыня", + "ежевика", + "ель", + "енот", + "жасмин", + "жемчуг", + "жираф", + "жук", + "журавль", + "звезда", + "земляника", + "зефир", + "зима", + "зимородок", + "зонтик", + "зубр", + "ива", + "игрушка", + "изумруд", + "имбирь", + "иней", + "инжир", + "ирис", + "искра", + "какао", + "кактус", + "калейдоскоп", + "калина", + "камелия", + "камень", + "камыш", + "канарейка", + "капель", + "карамель", + "карусель", + "каштан", + "кедр", + "кекс", + "кенгуру", + "кипарис", + "кисточка", + "кит", + "клавиша", + "клевер", + "клубника", + "клюква", + "клён", + "колибри", + "колокольчик", + "колос", + "комета", + "конверт", + "конфета", + "коралл", + "корица", + "космос", + "кот", + "котёнок", + "крекер", + "крендель", + "кристалл", + "крокус", + "кролик", + "крыжовник", + "кувшинка", + "кузнечик", + "купол", + "куропатка", + "лабрадор", + "лаванда", + "лампа", + "ландыш", + "ласточка", + "лебедь", + "лев", + "леденец", + "лемур", + "лепесток", + "лес", + "лесник", + "лето", + "лилия", + "лимон", + "липа", + "лис", + "лиса", + "лисичка", + "лист", + "листок", + "лодка", + "лоза", + "лосось", + "лотос", + "луг", + "луч", + "лучик", + "львёнок", + "лютик", + "магнолия", + "мак", + "малина", + "манго", + "мандарин", + "маргаритка", + "мармелад", + "марципан", + "маяк", + "медвежонок", + "мел", + "мелодия", + "метеор", + "мимоза", + "миндаль", + "мишка", + "можжевельник", + "море", + "мороженое", + "морошка", + "морс", + "мотылёк", + "мох", + "муравей", + "мята", + "нарцисс", + "незабудка", + "нектар", + "нить", + "нотка", + "облако", + "овсянка", + "огонёк", + "одуванчик", + "озеро", + "олень", + "олива", + "опал", + "орех", + "орешек", + "орхидея", + "осень", + "остров", + "оттенок", + "павлин", + "палитра", + "папоротник", + "парус", + "пастила", + "пеликан", + "пенёк", + "персик", + "песок", + "петрушка", + "пион", + "пихта", + "планета", + "платан", + "плед", + "плющ", + "подснежник", + "подушка", + "полюс", + "помело", + "пони", + "попугай", + "пряник", + "пуговица", + "пудель", + "пчела", + "пчёлка", + "пшеница", + "радуга", + "ракета", + "ракушка", + "рассвет", + "редис", + "репа", + "риф", + "рогалик", + "роза", + "розмарин", + "ромашка", + "роса", + "роща", + "рубин", + "ручей", + "рысь", + "рябина", + "рябчик", + "саламандра", + "самовар", + "сапфир", + "сахар", + "сверчок", + "свет", + "свирель", + "свиристель", + "север", + "сияние", + "скворец", + "скрипка", + "слива", + "слон", + "слонёнок", + "смородина", + "снег", + "снегирь", + "снежинка", + "сова", + "совёнок", + "сокол", + "солнце", + "соловей", + "сорока", + "сосна", + "соты", + "спираль", + "стебель", + "стрекоза", + "стриж", + "суслик", + "суфле", + "тайга", + "тамаринд", + "тапир", + "тень", + "тигр", + "тигрёнок", + "тополь", + "трель", + "тропа", + "туман", + "тыква", + "тюльпан", + "уголь", + "уголёк", + "узор", + "укроп", + "улитка", + "улыбка", + "утка", + "утро", + "утёнок", + "фазан", + "факел", + "фиалка", + "фикус", + "филин", + "финик", + "фонарь", + "фонтан", + "фрегат", + "хамелеон", + "хвоя", + "хомяк", + "цапля", + "цветок", + "чай", + "чайка", + "чашка", + "черепаха", + "черешня", + "черника", + "черёмуха", + "чечевица", + "чибис", + "чиж", + "шиповник", + "шишка", + "шмель", + "шорох", + "шпинат", + "щавель", + "щенок", + "эвкалипт", + "эдельвейс", + "эскимо", + "юла", + "юрта", + "яблоко", + "яблоня", + "ягода", + "ягуар", + "янтарь", + "ясень", + "ястреб", + "ящерица", + "ёж", + "ёлка" +] diff --git a/hub/examples/monster_preview.rs b/hub/examples/monster_preview.rs new file mode 100644 index 0000000..434f849 --- /dev/null +++ b/hub/examples/monster_preview.rs @@ -0,0 +1,22 @@ +//! Writes a deterministic contact sheet without opening a desktop window or account. +fn main() -> anyhow::Result<()> { + let output = std::env::args() + .nth(1) + .unwrap_or_else(|| "/tmp/vnox-monsters.png".into()); + let mut sheet = image::RgbImage::from_pixel(832, 624, image::Rgb([17, 19, 24])); + for index in 0..12 { + let id = format!("00000000-0000-4000-8000-{:012x}", index + 1); + let png = + vnox_hub::avatar::default_v2_png(&id).map_err(|_| anyhow::anyhow!("render failed"))?; + let image = image::load_from_memory(&png)?.to_rgb8(); + let image = image::imageops::resize(&image, 192, 192, image::imageops::FilterType::Nearest); + image::imageops::replace( + &mut sheet, + &image, + (index % 4 * 208 + 8).into(), + (index / 4 * 208 + 8).into(), + ); + } + sheet.save(output)?; + Ok(()) +} diff --git a/hub/migrations/0006_account_profile.sql b/hub/migrations/0006_account_profile.sql new file mode 100644 index 0000000..9ca4031 --- /dev/null +++ b/hub/migrations/0006_account_profile.sql @@ -0,0 +1,4 @@ +-- Generated accounts remain incomplete until the owner selects a handle. +ALTER TABLE accounts ADD COLUMN profile_complete INTEGER NOT NULL DEFAULT 0 + CHECK (profile_complete IN (0, 1)); +CREATE UNIQUE INDEX accounts_handle_case_insensitive ON accounts(handle COLLATE NOCASE); diff --git a/hub/migrations/0007_community_directory.sql b/hub/migrations/0007_community_directory.sql new file mode 100644 index 0000000..eb784a7 --- /dev/null +++ b/hub/migrations/0007_community_directory.sql @@ -0,0 +1,10 @@ +-- Separate unverified browser submissions from cryptographic server registrations. +CREATE TABLE community_servers ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL CHECK(length(name) BETWEEN 1 AND 80), + address TEXT NOT NULL UNIQUE, + description TEXT NOT NULL CHECK(length(description) <= 500), + owner_id TEXT NOT NULL REFERENCES accounts(user_id) ON DELETE CASCADE, + created_at INTEGER NOT NULL +); +CREATE INDEX community_servers_owner ON community_servers(owner_id); diff --git a/hub/migrations/0008_desktop_auth.sql b/hub/migrations/0008_desktop_auth.sql new file mode 100644 index 0000000..1ab9ccf --- /dev/null +++ b/hub/migrations/0008_desktop_auth.sql @@ -0,0 +1,17 @@ +CREATE TABLE desktop_authorizations ( + device_hash TEXT PRIMARY KEY, + code_hash TEXT NOT NULL UNIQUE, + public_key TEXT NOT NULL, + user_id TEXT REFERENCES accounts(user_id) ON DELETE CASCADE, + expires_at INTEGER NOT NULL +); +CREATE INDEX desktop_authorization_expiry ON desktop_authorizations(expires_at); +CREATE TABLE desktop_devices ( + refresh_hash TEXT PRIMARY KEY, + device_hash TEXT NOT NULL UNIQUE, + public_key TEXT NOT NULL, + user_id TEXT NOT NULL REFERENCES accounts(user_id) ON DELETE CASCADE, + created_at INTEGER NOT NULL, + expires_at INTEGER NOT NULL +); +CREATE INDEX desktop_device_expiry ON desktop_devices(expires_at); diff --git a/hub/migrations/0009_account_onboarding.sql b/hub/migrations/0009_account_onboarding.sql new file mode 100644 index 0000000..40b3afb --- /dev/null +++ b/hub/migrations/0009_account_onboarding.sql @@ -0,0 +1,8 @@ +-- Existing accounts have no row and never receive a first-registration prompt. +-- This migration is independent of optional handle/profile completion migrations. +CREATE TABLE account_onboarding ( + user_id TEXT PRIMARY KEY REFERENCES accounts(user_id) ON DELETE CASCADE, + decided_at INTEGER, + decision TEXT CHECK (decision IN ('rename', 'later')), + CHECK ((decided_at IS NULL) = (decision IS NULL)) +); diff --git a/hub/src/auth/mod.rs b/hub/src/auth/mod.rs index 885c67d..6a157e5 100644 --- a/hub/src/auth/mod.rs +++ b/hub/src/auth/mod.rs @@ -1,4 +1,5 @@ pub mod email; +mod nicknames; pub mod provider; mod routes; pub mod store; @@ -26,16 +27,19 @@ pub struct AuthService { /// Present only when a mail transport is configured. pub email: Option, limits: Mutex>, + profile_events: Mutex>>, + directory_reads: Mutex>, + directory_events: Mutex>>, avatar_events: Mutex>>, /// Bounds concurrent image decodes, which are the most expensive thing a session can ask for. - pub avatar_decoding: tokio::sync::Semaphore, + pub avatar_decoding: Arc, } -/// Avatar changes (upload or removal) per account and per source within one hour. -const AVATAR_PER_USER_HOUR: usize = 10; -const AVATAR_PER_SOURCE_HOUR: usize = 30; -const AVATAR_WINDOW: i64 = 3600; -const AVATAR_TRACKED_KEYS: usize = 8192; +/// Independent profile/avatar budgets per account and per source within one hour. +const CHANGE_PER_USER_HOUR: usize = 10; +const CHANGE_PER_SOURCE_HOUR: usize = 30; +const CHANGE_WINDOW: i64 = 3600; +const CHANGE_TRACKED_KEYS: usize = 8192; impl AuthService { pub fn new( @@ -62,8 +66,11 @@ impl AuthService { provider, email: None, limits: Mutex::new(HashMap::new()), + profile_events: Mutex::new(HashMap::new()), + directory_reads: Mutex::new(HashMap::new()), + directory_events: Mutex::new(HashMap::new()), avatar_events: Mutex::new(HashMap::new()), - avatar_decoding: tokio::sync::Semaphore::new(2), + avatar_decoding: Arc::new(tokio::sync::Semaphore::new(2)), } } /// Enables email sign-in. Callers do this only when SMTP is configured (or a test sender). @@ -78,7 +85,13 @@ impl AuthService { self.provider.is_some() } pub fn allow(&self, ip: &str) -> bool { - let Ok(mut limits) = self.limits.lock() else { + Self::allow_reads(&self.limits, ip) + } + pub fn allow_directory_read(&self, ip: &str) -> bool { + Self::allow_reads(&self.directory_reads, ip) + } + fn allow_reads(limits: &Mutex>, ip: &str) -> bool { + let Ok(mut limits) = limits.lock() else { return false; }; let now = store::now(); @@ -101,20 +114,30 @@ impl AuthService { /// budgets still had room. A refused attempt is not recorded, so a blocked client does /// not extend its own block. pub fn allow_avatar(&self, user: &str, source: &str) -> bool { - let Ok(mut events) = self.avatar_events.lock() else { + Self::allow_changes(&self.avatar_events, user, source) + } + /// Profile writes have an independent, bounded hourly account/source budget. + pub fn allow_profile(&self, user: &str, source: &str) -> bool { + Self::allow_changes(&self.profile_events, user, source) + } + pub fn allow_directory(&self, user: &str, source: &str) -> bool { + Self::allow_changes(&self.directory_events, user, source) + } + fn allow_changes(events: &Mutex>>, user: &str, source: &str) -> bool { + let Ok(mut events) = events.lock() else { return false; }; let now = store::now(); events.retain(|_, times| { - times.retain(|at| now - *at < AVATAR_WINDOW); + times.retain(|at| now - *at < CHANGE_WINDOW); !times.is_empty() }); let user_key = format!("u:{user}"); let source_key = format!("s:{source}"); let count = |key: &str| events.get(key).map_or(0, Vec::len); - if count(&user_key) >= AVATAR_PER_USER_HOUR - || count(&source_key) >= AVATAR_PER_SOURCE_HOUR - || events.len() + 2 > AVATAR_TRACKED_KEYS + if count(&user_key) >= CHANGE_PER_USER_HOUR + || count(&source_key) >= CHANGE_PER_SOURCE_HOUR + || events.len() + 2 > CHANGE_TRACKED_KEYS { return false; } diff --git a/hub/src/auth/nicknames.rs b/hub/src/auth/nicknames.rs new file mode 100644 index 0000000..03ecad9 --- /dev/null +++ b/hub/src/auth/nicknames.rs @@ -0,0 +1,41 @@ +//! First-registration display names, independent of provider names and public handles. +use rand::RngExt; +use std::sync::LazyLock; +static WORDS: LazyLock> = LazyLock::new(|| { + serde_json::from_str(include_str!("../../assets/nicknames/friendly-ru.json")) + .expect("checked-in nickname words are validated by tests") +}); + +pub(super) fn generate() -> String { + // ThreadRng is the OS-seeded cryptographic generator; rejection-sampled ranges + // avoid biased suffixes. Display names deliberately need not be unique handles. + let mut random = rand::rng(); + format!( + "{}_{:04}", + WORDS[random.random_range(0..WORDS.len())], + random.random_range(0..10_000) + ) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn bundled_words_and_generated_names_are_bounded_friendly_forms() { + assert!(WORDS.len() >= 300); + let unique: std::collections::HashSet<_> = WORDS.iter().collect(); + assert_eq!(unique.len(), WORDS.len()); + for word in WORDS.iter() { + assert!(word.chars().all(|c| ('а'..='я').contains(&c) || c == 'ё')); + assert!(word.chars().count() <= 59); + } + for _ in 0..1000 { + let name = generate(); + let (word, suffix) = name.rsplit_once('_').unwrap(); + assert!(WORDS.iter().any(|candidate| candidate == word)); + assert_eq!(suffix.len(), 4); + assert!(suffix.bytes().all(|c| c.is_ascii_digit())); + assert!(name.chars().count() <= 64); + } + } +} diff --git a/hub/src/auth/routes.rs b/hub/src/auth/routes.rs index 086e90e..8e8ff8b 100644 --- a/hub/src/auth/routes.rs +++ b/hub/src/auth/routes.rs @@ -8,13 +8,18 @@ use axum::{ extract::{DefaultBodyLimit, Query, State, rejection::JsonRejection}, http::{HeaderMap, StatusCode, header}, response::{IntoResponse, Redirect, Response}, - routing::{get, post}, + routing::{get, post, put}, }; use cookie::{Cookie, SameSite}; use oauth2::{CsrfToken, PkceCodeChallenge}; use serde::Deserialize; use serde_json::json; mod avatar; +mod desktop; +mod desktop_avatar; +mod directory; +mod onboarding; +mod profile; use std::sync::Arc; use subtle::ConstantTimeEq; const BROWSER: &str = "__Host-vnox_oauth"; @@ -22,6 +27,31 @@ const EMAIL_BROWSER: &str = "__Host-vnox_email"; const SESSION: &str = "__Host-vnox_session"; pub fn router() -> Router> { Router::new() + .route("/v1/auth/desktop/start", post(desktop::start)) + .route("/v1/auth/desktop/request", get(desktop::request)) + .route("/v1/auth/desktop/approve", post(desktop::approve)) + .route("/v1/auth/desktop/poll", post(desktop::poll)) + .route("/v1/auth/desktop/cancel", post(desktop::cancel)) + .route("/v1/auth/desktop/refresh", post(desktop::refresh)) + .route("/v1/auth/desktop/revoke", post(desktop::revoke)) + .layer(DefaultBodyLimit::max(4096)) + .route( + "/v1/auth/desktop/profile", + post(desktop_avatar::mutate).layer(DefaultBodyLimit::max(1024)), + ) + .route( + "/v1/auth/desktop/avatar", + post(desktop_avatar::mutate) + .delete(desktop_avatar::mutate) + .layer(DefaultBodyLimit::max(crate::avatar::MAX_UPLOAD_BYTES)), + ) + .route( + "/v1/servers", + get(directory::list) + .post(directory::create) + .layer(DefaultBodyLimit::max(4096)), + ) + .route("/v1/servers/{id}", axum::routing::delete(directory::remove)) .route("/v1/auth/github/start", get(start)) .route("/v1/auth/github/callback", get(callback)) .route( @@ -38,7 +68,24 @@ pub fn router() -> Router> { "/v1/account/avatar", post(avatar::upload).delete(avatar::remove), ) + .route( + "/v1/account/onboarding", + post(onboarding::decide).layer(DefaultBodyLimit::max(1024)), + ) + .route( + "/v1/account/display-name", + post(onboarding::rename).layer(DefaultBodyLimit::max(1024)), + ) + .route( + "/v1/avatars/default-v2/{user_id}", + get(avatar::generated_v2), + ) .route("/v1/avatars/{id}", get(avatar::serve)) + .route("/v1/avatars/default/{user_id}", get(avatar::generated)) + .route( + "/v1/account/profile", + put(profile::update).layer(DefaultBodyLimit::max(1024)), + ) } fn failure(status: StatusCode, code: &'static str) -> Response { (status, Json(json!({"error":code}))).into_response() @@ -323,8 +370,13 @@ async fn email_verify( return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); }; drop_old_session(auth, &headers).await; - let mut response = - Json(json!({"authenticated":true,"user":user,"csrf_token":csrf})).into_response(); + let Ok(pending) = store::onboarding_pending(&auth.pool, &user.user_id).await else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + }; + let mut response = Json( + json!({"authenticated":true,"user":user,"csrf_token":csrf,"onboarding_pending":pending}), + ) + .into_response(); set_cookie(&mut response, SESSION, token, store::SESSION_TTL); set_cookie(&mut response, EMAIL_BROWSER, String::new(), 0); response @@ -333,7 +385,11 @@ async fn session(State(state): State>, headers: HeaderMap) -> Resp if let Some(token) = cookie(&headers, SESSION) { match store::get_session(&state.auth.pool, &token).await { Ok(Some((user, csrf))) => { - return Json(json!({"authenticated":true,"user":user,"csrf_token":csrf})) + let Ok(pending) = store::onboarding_pending(&state.auth.pool, &user.user_id).await + else { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + }; + return Json(json!({"authenticated":true,"user":user,"csrf_token":csrf,"onboarding_pending":pending})) .into_response(); } Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), diff --git a/hub/src/auth/routes/avatar.rs b/hub/src/auth/routes/avatar.rs index 8a5931f..e6c92cb 100644 --- a/hub/src/auth/routes/avatar.rs +++ b/hub/src/auth/routes/avatar.rs @@ -51,11 +51,14 @@ pub(super) async fn upload( let Ok(data) = to_bytes(body, avatar::MAX_UPLOAD_BYTES).await else { return failure(StatusCode::PAYLOAD_TOO_LARGE, "too_large"); }; - let Ok(permit) = state.auth.avatar_decoding.try_acquire() else { + let Ok(permit) = state.auth.avatar_decoding.clone().try_acquire_owned() else { return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); }; - let processed = tokio::task::spawn_blocking(move || avatar::normalize(&declared, &data)).await; - drop(permit); + let processed = tokio::task::spawn_blocking(move || { + let _permit = permit; + avatar::normalize(&declared, &data) + }) + .await; let png = match processed { Ok(Ok(png)) => png, Ok(Err(avatar::AvatarError::UnsupportedType)) => { @@ -79,7 +82,7 @@ pub(super) async fn upload( } /// `DELETE /v1/account/avatar`: drops the own photo; the answer carries the avatar now in -/// effect (the provider's, or null for the default). +/// effect (the generated VNOX default). pub(super) async fn remove(State(state): State>, headers: HeaderMap) -> Response { let user = match authorized(&state, &headers).await { Ok((user, _)) => user, @@ -92,10 +95,7 @@ pub(super) async fn remove(State(state): State>, headers: HeaderMa if avatar::remove(pool, &user.user_id).await.is_err() { return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); } - match avatar::provider_url(pool, &user.user_id).await { - Ok(url) => Json(json!({"avatar_url": url})).into_response(), - Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), - } + Json(json!({"avatar_url": avatar::default_url(&user.user_id)})).into_response() } /// `GET /v1/avatars/`: public and immutable, because the id is the content hash. @@ -130,3 +130,59 @@ pub(super) async fn serve(State(state): State>, Path(id): Path) -> Response { + generated_response(user_id, false).await +} + +pub(super) async fn generated_v2(Path(user_id): Path) -> Response { + generated_response(user_id, true).await +} + +async fn generated_response(user_id: String, version_two: bool) -> Response { + if !uuid::Uuid::parse_str(&user_id).is_ok_and(|id| id.to_string() == user_id) { + return failure(StatusCode::NOT_FOUND, "not_found"); + } + let Ok(permit) = DEFAULT_RENDERING.try_acquire() else { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + }; + let png = match tokio::task::spawn_blocking(move || { + // Keep the slot while rendering even if the requesting browser disconnects. + let _permit = permit; + if version_two { + avatar::default_v2_png(&user_id) + } else { + avatar::default_png(&user_id) + } + }) + .await + { + Ok(Ok(png)) => png, + _ => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + }; + let id = avatar::content_id(&png); + let mut response = png.into_response(); + let headers = response.headers_mut(); + headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("image/png")); + headers.insert( + header::CACHE_CONTROL, + HeaderValue::from_static("public, max-age=31536000, immutable"), + ); + headers.insert( + header::CONTENT_SECURITY_POLICY, + HeaderValue::from_static("default-src 'none'; sandbox; frame-ancestors 'none'"), + ); + headers.insert( + header::CONTENT_DISPOSITION, + HeaderValue::from_static("inline; filename=\"avatar.png\""), + ); + if let Ok(value) = HeaderValue::from_str(&format!("\"{id}\"")) { + headers.insert(header::ETAG, value); + } + response +} diff --git a/hub/src/auth/routes/desktop.rs b/hub/src/auth/routes/desktop.rs new file mode 100644 index 0000000..12af07e --- /dev/null +++ b/hub/src/auth/routes/desktop.rs @@ -0,0 +1,287 @@ +//! Browser-approved desktop enrollment. All bearer secrets stay outside browser URLs. +use super::{authorized, failure}; +use crate::{auth::store, web::HubState}; +use axum::{ + Json, + extract::{Query, State}, + http::{HeaderMap, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use std::sync::Arc; + +const REQUEST_TTL: i64 = 600; +const DEVICE_TTL: i64 = 30 * 24 * 3600; +const PROOF_TTL: i64 = 600; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Start { + public_key: String, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Code { + code: String, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Device { + device_secret: String, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Refresh { + refresh_secret: String, +} + +pub(super) fn secret_shape(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) +} +fn available(state: &HubState, headers: &HeaderMap) -> bool { + state.auth.allow_directory_read(&super::client_ip(headers)) +} +async fn cleanup(state: &HubState) -> anyhow::Result<()> { + sqlx::query("DELETE FROM desktop_authorizations WHERE expires_at<=?") + .bind(store::now()) + .execute(&state.database) + .await?; + sqlx::query("DELETE FROM desktop_devices WHERE expires_at<=?") + .bind(store::now()) + .execute(&state.database) + .await?; + Ok(()) +} + +pub(super) async fn start( + State(state): State>, + headers: HeaderMap, + Json(body): Json, +) -> Response { + if !available(&state, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let valid_key = hex::decode(&body.public_key) + .ok() + .and_then(|b| <[u8; 32]>::try_from(b).ok()) + .and_then(|b| ed25519_dalek::VerifyingKey::from_bytes(&b).ok()); + if !secret_shape(&body.public_key) || valid_key.is_none() { + return failure(StatusCode::BAD_REQUEST, "invalid_public_key"); + } + if cleanup(&state).await.is_err() { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + } + let device_secret = store::token(); + let code = store::token(); + let expires_at = store::now() + REQUEST_TTL; + // The SQL capacity guard and insert are atomic even across concurrent requests. + let result=sqlx::query("INSERT INTO desktop_authorizations(device_hash,code_hash,public_key,expires_at) SELECT ?,?,?,? WHERE (SELECT COUNT(*) FROM desktop_authorizations)<4096") + .bind(store::hash(&device_secret)).bind(store::hash(&code)).bind(&body.public_key).bind(expires_at).execute(&state.database).await; + if !matches!(result,Ok(r) if r.rows_affected()==1) { + return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"); + } + let mut uri = state.auth.origin.join("desktop").expect("fixed path"); + uri.query_pairs_mut().append_pair("code", &code); + Json(json!({"device_secret":device_secret,"user_code":code,"verification_uri":uri,"expires_at":expires_at,"interval":5})).into_response() +} + +pub(super) async fn request( + State(state): State>, + headers: HeaderMap, + Query(body): Query, +) -> Response { + if !available(&state, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + if !secret_shape(&body.code) { + return failure(StatusCode::BAD_REQUEST, "invalid_code"); + } + let row: Result,_>=sqlx::query_as("SELECT public_key,expires_at,user_id IS NOT NULL FROM desktop_authorizations WHERE code_hash=? AND expires_at>?") + .bind(store::hash(&body.code)).bind(store::now()).fetch_optional(&state.database).await; + match row { + Ok(Some((public_key, expires_at, approved))) => { + Json(json!({"public_key":public_key,"expires_at":expires_at,"approved":approved})) + .into_response() + } + Ok(None) => failure(StatusCode::GONE, "request_expired"), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +pub(super) async fn approve( + State(state): State>, + headers: HeaderMap, + Json(body): Json, +) -> Response { + let user = match authorized(&state, &headers).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !available(&state, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + if !secret_shape(&body.code) { + return failure(StatusCode::BAD_REQUEST, "invalid_code"); + } + // Approval cannot replace an account already bound to this request. + match sqlx::query("UPDATE desktop_authorizations SET user_id=? WHERE code_hash=? AND expires_at>? AND user_id IS NULL") + .bind(user.user_id).bind(store::hash(&body.code)).bind(store::now()).execute(&state.database).await { + Ok(r) if r.rows_affected()==1=>Json(json!({"approved":true})).into_response(), + Ok(_)=>failure(StatusCode::GONE,"request_expired"), + Err(_)=>failure(StatusCode::SERVICE_UNAVAILABLE,"try_later"), + } +} + +#[derive(Serialize)] +struct Claims<'a> { + version: u32, + issuer: String, + subject: &'a str, + public_key: &'a str, + issued_at: i64, + expires_at: i64, + audience: &'static str, + display_name: String, + avatar_url: Option, +} +async fn proof(state: &HubState, user: &str, key: &str) -> anyhow::Result { + let now = store::now(); + let profile = account(state, user).await?; + let claims = serde_json::to_vec(&Claims { + display_name: profile.display_name, + avatar_url: profile.avatar_url, + version: 1, + issuer: state.issuer.id.to_string(), + subject: user, + public_key: key, + issued_at: now, + expires_at: now + PROOF_TTL, + audience: "vnox-gateway", + })?; + Ok( + json!({"claims":hex::encode(&claims),"signature":hex::encode(state.issuer.sign_account_claims(&claims))}), + ) +} +#[derive(Serialize, sqlx::FromRow)] +pub(super) struct DesktopUser { + user_id: String, + handle: String, + display_name: String, + avatar_url: Option, +} +pub(super) async fn account(state: &HubState, user: &str) -> anyhow::Result { + Ok(sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,COALESCE('/v1/avatars/'||v.id,'/v1/avatars/default-v2/'||a.user_id) AS avatar_url FROM accounts a LEFT JOIN avatars v ON v.user_id=a.user_id WHERE a.user_id=?").bind(user).fetch_one(&state.database).await?) +} + +pub(super) async fn poll( + State(state): State>, + headers: HeaderMap, + Json(body): Json, +) -> Response { + if !available(&state, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + if !secret_shape(&body.device_secret) { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + } + let result:anyhow::Result>=async { + let mut tx=state.database.begin().await?; + let row:Option<(String,Option,i64)>=sqlx::query_as("SELECT public_key,user_id,expires_at FROM desktop_authorizations WHERE device_hash=?") + .bind(store::hash(&body.device_secret)).fetch_optional(&mut *tx).await?; + let Some((key,user,expires))=row else {anyhow::bail!("expired")}; + anyhow::ensure!(expires>store::now(),"expired"); + let Some(user)=user else {return Ok(None)}; + let removed=sqlx::query("DELETE FROM desktop_authorizations WHERE device_hash=?").bind(store::hash(&body.device_secret)).execute(&mut *tx).await?; + anyhow::ensure!(removed.rows_affected()==1,"consumed"); + let refresh=store::token(); + sqlx::query("INSERT INTO desktop_devices(refresh_hash,device_hash,public_key,user_id,created_at,expires_at) VALUES(?,?,?,?,?,?)") + .bind(store::hash(&refresh)).bind(store::hash(&body.device_secret)).bind(&key).bind(&user).bind(store::now()).bind(store::now()+DEVICE_TTL).execute(&mut *tx).await?; + let user_account:DesktopUser=sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,COALESCE('/v1/avatars/'||v.id,'/v1/avatars/default-v2/'||a.user_id) AS avatar_url FROM accounts a LEFT JOIN avatars v ON v.user_id=a.user_id WHERE a.user_id=?").bind(&user).fetch_one(&mut *tx).await?; + let result=json!({"account_proof":proof(&state,&user,&key).await?,"refresh_secret":refresh,"expires_at":store::now()+PROOF_TTL,"user":user_account}); + tx.commit().await?; + Ok(Some(result)) + }.await; + match result { + Ok(Some(value)) => Json(value).into_response(), + Ok(None) => (StatusCode::ACCEPTED, Json(json!({"status":"pending"}))).into_response(), + Err(_) => failure(StatusCode::GONE, "request_expired"), + } +} + +pub(super) async fn cancel( + State(state): State>, + Json(body): Json, +) -> Response { + if !secret_shape(&body.device_secret) { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + } + let result: anyhow::Result<()> = async { + let mut tx = state.database.begin().await?; + sqlx::query("DELETE FROM desktop_authorizations WHERE device_hash=?") + .bind(store::hash(&body.device_secret)) + .execute(&mut *tx) + .await?; + // A concurrent successful poll may already have consumed the request. The + // pairing secret still revokes that enrollment, including a lost HTTP reply. + sqlx::query("DELETE FROM desktop_devices WHERE device_hash=?") + .bind(store::hash(&body.device_secret)) + .execute(&mut *tx) + .await?; + tx.commit().await?; + Ok(()) + } + .await; + match result { + Ok(()) => StatusCode::NO_CONTENT.into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +pub(super) async fn refresh( + State(state): State>, + headers: HeaderMap, + Json(body): Json, +) -> Response { + if !available(&state, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + if !secret_shape(&body.refresh_secret) { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + } + let row: Result, _> = sqlx::query_as( + "SELECT public_key,user_id FROM desktop_devices WHERE refresh_hash=? AND expires_at>?", + ) + .bind(store::hash(&body.refresh_secret)) + .bind(store::now()) + .fetch_optional(&state.database) + .await; + let Ok(Some((key, user))) = row else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let result:anyhow::Result<_>=async {Ok(json!({"account_proof":proof(&state,&user,&key).await?,"expires_at":store::now()+PROOF_TTL,"user":account(&state,&user).await?}))}.await; + match result { + Ok(value) => Json(value).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +pub(super) async fn revoke( + State(state): State>, + Json(body): Json, +) -> Response { + if !secret_shape(&body.refresh_secret) { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + } + match sqlx::query("DELETE FROM desktop_devices WHERE refresh_hash=?") + .bind(store::hash(&body.refresh_secret)) + .execute(&state.database) + .await + { + Ok(_) => StatusCode::NO_CONTENT.into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} diff --git a/hub/src/auth/routes/desktop_avatar.rs b/hub/src/auth/routes/desktop_avatar.rs new file mode 100644 index 0000000..17879d7 --- /dev/null +++ b/hub/src/auth/routes/desktop_avatar.rs @@ -0,0 +1,177 @@ +//! Desktop avatar mutations require an active enrollment and its device signature. +use super::{allowed_avatar, desktop, failure}; +use crate::{auth::store, avatar, web::HubState}; +use axum::{ + Json, + body::{Body, to_bytes}, + extract::{OriginalUri, State}, + http::{HeaderMap, Method, StatusCode, header}, + response::{IntoResponse, Response}, +}; +use ed25519_dalek::{Signature, VerifyingKey}; +use sha2::{Digest, Sha256}; +use std::sync::Arc; + +const SIGN_CONTEXT: &[u8] = b"VNOX-CENTRAL-PROFILE-v1\0"; +fn transcript(method: &str, path: &str, timestamp: &str, body: &[u8]) -> Vec { + let mut bytes = SIGN_CONTEXT.to_vec(); + bytes.extend_from_slice(method.as_bytes()); + bytes.push(0); + bytes.extend_from_slice(path.as_bytes()); + bytes.push(0); + bytes.extend_from_slice(timestamp.as_bytes()); + bytes.push(0); + bytes.extend_from_slice(&Sha256::digest(body)); + bytes +} +fn field<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> { + headers.get(name)?.to_str().ok() +} + +pub(super) async fn mutate( + State(state): State>, + method: Method, + OriginalUri(uri): OriginalUri, + headers: HeaderMap, + body: Body, +) -> Response { + let profile = uri.path() == "/v1/auth/desktop/profile"; + let Some(secret) = field(&headers, "authorization").and_then(|s| s.strip_prefix("Bearer ")) + else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let Some(public) = field(&headers, "x-vnox-public-key") else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let Some(timestamp) = field(&headers, "x-vnox-timestamp") else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let Some(signature) = field(&headers, "x-vnox-signature") else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + if !desktop::secret_shape(secret) + || !desktop::secret_shape(public) + || !timestamp + .parse::() + .is_ok_and(|time| store::now().abs_diff(time) <= 60) + { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + } + let key = hex::decode(public) + .ok() + .and_then(|v| <[u8; 32]>::try_from(v).ok()) + .and_then(|v| VerifyingKey::from_bytes(&v).ok()); + let signature = hex::decode(signature) + .ok() + .and_then(|v| Signature::from_slice(&v).ok()); + let (Some(key), Some(signature)) = (key, signature) else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + let refresh_hash = store::hash(secret); + let row: Result,_> = sqlx::query_scalar("SELECT user_id FROM desktop_devices WHERE refresh_hash=? AND public_key=? AND expires_at>?") + .bind(&refresh_hash).bind(public).bind(store::now()).fetch_optional(&state.database).await; + let Ok(Some(user)) = row else { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + }; + if !(if profile { + state.auth.allow_profile(&user, &super::client_ip(&headers)) + } else { + allowed_avatar(&state.auth, &user, &headers) + }) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let Ok(bytes) = to_bytes( + body, + if profile { + 1024 + } else { + avatar::MAX_UPLOAD_BYTES + }, + ) + .await + else { + return failure(StatusCode::PAYLOAD_TOO_LARGE, "too_large"); + }; + if key + .verify_strict( + &transcript(method.as_str(), uri.path(), timestamp, &bytes), + &signature, + ) + .is_err() + { + return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + } + #[derive(serde::Deserialize)] + #[serde(deny_unknown_fields)] + struct Name { + display_name: String, + } + let display_name = if profile { + let Ok(name) = serde_json::from_slice::(&bytes) else { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + }; + let Some(name) = normalize_name(&name.display_name) else { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_display_name"); + }; + Some(name.to_owned()) + } else { + None + }; + let png = if profile { + None + } else if method == Method::POST { + if field(&headers, header::CONTENT_TYPE.as_str()) != Some("image/png") { + return failure(StatusCode::UNSUPPORTED_MEDIA_TYPE, "unsupported_type"); + } + let Ok(permit) = state.auth.avatar_decoding.clone().try_acquire_owned() else { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + }; + let result = tokio::task::spawn_blocking(move || { + let _permit = permit; + avatar::normalize("image/png", &bytes) + }) + .await; + match result { + Ok(Ok(png)) => Some(png), + Ok(Err(avatar::AvatarError::UnsupportedType | avatar::AvatarError::TypeMismatch)) => { + return failure(StatusCode::UNSUPPORTED_MEDIA_TYPE, "unsupported_type"); + } + Ok(Err(_)) => return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_image"), + Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } + } else { + if !bytes.is_empty() { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + } + None + }; + // Recheck enrollment under the write transaction after expensive decoding. Revocation + // that committed first must prevent the mutation, not only its response. + let result: anyhow::Result = async { + let mut tx=state.database.begin().await?; + let valid: bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM desktop_devices WHERE refresh_hash=? AND public_key=? AND user_id=? AND expires_at>?)") + .bind(&refresh_hash).bind(public).bind(&user).bind(store::now()).fetch_one(&mut *tx).await?; + if !valid {return Ok(false)} + if let Some(name)=display_name { + sqlx::query("UPDATE accounts SET display_name=? WHERE user_id=?").bind(name).bind(&user).execute(&mut *tx).await?; + } else if let Some(png)=png { + sqlx::query("INSERT INTO avatars(user_id,id,content_type,bytes,created_at) VALUES(?,?,?,?,?) ON CONFLICT(user_id) DO UPDATE SET id=excluded.id,content_type=excluded.content_type,bytes=excluded.bytes,created_at=excluded.created_at") + .bind(&user).bind(avatar::content_id(&png)).bind("image/png").bind(png).bind(store::now()).execute(&mut *tx).await?; + } else {sqlx::query("DELETE FROM avatars WHERE user_id=?").bind(&user).execute(&mut *tx).await?;} + tx.commit().await?; Ok(true) + }.await; + match result { + Ok(true) => match desktop::account(&state, &user).await { + Ok(user) => Json(serde_json::json!({"user":user})).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + }, + Ok(false) => failure(StatusCode::UNAUTHORIZED, "not_authenticated"), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +fn normalize_name(value: &str) -> Option<&str> { + if value.chars().any(|c| c.is_control() || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')) { return None; } + let name = value.trim(); + (1..=64).contains(&name.chars().count()).then_some(name) +} diff --git a/hub/src/auth/routes/directory.rs b/hub/src/auth/routes/directory.rs new file mode 100644 index 0000000..44973ef --- /dev/null +++ b/hub/src/auth/routes/directory.rs @@ -0,0 +1,241 @@ +//! Unverified community submissions; never resolves or connects to submitted addresses. +use super::{authorized, client_ip, failure, store}; +use crate::web::HubState; +use axum::{ + Json, + extract::{FromRequest, Path, Query, Request, State}, + http::{HeaderMap, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use std::{ + net::{IpAddr, SocketAddr}, + sync::Arc, +}; +use uuid::Uuid; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Submission { + name: String, + address: String, + description: String, +} +#[derive(Serialize, sqlx::FromRow)] +struct Listing { + id: String, + name: String, + address: String, + description: String, + owner_id: String, + created_at: i64, +} +#[derive(Deserialize)] +pub(super) struct Page { + after: Option, +} +fn text(value: &str, max: usize, empty: bool) -> Option { + if value.chars().any(|c| c.is_control() || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')) { return None; } + let value = value.trim(); + ((empty || !value.is_empty()) && value.chars().count() <= max).then(|| value.to_owned()) +} +fn public_ip(ip: IpAddr) -> bool { + match ip { + IpAddr::V4(ip) => { + let [a, b, c, _] = ip.octets(); + !ip.is_private() + && !ip.is_loopback() + && !ip.is_link_local() + && !ip.is_multicast() + && a != 0 + && a < 240 + && !(a == 100 && (64..=127).contains(&b)) + && !(a == 192 && (b == 0 || (b == 88 && c == 99))) + && !(a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100))) + && !(a == 203 && b == 0 && c == 113) + } + IpAddr::V6(ip) => ip + .to_ipv4_mapped() + .map(|v| public_ip(IpAddr::V4(v))) + .unwrap_or_else(|| { + let segments = ip.segments(); + (segments[0] & 0xe000) == 0x2000 + && !(segments[0] == 0x2001 && segments[1] < 0x200) + && !(segments[0] == 0x2001 && segments[1] == 0xdb8) + }), + } +} +fn address(value: &str) -> Option { + if value.len() > 260 + || value + .bytes() + .any(|b| b.is_ascii_whitespace() || b.is_ascii_control()) + { + return None; + } + if let Ok(mut socket) = value.parse::() { + if let IpAddr::V6(ip) = socket.ip() + && let Some(ip) = ip.to_ipv4_mapped() + { + socket.set_ip(IpAddr::V4(ip)); + } + return (socket.port() != 0 && public_ip(socket.ip())).then(|| socket.to_string()); + } + let (host, port) = value.rsplit_once(':')?; + let port: u16 = port.parse().ok()?; + let host = host.strip_suffix('.').unwrap_or(host).to_ascii_lowercase(); + if port == 0 || host.len() > 253 || !host.contains('.') || host.parse::().is_ok() { + return None; + } + if !host.split('.').all(|part| { + !part.is_empty() + && part.len() <= 63 + && part.as_bytes()[0].is_ascii_alphanumeric() + && part.as_bytes()[part.len() - 1].is_ascii_alphanumeric() + && part.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) { + return None; + } + let tld = host.rsplit('.').next()?; + if !tld.bytes().any(|b| b.is_ascii_alphabetic()) + || matches!( + tld, + "localhost" | "local" | "internal" | "test" | "invalid" | "example" | "onion" + ) + { + return None; + } + Some(format!("{host}:{port}")) +} +pub(super) async fn list( + State(state): State>, + headers: HeaderMap, + page: Result, axum::extract::rejection::QueryRejection>, +) -> Response { + if !state.auth.allow_directory_read(&client_ip(&headers)) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let Ok(Query(page)) = page else { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + }; + if page + .after + .as_ref() + .is_some_and(|id| Uuid::parse_str(id).is_err()) + { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + } + match sqlx::query_as::<_, Listing>( + "SELECT * FROM community_servers WHERE id > ? ORDER BY id LIMIT 100", + ) + .bind(page.after.unwrap_or_default()) + .fetch_all(&state.auth.pool) + .await + { + Ok(servers) => Json(json!({"servers":servers})).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} +pub(super) async fn create(State(state): State>, request: Request) -> Response { + let user = match authorized(&state, request.headers()).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !state + .auth + .allow_directory(&user.user_id, &client_ip(request.headers())) + { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let body = match Json::::from_request(request, &state).await { + Ok(Json(body)) => body, + Err(e) => { + return failure( + if e.status() == StatusCode::PAYLOAD_TOO_LARGE { + e.status() + } else { + StatusCode::BAD_REQUEST + }, + "invalid_request", + ); + } + }; + let (Some(name), Some(address), Some(description)) = ( + text(&body.name, 80, false), + address(&body.address), + text(&body.description, 500, true), + ) else { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_server"); + }; + let id = Uuid::new_v4().to_string(); + // The conditional INSERT makes the account cap atomic with concurrent submissions. + let result = sqlx::query_as::<_, Listing>("INSERT INTO community_servers(id,name,address,description,owner_id,created_at) SELECT ?,?,?,?,?,? WHERE (SELECT COUNT(*) FROM community_servers WHERE owner_id=?) < 10 RETURNING *") + .bind(id).bind(name).bind(address).bind(description).bind(&user.user_id).bind(store::now()).bind(&user.user_id).fetch_optional(&state.auth.pool).await; + match result { + Ok(Some(server)) => (StatusCode::CREATED, Json(json!({"server":server}))).into_response(), + Ok(None) => failure(StatusCode::CONFLICT, "listing_limit"), + Err(sqlx::Error::Database(e)) if e.is_unique_violation() => { + failure(StatusCode::CONFLICT, "address_taken") + } + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} +pub(super) async fn remove( + State(state): State>, + Path(id): Path, + headers: HeaderMap, +) -> Response { + let user = match authorized(&state, &headers).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !state + .auth + .allow_directory(&user.user_id, &client_ip(&headers)) + { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + match sqlx::query("DELETE FROM community_servers WHERE id=? AND owner_id=?") + .bind(id) + .bind(user.user_id) + .execute(&state.auth.pool) + .await + { + Ok(result) if result.rows_affected() == 1 => StatusCode::NO_CONTENT.into_response(), + Ok(_) => failure(StatusCode::NOT_FOUND, "server_not_found"), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn normalizes_addresses_and_rejects_unsafe_inputs() { + assert_eq!( + address("VOICE.Example.COM.:07600"), + Some("voice.example.com:7600".into()) + ); + assert_eq!(address("8.8.8.8:7600"), Some("8.8.8.8:7600".into())); + assert_eq!(address("[::ffff:8.8.8.8]:7600"), address("8.8.8.8:7600")); + for bad in [ + "127.0.0.1:7600", + "10.1.1.1:7600", + "[::1]:7600", + "[::ffff:127.0.0.1]:7600", + "voice.local:7600", + "foo:7600", + "foo.com:0", + "foo.com:65536", + "https://foo.com:7600", + "x@foo.com:7600", + "foo.com/path:7600", + "foo.com:7600\n", + "1.2.3.999:7600", + ] { + assert_eq!(address(bad), None, "{bad}"); + } + assert_eq!(text("line\nbreak", 80, false), None); + assert_eq!(text(&"x".repeat(81), 80, false), None); + } +} diff --git a/hub/src/auth/routes/onboarding.rs b/hub/src/auth/routes/onboarding.rs new file mode 100644 index 0000000..aa83616 --- /dev/null +++ b/hub/src/auth/routes/onboarding.rs @@ -0,0 +1,93 @@ +//! Profile-name decisions do not select or change the unique public handle. +use super::{authorized, client_ip, failure, store}; +use crate::web::HubState; +use axum::{ + Json, + extract::{FromRequest, Request, State}, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; +use serde_json::json; +use std::sync::Arc; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Choice { + decision: Decision, +} +#[derive(Deserialize)] +#[serde(rename_all = "lowercase")] +enum Decision { + Rename, + Later, +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Name { + display_name: String, +} + +pub(super) async fn decide(State(state): State>, request: Request) -> Response { + let user = match authorized(&state, request.headers()).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !state + .auth + .allow_profile(&user.user_id, &client_ip(request.headers())) + { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let Ok(Json(choice)) = Json::::from_request(request, &state).await else { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + }; + let decision = match choice.decision { + Decision::Rename => "rename", + Decision::Later => "later", + }; + match sqlx::query("UPDATE account_onboarding SET decided_at=?,decision=? WHERE user_id=? AND decided_at IS NULL") + .bind(store::now()).bind(decision).bind(&user.user_id).execute(&state.database).await { + Ok(_) => Json(json!({"onboarding_pending":false})).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +pub(super) async fn rename(State(state): State>, request: Request) -> Response { + let user = match authorized(&state, request.headers()).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !state + .auth + .allow_profile(&user.user_id, &client_ip(request.headers())) + { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let Ok(Json(name)) = Json::::from_request(request, &state).await else { + return failure(StatusCode::BAD_REQUEST, "invalid_request"); + }; + let Some(name) = normalize_name(&name.display_name) else { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_display_name"); + }; + // An explicit name save also resolves the first-registration prompt. Handle and + // pending handle-selection semantics are independent and never touched here. + let result: anyhow::Result<()> = async { + let mut tx = state.database.begin().await?; + sqlx::query("UPDATE accounts SET display_name=? WHERE user_id=?") + .bind(name).bind(&user.user_id).execute(&mut *tx).await?; + sqlx::query("UPDATE account_onboarding SET decided_at=?,decision='rename' WHERE user_id=? AND decided_at IS NULL") + .bind(store::now()).bind(&user.user_id).execute(&mut *tx).await?; + tx.commit().await?; + Ok(()) + }.await; + match result { + Ok(()) => Json(json!({"display_name":name,"onboarding_pending":false})).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} +fn normalize_name(value: &str) -> Option<&str> { + if value.chars().any(|c| c.is_control() || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')) { return None; } + let name = value.trim(); + (1..=64).contains(&name.chars().count()).then_some(name) +} diff --git a/hub/src/auth/routes/profile.rs b/hub/src/auth/routes/profile.rs new file mode 100644 index 0000000..c34cac3 --- /dev/null +++ b/hub/src/auth/routes/profile.rs @@ -0,0 +1,106 @@ +//! Owner-selected profile; provider logins never select or overwrite a handle. +use super::{authorized, client_ip, failure, store}; +use crate::web::HubState; +use axum::{ + Json, + extract::{FromRequest, Request, State}, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; +use serde_json::json; +use std::sync::Arc; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Profile { + handle: String, + display_name: String, +} + +fn normalize_handle(value: &str) -> Option { + let handle = value.trim().to_ascii_lowercase(); + if !(3..=24).contains(&handle.len()) + || !handle.as_bytes()[0].is_ascii_lowercase() + || !handle + .bytes() + .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'_') + || handle.starts_with("u_") + || matches!( + handle.as_str(), + "admin" + | "administrator" + | "root" + | "system" + | "support" + | "vnox" + | "central" + | "moderator" + | "everyone" + | "here" + ) + { + return None; + } + Some(handle) +} + +fn normalize_name(value: &str) -> Option<&str> { + // Reject controls before trimming, so line breaks cannot silently disappear. + if value.chars().any(|c| c.is_control() || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')) { + return None; + } + let name = value.trim(); + (1..=64).contains(&name.chars().count()).then_some(name) +} + +pub(super) async fn update(State(state): State>, request: Request) -> Response { + let user = match authorized(&state, request.headers()).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !state + .auth + .allow_profile(&user.user_id, &client_ip(request.headers())) + { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + // Extract only after authorization, including for oversized or invalid bodies. + let body = match Json::::from_request(request, &state).await { + Ok(Json(body)) => body, + Err(error) => { + return failure( + if error.status() == StatusCode::PAYLOAD_TOO_LARGE { + StatusCode::PAYLOAD_TOO_LARGE + } else { + StatusCode::BAD_REQUEST + }, + "invalid_request", + ); + } + }; + let Some(handle) = normalize_handle(&body.handle) else { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_handle"); + }; + let Some(name) = normalize_name(&body.display_name) else { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_display_name"); + }; + // One statement serializes first selection, uniqueness and subsequent edits. Never + // authorize a handle change using the potentially stale session snapshot above. + let result = sqlx::query_as::<_, store::User>( + "UPDATE accounts SET handle=?,display_name=?,profile_complete=1 \ + WHERE user_id=? AND (profile_complete=0 OR handle=?) \ + RETURNING user_id,handle,display_name,profile_complete,\ + COALESCE((SELECT '/v1/avatars/'||id FROM avatars WHERE user_id=accounts.user_id),'/v1/avatars/default-v2/'||accounts.user_id) AS avatar_url", + ) + .bind(&handle).bind(name).bind(&user.user_id).bind(&handle) + .fetch_optional(&state.auth.pool).await; + match result { + Ok(Some(user)) => Json(json!({"user":user})).into_response(), + Ok(None) => failure(StatusCode::CONFLICT, "handle_locked"), + Err(sqlx::Error::Database(error)) if error.is_unique_violation() => { + failure(StatusCode::CONFLICT, "handle_taken") + } + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} diff --git a/hub/src/auth/store.rs b/hub/src/auth/store.rs index 694bc08..aa353fe 100644 --- a/hub/src/auth/store.rs +++ b/hub/src/auth/store.rs @@ -14,6 +14,7 @@ pub struct User { pub handle: String, pub display_name: String, pub avatar_url: Option, + pub profile_complete: bool, } pub fn hash(value: &str) -> String { @@ -79,40 +80,21 @@ pub async fn consume_attempt( } pub async fn upsert_identity(pool: &SqlitePool, profile: &GitHubProfile) -> anyhow::Result { - let name = profile - .name - .as_deref() - .unwrap_or(&profile.login) - .chars() - .take(100) - .collect::(); - let avatar = profile.avatar_url.as_deref().filter(|value| { - url::Url::parse(value).is_ok_and(|url| { - url.scheme() == "https" - && url.host_str() == Some("avatars.githubusercontent.com") - && url.username().is_empty() - && url.password().is_none() - }) - }); - upsert_external(pool, "github.com", &profile.id.to_string(), &name, avatar).await + // Provider names and photos never select the public VNOX profile. + upsert_external(pool, "github.com", &profile.id.to_string()).await } /// Account for a verified, normalized email address. The display name is deliberately /// neutral: the local part of an address must never become a public profile string. pub async fn upsert_email_identity(pool: &SqlitePool, email: &str) -> anyhow::Result { - upsert_external(pool, "email", email, "Vnox user", None).await + upsert_external(pool, "email", email).await } -async fn upsert_external( - pool: &SqlitePool, - provider: &str, - subject: &str, - name: &str, - avatar: Option<&str>, -) -> anyhow::Result { +async fn upsert_external(pool: &SqlitePool, provider: &str, subject: &str) -> anyhow::Result { if let Some(user) = lookup_identity(pool, provider, subject).await? { return Ok(user); } + let name = super::nicknames::generate(); let id = Uuid::new_v4(); // Generated stable handle avoids claiming a privileged/reused handle from another provider. let handle = format!("u_{}", id.simple()); @@ -124,9 +106,13 @@ async fn upsert_external( .bind(handle) .bind(now()) .bind(name) - .bind(avatar) + .bind(None::<&str>) .execute(&mut *transaction) .await?; + sqlx::query("INSERT INTO account_onboarding(user_id) VALUES(?)") + .bind(id.to_string()) + .execute(&mut *transaction) + .await?; let inserted = sqlx::query("INSERT INTO external_identities(provider,subject,user_id,linked_at) VALUES(?,?,?,?) ON CONFLICT(provider,subject) DO NOTHING") .bind(provider).bind(subject).bind(id.to_string()).bind(now()).execute(&mut *transaction).await?; if inserted.rows_affected() == 0 { @@ -144,8 +130,8 @@ async fn lookup_identity( provider: &str, subject: &str, ) -> anyhow::Result> { - // The own avatar (same-origin path) wins over the provider URL kept on the account. - Ok(sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,COALESCE('/v1/avatars/'||v.id,a.avatar_url) AS avatar_url FROM accounts a LEFT JOIN avatars v ON v.user_id=a.user_id JOIN external_identities e ON e.user_id=a.user_id WHERE e.provider=? AND e.subject=?") + // Only same-origin own or generated avatars are exposed, including for legacy accounts. + Ok(sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.profile_complete,COALESCE('/v1/avatars/'||v.id,'/v1/avatars/default-v2/'||a.user_id) AS avatar_url FROM accounts a LEFT JOIN avatars v ON v.user_id=a.user_id JOIN external_identities e ON e.user_id=a.user_id WHERE e.provider=? AND e.subject=?") .bind(provider).bind(subject).fetch_optional(pool).await?) } @@ -168,8 +154,9 @@ pub async fn get_session( display_name: String, avatar_url: Option, csrf_token: String, + profile_complete: bool, } - let row: Option = sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,COALESCE('/v1/avatars/'||v.id,a.avatar_url) AS avatar_url,s.csrf_token FROM web_sessions s JOIN accounts a ON a.user_id=s.user_id LEFT JOIN avatars v ON v.user_id=a.user_id WHERE s.token_hash=? AND s.expires_at>?") + let row: Option = sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.profile_complete,COALESCE('/v1/avatars/'||v.id,'/v1/avatars/default-v2/'||a.user_id) AS avatar_url,s.csrf_token FROM web_sessions s JOIN accounts a ON a.user_id=s.user_id LEFT JOIN avatars v ON v.user_id=a.user_id WHERE s.token_hash=? AND s.expires_at>?") .bind(hash(session)).bind(now()).fetch_optional(pool).await?; Ok(row.map(|r| { ( @@ -178,8 +165,19 @@ pub async fn get_session( handle: r.handle, display_name: r.display_name, avatar_url: r.avatar_url, + profile_complete: r.profile_complete, }, r.csrf_token, ) })) } + +/// Only accounts created after onboarding was introduced have pending rows. +pub async fn onboarding_pending(pool: &SqlitePool, user_id: &str) -> anyhow::Result { + Ok(sqlx::query_scalar( + "SELECT EXISTS(SELECT 1 FROM account_onboarding WHERE user_id=? AND decided_at IS NULL)", + ) + .bind(user_id) + .fetch_one(pool) + .await?) +} diff --git a/hub/src/avatar.rs b/hub/src/avatar.rs index d6e1acb..2e66f00 100644 --- a/hub/src/avatar.rs +++ b/hub/src/avatar.rs @@ -6,6 +6,8 @@ //! resized and written again as a fresh PNG. A fresh PNG carries no EXIF, GPS, ICC, text or //! other ancillary chunks from the source, so metadata cannot survive. +mod monsters; + use std::io::Cursor; use image::{ @@ -149,17 +151,87 @@ pub async fn fetch(pool: &SqlitePool, id: &str) -> anyhow::Result anyhow::Result> { - Ok( - sqlx::query_scalar("SELECT avatar_url FROM accounts WHERE user_id=?") - .bind(user_id) - .fetch_optional(pool) - .await? - .flatten(), - ) +/// Same-origin default, derived only from a public account identifier. No provider image. +pub fn default_url(user_id: &str) -> String { + format!("/v1/avatars/default-v2/{user_id}") +} + +/// A bounded, deterministic mirrored mosaic. The endpoint never looks up an account, +/// so it cannot reveal whether an identifier is registered. Keep this algorithm stable: +/// generated images are immutable and may be cached by browsers. +pub fn default_png(user_id: &str) -> Result, AvatarError> { + let digest = Sha256::digest(format!("vnox-default-avatar-v1:{user_id}")); + let foreground = image::Rgb([ + 128 + digest[0] / 2, + 128 + digest[1] / 2, + 128 + digest[2] / 2, + ]); + let mut pixels = + image::RgbImage::from_pixel(OUTPUT_SIDE, OUTPUT_SIDE, image::Rgb([35, 35, 37])); + for y in 0..5_u32 { + for x in 0..3_u32 { + if digest[(3 + y * 3 + x) as usize] & 1 == 0 { + continue; + } + for mirrored in [x, 4 - x] { + for py in 28 + y * 40..28 + (y + 1) * 40 { + for px in 28 + mirrored * 40..28 + (mirrored + 1) * 40 { + pixels.put_pixel(px, py, foreground); + } + } + } + } + } + let mut out = Vec::new(); + PngEncoder::new(&mut out) + .write_image( + pixels.as_raw(), + OUTPUT_SIDE, + OUTPUT_SIDE, + image::ExtendedColorType::Rgb8, + ) + .map_err(|_| AvatarError::Invalid)?; + Ok(out) +} + +/// Versioned original pixel monsters. Keep v1 rendering unchanged for immutable cached URLs. +pub fn default_v2_png(user_id: &str) -> Result, AvatarError> { + let pixels = monsters::render(user_id); + let mut output = Vec::new(); + PngEncoder::new(&mut output) + .write_image( + pixels.as_raw(), + OUTPUT_SIDE, + OUTPUT_SIDE, + image::ExtendedColorType::Rgb8, + ) + .map_err(|_| AvatarError::Invalid)?; + Ok(output) } pub fn url_path(id: &str) -> String { format!("/v1/avatars/{id}") } + +#[cfg(test)] +mod generated_v2_tests { + use super::*; + #[test] + fn monsters_are_stable_bounded_distinct_and_preserve_the_old_renderer() { + let id = "11111111-1111-4111-8111-111111111111"; + let png = default_v2_png(id).unwrap(); + assert_eq!(png, default_v2_png(id).unwrap()); + assert_ne!(png, default_png(id).unwrap()); + let image = image::load_from_memory(&png).unwrap(); + assert_eq!((image.width(), image.height()), (256, 256)); + assert!(png.len() < 16 * 1024); + let variants: std::collections::HashSet<_> = (1..=64) + .map(|index| { + content_id( + &default_v2_png(&format!("00000000-0000-4000-8000-{index:012x}")).unwrap(), + ) + }) + .collect(); + assert_eq!(variants.len(), 64); + } +} diff --git a/hub/src/avatar/monsters.rs b/hub/src/avatar/monsters.rs new file mode 100644 index 0000000..c4441e1 --- /dev/null +++ b/hub/src/avatar/monsters.rs @@ -0,0 +1,157 @@ +//! Original procedural pixel characters; no external artwork or account lookup. +use image::{Rgb, RgbImage}; +use sha2::{Digest, Sha256}; + +const SIZE: u32 = 32; +const INK: [u8; 3] = [27, 31, 43]; +const WHITE: [u8; 3] = [255, 249, 237]; +const PALETTES: [([u8; 3], [u8; 3]); 10] = [ + ([29, 38, 49], [111, 213, 211]), + ([40, 31, 49], [201, 154, 241]), + ([33, 41, 36], [165, 218, 128]), + ([43, 34, 32], [246, 179, 113]), + ([36, 34, 49], [143, 180, 245]), + ([45, 31, 40], [240, 150, 187]), + ([38, 39, 29], [230, 216, 122]), + ([28, 42, 43], [123, 218, 177]), + ([43, 31, 34], [239, 139, 122]), + ([30, 35, 48], [176, 182, 242]), +]; + +fn rect(image: &mut RgbImage, x: i32, y: i32, w: i32, h: i32, color: [u8; 3]) { + for py in y.max(0)..(y + h).min(SIZE as i32) { + for px in x.max(0)..(x + w).min(SIZE as i32) { + image.put_pixel(px as u32, py as u32, Rgb(color)); + } + } +} +fn shade(color: [u8; 3], factor: u16) -> [u8; 3] { + color.map(|channel| (u16::from(channel) * factor / 100).min(255) as u8) +} +fn eye(image: &mut RgbImage, x: i32, y: i32, size: i32, gaze: i32) { + rect(image, x - 1, y, size + 2, size, INK); + rect(image, x, y - 1, size, size + 2, INK); + rect(image, x, y, size, size, WHITE); + let pupil = if size >= 7 { 3 } else { 2 }; + let center = (size - pupil) / 2; + rect( + image, + x + (center + gaze).clamp(0, size - pupil), + y + center, + pupil, + pupil + 1, + INK, + ); +} + +/// 32 logical pixels enlarged with nearest-neighbor so small avatars retain crisp features. +pub(super) fn render(user_id: &str) -> RgbImage { + let seed = Sha256::digest(format!("vnox-pixel-monster-v2:{user_id}")); + let (background, body) = PALETTES[seed[0] as usize % PALETTES.len()]; + let dark = shade(body, 69); + let light = shade(body, 115); + let mut image = RgbImage::from_pixel(SIZE, SIZE, Rgb(background)); + // A quiet backdrop and ground shadow keep the silhouette legible inside a round crop. + rect(&mut image, 7, 6, 18, 20, shade(background, 120)); + rect(&mut image, 9, 28, 14, 1, shade(background, 65)); + let half = 7 + i32::from(seed[1] % 3); + let left = 16 - half; + let top = 10 + i32::from(seed[2] % 2); + let bottom = 25; + // Feet, hands, then the body: overlapping pixels create simple original silhouettes. + let foot = i32::from(seed[3] % 2); + rect(&mut image, 10 - foot, 24, 4, 4, dark); + rect(&mut image, 18 + foot, 24, 4, 4, dark); + rect( + &mut image, + left - 3, + 18, + 4, + 4 + i32::from(seed[4] % 2), + dark, + ); + rect( + &mut image, + 16 + half - 1, + 18, + 4, + 4 + i32::from(seed[4] % 2), + dark, + ); + for y in top..bottom { + let cut = if y == top { + 3 + } else if y == top + 1 || y == bottom - 1 { + 1 + } else { + 0 + }; + rect(&mut image, left + cut, y, half * 2 - cut * 2, 1, body); + if y > top + 2 && y < bottom - 1 { + rect(&mut image, 16 + half - 2, y, 2, 1, dark); + } + } + match seed[5] % 4 { + 0 => { + // Two soft horns. + for x in [left + 2, 16 + half - 5] { + rect(&mut image, x, top - 3, 3, 5, dark); + rect(&mut image, x + 1, top - 4, 2, 3, WHITE); + } + } + 1 => { + // A single offset antenna. + let x = 13 + i32::from(seed[6] % 5); + rect(&mut image, x, top - 4, 2, 5, dark); + rect(&mut image, x - 1, top - 6, 4, 3, light); + } + 2 => { + // Broad folded ears. + rect(&mut image, left - 2, top, 4, 6, dark); + rect(&mut image, 16 + half - 2, top, 4, 6, dark); + rect(&mut image, left - 1, top + 1, 2, 3, light); + rect(&mut image, 16 + half - 1, top + 1, 2, 3, light); + } + _ => { + // A three-pixel tuft. + for (x, h) in [(13, 3), (16, 5), (19, 2)] { + rect(&mut image, x, top - h, 2, h + 1, dark); + } + } + } + let gaze = i32::from(seed[7] % 3) - 1; + if seed[8] % 4 == 0 { + eye(&mut image, 12, 13, 8, gaze); + } else { + eye(&mut image, 10, 14, 5, gaze); + eye(&mut image, 18, 14, 5, gaze); + if seed[9] & 1 == 0 { + rect(&mut image, 10, 12, 4, 1, dark); + rect(&mut image, 19, 12, 4, 1, dark); + } + } + rect(&mut image, 9, 21, 2, 1, light); + rect(&mut image, 23, 21, 2, 1, light); + match seed[10] % 3 { + 0 => { + rect(&mut image, 13, 22, 7, 2, INK); + rect(&mut image, 14, 24, 5, 1, INK); + rect(&mut image, 14, 22, 2, 1, WHITE); + } + 1 => { + rect(&mut image, 14, 22, 5, 2, INK); + rect(&mut image, 16, 23, 2, 1, [244, 130, 154]); + } + _ => { + rect(&mut image, 13, 22, 2, 1, INK); + rect(&mut image, 18, 22, 2, 1, INK); + rect(&mut image, 15, 23, 3, 1, INK); + } + } + image::imageops::resize( + &image, + super::OUTPUT_SIDE, + super::OUTPUT_SIDE, + image::imageops::FilterType::Nearest, + ) +} diff --git a/hub/src/identity.rs b/hub/src/identity.rs index aff2939..b53fa0b 100644 --- a/hub/src/identity.rs +++ b/hub/src/identity.rs @@ -1,7 +1,7 @@ use std::{fs::OpenOptions, io::Write, path::Path}; use anyhow::{Context, Result, bail}; -use ed25519_dalek::SigningKey; +use ed25519_dalek::{Signer, SigningKey}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -16,11 +16,19 @@ struct StoredIdentity { /// Stable issuer identity. Only its public projection is exposed through HTTP. pub struct Issuer { + signing_key: SigningKey, pub id: Uuid, pub public_key: String, } impl Issuer { + /// Sign an account admission claim with the stable private issuer key. + pub fn sign_account_claims(&self, claims: &[u8]) -> Vec { + let mut transcript = b"VNOX-CENTRAL-ACCOUNT-v1\0".to_vec(); + transcript.extend_from_slice(claims); + self.signing_key.sign(&transcript).to_bytes().to_vec() + } + pub fn load_or_create(directory: &Path) -> Result { let mut builder = std::fs::DirBuilder::new(); builder.recursive(true); @@ -66,6 +74,7 @@ impl Issuer { bail!("Hub issuer identity failed integrity validation"); } Ok(Self { + signing_key: key, id: stored.issuer_id, public_key: stored.public_key, }) diff --git a/hub/src/web.rs b/hub/src/web.rs index 295aafa..928ffc4 100644 --- a/hub/src/web.rs +++ b/hub/src/web.rs @@ -77,6 +77,8 @@ async fn info(State(state): State>) -> Json { "capabilities": { "email_auth": if state.auth.email_enabled() { "enabled" } else { "not_enabled" }, "github_auth": if state.auth.configured() { "enabled" } else { "not_enabled" }, + "account_profile": "enabled", + "community_directory": "enabled", "server_registry": "not_enabled", "friends": "not_enabled", "direct_messages": "not_enabled", diff --git a/hub/tests/auth.rs b/hub/tests/auth.rs index f91b2fd..8bb0c47 100644 --- a/hub/tests/auth.rs +++ b/hub/tests/auth.rs @@ -129,6 +129,23 @@ async fn login_browser_binding_replay_session_and_csrf() { response.headers()["location"], "https://central.loki-code.dev/account" ); + let persisted_cookie = response + .headers() + .get_all("set-cookie") + .iter() + .find(|v| v.to_str().unwrap().starts_with("__Host-vnox_session=")) + .unwrap() + .to_str() + .unwrap(); + for property in [ + "Secure", + "HttpOnly", + "SameSite=Lax", + "Path=/", + "Max-Age=604800", + ] { + assert!(persisted_cookie.contains(property), "{property}"); + } let session = response .headers() .get_all("set-cookie") diff --git a/hub/tests/avatar.rs b/hub/tests/avatar.rs index e40a83e..1e049c7 100644 --- a/hub/tests/avatar.rs +++ b/hub/tests/avatar.rs @@ -276,7 +276,10 @@ async fn assert_error(response: Response, status: StatusCode, code: &str) async fn upload_reencodes_to_a_square_png_and_serves_it_with_safe_headers() { let fx = fixture().await; let alice = fx.email_user("alice@example.com").await; - assert_eq!(fx.session_avatar(&alice).await, serde_json::Value::Null); + assert_eq!( + fx.session_avatar(&alice).await, + vnox_hub::avatar::default_url(&alice.user.user_id) + ); let response = fx.upload(&alice, "image/png", png(600, 300)).await; assert_eq!(response.status(), StatusCode::OK); @@ -325,7 +328,9 @@ async fn jpeg_and_webp_are_accepted_and_exif_and_png_text_chunks_are_removed() { .as_str() .unwrap() .to_owned(); - let body = bytes(fx.get(&url).await).await; + let body = to_bytes(fx.get(&url).await.into_body(), usize::MAX) + .await + .unwrap(); assert!(!contains(&body, b"SECRET")); assert!(!contains(&body, b"Exif")); assert_eq!(chunk_names(&body), ["IHDR", "IDAT", "IEND"]); @@ -339,7 +344,9 @@ async fn jpeg_and_webp_are_accepted_and_exif_and_png_text_chunks_are_removed() { .as_str() .unwrap() .to_owned(); - let body = bytes(fx.get(&url).await).await; + let body = to_bytes(fx.get(&url).await.into_body(), usize::MAX) + .await + .unwrap(); assert!(!contains(&body, b"SECRET")); assert_eq!(chunk_names(&body), ["IHDR", "IDAT", "IEND"]); @@ -453,7 +460,10 @@ async fn another_account_cannot_change_or_remove_my_avatar() { assert_eq!(response.status(), StatusCode::FORBIDDEN); } assert_eq!(fx.session_avatar(&alice).await, alice_url); - assert_eq!(fx.session_avatar(&bob).await, serde_json::Value::Null); + assert_eq!( + fx.session_avatar(&bob).await, + vnox_hub::avatar::default_url(&bob.user.user_id) + ); // Bob acting as himself only affects Bob. assert_eq!( @@ -622,32 +632,42 @@ async fn a_new_upload_replaces_the_old_one() { } #[tokio::test] -async fn delete_returns_the_provider_avatar_or_the_default() { +async fn delete_restores_vnox_default_and_legacy_provider_images_are_never_exposed() { let fx = fixture().await; let github = fx.github_user().await; - assert_eq!(fx.session_avatar(&github).await, GITHUB_AVATAR); + let default = vnox_hub::avatar::default_url(&github.user.user_id); + assert_eq!(fx.session_avatar(&github).await, default); let url = json(fx.upload(&github, "image/png", png(64, 64)).await).await["avatar_url"] .as_str() .unwrap() .to_owned(); - assert_ne!(fx.session_avatar(&github).await, GITHUB_AVATAR); + assert_eq!(fx.session_avatar(&github).await, url); let response = fx.remove(&github).await; assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.headers()["cache-control"], "no-store"); - assert_eq!(json(response).await["avatar_url"], GITHUB_AVATAR); - assert_eq!(fx.session_avatar(&github).await, GITHUB_AVATAR); + assert_eq!(json(response).await["avatar_url"], default); + assert_eq!(fx.session_avatar(&github).await, default); assert_eq!(fx.get(&url).await.status(), StatusCode::NOT_FOUND); assert_eq!(fx.avatar_rows().await, 0); - // Removing again is harmless, and an email account falls back to the default (null). + // Removing again is harmless; email accounts also get a stable VNOX default. assert_eq!(fx.remove(&github).await.status(), StatusCode::OK); let alice = fx.email_user("alice@example.com").await; fx.upload(&alice, "image/png", png(64, 64)).await; let response = fx.remove(&alice).await; - assert_eq!(json(response).await["avatar_url"], serde_json::Value::Null); - assert_eq!(fx.session_avatar(&alice).await, serde_json::Value::Null); + let alice_default = vnox_hub::avatar::default_url(&alice.user.user_id); + assert_eq!(json(response).await["avatar_url"], alice_default); + assert_eq!(fx.session_avatar(&alice).await, alice_default); + // Simulate an old row that still carries a GitHub provider URL. + sqlx::query("UPDATE accounts SET avatar_url=? WHERE user_id=?") + .bind(GITHUB_AVATAR) + .bind(&github.user.user_id) + .execute(&fx.state.database) + .await + .unwrap(); + assert_eq!(fx.session_avatar(&github).await, default); } #[tokio::test] @@ -740,3 +760,37 @@ async fn deleting_an_account_removes_its_avatar() { .unwrap(); assert_eq!(fx.avatar_rows().await, 0); } + +#[tokio::test] +async fn generated_defaults_are_stable_pngs_without_account_enumeration() { + let fx = fixture().await; + let id = uuid::Uuid::new_v4().to_string(); + let url = vnox_hub::avatar::default_url(&id); + let response = fx.get(&url).await; + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.headers()["content-type"], "image/png"); + assert_eq!( + response.headers()["cache-control"], + "public, max-age=31536000, immutable" + ); + let first = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + assert_eq!(image::load_from_memory(&first).unwrap().width(), 256); + assert_eq!( + first, + to_bytes(fx.get(&url).await.into_body(), usize::MAX) + .await + .unwrap() + ); + let other = vnox_hub::avatar::default_url(&uuid::Uuid::new_v4().to_string()); + assert_ne!( + first, + to_bytes(fx.get(&other).await.into_body(), usize::MAX) + .await + .unwrap() + ); + assert_eq!( + fx.get("/v1/avatars/default/not-a-uuid").await.status(), + StatusCode::NOT_FOUND + ); + assert_eq!(fx.avatar_rows().await, 0); +} diff --git a/hub/tests/desktop_auth.rs b/hub/tests/desktop_auth.rs new file mode 100644 index 0000000..6f1f9bb --- /dev/null +++ b/hub/tests/desktop_auth.rs @@ -0,0 +1,614 @@ +use axum::{ + Router, + body::{Body, to_bytes}, + http::{Request, StatusCode}, +}; +use ed25519_dalek::{Signature, VerifyingKey}; +use serde_json::{Value, json}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{self, store}, + database, + identity::Issuer, + web::{self, HubState}, +}; +async fn setup() -> (tempfile::TempDir, Arc, Router, String, String) { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let pool = database::open(dir.path(), &issuer).await.unwrap(); + let auth = auth::AuthService::with_provider( + pool.clone(), + url::Url::parse("https://central.loki-code.dev").unwrap(), + None, + ); + let user = store::upsert_email_identity(&pool, "test@example.org") + .await + .unwrap(); + let (session, csrf) = store::create_session(&pool, &user.user_id).await.unwrap(); + let state = Arc::new(HubState { + database: pool, + auth: Arc::new(auth), + issuer, + smtp_configured: false, + started: std::time::Instant::now(), + }); + let app = web::router(state.clone()); + (dir, state, app, session, csrf) +} +async fn call( + app: &Router, + path: &str, + body: Value, + session: Option<(&str, &str)>, +) -> (StatusCode, Value) { + let mut b = Request::builder() + .method("POST") + .uri(path) + .header("content-type", "application/json"); + if let Some((session, csrf)) = session { + b = b + .header("origin", "https://central.loki-code.dev") + .header("cookie", format!("__Host-vnox_session={session}")) + .header("x-csrf-token", csrf); + } + let response = app + .clone() + .oneshot(b.body(Body::from(body.to_string())).unwrap()) + .await + .unwrap(); + let status = response.status(); + let bytes = to_bytes(response.into_body(), 65536).await.unwrap(); + ( + status, + serde_json::from_slice(&bytes).unwrap_or(Value::Null), + ) +} +async fn start(app: &Router) -> (String, Value) { + let key = ed25519_dalek::SigningKey::generate(&mut rand::rng()); + let public = hex::encode(key.verifying_key().to_bytes()); + let (status, request) = call( + app, + "/v1/auth/desktop/start", + json!({"public_key":public}), + None, + ) + .await; + assert_eq!(status, StatusCode::OK); + (public, request) +} +#[tokio::test] +async fn browser_approval_proof_binding_single_use_and_revocation() { + let (_dir, state, app, session, csrf) = setup().await; + let (public, request) = start(&app).await; + let code = request["user_code"].as_str().unwrap(); + let secret = request["device_secret"].as_str().unwrap(); + assert!( + !request["verification_uri"] + .as_str() + .unwrap() + .contains(secret) + ); + assert_eq!( + call( + &app, + "/v1/auth/desktop/poll", + json!({"device_secret":secret}), + None + ) + .await + .0, + StatusCode::ACCEPTED + ); + assert_eq!( + call(&app, "/v1/auth/desktop/approve", json!({"code":code}), None) + .await + .0, + StatusCode::FORBIDDEN + ); + assert_eq!( + call( + &app, + "/v1/auth/desktop/approve", + json!({"code":code}), + Some((&session, "wrong")) + ) + .await + .0, + StatusCode::FORBIDDEN + ); + assert_eq!( + call( + &app, + "/v1/auth/desktop/approve", + json!({"code":code}), + Some((&session, &csrf)) + ) + .await + .0, + StatusCode::OK + ); + let (status, approved) = call( + &app, + "/v1/auth/desktop/poll", + json!({"device_secret":secret}), + None, + ) + .await; + assert_eq!(status, StatusCode::OK); + let claims = hex::decode(approved["account_proof"]["claims"].as_str().unwrap()).unwrap(); + let claim: Value = serde_json::from_slice(&claims).unwrap(); + assert_eq!(claim["public_key"], public); + assert_eq!(claim["subject"], approved["user"]["user_id"]); + assert_eq!( + claim["expires_at"].as_i64().unwrap() - claim["issued_at"].as_i64().unwrap(), + 600 + ); + let key = VerifyingKey::from_bytes( + &hex::decode(&state.issuer.public_key) + .unwrap() + .try_into() + .unwrap(), + ) + .unwrap(); + let sig = Signature::from_slice( + &hex::decode(approved["account_proof"]["signature"].as_str().unwrap()).unwrap(), + ) + .unwrap(); + let mut transcript = b"VNOX-CENTRAL-ACCOUNT-v1\0".to_vec(); + transcript.extend_from_slice(&claims); + key.verify_strict(&transcript, &sig).unwrap(); + assert_eq!( + call( + &app, + "/v1/auth/desktop/poll", + json!({"device_secret":secret}), + None + ) + .await + .0, + StatusCode::GONE + ); + let refresh = approved["refresh_secret"].as_str().unwrap(); + let body = json!({"refresh_secret":refresh}); + assert_eq!( + call(&app, "/v1/auth/desktop/refresh", body.clone(), None) + .await + .0, + StatusCode::OK + ); + let stored: String = sqlx::query_scalar("SELECT refresh_hash FROM desktop_devices") + .fetch_one(&state.database) + .await + .unwrap(); + assert_eq!(stored, store::hash(refresh)); + assert_ne!(stored, refresh); + assert_eq!( + call(&app, "/v1/auth/desktop/revoke", body.clone(), None) + .await + .0, + StatusCode::NO_CONTENT + ); + assert_eq!( + call(&app, "/v1/auth/desktop/refresh", body, None).await.0, + StatusCode::UNAUTHORIZED + ); +} +#[tokio::test] +async fn canceled_expired_and_invalid_requests_fail_closed() { + let (_dir, state, app, session, csrf) = setup().await; + for expire in [false, true] { + let (_, request) = start(&app).await; + let secret = request["device_secret"].as_str().unwrap(); + let code = request["user_code"].as_str().unwrap(); + if expire { + sqlx::query("UPDATE desktop_authorizations SET expires_at=0") + .execute(&state.database) + .await + .unwrap(); + } else { + assert_eq!( + call( + &app, + "/v1/auth/desktop/cancel", + json!({"device_secret":secret}), + None + ) + .await + .0, + StatusCode::NO_CONTENT + ); + } + assert_eq!( + call( + &app, + "/v1/auth/desktop/approve", + json!({"code":code}), + Some((&session, &csrf)) + ) + .await + .0, + StatusCode::GONE + ); + assert_eq!( + call( + &app, + "/v1/auth/desktop/poll", + json!({"device_secret":secret}), + None + ) + .await + .0, + StatusCode::GONE + ); + } + assert_eq!( + call( + &app, + "/v1/auth/desktop/start", + json!({"public_key":"bad"}), + None + ) + .await + .0, + StatusCode::BAD_REQUEST + ); +} + +#[tokio::test] +async fn cancel_also_revokes_an_enrollment_whose_poll_response_was_lost() { + let (_dir, _state, app, session, csrf) = setup().await; + let (_, request) = start(&app).await; + let code = request["user_code"].as_str().unwrap(); + let device = json!({"device_secret":request["device_secret"]}); + assert_eq!( + call( + &app, + "/v1/auth/desktop/approve", + json!({"code":code}), + Some((&session, &csrf)) + ) + .await + .0, + StatusCode::OK + ); + let (status, approved) = call(&app, "/v1/auth/desktop/poll", device.clone(), None).await; + assert_eq!(status, StatusCode::OK); + assert_eq!( + call(&app, "/v1/auth/desktop/cancel", device, None).await.0, + StatusCode::NO_CONTENT + ); + let refresh = json!({"refresh_secret":approved["refresh_secret"]}); + assert_eq!( + call(&app, "/v1/auth/desktop/refresh", refresh, None) + .await + .0, + StatusCode::UNAUTHORIZED + ); +} + +async fn enroll( + app: &Router, + session: &str, + csrf: &str, +) -> (ed25519_dalek::SigningKey, String, Value) { + let key = ed25519_dalek::SigningKey::generate(&mut rand::rng()); + let (_, request) = call( + app, + "/v1/auth/desktop/start", + json!({"public_key":hex::encode(key.verifying_key().to_bytes())}), + None, + ) + .await; + assert_eq!( + call( + app, + "/v1/auth/desktop/approve", + json!({"code":request["user_code"]}), + Some((session, csrf)) + ) + .await + .0, + StatusCode::OK + ); + let (status, approved) = call( + app, + "/v1/auth/desktop/poll", + json!({"device_secret":request["device_secret"]}), + None, + ) + .await; + assert_eq!(status, StatusCode::OK); + ( + key, + approved["refresh_secret"].as_str().unwrap().to_owned(), + approved["user"].clone(), + ) +} +fn test_png() -> Vec { + let picture = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel( + 64, + 64, + image::Rgba([20, 100, 220, 255]), + )); + let mut bytes = std::io::Cursor::new(Vec::new()); + picture + .write_to(&mut bytes, image::ImageFormat::Png) + .unwrap(); + bytes.into_inner() +} +async fn mutate( + app: &Router, + method: &str, + path: &str, + key: &ed25519_dalek::SigningKey, + secret: &str, + bytes: Vec, + timestamp: i64, +) -> (StatusCode, Value) { + use ed25519_dalek::Signer; + use sha2::{Digest, Sha256}; + let timestamp = timestamp.to_string(); + let mut signed = b"VNOX-CENTRAL-PROFILE-v1\0".to_vec(); + signed.extend_from_slice(method.as_bytes()); + signed.push(0); + signed.extend_from_slice(path.as_bytes()); + signed.push(0); + signed.extend_from_slice(timestamp.as_bytes()); + signed.push(0); + signed.extend_from_slice(&Sha256::digest(&bytes)); + let request = Request::builder() + .method(method) + .uri(path) + .header("authorization", format!("Bearer {secret}")) + .header( + "x-vnox-public-key", + hex::encode(key.verifying_key().to_bytes()), + ) + .header("x-vnox-timestamp", timestamp) + .header( + "x-vnox-signature", + hex::encode(key.sign(&signed).to_bytes()), + ) + .header( + "content-type", + if path.ends_with("profile") { + "application/json" + } else { + "image/png" + }, + ) + .body(Body::from(bytes)) + .unwrap(); + let response = app.clone().oneshot(request).await.unwrap(); + let status = response.status(); + let bytes = to_bytes(response.into_body(), 65536).await.unwrap(); + ( + status, + serde_json::from_slice(&bytes).unwrap_or(Value::Null), + ) +} +#[tokio::test] +async fn desktop_avatar_upload_delete_and_website_refresh_share_one_account() { + let (_dir, state, app, session, csrf) = setup().await; + let (key, secret, user) = enroll(&app, &session, &csrf).await; + let user_id = user["user_id"].as_str().unwrap(); + let website_url = vnox_hub::avatar::replace(&state.database, user_id, &test_png()) + .await + .unwrap(); + let (status, fresh) = call( + &app, + "/v1/auth/desktop/refresh", + json!({"refresh_secret":secret}), + None, + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(fresh["user"]["avatar_url"], website_url); + let claims: Value = serde_json::from_slice( + &hex::decode(fresh["account_proof"]["claims"].as_str().unwrap()).unwrap(), + ) + .unwrap(); + assert_eq!(claims["avatar_url"], website_url); + let (status, uploaded) = mutate( + &app, + "POST", + "/v1/auth/desktop/avatar", + &key, + &secret, + test_png(), + store::now(), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(uploaded["user"]["user_id"], user_id); + let url = uploaded["user"]["avatar_url"].as_str().unwrap(); + assert_ne!( + url, website_url, + "desktop normalizes source to a fresh stored 256px PNG" + ); + let response = app + .clone() + .oneshot(Request::builder().uri(url).body(Body::empty()).unwrap()) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let bytes = to_bytes(response.into_body(), 1024 * 1024).await.unwrap(); + let image = image::load_from_memory(&bytes).unwrap(); + assert_eq!((image.width(), image.height()), (256, 256)); + let (status, deleted) = mutate( + &app, + "DELETE", + "/v1/auth/desktop/avatar", + &key, + &secret, + Vec::new(), + store::now(), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!( + deleted["user"]["avatar_url"], + format!("/v1/avatars/default-v2/{user_id}") + ); +} +#[tokio::test] +async fn desktop_profile_name_changes_without_handle_or_identity_reassignment() { + let (_dir, _state, app, session, csrf) = setup().await; + let (key, secret, user) = enroll(&app, &session, &csrf).await; + let (status, updated) = mutate( + &app, + "POST", + "/v1/auth/desktop/profile", + &key, + &secret, + br#"{"display_name":" New Name "}"#.to_vec(), + store::now(), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(updated["user"]["display_name"], "New Name"); + assert_eq!(updated["user"]["handle"], user["handle"]); + assert_eq!(updated["user"]["user_id"], user["user_id"]); + let (_, fresh) = call( + &app, + "/v1/auth/desktop/refresh", + json!({"refresh_secret":secret}), + None, + ) + .await; + let claim: Value = serde_json::from_slice( + &hex::decode(fresh["account_proof"]["claims"].as_str().unwrap()).unwrap(), + ) + .unwrap(); + assert_eq!(claim["display_name"], "New Name"); + for name in ["", "\nBad", "Bad\u{202e}Name"] { + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/profile", + &key, + &secret, + serde_json::to_vec(&json!({"display_name":name})).unwrap(), + store::now() + ) + .await + .0, + StatusCode::UNPROCESSABLE_ENTITY + ); + } + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/profile", + &key, + &secret, + serde_json::to_vec(&json!({"display_name":"x".repeat(65)})).unwrap(), + store::now() + ) + .await + .0, + StatusCode::UNPROCESSABLE_ENTITY + ); +} +#[tokio::test] +async fn desktop_avatar_requires_live_secret_device_signature_and_bounded_valid_png() { + let (_dir, state, app, session, csrf) = setup().await; + let (key, secret, _) = enroll(&app, &session, &csrf).await; + let other = ed25519_dalek::SigningKey::generate(&mut rand::rng()); + for (signer, secret, time) in [ + (&other, secret.as_str(), store::now()), + ( + &key, + "0000000000000000000000000000000000000000000000000000000000000000", + store::now(), + ), + (&key, secret.as_str(), store::now() - 61), + ] { + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/avatar", + signer, + secret, + test_png(), + time + ) + .await + .0, + StatusCode::UNAUTHORIZED + ); + } + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/avatar", + &key, + &secret, + b"not png".to_vec(), + store::now() + ) + .await + .0, + StatusCode::UNSUPPORTED_MEDIA_TYPE + ); + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/avatar", + &key, + &secret, + vec![0; 2 * 1024 * 1024 + 1], + store::now() + ) + .await + .0, + StatusCode::PAYLOAD_TOO_LARGE + ); + sqlx::query("UPDATE desktop_devices SET expires_at=0") + .execute(&state.database) + .await + .unwrap(); + assert_eq!( + mutate( + &app, + "DELETE", + "/v1/auth/desktop/avatar", + &key, + &secret, + Vec::new(), + store::now() + ) + .await + .0, + StatusCode::UNAUTHORIZED + ); + sqlx::query("DELETE FROM desktop_devices") + .execute(&state.database) + .await + .unwrap(); + assert_eq!( + mutate( + &app, + "POST", + "/v1/auth/desktop/profile", + &key, + &secret, + br#"{"display_name":"Changed"}"#.to_vec(), + store::now() + ) + .await + .0, + StatusCode::UNAUTHORIZED + ); +} diff --git a/hub/tests/directory.rs b/hub/tests/directory.rs new file mode 100644 index 0000000..cade297 --- /dev/null +++ b/hub/tests/directory.rs @@ -0,0 +1,230 @@ +use axum::{ + Router, + body::{Body, to_bytes}, + http::{Request, StatusCode}, +}; +use serde_json::{Value, json}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{self, store}, + database, + identity::Issuer, + web::{self, HubState}, +}; +const ORIGIN: &str = "https://central.loki-code.dev"; +struct Fixture { + _dir: tempfile::TempDir, + state: Arc, + app: Router, +} +struct Session { + user: store::User, + token: String, + csrf: String, +} +async fn fixture() -> Fixture { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let pool = database::open(dir.path(), &issuer).await.unwrap(); + let auth = auth::AuthService::with_provider(pool.clone(), ORIGIN.parse().unwrap(), None); + let state = Arc::new(HubState { + database: pool, + auth: Arc::new(auth), + issuer, + smtp_configured: false, + started: std::time::Instant::now(), + }); + Fixture { + _dir: dir, + app: web::router(state.clone()), + state, + } +} +impl Fixture { + async fn login(&self, email: &str) -> Session { + let user = store::upsert_email_identity(&self.state.database, email) + .await + .unwrap(); + let (token, csrf) = store::create_session(&self.state.database, &user.user_id) + .await + .unwrap(); + Session { user, token, csrf } + } + fn request(&self, s: &Session, body: String) -> Request { + Request::builder() + .method("POST") + .uri("/v1/servers") + .header("origin", ORIGIN) + .header("content-type", "application/json") + .header("cookie", format!("__Host-vnox_session={}", s.token)) + .header("x-csrf-token", &s.csrf) + .body(Body::from(body)) + .unwrap() + } + async fn send(&self, r: Request) -> (StatusCode, Value) { + let r = self.app.clone().oneshot(r).await.unwrap(); + assert_eq!(r.headers()["cache-control"], "no-store"); + let status = r.status(); + ( + status, + serde_json::from_slice(&to_bytes(r.into_body(), 1048576).await.unwrap()).unwrap(), + ) + } +} +#[tokio::test] +async fn directory_auth_ownership_normalization_and_persistence() { + let f = fixture().await; + let owner = f.login("owner@example.org").await; + let other = f.login("other@example.org").await; + let body = + json!({"name":" Voice ","address":"Voice.Example.COM.:07600","description":" public "}) + .to_string(); + let mut unauth = f.request(&owner, body.clone()); + unauth.headers_mut().remove("cookie"); + assert_eq!(f.send(unauth).await.0, StatusCode::UNAUTHORIZED); + for header in ["origin", "x-csrf-token"] { + let mut req = f.request(&owner, body.clone()); + req.headers_mut().remove(header); + assert_eq!(f.send(req).await.0, StatusCode::FORBIDDEN); + } + let (status, result) = f.send(f.request(&owner, body.clone())).await; + assert_eq!(status, StatusCode::CREATED); + assert_eq!(result["server"]["address"], "voice.example.com:7600"); + assert_eq!(result["server"]["owner_id"], owner.user.user_id); + assert_eq!( + f.send(f.request(&owner, body)).await.0, + StatusCode::CONFLICT + ); + let id = result["server"]["id"].as_str().unwrap(); + let mut remove = f.request(&other, String::new()); + *remove.method_mut() = axum::http::Method::DELETE; + *remove.uri_mut() = format!("/v1/servers/{id}").parse().unwrap(); + assert_eq!(f.send(remove).await.0, StatusCode::NOT_FOUND); + let pool = database::open(f._dir.path(), &f.state.issuer) + .await + .unwrap(); + let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM community_servers") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(count, 1); + let (_, page) = f + .send( + Request::builder() + .uri("/v1/servers") + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(page["servers"].as_array().unwrap().len(), 1); + sqlx::query("DELETE FROM accounts WHERE user_id=?") + .bind(owner.user.user_id) + .execute(&pool) + .await + .unwrap(); + let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM community_servers") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(count, 0); +} +#[tokio::test] +async fn directory_rejects_bad_json_oversize_and_bounds() { + let f = fixture().await; + let s = f.login("bounds@example.org").await; + assert_eq!( + f.send(f.request(&s, "{".into())).await.0, + StatusCode::BAD_REQUEST + ); + assert_eq!( + f.send(f.request(&s, "x".repeat(4097))).await.0, + StatusCode::PAYLOAD_TOO_LARGE + ); + for body in [ + json!({"name":"","address":"voice.example.com:7600","description":""}), + json!({"name":"voice","address":"127.0.0.1:7600","description":""}), + json!({"name":"voice","address":"voice.example.com:7600","description":"x".repeat(501)}), + ] { + assert_eq!( + f.send(f.request(&s, body.to_string())).await.0, + StatusCode::UNPROCESSABLE_ENTITY + ); + } +} +#[tokio::test] +async fn directory_owner_can_remove_and_rate_budget_is_independent() { + let f = fixture().await; + let s = f.login("delete@example.org").await; + let (_, result) = f + .send(f.request( + &s, + json!({"name":"Voice","address":"voice.example.org:7600","description":""}).to_string(), + )) + .await; + let mut req = f.request(&s, String::new()); + *req.method_mut() = axum::http::Method::DELETE; + *req.uri_mut() = format!("/v1/servers/{}", result["server"]["id"].as_str().unwrap()) + .parse() + .unwrap(); + let response = f.app.clone().oneshot(req).await.unwrap(); + assert_eq!(response.status(), StatusCode::NO_CONTENT); + for _ in 0..8 { + assert!(f.state.auth.allow_directory(&s.user.user_id, "local")); + } + assert!(!f.state.auth.allow_directory(&s.user.user_id, "local")); + assert!(f.state.auth.allow_avatar(&s.user.user_id, "local")); +} +#[tokio::test] +async fn directory_account_cap_and_pagination_are_bounded() { + let f = fixture().await; + let s = f.login("cap@example.org").await; + for i in 0..105 { + sqlx::query("INSERT INTO community_servers(id,name,address,description,owner_id,created_at) VALUES (?,?,?,?,?,?)") + .bind(format!("00000000-0000-4000-8000-{i:012}" )).bind("voice").bind(format!("voice{i}.example.org:7600")).bind("").bind(&s.user.user_id).bind(1_i64).execute(&f.state.database).await.unwrap(); + } + let (status, page) = f + .send( + Request::builder() + .uri("/v1/servers") + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(page["servers"].as_array().unwrap().len(), 100); + let last = page["servers"][99]["id"].as_str().unwrap(); + let (_, next) = f + .send( + Request::builder() + .uri(format!("/v1/servers?after={last}")) + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(next["servers"].as_array().unwrap().len(), 5); + assert_eq!( + f.send(f.request( + &s, + json!({"name":"voice","address":"new.example.org:7600","description":""}).to_string() + )) + .await + .1["error"], + "listing_limit" + ); +} + +#[tokio::test] +async fn directory_reads_do_not_consume_login_budget() { + let f = fixture().await; + for _ in 0..20 { + assert!(f.state.auth.allow_directory_read("source")); + } + assert!(!f.state.auth.allow_directory_read("source")); + assert!(f.state.auth.allow("source")); +} diff --git a/hub/tests/email_auth.rs b/hub/tests/email_auth.rs index 4020eca..3fa7151 100644 --- a/hub/tests/email_auth.rs +++ b/hub/tests/email_auth.rs @@ -288,7 +288,12 @@ async fn full_flow_creates_neutral_account_and_session() { } let json: Value = serde_json::from_str(&body).unwrap(); assert_eq!(json["authenticated"], true); - assert_eq!(json["user"]["display_name"], "Vnox user"); + let initial_name = json["user"]["display_name"].as_str().unwrap(); + let (word, digits) = initial_name.rsplit_once('_').unwrap(); + assert!(word.chars().all(|c| ('а'..='я').contains(&c) || c == 'ё')); + assert_eq!(digits.len(), 4); + assert!(digits.bytes().all(|c| c.is_ascii_digit())); + assert_eq!(json["onboarding_pending"], true); let handle = json["user"]["handle"].as_str().unwrap(); assert!(handle.starts_with("u_") && !handle.contains("alice")); assert!(!body.to_lowercase().contains("alice")); diff --git a/hub/tests/foundation.rs b/hub/tests/foundation.rs index c9bace7..3646142 100644 --- a/hub/tests/foundation.rs +++ b/hub/tests/foundation.rs @@ -184,6 +184,7 @@ async fn capabilities_and_routes_do_not_pretend_authentication_is_ready() { "marketplace", ] { assert_eq!(value["capabilities"][capability], "not_enabled"); + assert_eq!(value["capabilities"]["account_profile"], "enabled"); } assert_eq!(value["smtp"]["public_send_endpoint"], false); assert!(value.get("secret_key").is_none()); @@ -203,7 +204,10 @@ async fn capabilities_and_routes_do_not_pretend_authentication_is_ready() { .unwrap(); assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE); } - for path in ["/v1/servers/register", "/v1/mail/send"] { + for (path, expected) in [ + ("/v1/servers/register", StatusCode::METHOD_NOT_ALLOWED), + ("/v1/mail/send", StatusCode::NOT_FOUND), + ] { let response = app .clone() .oneshot( @@ -215,7 +219,7 @@ async fn capabilities_and_routes_do_not_pretend_authentication_is_ready() { ) .await .unwrap(); - assert_eq!(response.status(), StatusCode::NOT_FOUND); + assert_eq!(response.status(), expected); } } diff --git a/hub/tests/onboarding.rs b/hub/tests/onboarding.rs new file mode 100644 index 0000000..59b4209 --- /dev/null +++ b/hub/tests/onboarding.rs @@ -0,0 +1,265 @@ +//! Own-origin profile onboarding with isolated SQLite and synthetic email/GitHub identities. +use axum::{ + Router, + body::{Body, to_bytes}, + http::{Request, StatusCode}, +}; +use serde_json::{Value, json}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{self, provider::GitHubProfile, store}, + database, + identity::Issuer, + web::{self, HubState}, +}; +const ORIGIN: &str = "https://central.loki-code.dev"; +struct Fixture { + _dir: tempfile::TempDir, + state: Arc, + app: Router, +} +async fn fixture() -> Fixture { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let pool = database::open(dir.path(), &issuer).await.unwrap(); + let auth = auth::AuthService::with_provider(pool.clone(), ORIGIN.parse().unwrap(), None); + let state = Arc::new(HubState { + database: pool, + auth: Arc::new(auth), + issuer, + smtp_configured: false, + started: std::time::Instant::now(), + }); + Fixture { + _dir: dir, + app: web::router(state.clone()), + state, + } +} +async fn call( + f: &Fixture, + user: &str, + path: &str, + body: Value, + origin: &str, + csrf_ok: bool, +) -> (StatusCode, Value) { + let (token, csrf) = store::create_session(&f.state.database, user) + .await + .unwrap(); + let req = Request::builder() + .method("POST") + .uri(path) + .header("origin", origin) + .header("content-type", "application/json") + .header("cookie", format!("__Host-vnox_session={token}")) + .header("x-csrf-token", if csrf_ok { csrf } else { "bad".into() }) + .body(Body::from(body.to_string())) + .unwrap(); + let r = f.app.clone().oneshot(req).await.unwrap(); + let status = r.status(); + let bytes = to_bytes(r.into_body(), 2048).await.unwrap(); + (status, serde_json::from_slice(&bytes).unwrap()) +} +fn generated(name: &str) { + let (word, digits) = name.rsplit_once('_').unwrap(); + assert!(word.chars().all(|c| ('а'..='я').contains(&c) || c == 'ё')); + assert_eq!(digits.len(), 4); + assert!(digits.bytes().all(|c| c.is_ascii_digit())); +} +#[tokio::test] +async fn names_are_generated_once_for_both_providers_and_user_names_survive_login() { + let f = fixture().await; + let mail = store::upsert_email_identity(&f.state.database, "first@example.org") + .await + .unwrap(); + generated(&mail.display_name); + let profile = GitHubProfile { + id: 1234, + login: "provider_private_login".into(), + name: Some("Provider Name".into()), + avatar_url: None, + }; + let github = store::upsert_identity(&f.state.database, &profile) + .await + .unwrap(); + generated(&github.display_name); + assert_ne!(github.display_name, "Provider Name"); + assert!( + store::onboarding_pending(&f.state.database, &github.user_id) + .await + .unwrap() + ); + for (user, provider) in [(&mail, "email"), (&github, "github")] { + let before = user.handle.clone(); + let (status, body) = call( + &f, + &user.user_id, + "/v1/account/display-name", + json!({"display_name":" Моё имя "}), + ORIGIN, + true, + ) + .await; + assert_eq!(status, StatusCode::OK); + assert_eq!(body["display_name"], "Моё имя"); + assert!( + !store::onboarding_pending(&f.state.database, &user.user_id) + .await + .unwrap() + ); + let again = if provider == "email" { + store::upsert_email_identity(&f.state.database, "first@example.org") + .await + .unwrap() + } else { + store::upsert_identity(&f.state.database, &profile) + .await + .unwrap() + }; + assert_eq!(again.display_name, "Моё имя"); + assert_eq!(again.handle, before); + } +} +#[tokio::test] +async fn decision_is_persisted_idempotently_and_not_created_for_existing_accounts() { + let f = fixture().await; + let user = store::upsert_email_identity(&f.state.database, "new@example.org") + .await + .unwrap(); + assert!( + store::onboarding_pending(&f.state.database, &user.user_id) + .await + .unwrap() + ); + for decision in ["later", "rename"] { + assert_eq!( + call( + &f, + &user.user_id, + "/v1/account/onboarding", + json!({"decision":decision}), + ORIGIN, + true + ) + .await + .0, + StatusCode::OK + ); + } + let saved: String = + sqlx::query_scalar("SELECT decision FROM account_onboarding WHERE user_id=?") + .bind(&user.user_id) + .fetch_one(&f.state.database) + .await + .unwrap(); + assert_eq!(saved, "later"); + assert!( + !store::onboarding_pending(&f.state.database, &user.user_id) + .await + .unwrap() + ); + sqlx::query("DELETE FROM account_onboarding WHERE user_id=?") + .bind(&user.user_id) + .execute(&f.state.database) + .await + .unwrap(); + let again = store::upsert_email_identity(&f.state.database, "new@example.org") + .await + .unwrap(); + assert_eq!(again.display_name, user.display_name); + assert!( + !store::onboarding_pending(&f.state.database, &user.user_id) + .await + .unwrap() + ); +} +#[tokio::test] +async fn session_exposes_pending_and_actions_require_origin_csrf_and_valid_name() { + let f = fixture().await; + let user = store::upsert_email_identity(&f.state.database, "secure@example.org") + .await + .unwrap(); + let (token, _) = store::create_session(&f.state.database, &user.user_id) + .await + .unwrap(); + let r = f + .app + .clone() + .oneshot( + Request::builder() + .uri("/v1/auth/session") + .header("cookie", format!("__Host-vnox_session={token}")) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + let body: Value = + serde_json::from_slice(&to_bytes(r.into_body(), 4096).await.unwrap()).unwrap(); + assert_eq!(body["onboarding_pending"], true); + for path in ["/v1/account/onboarding", "/v1/account/display-name"] { + for (origin, csrf) in [("https://evil.example", true), (ORIGIN, false)] { + assert_eq!( + call( + &f, + &user.user_id, + path, + json!({"decision":"later"}), + origin, + csrf + ) + .await + .0, + StatusCode::FORBIDDEN + ); + } + } + for name in ["", "bad\nname", "\u{202e}evil"] { + assert_eq!( + call( + &f, + &user.user_id, + "/v1/account/display-name", + json!({"display_name":name}), + ORIGIN, + true + ) + .await + .0, + StatusCode::UNPROCESSABLE_ENTITY + ); + } + assert_eq!( + call( + &f, + &user.user_id, + "/v1/account/display-name", + json!({"display_name":"Name","handle":"override"}), + ORIGIN, + true + ) + .await + .0, + StatusCode::BAD_REQUEST + ); + assert_eq!( + call( + &f, + &user.user_id, + "/v1/account/onboarding", + json!({"decision":"bad"}), + ORIGIN, + true + ) + .await + .0, + StatusCode::BAD_REQUEST + ); +} diff --git a/hub/tests/profile.rs b/hub/tests/profile.rs new file mode 100644 index 0000000..f1c65fa --- /dev/null +++ b/hub/tests/profile.rs @@ -0,0 +1,412 @@ +use axum::{ + Router, + body::{Body, to_bytes}, + http::{Request, StatusCode}, +}; +use serde_json::{Value, json}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{self, store}, + database, + identity::Issuer, + web::{self, HubState}, +}; +const ORIGIN: &str = "https://central.loki-code.dev"; +struct Fixture { + _dir: tempfile::TempDir, + state: Arc, + app: Router, +} +struct Session { + user: store::User, + token: String, + csrf: String, +} +async fn fixture() -> Fixture { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let pool = database::open(dir.path(), &issuer).await.unwrap(); + let auth = auth::AuthService::with_provider(pool.clone(), ORIGIN.parse().unwrap(), None); + let state = Arc::new(HubState { + database: pool, + auth: Arc::new(auth), + issuer, + smtp_configured: false, + started: std::time::Instant::now(), + }); + Fixture { + _dir: dir, + app: web::router(state.clone()), + state, + } +} +impl Fixture { + async fn login(&self, email: &str) -> Session { + let user = store::upsert_email_identity(&self.state.database, email) + .await + .unwrap(); + let (token, csrf) = store::create_session(&self.state.database, &user.user_id) + .await + .unwrap(); + Session { user, token, csrf } + } + fn request(&self, s: &Session, body: String) -> Request { + Request::builder() + .method("PUT") + .uri("/v1/account/profile") + .header("origin", ORIGIN) + .header("content-type", "application/json") + .header("cookie", format!("__Host-vnox_session={}", s.token)) + .header("x-csrf-token", &s.csrf) + .body(Body::from(body)) + .unwrap() + } + async fn send(&self, r: Request) -> (StatusCode, Value) { + let r = self.app.clone().oneshot(r).await.unwrap(); + assert_eq!(r.headers()["cache-control"], "no-store"); + let status = r.status(); + ( + status, + serde_json::from_slice(&to_bytes(r.into_body(), 8192).await.unwrap()).unwrap(), + ) + } + async fn update(&self, s: &Session, handle: &str, name: &str) -> (StatusCode, Value) { + self.send(self.request(s, json!({"handle":handle,"display_name":name}).to_string())) + .await + } +} +#[tokio::test] +async fn selection_is_normalized_persisted_and_preserved_by_login() { + let f = fixture().await; + let s = f.login("person@example.org").await; + assert!(!s.user.profile_complete); + let (status, body) = f.update(&s, " Alice_123 ", " Алиса ").await; + assert_eq!(status, StatusCode::OK); + assert_eq!(body["user"]["handle"], "alice_123"); + assert_eq!(body["user"]["display_name"], "Алиса"); + assert_eq!(body["user"]["profile_complete"], true); + assert_eq!(body["user"]["user_id"], s.user.user_id); + let again = f.login("person@example.org").await; + assert_eq!(again.user.handle, "alice_123"); + assert_eq!(again.user.display_name, "Алиса"); + assert!(again.user.profile_complete); + let session = f + .send( + Request::builder() + .uri("/v1/auth/session") + .header("cookie", format!("__Host-vnox_session={}", s.token)) + .body(Body::empty()) + .unwrap(), + ) + .await; + assert_eq!(session.1["user"], body["user"]); + assert_eq!( + f.update(&s, "ALICE_123", "New name").await.0, + StatusCode::OK + ); + assert_eq!( + f.update(&again, "other_handle", "Changed").await.1["error"], + "handle_locked" + ); +} +#[tokio::test] +async fn case_insensitive_claims_are_atomic_and_leave_loser_unchanged() { + let f = fixture().await; + let a = f.login("a@example.org").await; + let b = f.login("b@example.org").await; + let (left, right) = tokio::join!( + f.update(&a, "Same_Name", "A"), + f.update(&b, "same_name", "B") + ); + assert_eq!( + [left.0, right.0] + .iter() + .filter(|s| **s == StatusCode::OK) + .count(), + 1 + ); + let (loser, conflict) = if left.0 == StatusCode::OK { + (&b, right) + } else { + (&a, left) + }; + assert_eq!(conflict.0, StatusCode::CONFLICT); + assert_eq!(conflict.1["error"], "handle_taken"); + let (user, _) = store::get_session(&f.state.database, &loser.token) + .await + .unwrap() + .unwrap(); + assert_eq!(user.handle, loser.user.handle); + assert_eq!(user.display_name, loser.user.display_name); + assert!(!user.profile_complete); +} +#[tokio::test] +async fn concurrent_sessions_cannot_choose_two_handles() { + let f = fixture().await; + let a = f.login("a@example.org").await; + let b = f.login("a@example.org").await; + let (left, right) = tokio::join!( + f.update(&a, "first_name", "A"), + f.update(&b, "second_name", "B") + ); + assert_eq!( + [left.0, right.0] + .iter() + .filter(|s| **s == StatusCode::OK) + .count(), + 1 + ); + assert_eq!( + if left.0 == StatusCode::OK { + right.1 + } else { + left.1 + }["error"], + "handle_locked" + ); +} +#[tokio::test] +async fn authorization_and_expiry_precede_body_parsing() { + let f = fixture().await; + let s = f.login("a@example.org").await; + for (header, error) in [ + ("origin", "invalid_origin"), + ("cookie", "not_authenticated"), + ("x-csrf-token", "invalid_csrf"), + ] { + let mut r = f.request(&s, "invalid JSON".into()); + r.headers_mut().remove(header); + assert_eq!(f.send(r).await.1["error"], error); + } + let mut r = f.request(&s, "x".repeat(2048)); + r.headers_mut() + .insert("origin", "https://wrong.example".parse().unwrap()); + assert_eq!(f.send(r).await.1["error"], "invalid_origin"); + sqlx::query("UPDATE web_sessions SET expires_at=0") + .execute(&f.state.database) + .await + .unwrap(); + assert_eq!( + f.update(&s, "valid_name", "Name").await.0, + StatusCode::UNAUTHORIZED + ); +} +#[tokio::test] +async fn validation_and_body_limits_leave_account_unchanged() { + let f = fixture().await; + for handle in [ + "ab", + "1alice", + "алиса", + "with-dash", + "with space", + "u_placeholder", + "ADMIN", + "root", + "everyone", + "abcdefghijklmnopqrstuvwxyz", + ] { + let s = f + .login(&format!("{}@example.org", uuid::Uuid::new_v4())) + .await; + assert_eq!( + f.update(&s, handle, "Name").await.1["error"], + "invalid_handle" + ); + assert!( + !store::get_session(&f.state.database, &s.token) + .await + .unwrap() + .unwrap() + .0 + .profile_complete + ); + } + let s = f.login("names@example.org").await; + for name in [ + "".to_owned(), + " ".into(), + "n\n".into(), + "x\u{202e}y".into(), + "x\u{2066}y".into(), + "я".repeat(65), + ] { + assert_eq!( + f.update(&s, "valid_name", &name).await.1["error"], + "invalid_display_name" + ); + } + for body in [ + "{}", + "{", + r#"{"handle":"valid_name","display_name":"Name","user_id":"other"}"#, + ] { + assert_eq!( + f.send(f.request(&s, body.into())).await.0, + StatusCode::BAD_REQUEST + ); + } + let s = f.login("large@example.org").await; + assert_eq!( + f.send(f.request(&s, "x".repeat(1025))).await.0, + StatusCode::PAYLOAD_TOO_LARGE + ); + assert_eq!( + f.update(&s, "valid_name", &"я".repeat(64)).await.0, + StatusCode::OK + ); +} +#[tokio::test] +async fn budgets_limit_accounts_and_sources_independently() { + let f = fixture().await; + let s = f.login("person@example.org").await; + for _ in 0..10 { + assert_eq!(f.update(&s, "person", "Person").await.0, StatusCode::OK); + } + assert_eq!( + f.update(&s, "person", "Person").await.0, + StatusCode::TOO_MANY_REQUESTS + ); + for i in 0..2 { + let s = f.login(&format!("p{i}@example.org")).await; + for _ in 0..10 { + assert_eq!( + f.update(&s, &format!("person{i}"), "Name").await.0, + StatusCode::OK + ); + } + } + let s = f.login("last@example.org").await; + assert_eq!( + f.update(&s, "last_person", "Name").await.0, + StatusCode::TOO_MANY_REQUESTS + ); + let mut r = f.request( + &s, + json!({"handle":"last_person","display_name":"Name"}).to_string(), + ); + r.headers_mut() + .insert("x-forwarded-for", "192.0.2.1".parse().unwrap()); + assert_eq!(f.send(r).await.0, StatusCode::OK); +} + +#[tokio::test] +async fn github_relogin_preserves_selected_profile_and_own_avatar() { + use vnox_hub::auth::provider::GitHubProfile; + let f = fixture().await; + let mut provider = GitHubProfile { + id: 123, + login: "original".into(), + name: Some("Provider name".into()), + avatar_url: Some("https://avatars.githubusercontent.com/u/123".into()), + }; + let user = store::upsert_identity(&f.state.database, &provider) + .await + .unwrap(); + let (token, csrf) = store::create_session(&f.state.database, &user.user_id) + .await + .unwrap(); + let s = Session { user, token, csrf }; + let png = image::RgbImage::new(256, 256); + let mut bytes = std::io::Cursor::new(Vec::new()); + png.write_to(&mut bytes, image::ImageFormat::Png).unwrap(); + let avatar = vnox_hub::avatar::replace(&f.state.database, &s.user.user_id, bytes.get_ref()) + .await + .unwrap(); + let (status, body) = f.update(&s, "my_handle", "My name").await; + assert_eq!(status, StatusCode::OK); + assert_eq!(body["user"]["avatar_url"], avatar); + provider.login = "renamed".into(); + provider.name = Some("New provider name".into()); + let user = store::upsert_identity(&f.state.database, &provider) + .await + .unwrap(); + assert_eq!(user.user_id, s.user.user_id); + assert_eq!(user.handle, "my_handle"); + assert_eq!(user.display_name, "My name"); + assert_eq!(user.avatar_url.as_deref(), Some(avatar.as_str())); + assert!(user.profile_complete); +} + +#[tokio::test] +async fn upgrading_legacy_database_preserves_accounts_bindings_sessions_and_avatars() { + let dir = tempfile::tempdir().unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + } + let issuer = Issuer::load_or_create(dir.path()).unwrap(); + let migrations = tempfile::tempdir().unwrap(); + let source = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("migrations"); + for name in [ + "0001_foundation.sql", + "0002_github_auth.sql", + "0003_email_auth.sql", + "0004_email_resend_chain.sql", + "0005_avatars.sql", + ] { + std::fs::copy(source.join(name), migrations.path().join(name)).unwrap(); + } + let options = sqlx::sqlite::SqliteConnectOptions::new() + .filename(dir.path().join("hub.sqlite3")) + .create_if_missing(true) + .foreign_keys(true); + let pool = sqlx::sqlite::SqlitePoolOptions::new() + .max_connections(1) + .connect_with(options) + .await + .unwrap(); + sqlx::migrate::Migrator::new(migrations.path()) + .await + .unwrap() + .run(&pool) + .await + .unwrap(); + sqlx::query("INSERT INTO accounts(user_id,handle,created_at,display_name) VALUES('legacy-id','u_legacy',1,'Legacy')").execute(&pool).await.unwrap(); + sqlx::query("INSERT INTO external_identities(provider,subject,user_id,linked_at) VALUES('email','legacy@example.org','legacy-id',1)").execute(&pool).await.unwrap(); + let (token, csrf) = store::create_session(&pool, "legacy-id").await.unwrap(); + let avatar = vnox_hub::avatar::replace(&pool, "legacy-id", b"stored legacy avatar") + .await + .unwrap(); + pool.close().await; + let upgraded = database::open(dir.path(), &issuer).await.unwrap(); + let (user, stored_csrf) = store::get_session(&upgraded, &token) + .await + .unwrap() + .unwrap(); + assert_eq!(stored_csrf, csrf); + assert_eq!(user.user_id, "legacy-id"); + assert_eq!(user.handle, "u_legacy"); + assert_eq!(user.display_name, "Legacy"); + assert!(!user.profile_complete); + assert_eq!(user.avatar_url.as_deref(), Some(avatar.as_str())); + assert_eq!( + store::upsert_email_identity(&upgraded, "legacy@example.org") + .await + .unwrap() + .user_id, + "legacy-id" + ); + // The new index also protects writes outside the HTTP handler. + assert!( + sqlx::query("INSERT INTO accounts(user_id,handle,created_at) VALUES('other','U_LEGACY',1)") + .execute(&upgraded) + .await + .is_err() + ); + upgraded.close().await; + let reopened = database::open(dir.path(), &issuer).await.unwrap(); + assert!( + store::get_session(&reopened, &token) + .await + .unwrap() + .is_some() + ); +} diff --git a/protocol/lnex.proto b/protocol/lnex.proto index 84fb355..53c7a1d 100644 --- a/protocol/lnex.proto +++ b/protocol/lnex.proto @@ -12,7 +12,7 @@ message Hello { reserved 5; reserved "server_eph_pubkey"; bool private_mode = 6; - // Ed25519 over "VNOX-LNEx-v3-server-hello\0" || tls_exporter || Hello(signature empty). + // Ed25519 over "VNOX-LNEx-v4-server-hello\0" || tls_exporter || Hello(signature empty). bytes server_signature = 7; } @@ -24,12 +24,20 @@ message Auth { bytes signature = 4; reserved 5; reserved "client_eph_pubkey"; + // Mandatory Central account proof, bound to client_pubkey (LNEx v4). + bytes account_proof = 6; } message Session { string session_id = 1; string token = 2; int64 expires_at = 3; + bool node_admin = 4; + // Live ProfileUpdate/ProfileUpdated support. Absent on legacy v4 gateways. + bool live_profiles = 5; + bool node_settings = 6; + bool channel_structure = 7; + bool forums = 8; } // ─── KEEPALIVE ────────────────────────────────────────────────────────────── @@ -54,12 +62,16 @@ message ChannelState { repeated MemberInfo members = 4; string voice_endpoint = 5; optional string guild_id = 6; + optional string parent_id = 7; + int32 position = 8; } message MemberInfo { string user_id = 1; string nickname = 2; bool in_voice = 3; + string account_id = 4; + string avatar_url = 5; } message ChannelCreate { @@ -67,6 +79,8 @@ message ChannelCreate { string channel_name = 2; string kind = 3; optional string guild_id = 4; + optional string parent_id = 5; + int32 position = 6; } message ChannelDelete { string channel_id = 1; } @@ -74,6 +88,8 @@ message ChannelDelete { string channel_id = 1; } message ChannelEdit { string channel_id = 1; string channel_name = 2; + optional string parent_id = 3; + optional int32 position = 4; } message ChannelList { @@ -86,9 +102,11 @@ message ChannelListItem { string channel_name = 2; string kind = 3; optional string guild_id = 4; + optional string parent_id = 5; + int32 position = 6; } -message UserJoin { string channel_id = 1; string user_id = 2; string nickname = 3; } +message UserJoin { string channel_id = 1; string user_id = 2; string nickname = 3; string account_id = 4; string avatar_url = 5; } message UserLeave { string channel_id = 1; string user_id = 2; } // ─── CHAT ──────────────────────────────────────────────────────────────────── @@ -464,3 +482,45 @@ message VoiceFrame { bytes opus = 5; string sender_user_id = 6; } + +// Additive channel image attachment extension; see docs/02-protocol/chat-attachments.md. +message AttachmentUpload { string request_id = 1; string channel_id = 2; bytes data = 3; } +message AttachmentFetch { string request_id = 1; string channel_id = 2; string attachment_id = 3; } +message AttachmentResult { string request_id = 1; string channel_id = 2; string attachment_id = 3; string mime = 4; bytes data = 5; string error = 6; } + +// Additive live Central profile metadata, LNEx v4. Authorization is unchanged. +message ProfileUpdate { bytes account_proof = 1; } +message ProfileUpdated { + string user_id = 1; + string account_id = 2; + string nickname = 3; + string avatar_url = 4; + int64 issued_at = 5; +} + +// Optional post-SESSION feature report; no AUTH transcript extension. +message ClientFeatures { bool node_settings = 1; bool channel_structure = 2; bool forums = 3; } +message NodeSettingsRequest { string request_id = 1; bool update = 2; string display_name = 3; string description = 4; } +message NodeSettingsResult { string request_id = 1; string display_name = 2; string description = 3; uint32 error_code = 4; string error = 5; } +enum ForumOperation { + FORUM_OPERATION_UNSPECIFIED = 0; + FORUM_OPERATION_LIST = 1; + FORUM_OPERATION_CREATE = 2; + FORUM_OPERATION_OPEN = 3; + FORUM_OPERATION_REPLY = 4; +} +message ForumRequest { + string request_id = 1; + ForumOperation operation = 2; + string channel_id = 3; + string topic_id = 4; + string title = 5; + string content = 6; + uint32 offset = 7; + uint32 limit = 8; + string message_id = 9; +} +message ForumTopic { string topic_id = 1; string channel_id = 2; string title = 3; MemberInfo author = 4; int64 created_at = 5; int64 updated_at = 6; uint32 reply_count = 7; } +message ForumPost { ChatMessage message = 1; MemberInfo author = 2; string topic_id = 3; uint64 ordinal = 4; } +message ForumResult { string request_id = 1; ForumOperation operation = 2; string channel_id = 3; string topic_id = 4; repeated ForumTopic topics = 5; repeated ForumPost posts = 6; bool has_more = 7; uint32 error_code = 8; string error = 9; } +message ForumChanged { string channel_id = 1; ForumTopic topic = 2; ForumPost post = 3; }