diff --git a/Cargo.lock b/Cargo.lock index c48f048..2d60dcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aead" version = "0.6.1" @@ -242,12 +248,24 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + [[package]] name = "bytes" version = "1.12.1" @@ -417,6 +435,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" +[[package]] +name = "crc32fast" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-queue" version = "0.3.14" @@ -652,6 +679,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + [[package]] name = "fiat-crypto" version = "0.3.0" @@ -670,6 +706,17 @@ version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide 0.9.1", + "zlib-rs", +] + [[package]] name = "flume" version = "0.12.0" @@ -1134,6 +1181,32 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "image" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104" +dependencies = [ + "bytemuck", + "byteorder-lite", + "image-webp", + "moxcms", + "num-traits", + "png", + "zune-core", + "zune-jpeg", +] + +[[package]] +name = "image-webp" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3" +dependencies = [ + "byteorder-lite", + "quick-error", +] + [[package]] name = "indexmap" version = "2.14.2" @@ -1370,6 +1443,26 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "mio" version = "1.2.4" @@ -1381,6 +1474,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "moxcms" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b" +dependencies = [ + "num-traits", + "pxfm", +] + [[package]] name = "multimap" version = "0.10.1" @@ -1557,6 +1660,19 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + [[package]] name = "poly1305" version = "0.9.1" @@ -1661,6 +1777,18 @@ dependencies = [ "prost", ] +[[package]] +name = "pxfm" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" + +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quinn" version = "0.11.12" @@ -2223,6 +2351,12 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "simd_cesu8" version = "1.2.0" @@ -2983,6 +3117,7 @@ dependencies = [ "axum", "cookie", "ed25519-dalek", + "image", "hex", "hmac", "lettre", @@ -3462,11 +3597,32 @@ dependencies = [ "yoke", "zerofrom", "zerovec-derive", +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + ] [[package]] name = "zerovec-derive" version = "0.11.7" + +[[package]] +name = "zune-core" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" + +[[package]] +name = "zune-jpeg" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296" +dependencies = [ + "zune-core", +] source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" dependencies = [ diff --git a/docs/00-status.md b/docs/00-status.md index daf8cb7..b305310 100644 --- a/docs/00-status.md +++ b/docs/00-status.md @@ -13,7 +13,8 @@ Last updated: 2026-10-24. - 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/avatar 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 after first sign-in 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). diff --git a/docs/design/accounts-and-avatars.md b/docs/design/accounts-and-avatars.md index ee4b8dd..dea760b 100644 --- a/docs/design/accounts-and-avatars.md +++ b/docs/design/accounts-and-avatars.md @@ -10,7 +10,7 @@ Связать второй способ входа можно из аутентифицированного аккаунта после повторного подтверждения; требуется доказать владение добавляемым способом. Отвязка последнего способа входа запрещена без настроенного восстановления. Добавление/отзыв устройств, email и способов входа сопровождаются уведомлениями. -**Статус реализации (Hub, этап 1):** вход по почте с одноразовым кодом работает (`hub/AUTH.md`). Новый аккаунт получает сгенерированный handle `u_...` и нейтральное имя; выбор handle, отображаемого имени и аватара после первого подтверждения ещё не реализован. Связывание второго способа входа, passkey/MFA и уведомления о новых способах входа тоже впереди. +**Статус реализации (Hub, этап 1):** вход по почте с одноразовым кодом работает (`hub/AUTH.md`). Новый аккаунт получает сгенерированный handle `u_...` и нейтральное имя; выбор handle и отображаемого имени после первого подтверждения ещё не реализован. Собственный аватар можно загрузить и убрать на странице аккаунта сайта (реализовано в Hub, см. ниже). Связывание второго способа входа, passkey/MFA и уведомления о новых способах входа тоже впереди. ## GitHub OAuth @@ -42,6 +42,8 @@ Email подтверждает контроль почтового ящика, Первоначальные ограничения клиента: исходный файл до10MiB, сторона до4096px, лимит памяти декодера64MiB. Ограничения проверяются при чтении/декодировании, ошибки показываются в диалоге. SVG, внешние URL и анимации не поддерживаются в первой версии. Оригинал не сохраняется после нормализации; выходной PNG не переносит исходные EXIF/геометки. Отмена и ошибка не заменяют прежний аватар; запись атомарная. +**Реализация в Hub (веб):** `POST/DELETE /v1/account/avatar`, `GET /v1/avatars/` (`hub/AUTH.md`). Hub принимает PNG/JPEG/WebP до 2 МиБ и 4096 px, проверяет тип по сигнатуре, полностью декодирует и заново кодирует в PNG 256×256 без метаданных, хранит один аватар на аккаунт в SQLite BLOB; адрес содержит хеш содержимого и кэшируется как immutable. Для сайта кадрирование делается на canvas до отправки, Hub дополнительно обрезает по центру. Для аккаунта GitHub «Убрать фото» возвращает аватар GitHub. Клиент Vnox пока не использует этот API. Резервные копии базы могут хранить удалённые данные до 30 дней (решение владельца; ротацию копий ещё нужно реализовать). + До Hub API аватар собственный/локальный и не выдаётся за синхронизированный с друзьями. Впоследствии Hub принимает нормализованное изображение, повторно проверяет/перекодирует его, обновляет avatar_id/revision и доступ по политике профиля. Изображение хранится до замены/удаления профиля; правило DM-вложения «4 дня» не применяется. Старые версии и кэши очищаются по отдельной политике. ## Ещё решения до публичного запуска diff --git a/hub/AUTH.md b/hub/AUTH.md index df7a721..ad76911 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, display name and avatar after registration is not implemented yet (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. 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. ### Mail and logging @@ -68,3 +68,39 @@ The message is rendered by `mail::templates::render_email_code` (Russian or Engl - Rate-limit counters for the in-memory limiter reset on restart; the email ledger persists. - Access to the mailbox, or to the SQLite file together with `email-auth.key` during the ten-minute window, defeats the code. - Tests use an injected sender; real SMTP delivery has not been exercised from tests. + +## Own avatar + +A signed-in web session can set its own photo. Without one, `avatar_url` in `GET /v1/auth/session` (and in the email verify answer) is the provider's avatar (GitHub) or `null`; with one it is the same-origin path `/v1/avatars/`. + +### Contract + +- `POST /v1/account/avatar`: the body is the raw image, `Content-Type` is `image/png`, `image/jpeg` or `image/webp`. Answers `200 {"avatar_url":"/v1/avatars/"}`. +- `DELETE /v1/account/avatar`: removes the own photo. Answers `200 {"avatar_url": }`; repeating it is harmless. +- `GET /v1/avatars/`: public. `` is 64 lowercase hex characters, the SHA-256 of the stored bytes. `Cache-Control: public, max-age=31536000, immutable`, `Content-Type: image/png`, `X-Content-Type-Options: nosniff`, `Content-Security-Policy: default-src 'none'; sandbox; frame-ancestors 'none'`, `ETag`. Unknown or malformed ids give a `404` JSON error. After a replacement or removal the old URL gives `404` (browser caches may still hold it until they evict it). +- The two changing endpoints require the exact `Origin`, the session cookie and `X-CSRF-Token`, checked in that order, exactly as logout (shared `authorized` gate). All their responses, errors included, are `no-store`. +- Errors: `401 not_authenticated`, `403 invalid_origin` / `invalid_csrf`, `413 too_large`, `415 unsupported_type` / `type_mismatch`, `422 bad_dimensions` / `invalid_image`, `429 try_later`, `503 try_later`. + +### Upload pipeline + +1. Authentication, Origin and CSRF are checked before the body is read. +2. The declared `Content-Type` must be one of the three types. A cheap refusal here does not spend rate-limit budget. +3. Rate limit, in memory: 10 changes per hour per account and 30 per hour per source (`X-Forwarded-For`, /64 grouping for IPv6, as above). Uploads and removals both count; a refused request is not recorded. At most two decodes run at the same time, the rest get `429`. +4. Body limit 2 MiB: refused from `Content-Length` when announced, and the read itself stops at the limit for chunked bodies. +5. Magic bytes decide the real format (PNG, JPEG, WebP; SVG, GIF and everything else are `unsupported_type`). A different declared type is `type_mismatch`, so a renamed file does not pass. +6. The header is read first: more than 4096 or fewer than 32 pixels on a side is `bad_dimensions` before any pixel buffer exists (decoder limits: 4096 per side, 128 MiB allocation). A small file that claims 60000x60000 never allocates. +7. The image is fully decoded (corrupt or truncated data is `invalid_image`), rotated by its EXIF orientation, center-cropped to a square, resized to 256x256 (Lanczos3) and written as a new PNG (RGB, or RGBA when the source has transparency). Nothing from the source file is copied, so EXIF, GPS, ICC profiles, text chunks and trailing data are gone. The decode runs on a blocking thread. Animated sources yield a single still frame or are refused. +8. The row is written with `INSERT ... ON CONFLICT(user_id) DO UPDATE`, so there is one avatar per account and the old bytes are overwritten in the same statement. The original upload is never stored. + +### Storage + +SQLite BLOB in table `avatars` (migration `0005_avatars.sql`), keyed by `user_id` with `ON DELETE CASCADE`. Reasons: a normalized avatar is small (typically 20 to 150 KB), so the database stays small; replacement and account deletion are transactional with the account row; there is no second directory whose permissions, orphan files or backup consistency need care; the existing data-directory rules and the existing backup (database plus issuer together) cover it with no new moving parts. Files stay `0600` because they are inside the private data directory like the database itself. If avatars ever grow in number or size, serving can move to files or object storage behind the same URLs. + +The provider avatar URL stays on `accounts.avatar_url` and is the fallback after removal. + +### Known limits + +- Rate-limit counters are in memory and reset on restart. +- 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. diff --git a/hub/Cargo.toml b/hub/Cargo.toml index 248a13f..a1801ef 100644 --- a/hub/Cargo.toml +++ b/hub/Cargo.toml @@ -23,6 +23,8 @@ uuid = { workspace = true, features = ["serde"] } sqlx = { version = "0.9", default-features = false, features = ["sqlite", "runtime-tokio", "migrate", "macros"] } lettre = { version = "0.11.23", default-features = false, features = ["builder", "smtp-transport", "tokio1-rustls", "aws-lc-rs", "webpki-roots"] } url = "2" +# Avatar re-encoding: only PNG, JPEG and WebP decoders and the PNG encoder; no GIF, SVG or other formats. +image = { version = "0.25.10", default-features = false, features = ["png", "jpeg", "webp"] } reqwest.workspace = true sha2.workspace = true diff --git a/hub/README.md b/hub/README.md index 7b03769..1c88a1a 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. +- 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. GitHub OAuth and email one-time-code sign-in are implemented (`AUTH.md`); email sign-in diff --git a/hub/migrations/0005_avatars.sql b/hub/migrations/0005_avatars.sql new file mode 100644 index 0000000..9581e4c --- /dev/null +++ b/hub/migrations/0005_avatars.sql @@ -0,0 +1,12 @@ +-- One own avatar per account, stored as a re-encoded PNG (no source metadata). The row is +-- replaced on upload and removed on "remove photo" or when the account is deleted. `id` is the +-- hex SHA-256 of the stored bytes; it names the immutable public URL /v1/avatars/. +-- accounts.avatar_url keeps the provider (GitHub) avatar as the fallback. +CREATE TABLE avatars ( + user_id TEXT PRIMARY KEY REFERENCES accounts(user_id) ON DELETE CASCADE, + id TEXT NOT NULL, + content_type TEXT NOT NULL, + bytes BLOB NOT NULL, + created_at INTEGER NOT NULL +); +CREATE INDEX avatars_by_id ON avatars(id); diff --git a/hub/src/auth/mod.rs b/hub/src/auth/mod.rs index a1f93d6..885c67d 100644 --- a/hub/src/auth/mod.rs +++ b/hub/src/auth/mod.rs @@ -26,8 +26,17 @@ pub struct AuthService { /// Present only when a mail transport is configured. pub email: Option, limits: 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, } +/// 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; + impl AuthService { pub fn new( pool: SqlitePool, @@ -53,6 +62,8 @@ impl AuthService { provider, email: None, limits: Mutex::new(HashMap::new()), + avatar_events: Mutex::new(HashMap::new()), + avatar_decoding: tokio::sync::Semaphore::new(2), } } /// Enables email sign-in. Callers do this only when SMTP is configured (or a test sender). @@ -86,4 +97,29 @@ impl AuthService { limits.entry(ip.to_owned()).or_insert((now, 0)).1 += 1; true } + /// Records one avatar change attempt for `user` and `source` and reports whether both + /// 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 { + return false; + }; + let now = store::now(); + events.retain(|_, times| { + times.retain(|at| now - *at < AVATAR_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 + { + return false; + } + events.entry(user_key).or_default().push(now); + events.entry(source_key).or_default().push(now); + true + } } diff --git a/hub/src/auth/routes.rs b/hub/src/auth/routes.rs index 934ebda..086e90e 100644 --- a/hub/src/auth/routes.rs +++ b/hub/src/auth/routes.rs @@ -14,6 +14,7 @@ use cookie::{Cookie, SameSite}; use oauth2::{CsrfToken, PkceCodeChallenge}; use serde::Deserialize; use serde_json::json; +mod avatar; use std::sync::Arc; use subtle::ConstantTimeEq; const BROWSER: &str = "__Host-vnox_oauth"; @@ -33,6 +34,11 @@ pub fn router() -> Router> { ) .route("/v1/auth/session", get(session)) .route("/v1/auth/logout", post(logout)) + .route( + "/v1/account/avatar", + post(avatar::upload).delete(avatar::remove), + ) + .route("/v1/avatars/{id}", get(avatar::serve)) } fn failure(status: StatusCode, code: &'static str) -> Response { (status, Json(json!({"error":code}))).into_response() @@ -336,23 +342,44 @@ async fn session(State(state): State>, headers: HeaderMap) -> Resp } Json(json!({"authenticated":false})).into_response() } -async fn logout(State(state): State>, headers: HeaderMap) -> Response { - if !same_origin(&state.auth, &headers) { - return failure(StatusCode::FORBIDDEN, "invalid_origin"); +/// Session gate for state-changing requests by a signed-in user: exact `Origin`, a live +/// session, and the session's CSRF token in `X-CSRF-Token`. Returns the user and session token. +async fn authorized( + state: &HubState, + headers: &HeaderMap, +) -> Result<(store::User, String), Box> { + if !same_origin(&state.auth, headers) { + return Err(Box::new(failure(StatusCode::FORBIDDEN, "invalid_origin"))); } - let Some(token) = cookie(&headers, SESSION) else { - return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + let Some(token) = cookie(headers, SESSION) else { + return Err(Box::new(failure( + StatusCode::UNAUTHORIZED, + "not_authenticated", + ))); }; - let Ok(Some((_, csrf))) = store::get_session(&state.auth.pool, &token).await else { - return failure(StatusCode::UNAUTHORIZED, "not_authenticated"); + let Ok(Some((user, csrf))) = store::get_session(&state.auth.pool, &token).await else { + return Err(Box::new(failure( + StatusCode::UNAUTHORIZED, + "not_authenticated", + ))); }; let supplied = headers .get("x-csrf-token") .map(|v| v.as_bytes()) .unwrap_or_default(); if !bool::from(csrf.as_bytes().ct_eq(supplied)) { - return failure(StatusCode::FORBIDDEN, "invalid_csrf"); + return Err(Box::new(failure(StatusCode::FORBIDDEN, "invalid_csrf"))); } + Ok((user, token)) +} +fn allowed_avatar(auth: &AuthService, user_id: &str, headers: &HeaderMap) -> bool { + auth.allow_avatar(user_id, &client_ip(headers)) +} +async fn logout(State(state): State>, headers: HeaderMap) -> Response { + let token = match authorized(&state, &headers).await { + Ok((_, token)) => token, + Err(response) => return *response, + }; if sqlx::query("DELETE FROM web_sessions WHERE token_hash=?") .bind(store::hash(&token)) .execute(&state.auth.pool) diff --git a/hub/src/auth/routes/avatar.rs b/hub/src/auth/routes/avatar.rs new file mode 100644 index 0000000..8a5931f --- /dev/null +++ b/hub/src/auth/routes/avatar.rs @@ -0,0 +1,132 @@ +//! Own-avatar endpoints. Changing requires an authenticated web session, the exact `Origin` +//! and the session's `X-CSRF-Token` (the same gate as logout). Serving is public. + +use super::{HeaderMap, StatusCode, allowed_avatar, authorized, failure}; +use crate::{avatar, web::HubState}; +use axum::{ + Json, + body::{Body, to_bytes}, + extract::{Path, State}, + http::{HeaderValue, header}, + response::{IntoResponse, Response}, +}; +use serde_json::json; +use std::sync::Arc; + +/// `POST /v1/account/avatar`: body is the raw image, `Content-Type` its type. +pub(super) async fn upload( + State(state): State>, + headers: HeaderMap, + body: Body, +) -> Response { + let user = match authorized(&state, &headers).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + let declared = headers + .get(header::CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .map(|v| { + v.split(';') + .next() + .unwrap_or("") + .trim() + .to_ascii_lowercase() + }) + .unwrap_or_default(); + if !matches!(declared.as_str(), "image/png" | "image/jpeg" | "image/webp") { + return failure(StatusCode::UNSUPPORTED_MEDIA_TYPE, "unsupported_type"); + } + if !allowed_avatar(&state.auth, &user.user_id, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + // Reject by announced size first; the read below enforces the limit for chunked bodies. + let announced = headers + .get(header::CONTENT_LENGTH) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.parse::().ok()); + if announced.is_some_and(|n| n > avatar::MAX_UPLOAD_BYTES) { + return failure(StatusCode::PAYLOAD_TOO_LARGE, "too_large"); + } + 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 { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + }; + let processed = tokio::task::spawn_blocking(move || avatar::normalize(&declared, &data)).await; + drop(permit); + let png = match processed { + Ok(Ok(png)) => png, + Ok(Err(avatar::AvatarError::UnsupportedType)) => { + return failure(StatusCode::UNSUPPORTED_MEDIA_TYPE, "unsupported_type"); + } + Ok(Err(avatar::AvatarError::TypeMismatch)) => { + return failure(StatusCode::UNSUPPORTED_MEDIA_TYPE, "type_mismatch"); + } + Ok(Err(avatar::AvatarError::BadDimensions)) => { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "bad_dimensions"); + } + Ok(Err(avatar::AvatarError::Invalid)) => { + return failure(StatusCode::UNPROCESSABLE_ENTITY, "invalid_image"); + } + Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + }; + match avatar::replace(&state.auth.pool, &user.user_id, &png).await { + Ok(url) => Json(json!({"avatar_url": url})).into_response(), + Err(_) => failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + } +} + +/// `DELETE /v1/account/avatar`: drops the own photo; the answer carries the avatar now in +/// effect (the provider's, or null for the default). +pub(super) async fn remove(State(state): State>, headers: HeaderMap) -> Response { + let user = match authorized(&state, &headers).await { + Ok((user, _)) => user, + Err(response) => return *response, + }; + if !allowed_avatar(&state.auth, &user.user_id, &headers) { + return failure(StatusCode::TOO_MANY_REQUESTS, "try_later"); + } + let pool = &state.auth.pool; + 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"), + } +} + +/// `GET /v1/avatars/`: public and immutable, because the id is the content hash. +pub(super) async fn serve(State(state): State>, Path(id): Path) -> Response { + if !avatar::valid_id(&id) { + return failure(StatusCode::NOT_FOUND, "not_found"); + } + let (content_type, bytes) = match avatar::fetch(&state.auth.pool, &id).await { + Ok(Some(found)) => found, + Ok(None) => return failure(StatusCode::NOT_FOUND, "not_found"), + Err(_) => return failure(StatusCode::SERVICE_UNAVAILABLE, "try_later"), + }; + let mut response = bytes.into_response(); + let headers = response.headers_mut(); + if let Ok(value) = HeaderValue::from_str(&content_type) { + headers.insert(header::CONTENT_TYPE, value); + } + 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/store.rs b/hub/src/auth/store.rs index 1b7ef13..694bc08 100644 --- a/hub/src/auth/store.rs +++ b/hub/src/auth/store.rs @@ -144,7 +144,8 @@ async fn lookup_identity( provider: &str, subject: &str, ) -> anyhow::Result> { - Ok(sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.avatar_url FROM accounts a JOIN external_identities e ON e.user_id=a.user_id WHERE e.provider=? AND e.subject=?") + // 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=?") .bind(provider).bind(subject).fetch_optional(pool).await?) } @@ -168,7 +169,7 @@ pub async fn get_session( avatar_url: Option, csrf_token: String, } - let row: Option = sqlx::query_as("SELECT a.user_id,a.handle,a.display_name,a.avatar_url,s.csrf_token FROM web_sessions s JOIN accounts a ON a.user_id=s.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,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>?") .bind(hash(session)).bind(now()).fetch_optional(pool).await?; Ok(row.map(|r| { ( diff --git a/hub/src/avatar.rs b/hub/src/avatar.rs new file mode 100644 index 0000000..d6e1acb --- /dev/null +++ b/hub/src/avatar.rs @@ -0,0 +1,165 @@ +//! Own-avatar processing and storage. +//! +//! Uploads are never stored as received. The bytes are sniffed (PNG, JPEG or WebP only), +//! the header dimensions are checked before any pixel buffer exists, the image is fully +//! decoded under a memory limit, rotated by its EXIF orientation, cropped to a square, +//! 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. + +use std::io::Cursor; + +use image::{ + DynamicImage, ImageDecoder, ImageEncoder, ImageFormat, Limits, codecs::png::PngEncoder, + imageops::FilterType, +}; +use sha2::{Digest, Sha256}; +use sqlx::SqlitePool; + +/// Request body limit (2 MiB). +pub const MAX_UPLOAD_BYTES: usize = 2 * 1024 * 1024; +/// Largest accepted source side, checked from the header before decoding. +pub const MAX_SOURCE_SIDE: u32 = 4096; +/// Smallest accepted source side. +pub const MIN_SOURCE_SIDE: u32 = 32; +/// Side of the stored square. +pub const OUTPUT_SIDE: u32 = 256; +/// Decoder allocation budget; a 4096x4096 RGBA frame is 64 MiB. +const MAX_DECODE_ALLOC: u64 = 128 * 1024 * 1024; + +#[derive(Debug, PartialEq, Eq)] +pub enum AvatarError { + /// Not PNG, JPEG or WebP (SVG, GIF and everything else included). + UnsupportedType, + /// Declared and detected types differ. + TypeMismatch, + /// Larger than 4096 on a side, or smaller than 32. + BadDimensions, + /// Looks like a supported image but does not decode. + Invalid, +} + +/// The MIME type of a supported format. +pub fn mime_for(format: ImageFormat) -> Option<&'static str> { + match format { + ImageFormat::Png => Some("image/png"), + ImageFormat::Jpeg => Some("image/jpeg"), + ImageFormat::WebP => Some("image/webp"), + _ => None, + } +} + +/// Validates and re-encodes an upload. `declared` is the request `Content-Type` without +/// parameters. CPU and memory heavy: call from a blocking thread. +pub fn normalize(declared: &str, data: &[u8]) -> Result, AvatarError> { + let format = image::guess_format(data).map_err(|_| AvatarError::UnsupportedType)?; + let detected = mime_for(format).ok_or(AvatarError::UnsupportedType)?; + if detected != declared { + return Err(AvatarError::TypeMismatch); + } + let mut limits = Limits::default(); + limits.max_image_width = Some(MAX_SOURCE_SIDE); + limits.max_image_height = Some(MAX_SOURCE_SIDE); + limits.max_alloc = Some(MAX_DECODE_ALLOC); + let mut reader = image::ImageReader::with_format(Cursor::new(data), format); + reader.limits(limits); + let mut decoder = reader.into_decoder().map_err(|error| match error { + image::ImageError::Limits(_) => AvatarError::BadDimensions, + _ => AvatarError::Invalid, + })?; + // Header-only: nothing has been allocated for pixels yet. + let (width, height) = decoder.dimensions(); + if width > MAX_SOURCE_SIDE + || height > MAX_SOURCE_SIDE + || width < MIN_SOURCE_SIDE + || height < MIN_SOURCE_SIDE + { + return Err(AvatarError::BadDimensions); + } + let orientation = decoder.orientation().map_err(|_| AvatarError::Invalid)?; + let mut picture = DynamicImage::from_decoder(decoder).map_err(|_| AvatarError::Invalid)?; + picture.apply_orientation(orientation); + let square = picture.resize_to_fill(OUTPUT_SIDE, OUTPUT_SIDE, FilterType::Lanczos3); + let mut out = Vec::new(); + let encoder = PngEncoder::new(&mut out); + let written = if square.color().has_alpha() { + let pixels = square.to_rgba8(); + encoder.write_image( + pixels.as_raw(), + OUTPUT_SIDE, + OUTPUT_SIDE, + image::ExtendedColorType::Rgba8, + ) + } else { + let pixels = square.to_rgb8(); + encoder.write_image( + pixels.as_raw(), + OUTPUT_SIDE, + OUTPUT_SIDE, + image::ExtendedColorType::Rgb8, + ) + }; + written.map_err(|_| AvatarError::Invalid)?; + Ok(out) +} + +/// Public identifier: hex SHA-256 of the stored bytes. +pub fn content_id(bytes: &[u8]) -> String { + hex::encode(Sha256::digest(bytes)) +} + +/// True for a 64 character lowercase hex identifier. +pub fn valid_id(id: &str) -> bool { + id.len() == 64 && id.bytes().all(|c| matches!(c, b'0'..=b'9' | b'a'..=b'f')) +} + +/// Replaces the account's avatar atomically and returns its URL path. +pub async fn replace(pool: &SqlitePool, user_id: &str, png: &[u8]) -> anyhow::Result { + let id = content_id(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_id) + .bind(&id) + .bind("image/png") + .bind(png) + .bind(crate::auth::store::now()) + .execute(pool) + .await?; + Ok(url_path(&id)) +} + +/// Deletes the account's own avatar; true if one existed. +pub async fn remove(pool: &SqlitePool, user_id: &str) -> anyhow::Result { + let done = sqlx::query("DELETE FROM avatars WHERE user_id=?") + .bind(user_id) + .execute(pool) + .await?; + Ok(done.rows_affected() > 0) +} + +/// Content type and bytes for a public avatar id. +pub async fn fetch(pool: &SqlitePool, id: &str) -> anyhow::Result)>> { + Ok( + sqlx::query_as("SELECT content_type,bytes FROM avatars WHERE id=? LIMIT 1") + .bind(id) + .fetch_optional(pool) + .await?, + ) +} + +/// The provider avatar kept on the account, if any. +pub async fn provider_url(pool: &SqlitePool, user_id: &str) -> anyhow::Result> { + Ok( + sqlx::query_scalar("SELECT avatar_url FROM accounts WHERE user_id=?") + .bind(user_id) + .fetch_optional(pool) + .await? + .flatten(), + ) +} + +pub fn url_path(id: &str) -> String { + format!("/v1/avatars/{id}") +} diff --git a/hub/src/lib.rs b/hub/src/lib.rs index 52374d9..000266d 100644 --- a/hub/src/lib.rs +++ b/hub/src/lib.rs @@ -1,4 +1,5 @@ pub mod auth; +pub mod avatar; pub mod config; pub mod database; pub mod identity; diff --git a/hub/src/web.rs b/hub/src/web.rs index dcf3bcf..295aafa 100644 --- a/hub/src/web.rs +++ b/hub/src/web.rs @@ -106,6 +106,7 @@ async fn index() -> Html<&'static str> { async fn headers(request: Request, next: Next) -> Response { let mut response = next.run(request).await; + // Handlers that set their own cache policy or CSP (avatar delivery) keep them. for (name, value) in [ ("cache-control", "no-store"), ("x-content-type-options", "nosniff"), @@ -115,6 +116,10 @@ async fn headers(request: Request, next: Next) -> Response { "default-src 'none'; style-src 'unsafe-inline'; img-src 'self'; frame-ancestors 'none'", ), ] { + let present = response.headers().contains_key(name); + if present && matches!(name, "cache-control" | "content-security-policy") { + continue; + } response .headers_mut() .insert(name, HeaderValue::from_static(value)); diff --git a/hub/tests/avatar.rs b/hub/tests/avatar.rs new file mode 100644 index 0000000..e40a83e --- /dev/null +++ b/hub/tests/avatar.rs @@ -0,0 +1,742 @@ +use axum::{ + Router, + body::{Body, to_bytes}, + http::{Method, Request, Response, StatusCode}, +}; +use image::{ExtendedColorType, ImageEncoder, RgbImage}; +use std::sync::Arc; +use tower::ServiceExt; +use vnox_hub::{ + auth::{ + self, + provider::GitHubProfile, + store::{self, User}, + }, + database, + identity::Issuer, + web::{self, HubState}, +}; + +const ORIGIN: &str = "https://central.loki-code.dev"; +const GITHUB_AVATAR: &str = "https://avatars.githubusercontent.com/u/123?v=4"; + +struct Session { + user: User, + token: String, + csrf: String, +} + +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, + } +} + +impl Fixture { + async fn email_user(&self, address: &str) -> Session { + let user = store::upsert_email_identity(&self.state.database, address) + .await + .unwrap(); + self.session(user).await + } + async fn github_user(&self) -> Session { + let profile = GitHubProfile { + id: 123, + login: "person".into(), + name: Some("Person".into()), + avatar_url: Some(GITHUB_AVATAR.into()), + }; + let user = store::upsert_identity(&self.state.database, &profile) + .await + .unwrap(); + self.session(user).await + } + async fn session(&self, user: User) -> Session { + let (token, csrf) = store::create_session(&self.state.database, &user.user_id) + .await + .unwrap(); + Session { user, token, csrf } + } + async fn send(&self, request: Request) -> Response { + self.app.clone().oneshot(request).await.unwrap() + } + /// A well-formed request; callers override pieces to break it. + fn change<'a>( + &'a self, + method: Method, + session: Option<&Session>, + csrf: Option<&str>, + origin: Option<&str>, + content_type: &str, + body: Vec, + ) -> impl std::future::Future> + use<'a> { + let mut request = Request::builder() + .method(method) + .uri("/v1/account/avatar") + .header("content-type", content_type); + if let Some(session) = session { + request = request.header("cookie", format!("__Host-vnox_session={}", session.token)); + } + if let Some(csrf) = csrf { + request = request.header("x-csrf-token", csrf); + } + if let Some(origin) = origin { + request = request.header("origin", origin); + } + self.send(request.body(Body::from(body)).unwrap()) + } + async fn upload(&self, session: &Session, content_type: &str, body: Vec) -> Response { + self.change( + Method::POST, + Some(session), + Some(&session.csrf), + Some(ORIGIN), + content_type, + body, + ) + .await + } + async fn remove(&self, session: &Session) -> Response { + self.change( + Method::DELETE, + Some(session), + Some(&session.csrf), + Some(ORIGIN), + "application/json", + Vec::new(), + ) + .await + } + async fn get(&self, path: &str) -> Response { + self.send(Request::builder().uri(path).body(Body::empty()).unwrap()) + .await + } + async fn session_avatar(&self, session: &Session) -> serde_json::Value { + let response = self + .send( + Request::builder() + .uri("/v1/auth/session") + .header("cookie", format!("__Host-vnox_session={}", session.token)) + .body(Body::empty()) + .unwrap(), + ) + .await; + json(response).await["user"]["avatar_url"].clone() + } + async fn avatar_rows(&self) -> i64 { + sqlx::query_scalar("SELECT COUNT(*) FROM avatars") + .fetch_one(&self.state.database) + .await + .unwrap() + } +} + +async fn json(response: Response) -> serde_json::Value { + serde_json::from_slice(&to_bytes(response.into_body(), 1 << 20).await.unwrap()).unwrap() +} +async fn bytes(response: Response) -> Vec { + to_bytes(response.into_body(), 1 << 22) + .await + .unwrap() + .to_vec() +} + +fn picture(width: u32, height: u32) -> RgbImage { + RgbImage::from_fn(width, height, |x, y| { + image::Rgb([(x % 256) as u8, (y % 256) as u8, ((x + y) % 256) as u8]) + }) +} +fn png(width: u32, height: u32) -> Vec { + let mut out = Vec::new(); + image::codecs::png::PngEncoder::new(&mut out) + .write_image( + picture(width, height).as_raw(), + width, + height, + ExtendedColorType::Rgb8, + ) + .unwrap(); + out +} +fn jpeg(width: u32, height: u32) -> Vec { + let mut out = Vec::new(); + image::codecs::jpeg::JpegEncoder::new(&mut out) + .write_image( + picture(width, height).as_raw(), + width, + height, + ExtendedColorType::Rgb8, + ) + .unwrap(); + out +} +fn webp(width: u32, height: u32) -> Vec { + let mut out = Vec::new(); + image::codecs::webp::WebPEncoder::new_lossless(&mut out) + .write_image( + picture(width, height).as_raw(), + width, + height, + ExtendedColorType::Rgb8, + ) + .unwrap(); + out +} +/// Inserts an EXIF APP1 segment carrying `marker` right after the JPEG start marker. +fn with_exif(jpeg: &[u8], marker: &str) -> Vec { + let mut payload = b"Exif\0\0".to_vec(); + payload.extend_from_slice(marker.as_bytes()); + let mut out = jpeg[..2].to_vec(); + out.extend_from_slice(&[0xFF, 0xE1]); + out.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes()); + out.extend_from_slice(&payload); + out.extend_from_slice(&jpeg[2..]); + out +} +fn crc32(data: &[u8]) -> u32 { + let mut crc = !0u32; + for byte in data { + crc ^= u32::from(*byte); + for _ in 0..8 { + crc = if crc & 1 == 1 { + (crc >> 1) ^ 0xEDB8_8320 + } else { + crc >> 1 + }; + } + } + !crc +} +/// Inserts an ancillary chunk before IEND. +fn with_chunk(png: &[u8], kind: &[u8; 4], data: &[u8]) -> Vec { + let end = png.len() - 12; + let mut out = png[..end].to_vec(); + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + let mut body = kind.to_vec(); + body.extend_from_slice(data); + out.extend_from_slice(&body); + out.extend_from_slice(&crc32(&body).to_be_bytes()); + out.extend_from_slice(&png[end..]); + out +} +/// Rewrites the IHDR size of a PNG and fixes its CRC: a tiny file that claims huge pixels. +fn claim_size(png: &[u8], width: u32, height: u32) -> Vec { + let mut out = png.to_vec(); + out[16..20].copy_from_slice(&width.to_be_bytes()); + out[20..24].copy_from_slice(&height.to_be_bytes()); + let crc = crc32(&out[12..29]); + out[29..33].copy_from_slice(&crc.to_be_bytes()); + out +} +fn chunk_names(png: &[u8]) -> Vec { + let mut names = Vec::new(); + let mut at = 8; + while at + 8 <= png.len() { + let length = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + names.push(String::from_utf8_lossy(&png[at + 4..at + 8]).into_owned()); + at += 12 + length; + } + names +} +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + haystack.windows(needle.len()).any(|w| w == needle) +} + +async fn assert_error(response: Response, status: StatusCode, code: &str) { + assert_eq!(response.status(), status); + assert_eq!(response.headers()["cache-control"], "no-store"); + assert_eq!(json(response).await["error"], code); +} + +#[tokio::test] +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); + + let response = fx.upload(&alice, "image/png", png(600, 300)).await; + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.headers()["cache-control"], "no-store"); + let url = json(response).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + assert!(url.starts_with("/v1/avatars/")); + assert_eq!(url.len(), "/v1/avatars/".len() + 64); + assert_eq!(fx.session_avatar(&alice).await, url); + + let served = fx.get(&url).await; + assert_eq!(served.status(), StatusCode::OK); + let h = served.headers(); + assert_eq!(h["content-type"], "image/png"); + assert_eq!(h["x-content-type-options"], "nosniff"); + assert_eq!(h["cache-control"], "public, max-age=31536000, immutable"); + assert!( + h["content-security-policy"] + .to_str() + .unwrap() + .contains("default-src 'none'") + ); + assert!(h.get("set-cookie").is_none()); + let body = bytes(served).await; + let decoded = image::load_from_memory(&body).unwrap(); + assert_eq!((decoded.width(), decoded.height()), (256, 256)); + // The id is the content hash. + assert_eq!( + url, + format!("/v1/avatars/{}", vnox_hub::avatar::content_id(&body)) + ); +} + +#[tokio::test] +async fn jpeg_and_webp_are_accepted_and_exif_and_png_text_chunks_are_removed() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + + let tagged = with_exif(&jpeg(200, 200), "SECRET-GPS-48.8566N"); + assert!(contains(&tagged, b"SECRET-GPS")); + let response = fx.upload(&alice, "image/jpeg", tagged).await; + assert_eq!(response.status(), StatusCode::OK); + let url = json(response).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + let body = bytes(fx.get(&url).await).await; + assert!(!contains(&body, b"SECRET")); + assert!(!contains(&body, b"Exif")); + assert_eq!(chunk_names(&body), ["IHDR", "IDAT", "IEND"]); + + let marked = with_chunk(&png(100, 100), b"tEXt", b"Comment\0SECRET-AUTHOR"); + let marked = with_chunk(&marked, b"eXIf", b"SECRET-EXIF"); + assert!(contains(&marked, b"SECRET-AUTHOR")); + let response = fx.upload(&alice, "image/png", marked).await; + assert_eq!(response.status(), StatusCode::OK); + let url = json(response).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + let body = bytes(fx.get(&url).await).await; + assert!(!contains(&body, b"SECRET")); + assert_eq!(chunk_names(&body), ["IHDR", "IDAT", "IEND"]); + + let response = fx.upload(&alice, "image/webp", webp(120, 90)).await; + assert_eq!(response.status(), StatusCode::OK); + let url = json(response).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + let served = fx.get(&url).await; + assert_eq!(served.headers()["content-type"], "image/png"); + let decoded = image::load_from_memory(&bytes(served).await).unwrap(); + assert_eq!((decoded.width(), decoded.height()), (256, 256)); +} + +#[tokio::test] +async fn changes_need_session_csrf_and_exact_origin() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let image = png(64, 64); + + for method in [Method::POST, Method::DELETE] { + let go = |session, csrf: Option<&str>, origin: Option<&str>| { + fx.change( + method.clone(), + session, + csrf, + origin, + "image/png", + image.clone(), + ) + }; + assert_error( + go(None, Some(&alice.csrf), Some(ORIGIN)).await, + StatusCode::UNAUTHORIZED, + "not_authenticated", + ) + .await; + assert_error( + go(Some(&alice), None, Some(ORIGIN)).await, + StatusCode::FORBIDDEN, + "invalid_csrf", + ) + .await; + assert_error( + go(Some(&alice), Some("0".repeat(64).as_str()), Some(ORIGIN)).await, + StatusCode::FORBIDDEN, + "invalid_csrf", + ) + .await; + for origin in [ + None, + Some("https://evil.example"), + Some("http://central.loki-code.dev"), + Some("https://central.loki-code.dev:8443"), + Some("null"), + ] { + assert_error( + go(Some(&alice), Some(&alice.csrf), origin).await, + StatusCode::FORBIDDEN, + "invalid_origin", + ) + .await; + } + } + assert_eq!(fx.avatar_rows().await, 0); +} + +#[tokio::test] +async fn another_account_cannot_change_or_remove_my_avatar() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let bob = fx.email_user("bob@example.com").await; + let response = fx.upload(&alice, "image/png", png(64, 64)).await; + let alice_url = json(response).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + + // Bob's session with Alice's CSRF token, and Alice's cookie with Bob's token. + let mixed = [ + fx.change( + Method::DELETE, + Some(&bob), + Some(&alice.csrf), + Some(ORIGIN), + "x/y", + vec![], + ) + .await, + fx.change( + Method::POST, + Some(&bob), + Some(&alice.csrf), + Some(ORIGIN), + "image/png", + png(64, 64), + ) + .await, + fx.change( + Method::DELETE, + Some(&alice), + Some(&bob.csrf), + Some(ORIGIN), + "x/y", + vec![], + ) + .await, + ]; + for response in mixed { + 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); + + // Bob acting as himself only affects Bob. + assert_eq!( + fx.upload(&bob, "image/png", png(80, 80)).await.status(), + StatusCode::OK + ); + assert_eq!(fx.remove(&bob).await.status(), StatusCode::OK); + assert_eq!(fx.session_avatar(&alice).await, alice_url); + assert_eq!(fx.get(&alice_url).await.status(), StatusCode::OK); +} + +#[tokio::test] +async fn wrong_types_and_swapped_extensions_are_refused() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let gif = b"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;".to_vec(); + let svg = br#""#.to_vec(); + + for (declared, body, code) in [ + ("image/gif", gif.clone(), "unsupported_type"), + ("image/png", gif, "unsupported_type"), + ("image/svg+xml", svg.clone(), "unsupported_type"), + ("image/png", svg, "unsupported_type"), + ("text/plain", b"hello".to_vec(), "unsupported_type"), + ("application/json", b"{}".to_vec(), "unsupported_type"), + ( + "image/png", + b"not an image at all".to_vec(), + "unsupported_type", + ), + // A JPEG sent as a PNG, a PNG sent as a JPEG, WebP sent as PNG: the name does not matter. + ("image/png", jpeg(64, 64), "type_mismatch"), + ("image/jpeg", png(64, 64), "type_mismatch"), + ("image/png", webp(64, 64), "type_mismatch"), + ] { + assert_error( + fx.upload(&alice, declared, body).await, + StatusCode::UNSUPPORTED_MEDIA_TYPE, + code, + ) + .await; + } + // No content type at all. + let response = fx + .send( + Request::builder() + .method(Method::POST) + .uri("/v1/account/avatar") + .header("cookie", format!("__Host-vnox_session={}", alice.token)) + .header("x-csrf-token", &alice.csrf) + .header("origin", ORIGIN) + .body(Body::from(png(64, 64))) + .unwrap(), + ) + .await; + assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); + assert_eq!(fx.avatar_rows().await, 0); +} + +#[tokio::test] +async fn oversized_bodies_are_refused() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let mut big = png(64, 64); + big.resize(2 * 1024 * 1024 + 1, 0); + // Without a Content-Length (chunked), the read itself stops at the limit. + assert_error( + fx.upload(&alice, "image/png", big.clone()).await, + StatusCode::PAYLOAD_TOO_LARGE, + "too_large", + ) + .await; + // With an announced length, it is refused before reading. + let response = fx + .send( + Request::builder() + .method(Method::POST) + .uri("/v1/account/avatar") + .header("cookie", format!("__Host-vnox_session={}", alice.token)) + .header("x-csrf-token", &alice.csrf) + .header("origin", ORIGIN) + .header("content-type", "image/png") + .header("content-length", big.len()) + .body(Body::from(big)) + .unwrap(), + ) + .await; + assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE); + // Unauthenticated callers never get their body read or decoded. + let response = fx + .change( + Method::POST, + None, + None, + Some(ORIGIN), + "image/png", + vec![0; 3 << 20], + ) + .await; + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); + assert_eq!(fx.avatar_rows().await, 0); +} + +#[tokio::test] +async fn decompression_bomb_and_odd_dimensions_are_refused_before_decoding() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + + // Tiny files whose headers claim huge or oversized pictures. + for (w, h) in [(60_000, 60_000), (4097, 64), (64, 4097), (u32::MAX >> 1, 1)] { + let lie = claim_size(&png(64, 64), w, h); + assert!(lie.len() < 100_000); + assert_error( + fx.upload(&alice, "image/png", lie).await, + StatusCode::UNPROCESSABLE_ENTITY, + "bad_dimensions", + ) + .await; + } + // Real, merely oversized or undersized pictures. + assert_error( + fx.upload(&alice, "image/png", png(4097, 40)).await, + StatusCode::UNPROCESSABLE_ENTITY, + "bad_dimensions", + ) + .await; + assert_error( + fx.upload(&alice, "image/jpeg", jpeg(16, 16)).await, + StatusCode::UNPROCESSABLE_ENTITY, + "bad_dimensions", + ) + .await; + // Truncated and corrupted data. + let mut cut = png(300, 300); + cut.truncate(cut.len() / 2); + assert_error( + fx.upload(&alice, "image/png", cut).await, + StatusCode::UNPROCESSABLE_ENTITY, + "invalid_image", + ) + .await; + let mut cut = jpeg(300, 300); + cut.truncate(60); + let response = fx.upload(&alice, "image/jpeg", cut).await; + assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); + assert_eq!(fx.avatar_rows().await, 0); +} + +#[tokio::test] +async fn a_new_upload_replaces_the_old_one() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let first = json(fx.upload(&alice, "image/png", png(100, 100)).await).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + let second = json(fx.upload(&alice, "image/jpeg", jpeg(150, 150)).await).await["avatar_url"] + .as_str() + .unwrap() + .to_owned(); + assert_ne!(first, second); + assert_eq!(fx.avatar_rows().await, 1); + assert_eq!(fx.get(&first).await.status(), StatusCode::NOT_FOUND); + assert_eq!(fx.get(&second).await.status(), StatusCode::OK); + assert_eq!(fx.session_avatar(&alice).await, second); +} + +#[tokio::test] +async fn delete_returns_the_provider_avatar_or_the_default() { + let fx = fixture().await; + let github = fx.github_user().await; + assert_eq!(fx.session_avatar(&github).await, GITHUB_AVATAR); + + 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); + + 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!(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). + 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); +} + +#[tokio::test] +async fn avatar_urls_are_validated_and_unknown_ids_are_not_found() { + let fx = fixture().await; + for path in [ + "/v1/avatars/".to_owned(), + "/v1/avatars/abc".to_owned(), + format!("/v1/avatars/{}", "0".repeat(64)), + format!("/v1/avatars/{}", "G".repeat(64)), + format!("/v1/avatars/{}", "A".repeat(64)), + "/v1/avatars/..%2F..%2Fhub.sqlite3".to_owned(), + ] { + let response = fx.get(&path).await; + assert_eq!(response.status(), StatusCode::NOT_FOUND, "{path}"); + assert_eq!(response.headers()["cache-control"], "no-store"); + } +} + +#[tokio::test] +async fn upload_rate_limit_applies_per_account() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + let bob = fx.email_user("bob@example.com").await; + let image = png(64, 64); + for _ in 0..10 { + assert_eq!( + fx.upload(&alice, "image/png", image.clone()).await.status(), + StatusCode::OK + ); + } + assert_error( + fx.upload(&alice, "image/png", image.clone()).await, + StatusCode::TOO_MANY_REQUESTS, + "try_later", + ) + .await; + assert_error( + fx.remove(&alice).await, + StatusCode::TOO_MANY_REQUESTS, + "try_later", + ) + .await; + // Another account from the same source still has its own budget. + assert_eq!( + fx.upload(&bob, "image/png", image).await.status(), + StatusCode::OK + ); +} + +#[tokio::test] +async fn upload_rate_limit_applies_per_source() { + let fx = fixture().await; + let image = png(64, 64); + let mut accepted = 0; + for n in 0..40 { + let user = fx.email_user(&format!("user{n}@example.com")).await; + let response = fx + .send( + Request::builder() + .method(Method::POST) + .uri("/v1/account/avatar") + .header("cookie", format!("__Host-vnox_session={}", user.token)) + .header("x-csrf-token", &user.csrf) + .header("origin", ORIGIN) + .header("x-forwarded-for", "203.0.113.9") + .header("content-type", "image/png") + .body(Body::from(image.clone())) + .unwrap(), + ) + .await; + match response.status() { + StatusCode::OK => accepted += 1, + StatusCode::TOO_MANY_REQUESTS => break, + other => panic!("unexpected {other}"), + } + } + assert_eq!(accepted, 30); +} + +#[tokio::test] +async fn deleting_an_account_removes_its_avatar() { + let fx = fixture().await; + let alice = fx.email_user("alice@example.com").await; + fx.upload(&alice, "image/png", png(64, 64)).await; + sqlx::query("DELETE FROM accounts WHERE user_id=?") + .bind(&alice.user.user_id) + .execute(&fx.state.database) + .await + .unwrap(); + assert_eq!(fx.avatar_rows().await, 0); +}