//! Server-side plumbing: the application context, the person behind the //! current request, and the role guards. //! //! Every guard here is the real one. The UI hides what a role may not do, but //! the answer that matters is the one given in this module, on the server. //! //! There are no local passwords — im holds the central session over OIDC and //! `iz-client` (vendored from `im-client` via `in-client`) holds this app's //! side of it. This module's `current_user` maps im's claims onto the local //! user row, provisioning it on first sight. use std::sync::Arc; use iz_core::board::Transition; use iz_core::mail::Engine; use iz_core::store::{Freeing, Store, StoreError, User}; use serde::{Deserialize, Serialize}; use sha2::Digest; use topcoat::asset::AssetBundle; use topcoat::context::{Cx, app_context, memoize, try_app_context}; use topcoat::cookie::{Cookies, cookies}; use topcoat::router::request::{client_ip, headers}; use topcoat::router::{ route, Body, HeaderValue, Next, StatusCode, header, response::IntoResponse, response::Response, to_bytes, }; /// The workspace store, put into context by the router. pub fn store(cx: &Cx) -> Arc { app_context::>(cx).clone() } /// The loaded `config/iz.toml`, put into context by `main.rs`. pub fn config(cx: &Cx) -> iz_core::Config { app_context::(cx).clone() } /// One proxy hop in front of the process. `iz` is reached through that hop, /// so [`client_ip`] reads the address it appended rather than the hop's own. /// A direct connection, with no hop in front, is its own address. pub fn trusted_proxies() -> topcoat::router::TrustedProxies { topcoat::router::TrustedProxies::new().nearest(1) } /// A stable-enough label for the client. /// /// [`client_ip`] under [`trusted_proxies`]: the address the one trusted hop /// reported, or the peer when the request did not come through it. `unknown` /// when neither is there. A client-supplied `x-forwarded-for` is not read /// unless that hop appended it. pub fn client_label(cx: &Cx) -> String { client_ip(cx) .map(|ip| ip.to_string()) .unwrap_or_else(|| "unknown".to_string()) } /// The app-level `setting` key the background beat in `main.rs` stores /// im's family list under, and `layout.rs`'s switcher renders from. The /// value is a JSON array of `{key, name, url}` — exactly what im's /// `/family` served. pub const FAMILY_KEY: &str = "family"; /// The app-level `setting` key the storage mode form writes: which surface /// keeps the attachment bytes, `"local"` or `"in"`. The store's /// `set_storage_backend` owns the write and the announcement; the constant /// is named here beside `FAMILY_KEY` because tests read the row directly. pub const STORAGE_BACKEND_KEY: &str = "storage_backend"; /// The app-level `setting` key the public-address field on Settings /// writes: where a sign-out that started here sends the browser. Saving /// empty clears the row rather than storing an empty string, so what the /// fallback chain sits under is always either a usable address or nothing. pub const PUBLIC_URL_KEY: &str = "public_url"; /// Resolves where a sign-out that started here sends the browser, per /// request: the public address an admin stored wins; a missing or /// unreadable one is no opinion, and iz-client falls back to the /// configured chain — `base_url`, else the address bound. Registered as /// an app context in `main.rs`; this function is the shape that /// registration wants. pub fn logout_back(cx: &Cx) -> iz_client::BackAnswer<'_> { Box::pin(async move { match store(cx).get_setting(PUBLIC_URL_KEY).await { Ok(Some(value)) if !value.trim().is_empty() => Some(value), _ => None, } }) } /// The mail engine, or the fact that there is nobody to hand a crossing to. /// /// The running server always has an engine: the sender is workspace settings /// now, so it can appear at any moment and the engine reads it per send. The /// `None` is for tests, which drive the router without one and assert on the /// ledger rather than on a mail server. #[derive(Clone)] pub struct Mail(Option>); impl Mail { /// No engine at all. Crossings are recorded and nothing is handed on. pub fn silent() -> Self { Self(None) } pub fn sending(engine: std::sync::Arc) -> Self { Self(Some(engine)) } /// Hands a committed crossing to the engine, off the request. /// /// The move is already written by the time this is called and the response /// does not wait for it: a card that took thirty seconds to drop because /// somebody's SMTP host was slow would be a board broken by its own mail /// feature. What the send is owed is in the ledger, so a process that dies /// mid-send loses nothing — the sweep picks it up. pub fn after(&self, transition: Transition) { let Some(engine) = self.0.clone() else { return; }; tokio::spawn(async move { let report = engine.on_transition(&transition).await; Self::log(report); }); } /// Kicks the engine to send an invite mail now rather than on the next /// sweep, off the request the same way `after` is. /// /// The invite is already on the ledger by the time this is called — this /// only makes the wait before it leaves as short as the request itself, /// so the admin does not sit wondering whether the mail is coming. pub fn after_invite(&self) { let Some(engine) = self.0.clone() else { return; }; tokio::spawn(async move { let report = engine .deliver_owed(time::OffsetDateTime::now_utc(), 8) .await; Self::log(report); }); } /// Hands a recorded activity to the engine, off the request, the same way /// `after` hands a transition. /// /// The activity row is already written by the time this is called; this /// rehydrates it and lets the engine decide whether any rule owes a mail. /// `store` is the same store the request already wrote through — the /// engine has no public handle back to it, so the caller carries it. pub fn after_activity( &self, store: std::sync::Arc, activity_id: String, ) { let Some(engine) = self.0.clone() else { return; }; tokio::spawn(async move { let ev = store.event(&activity_id).await; let report = match ev { Ok(Some(iz_core::store::Event::Happened(ev))) => { match engine.on_activity(&ev).await { Ok(_) => { engine .deliver_owed(time::OffsetDateTime::now_utc(), 8) .await } Err(problem) => Err(problem), } } Ok(_) => return, Err(problem) => Err(problem), }; Self::log(report); }); } /// Hands a committed delete to the engine, off the request, the same way. /// /// A blocker being deleted frees the tasks that were waiting on it just as /// finishing it would, so the unblocked rule fires on both. The freeing is /// already written; this only reads it. pub fn after_freeing(&self, freeing: Freeing, freed: Vec) { let Some(engine) = self.0.clone() else { return; }; tokio::spawn(async move { let report = engine.on_freeing(&freeing, &freed).await; Self::log(report); }); } /// Sends one test mail and waits for the answer, because the answer is the /// whole point of pressing the button. `None` means this process has no /// engine at all, which happens only in tests. pub async fn test(&self, to: &str) -> Option> { let engine = self.0.clone()?; Some(engine.send_test(to).await) } /// Asks the mail server whether it would have us, sending nothing. pub async fn check(&self) -> Option> { let engine = self.0.clone()?; Some(engine.check_sender().await) } fn log(report: iz_core::store::Result) { match report { Ok(report) if report.sent + report.failed + report.abandoned > 0 => { println!( "iz mail {} sent, {} to retry, {} given up on", report.sent, report.failed, report.abandoned ); } Ok(_) => {} Err(problem) => eprintln!("iz mail the ledger could not be read: {problem}"), } } } /// The engine for this request, or a silent one when the router was built /// without an engine at all. pub fn mail(cx: &Cx) -> Mail { try_app_context::(cx) .cloned() .unwrap_or_else(Mail::silent) } /// Any cookie the request presented, by name — the client-set, non-`HttpOnly` /// ones (e.g. `iz_rows_
`) that JS reads and writes on its own. pub fn presented_cookie(cx: &Cx, name: &str) -> Option { cookies(cx).get(name).map(|c| c.value().to_string()) } /// The stylesheet this binary serves must be the one compiled into it. /// /// `asset!` embeds an asset's declaration — its id and source path — into /// the binary, but the served bytes live in the bundle directory beside the /// executable, and topcoat loads whatever manifest it finds there. A bundle /// left behind by another deploy sits beside a newer binary without a word /// of complaint, and the pages then reference a stylesheet whose bytes are /// from another generation — the mixed generation a browser once caught on /// production. `build.rs` stamps the compiled stylesheet's SHA-256 into the /// binary; this hashes the bundle's bytes against it, so a foreign bundle /// refuses the boot instead of serving under it. /// /// Returns the startup log line naming the served fingerprint, or the /// reason the boot must not proceed. pub fn stylesheet_guard(bundle: &AssetBundle) -> Result { let expected = env!("IZ_STYLE_FINGERPRINT"); let stylesheet = bundle .catalog() .assets() .find(|asset| { let name = asset.name(); name.starts_with("main-") && name.ends_with(".css") }) .ok_or_else(|| { format!( "the asset bundle at {} carries no stylesheet", bundle.dir().display() ) })?; let bytes = std::fs::read(bundle.dir().join(stylesheet.name())).map_err(|err| { format!( "the bundled stylesheet {} could not be read: {err}", stylesheet.name() ) })?; let digest = sha2::Sha256::digest(&bytes); let actual = format!( "sha256:{}", digest.as_slice().iter().map(|b| format!("{b:02x}")).collect::() ); if actual != expected { return Err(format!( "the asset bundle at {} is from another build: stylesheet {} is {actual} but this binary was compiled against {expected}; run `topcoat asset bundle` and redeploy", bundle.dir().display(), stylesheet.name() )); } Ok(format!( "assets stylesheet {} ({actual})", stylesheet.name() )) } /// The person behind this request, or nobody, or the store failing to say. /// /// im is asked on every request — the cookie holds an opaque session token, /// introspected per call — so an admin revoking the person in im signs them /// out here at once. The first sight of a `sub` inserts the local row (open /// provisioning: any im user who completes SSO gets one; a legacy row with /// the same address is claimed onto the new `sub`); every later sight /// refreshes the address and name, syncs the admin role from im's flag, and /// stamps the last sign-in time. /// /// A store error is not "nobody" — a busy database mid-drag is a fact about /// the database, not about whether this browser is signed in, and a caller /// that folded the two together would send a signed-in person to a sign-in /// screen because a write elsewhere held a lock for a moment. /// /// A disabled account reads as nobody: the cookie is im's business and is /// left untouched, but every guard below refuses as if signed out. #[memoize(as_ref)] pub async fn current_user(cx: &Cx) -> Result, StoreError> { let Some(claims) = iz_client::current_user(cx).await else { return Ok(None); }; let user = store(cx) .provision_user(&claims.sub, &claims.email, &claims.name, claims.admin) .await?; if user.disabled { return Ok(None); } Ok(Some(user)) } /// The person behind this request, or a refusal the caller can return as-is. /// /// A store error becomes `Refusal::Unavailable` rather than `SignInFirst`, /// which would tell a signed-in person to sign in again over what was only /// a database hiccup. pub async fn require_user(cx: &Cx) -> Result { match current_user(cx).await { Ok(Some(user)) => Ok(user.clone()), Ok(None) => Err(Refusal::SignInFirst), Err(error) => Err(error.clone().into()), } } /// The admin behind this request. A member or a viewer is refused *here*, not /// merely hidden from in the UI. pub async fn require_admin(cx: &Cx) -> Result { let user = require_user(cx).await?; if user.role.can_administer() { Ok(user) } else { Err(Refusal::Forbidden) } } /// The person behind this request if they may change the board. pub async fn require_writer(cx: &Cx) -> Result { let user = require_user(cx).await?; if user.role.can_write_tasks() { Ok(user) } else { Err(Refusal::Forbidden) } } /// The lightweight am-I-signed-in probe the live script's error path asks: /// `204` with no body when the caller carries a live session, `401` when /// anything about the caller — no cookie, a revoked or disabled one, a /// store that cannot answer — says otherwise. It exists so a dropped live /// stream can tell "the server restarted" from "I was signed out" without /// fetching a page of HTML to learn it. #[route(GET "/api/me")] async fn me(cx: &Cx) -> topcoat::Result { match current_user(cx).await { Ok(Some(_)) => StatusCode::NO_CONTENT.into_response(cx), _ => (StatusCode::UNAUTHORIZED, "").into_response(cx), } } impl From for Refusal { fn from(error: StoreError) -> Self { eprintln!("store error: {error}"); Refusal::Unavailable } } /// Everything a refused call is allowed to say. /// /// The codes are the contract's: a browser without script only ever sees /// them through the address bar, so each one has a short word form /// ([`Refusal::code`]) beside its sentence. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Refusal { /// Signed in, but refused here rather than merely hidden in the UI. Forbidden, /// No session, a revoked one, or a disabled account — deliberately one /// answer for all three. SignInFirst, /// A card with no title is not a card. EmptyTitle, /// A person with no name is not a person the board can show. EmptyName, /// A limit of nothing, or a limit wider than the disk should promise. BadLimit, /// A security number below its floor or past its ceiling. Not `BadLimit`: /// that sentence is about megabytes, and these knobs are counts, minutes /// and days. BadPolicy, /// The timezone field was not one of the offsets the form offers. /// The theme field was not one of the values the form offers. BadTheme, /// The ui field was not one of the values the form offers. BadUi, /// The language field was not one of the values the form offers. BadLanguage, /// The email field did not look like an address. BadEmail, /// Something in the allowed-types list is not a file extension. BadFileType, /// A public address that is not an http:// or https:// origin. BadOrigin, /// The sender panel was saved with a field it cannot work without, or with /// one that is not what it claims to be. The message names the field: this /// is a form somebody is filling in, not an attacker probing, and "that did /// not work" would send them round the panel guessing. BadSender(String), /// A comment with nothing in it. EmptyComment, /// The upload arrived without a file in it. NoFile, /// The bytes kept coming past what this workspace allows per file. FileTooBig, /// The name does not end in one of the extensions the admin allows. FileTypeNotAllowed, /// The bytes uploaded as a profile photo do not sniff as an image. NotAnImage, /// A rule with no subject line: the mail it sends would arrive blank. EmptySubject, /// A tag with no name is not a tag the board can show. EmptyTag, /// A tag with cards on it stays. Emptying it is the admin's job, and it /// is one they can see the size of. TagInUse, /// A message with no body: there is nothing for the recipient to read. EmptyBody, /// The date field did not hold a date. BadDeadline, /// The moment field's time box did not hold a 24h `HH:MM`, or a time /// arrived with no day to sit on. BadClock, /// The link asked for would put a task behind itself. Cycle, /// The card was already moved out of the column this request thought it /// was in, by somebody else, while this person was deciding. MovedAlready, /// A task headed for a done column while its subtasks are not done. The /// count is deliberately not in the sentence: the parent's card already /// carries it, and a number cannot survive the round trip through the /// address bar that a browser without script makes. SubtasksOpen, /// Subtasks go one level deep: the task named as a parent is itself a /// part, or the task being filed already has parts of its own. NotNestable, /// No such task — or none this account may see. Deliberately one answer for /// both. NotFound, /// A recipient id that names nobody in this workspace. NoSuchMember, /// The address typed into the add-member form already has a row here. AlreadyMember, /// in refused the push: the bytes iz holds have spent the limit im set. StorageQuota, /// Storage through the Files service was asked for while `[storage.in]` /// holds no token — there is no key to speak with. StorageNotConfigured, /// The Files service is not in the family mirror, so there is no URL to /// push to. im's panel puts it there; iz does not guess. StorageNotListed, /// The Files service did not answer — down, mid-deploy, or the wire /// between. An upload is not queued for later; it is refused now. StorageUnavailable, Unavailable, } impl Refusal { /// The refusal in words. pub fn message(&self) -> String { match self { Refusal::Forbidden => "Not permitted.".to_string(), Refusal::SignInFirst => "Sign in first.".to_string(), Refusal::EmptyTitle => "Give the task a title.".to_string(), Refusal::EmptyName => "Give yourself a name.".to_string(), Refusal::BadLimit => { "A limit has to be at least 1 MB, and no wider than 500 MB per file.".to_string() } Refusal::BadPolicy => "A security number is out of range.".to_string(), Refusal::BadTheme => "That is not a theme.".to_string(), Refusal::BadUi => "That is not an interface.".to_string(), Refusal::BadLanguage => "That is not a language.".to_string(), Refusal::BadEmail => "That is not an address.".to_string(), Refusal::BadFileType => { "File types are extensions — png, pdf, zip — separated by commas.".to_string() } Refusal::BadOrigin => "Not an http:// or https:// address.".to_string(), Refusal::BadSender(problem) => problem.clone(), Refusal::EmptyComment => "Write something first.".to_string(), Refusal::NoFile => "Choose a file first.".to_string(), Refusal::FileTooBig => "Too big for this workspace.".to_string(), Refusal::FileTypeNotAllowed => { "That kind of file is not on this workspace's allowed list.".to_string() } Refusal::NotAnImage => "That is not an image.".to_string(), Refusal::EmptySubject => "Give the rule a subject line.".to_string(), Refusal::EmptyTag => "Give the tag a name.".to_string(), Refusal::TagInUse => "This tag still has cards.".to_string(), Refusal::EmptyBody => "Write something first.".to_string(), Refusal::BadDeadline => "That is not a date.".to_string(), Refusal::BadClock => "That is not a time.".to_string(), Refusal::Cycle => "That link would put this task behind itself.".to_string(), Refusal::MovedAlready => "Somebody moved this card first.".to_string(), Refusal::SubtasksOpen => "Subtasks are still open.".to_string(), Refusal::NotNestable => "Subtasks go one level deep.".to_string(), Refusal::NotFound => "No such task.".to_string(), Refusal::NoSuchMember => "No such member.".to_string(), Refusal::AlreadyMember => "Already a member.".to_string(), Refusal::StorageNotConfigured => "The Files service has no key.".to_string(), Refusal::StorageNotListed => "The Files service is not in the family.".to_string(), Refusal::StorageUnavailable => "The Files service is unreachable.".to_string(), Refusal::StorageQuota => "The Files service is full.".to_string(), Refusal::Unavailable => "Something went wrong.".to_string(), } } /// `message()`, in a user's language. English is the canonical /// sentence; the Turkish match arms every variant — the compiler holds /// it exhaustive, so a refusal cannot land untranslated — and /// `BadSender` falls back to English because its sentence is built /// where the complaint is decided, already in the caller's language. pub fn message_in(&self, lang: crate::i18n::Lang) -> String { use crate::i18n::Lang::Tr; if lang != Tr { return self.message(); } match self { Refusal::Forbidden => "İzin verilmiyor.".to_string(), Refusal::SignInFirst => "Önce oturum aç.".to_string(), Refusal::EmptyTitle => "Göreve bir başlık ver.".to_string(), Refusal::EmptyComment => "Önce bir şey yaz.".to_string(), Refusal::NoFile => "Önce bir dosya seç.".to_string(), Refusal::FileTooBig => "Bu çalışma alanı için çok büyük.".to_string(), Refusal::FileTypeNotAllowed => { "Bu dosya türü bu çalışma alanının izin verdiği listede değil.".to_string() } Refusal::NotAnImage => "Bu bir resim değil.".to_string(), Refusal::BadDeadline => "Bu bir tarih değil.".to_string(), Refusal::BadClock => "Bu bir saat değil.".to_string(), Refusal::Cycle => "Bu bağlantı görevi kendi arkasına koyar.".to_string(), Refusal::MovedAlready => "Bu kartı başka biri zaten taşıdı.".to_string(), Refusal::SubtasksOpen => "Alt görevler hâlâ açık.".to_string(), Refusal::NotNestable => "Alt görevler tek seviyedir.".to_string(), Refusal::NotFound => "Böyle bir görev yok.".to_string(), Refusal::NoSuchMember => "Böyle bir üye yok.".to_string(), Refusal::AlreadyMember => "Zaten üye.".to_string(), Refusal::StorageNotConfigured => "Dosyalar hizmetinin anahtarı yok.".to_string(), Refusal::StorageNotListed => "Dosyalar hizmeti ailede değil.".to_string(), Refusal::StorageUnavailable => "Dosyalar hizmetine erişilemiyor.".to_string(), Refusal::StorageQuota => "Dosyalar hizmeti dolu.".to_string(), Refusal::Unavailable => "Bir şeyler ters gitti.".to_string(), Refusal::BadLimit => { "Limit en az 1 MB, dosya başına en çok 500 MB olabilir.".to_string() } Refusal::BadPolicy => "Güvenlik sayısı aralığın dışında.".to_string(), Refusal::BadTheme => "Bu bir tema değil.".to_string(), Refusal::BadUi => "Bu bir arayüz değil.".to_string(), Refusal::BadLanguage => "Bu bir dil değil.".to_string(), Refusal::BadEmail => "Bu bir adres değil.".to_string(), Refusal::BadFileType => { "Dosya türleri virgülle ayrılmış uzantılardır — png, pdf, zip.".to_string() } Refusal::BadOrigin => "http:// veya https:// adresi değil.".to_string(), // `BadSender`'s sentence is built where the complaint is // decided (`settings.rs`), already in the caller's language — // this is only reached for a request with no admin to read a // language off of, so it falls back to English like the rest. Refusal::BadSender(_) => self.message(), Refusal::EmptySubject => "Kurala bir konu ver.".to_string(), Refusal::EmptyTag => "Etikete bir ad ver.".to_string(), Refusal::TagInUse => "Bu etikette kartlar var.".to_string(), Refusal::EmptyBody => "Önce bir şey yaz.".to_string(), Refusal::EmptyName => "Kendine bir isim ver.".to_string(), } } /// The refusal a `code` names, or nothing. Nothing for an unknown word: the /// query is whatever the address bar holds, so a code that is not one of /// ours says nothing at all rather than something invented. pub fn from_code(code: &str) -> Option { Some(match code { "forbidden" => Refusal::Forbidden, "sign-in-first" => Refusal::SignInFirst, "empty-title" => Refusal::EmptyTitle, "empty-name" => Refusal::EmptyName, "bad-limit" => Refusal::BadLimit, "bad-policy" => Refusal::BadPolicy, "bad-theme" => Refusal::BadTheme, "bad-ui" => Refusal::BadUi, "bad-language" => Refusal::BadLanguage, "bad-email" => Refusal::BadEmail, "bad-file-type" => Refusal::BadFileType, "bad-origin" => Refusal::BadOrigin, // The specific complaint (which field, and why) lives only in the // response the save itself returned; a code that survived a round // trip through the address bar carries no more than this. "bad-sender" => Refusal::BadSender("That sender setting will not work.".to_string()), "empty-comment" => Refusal::EmptyComment, "no-file" => Refusal::NoFile, "file-too-big" => Refusal::FileTooBig, "file-type" => Refusal::FileTypeNotAllowed, "not-an-image" => Refusal::NotAnImage, "empty-subject" => Refusal::EmptySubject, "empty-tag" => Refusal::EmptyTag, "tag-in-use" => Refusal::TagInUse, "empty-body" => Refusal::EmptyBody, "bad-deadline" => Refusal::BadDeadline, "bad-clock" => Refusal::BadClock, "cycle" => Refusal::Cycle, "moved-already" => Refusal::MovedAlready, "subtasks-open" => Refusal::SubtasksOpen, "not-nestable" => Refusal::NotNestable, "not-found" => Refusal::NotFound, "no-such-member" => Refusal::NoSuchMember, "already-member" => Refusal::AlreadyMember, "storage-quota" => Refusal::StorageQuota, "storage-not-configured" => Refusal::StorageNotConfigured, "storage-not-listed" => Refusal::StorageNotListed, "storage-unavailable" => Refusal::StorageUnavailable, "unavailable" => Refusal::Unavailable, _ => return None, }) } /// The refusal as a short word, for the address bar. /// /// A browser without script never sees a call's return value: it posts the /// form, follows the redirect, and the page it lands on has to be told what /// happened. That telling goes through the query, so every refusal needs a /// name that survives a round trip through a URL. pub fn code(&self) -> &'static str { match self { Refusal::Forbidden => "forbidden", Refusal::SignInFirst => "sign-in-first", Refusal::EmptyTitle => "empty-title", Refusal::EmptyName => "empty-name", Refusal::BadLimit => "bad-limit", Refusal::BadPolicy => "bad-policy", Refusal::BadTheme => "bad-theme", Refusal::BadUi => "bad-ui", Refusal::BadLanguage => "bad-language", Refusal::BadEmail => "bad-email", Refusal::BadFileType => "bad-file-type", Refusal::BadOrigin => "bad-origin", Refusal::BadSender(_) => "bad-sender", Refusal::EmptyComment => "empty-comment", Refusal::NoFile => "no-file", Refusal::FileTooBig => "file-too-big", Refusal::FileTypeNotAllowed => "file-type", Refusal::NotAnImage => "not-an-image", Refusal::EmptySubject => "empty-subject", Refusal::EmptyTag => "empty-tag", Refusal::TagInUse => "tag-in-use", Refusal::EmptyBody => "empty-body", Refusal::BadDeadline => "bad-deadline", Refusal::BadClock => "bad-clock", Refusal::Cycle => "cycle", Refusal::MovedAlready => "moved-already", Refusal::SubtasksOpen => "subtasks-open", Refusal::NotNestable => "not-nestable", Refusal::NotFound => "not-found", Refusal::NoSuchMember => "no-such-member", Refusal::AlreadyMember => "already-member", Refusal::StorageNotConfigured => "storage-not-configured", Refusal::StorageNotListed => "storage-not-listed", Refusal::StorageUnavailable => "storage-unavailable", Refusal::StorageQuota => "storage-quota", Refusal::Unavailable => "unavailable", } } } /// Which call a carried refusal belongs to. Two forms on one page must not both /// claim the same sentence, so the redirect names the call and this is the name: /// the last piece of the server function's path, with anything that is not a /// plain word dropped so it can only ever be compared, never rendered. pub fn call_id(path: &str) -> String { path.rsplit('/') .next() .unwrap_or(path) .chars() .filter(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-') .collect() } /// What a call refused with, however the browser without script carried it /// back — read straight off the query, since there is no client-side action /// value to check first the way a hydrated page in the old UI had. /// /// Every topcoat page is rendered server-side on every request, so there is /// only ever the query to read. pub fn refusal_of(cx: &Cx, call: &str) -> Option { let query = topcoat::router::request::uri(cx).query()?; let mut code = None; let mut on = None; for pair in query.split('&') { if let Some((key, value)) = pair.split_once('=') { match key { "refusal" => code = Some(value), "on" => on = Some(value), _ => {} } } } if on? != call { return None; } Refusal::from_code(code?) } /// The page a form was posted from: the `Referer`, with the answer any /// earlier post left on its query dropped, or `nowhere` when no `Referer` /// came with the request. /// /// The feedback pairs — `refusal=`, `on=`, `why=`, `saved=` — are how a page /// renders what the last post did. Sending the browser back with them still /// on the query re-renders that old answer under the new post's own: a change /// that succeeded right after one that was refused announces the refusal /// again, and reads as having failed. The answer this redirect carries — the /// pairs it was built with, or the body [`carry_refusal_on_redirect`] copies /// onto the query — is the one that shows; an earlier one never survives the /// trip. pub fn back_to(cx: &Cx, nowhere: &str) -> String { let referer = headers(cx) .get(header::REFERER) .and_then(|value| value.to_str().ok()) .unwrap_or(nowhere); let (path, query) = referer.split_once('?').unwrap_or((referer, "")); let pairs: Vec<&str> = query .split('&') .filter(|pair| { !pair.is_empty() && !["refusal", "on", "why", "saved"].iter().any(|key| { pair.strip_prefix(*key) .is_some_and(|rest| rest.starts_with('=')) }) }) .collect(); if pairs.is_empty() { return path.to_string(); } format!("{path}?{}", pairs.join("&")) } /// Puts a refusal on the redirect a browser without script follows. /// /// A hydrated page reads the call's return value straight off the action. A /// browser without script has no such thing: it posts the form, the server /// function handler answers with a redirect back to the page it came from, and /// the value — the whole refusal — sits in a body nobody will ever look at. /// The click then looks like nothing happening, which is the worst answer /// iz can give. /// /// So the refusal is copied onto the `Location`, as `?refusal=&on=`, /// and the page renders it from the query. This is one place rather than /// thirty-eight because the shape is the same for every refusing call, present /// and future: nothing here knows what any of them do. /// /// Requests carrying script are untouched — they are answered with the value /// itself and never see a redirect. /// /// Ported from axum's `middleware::from_fn` onto a topcoat `#[layer]`. topcoat's /// Post/Redirect/Get helper (`error::see_other`) answers with `303 See Other`, /// not the `302 Found` axum's server-function redirect used, so the status this /// checks for is `SEE_OTHER` — the guard's three conditions are otherwise /// unchanged. #[topcoat::router::layer("/api")] async fn carry_refusal_on_redirect( cx: &Cx, body: Body, next: Next<'_>, ) -> topcoat::Result { // A form post from a browser asks for a page back. A server-function call // from the hydrated bundle does not. let wants_page = headers(cx) .get(header::ACCEPT) .and_then(|value| value.to_str().ok()) .is_some_and(|value| value.contains("text/html")); let called = call_id(topcoat::router::request::uri(cx).path()); let has_referer = headers(cx).contains_key(header::REFERER); let response = next.run(cx, body).await?; if !wants_page || !has_referer || response.status() != StatusCode::SEE_OTHER { return Ok(response); } let (mut parts, body) = response.into_parts(); // The body of one of these redirects is a serialised `Option` and // nothing else; the cap is there so a response that is something else // entirely cannot be read into memory whole. A body that fails to parse — // an empty one included, the shape a route with nothing to say back sends // — is read as "no refusal", never as "leave the Location alone": the // Referer sanitization below has to run on every redirect this layer // sees, not only the ones that happen to carry a refusal. let Ok(bytes) = to_bytes(body, 64 * 1024).await else { return Ok(Response::from_parts(parts, Body::empty())); }; let refusal = serde_json::from_slice::>(&bytes) .ok() .flatten(); if let Some(location) = parts .headers .get(header::LOCATION) .and_then(|v| v.to_str().ok()) { let rewritten = match refusal { Some(refusal) => carrying(location, refusal.code(), &called), None => Some(same_origin(location).to_string()), }; if let Some(carried) = rewritten && let Ok(value) = HeaderValue::from_str(&carried) { parts.headers.insert(header::LOCATION, value); } } Ok(Response::from_parts(parts, Body::from(bytes))) } /// `location` with the refusal in its query. /// /// The redirect goes back to the page the form was posted from, and that page /// may already carry a query — `?task=DZ-01` is how a browser without script /// opens the modal at all — so the two pairs are merged in, and the pair from /// any earlier refusal is dropped rather than stacked on top of. fn carrying(location: &str, code: &str, called: &str) -> Option { if called.is_empty() { return None; } // The Location we are rewriting came from the form post's Referer, and on a // cross-origin post the Referer is whatever the other site is. Sending the // browser back there would make iz an open redirect, so the address is // rebuilt from its path and query alone and anything that is not a plain // absolute path is answered with the board. let here = same_origin(location); let (path, query) = match here.split_once('?') { Some((path, query)) => (path, query), None => (here, ""), }; let mut pairs: Vec = query .split('&') .filter(|pair| { !pair.is_empty() && !pair.starts_with("refusal=") && !pair.starts_with("on=") }) .map(str::to_string) .collect(); pairs.push(format!("refusal={code}&on={called}")); Some(format!("{path}?{}", pairs.join("&"))) } /// The path and query of `location`, with scheme and authority dropped. A /// protocol-relative address (`//elsewhere.example/`) is another host wearing a /// path's clothes, and a browser reads a backslash there as a slash, so both /// are answered with the board rather than trusted. fn same_origin(location: &str) -> &str { let rest = match location.split_once("://") { Some((_scheme, rest)) => match rest.find(['/', '?']) { Some(at) => &rest[at..], None => "/", }, None => location, }; let mut characters = rest.chars(); match (characters.next(), characters.next()) { (Some('/'), Some('/' | '\\')) => "/", (Some('/'), _) => rest, _ => "/", } } /// The cache directives a response cannot choose for itself. /// /// HTML is revalidated on every load: a page the browser kept from before a /// deploy is the old app answering under the new one's address, which is how a /// fixed bug once came back after shipping. So every response whose body is a /// document gets `no-cache`. Responses that already carry a directive keep it — /// topcoat stamps the fingerprinted assets under `/_topcoat/assets` with a year /// of `immutable` and the live stream with `no-cache`, and the photo and /// attachment handlers stamp their own — and everything else (redirects, JSON /// answers) ships none, the idiom they already ride on: no directive, nothing /// cached. One layer at `/`, because the decision is about what the bytes are, /// not which route built them. #[topcoat::router::layer("/")] async fn cache_directives(cx: &Cx, body: Body, next: Next<'_>) -> topcoat::Result { let response = next.run(cx, body).await?; let (mut parts, body) = response.into_parts(); let is_html = parts .headers .get(header::CONTENT_TYPE) .and_then(|value| value.to_str().ok()) .is_some_and(|value| value.starts_with("text/html")); if !is_html || parts.headers.contains_key(header::CACHE_CONTROL) { return Ok(Response::from_parts(parts, body)); } parts .headers .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache")); Ok(Response::from_parts(parts, body)) } #[cfg(test)] mod refusal_redirect_tests { use super::carrying; #[test] fn a_bare_address_gains_a_query() { assert_eq!( carrying("http://iz.sh/", "cycle", "link_tasks").as_deref(), Some("/?refusal=cycle&on=link_tasks") ); } #[test] fn an_open_modal_stays_open() { assert_eq!( carrying("http://iz.sh/?task=DZ-01", "cycle", "link_tasks").as_deref(), Some("/?task=DZ-01&refusal=cycle&on=link_tasks") ); } #[test] fn a_second_refusal_replaces_the_first() { assert_eq!( carrying( "http://iz.sh/?task=DZ-01&refusal=cycle&on=link_tasks", "not-found", "link_tasks" ) .as_deref(), Some("/?task=DZ-01&refusal=not-found&on=link_tasks") ); } // The Referer of a cross-origin post is the other site's address, and it // reaches this function as the Location. iz answers on its own ground or // not at all. #[test] fn another_site_cannot_be_redirected_to() { for elsewhere in [ "https://elsewhere.example", "//elsewhere.example/steal", "/\\elsewhere.example/steal", "javascript:alert(1)", "", ] { assert_eq!( carrying(elsewhere, "cycle", "link_tasks").as_deref(), Some("/?refusal=cycle&on=link_tasks"), "{elsewhere} was not brought home" ); } // An address with a path keeps the path — it is read as a path on this // site, which is the point: whatever the Referer claimed, the browser // is sent somewhere on iz. let carried = carrying( "http://elsewhere.example/steal?task=DZ-01", "cycle", "link_tasks", ); assert_eq!( carried.as_deref(), Some("/steal?task=DZ-01&refusal=cycle&on=link_tasks") ); } #[test] fn a_path_on_this_site_is_kept() { assert_eq!( carrying("/board?task=DZ-01", "cycle", "link_tasks").as_deref(), Some("/board?task=DZ-01&refusal=cycle&on=link_tasks") ); } #[test] fn a_call_with_no_name_carries_nothing() { assert_eq!(carrying("http://iz.sh/", "cycle", ""), None); } } #[cfg(test)] mod refusal_message_tests { use super::Refusal; #[test] fn every_refusal_survives_the_address_bar() { let all = [ Refusal::Forbidden, Refusal::SignInFirst, Refusal::EmptyTitle, Refusal::EmptyComment, Refusal::NoFile, Refusal::FileTooBig, Refusal::FileTypeNotAllowed, Refusal::BadDeadline, Refusal::BadClock, Refusal::BadTheme, Refusal::BadLanguage, Refusal::BadEmail, Refusal::Cycle, Refusal::MovedAlready, Refusal::SubtasksOpen, Refusal::NotNestable, Refusal::NotFound, Refusal::NoSuchMember, Refusal::AlreadyMember, Refusal::TagInUse, Refusal::BadLimit, Refusal::BadPolicy, Refusal::Unavailable, ]; for refusal in all { assert_eq!( Refusal::from_code(refusal.code()).as_ref(), Some(&refusal), "{} did not come back as itself", refusal.code() ); } } #[test] fn an_unknown_code_says_nothing() { assert_eq!(Refusal::from_code("not-a-refusal"), None); assert_eq!(Refusal::from_code(""), None); } } #[cfg(test)] mod refusal_of_tests { use super::{Cx, Refusal, refusal_of}; use topcoat::context::CxTestBuilder; fn cx_at(uri: &str) -> Cx { let (parts, ()) = http::Request::builder() .uri(uri) .body(()) .unwrap() .into_parts(); CxTestBuilder::new().request_context(parts).build() } #[test] fn reads_a_matching_refusal_off_the_query() { let cx = cx_at("/?refusal=cycle&on=sign_in"); assert_eq!(refusal_of(&cx, "sign_in"), Some(Refusal::Cycle)); } #[test] fn a_refusal_for_another_call_is_not_this_one() { let cx = cx_at("/?refusal=cycle&on=link_tasks"); assert_eq!(refusal_of(&cx, "sign_in"), None); } #[test] fn no_query_carries_nothing() { let cx = cx_at("/"); assert_eq!(refusal_of(&cx, "sign_in"), None); } } #[cfg(test)] mod call_id_tests { use super::call_id; #[test] fn a_call_is_named_by_its_last_path_piece() { assert_eq!(call_id("/api/link_tasks"), "link_tasks"); assert_eq!(call_id("/api/link_tasks?x=1"), "link_tasksx1"); } } #[cfg(test)] mod client_label_tests { use std::net::SocketAddr; use topcoat::router::response::IntoResponse; use topcoat::router::{ Body, Method, RemoteAddr, RouteFn, RouteFuture, Router, request::Request, to_bytes, }; use super::{client_label, trusted_proxies}; fn echo(cx: &topcoat::context::Cx, _body: Body) -> RouteFuture<'_> { Box::pin(async move { client_label(cx).into_response(cx) }) } fn router(trust_one_hop: bool) -> Router { let builder = Router::builder().route(RouteFn::new(Method::GET, "/label", echo)); if trust_one_hop { builder.trusted_proxies(trusted_proxies()).build() } else { builder.build() } } async fn label(trust_one_hop: bool, peer: &str, forwarded: Option<&str>) -> String { let mut builder = Request::builder() .method(Method::GET) .uri("/label") .extension(RemoteAddr(peer.parse::().unwrap())); if let Some(value) = forwarded { builder = builder.header("x-forwarded-for", value); } let response = router(trust_one_hop) .handle(builder.body(Body::empty()).unwrap()) .await; String::from_utf8(to_bytes(response.into_body(), 64).await.unwrap().to_vec()).unwrap() } #[tokio::test] async fn one_trusted_hop_reports_the_client_not_the_proxy() { let got = label(true, "10.0.0.1:4242", Some("198.51.100.1")).await; assert_eq!(got, "198.51.100.1"); } #[tokio::test] async fn a_missing_header_falls_back_to_the_peer() { let got = label(true, "203.0.113.9:4242", None).await; assert_eq!(got, "203.0.113.9"); } #[tokio::test] async fn an_untrusted_peer_cannot_spoof_the_forwarded_header() { let got = label(false, "203.0.113.9:4242", Some("198.51.100.1")).await; assert_eq!(got, "203.0.113.9"); } }