diff --git a/Cargo.lock b/Cargo.lock index cd308dfc..0071e810 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,7 +397,7 @@ dependencies = [ "serde", "serde_json", "url", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -3486,7 +3486,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -5120,7 +5120,7 @@ dependencies = [ "tokio-util", "twox-hash 2.1.5", "url", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -6897,7 +6897,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams 0.4.2", "web-sys", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -7287,9 +7287,9 @@ checksum = "eec689c0bc40ff2458a5977b6619cb718087084a18e02a131c599b62d05e1a5f" [[package]] name = "rustls-webpki" -version = "0.103.15" +version = "0.103.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +checksum = "e3a0760a887c14823fca5a1c970b01d47c328c696c0c64829d1df50e8f853fd5" dependencies = [ "aws-lc-rs", "ring", @@ -9719,9 +9719,9 @@ dependencies = [ [[package]] name = "webpki-root-certs" -version = "1.0.9" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b96554aa2acc8ccdb7e1c9a58a7a68dd5d13bccc69cd124cb09406db612a1c9b" +checksum = "293198dcebfb821155b5092ab94b5a82ff180eddd7241df3bd6e66a4ffd1a585" dependencies = [ "rustls-pki-types", ] @@ -9732,14 +9732,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" dependencies = [ - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] name = "webpki-roots" -version = "1.0.9" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +checksum = "7265c9301c16a8711c992c2d8da90ee77ef54b1ed51482d6dfeb00dc756b1297" dependencies = [ "rustls-pki-types", ] @@ -10367,7 +10367,7 @@ dependencies = [ "tokio", "tokio-util", "tracing", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", "x509-parser", "zenoh-buffers", "zenoh-codec", @@ -10451,7 +10451,7 @@ dependencies = [ "tokio-rustls", "tokio-util", "tracing", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", "x509-parser", "zenoh-config", "zenoh-core", diff --git a/crates/common/src/auth/permissions.rs b/crates/common/src/auth/permissions.rs index f18b0d5a..1c0a1e00 100644 --- a/crates/common/src/auth/permissions.rs +++ b/crates/common/src/auth/permissions.rs @@ -114,6 +114,12 @@ impl Server { // inbuxa: DL-20: the lists and the check are the server's Permission::SysDeliverabilityUpdate, Permission::SysDeliverabilityCheck, + // inbuxa: system Sieve scripts run with the trusted functions, + // whose `exec` starts any program as the server's user: no tenant + // policy hands that out + Permission::SysSieveSystemScriptCreate, + Permission::SysSieveSystemScriptUpdate, + Permission::SysSieveSystemScriptDestroy, ] { permissions.disabled.set(permission as usize); } diff --git a/crates/common/src/config/network.rs b/crates/common/src/config/network.rs index 0b95d210..232ebcb4 100644 --- a/crates/common/src/config/network.rs +++ b/crates/common/src/config/network.rs @@ -79,6 +79,10 @@ pub struct Http { /// inbuxa: peers whose X-Forwarded-For / Forwarded headers are believed. /// Loopback and private networks, or `INBUXA_HTTP_TRUSTED_PROXIES`. pub trusted_proxies: Vec, + /// inbuxa: the one forwarding header the trusted proxies write. + /// `INBUXA_HTTP_FORWARDED_HEADER`, X-Forwarded-For unless it says + /// `forwarded`. + pub forwarded_header: ForwardedHeader, pub redirect_root: Option, /// inbuxa: HTTP Basic accepted on every endpoint, not only DAV (contract /// C-23). True in bootstrap and recovery mode, or with @@ -517,6 +521,30 @@ impl Http { _ => default_trusted_proxies(), }; + // inbuxa: read only the header the proxy writes; it passes any other + // through as the client sent it + let forwarded_header = match types::branding::env_var("HTTP_FORWARDED_HEADER") { + Ok(value) if value.trim().eq_ignore_ascii_case("forwarded") => { + ForwardedHeader::Forwarded + } + Ok(value) + if value.trim().is_empty() + || value.trim().eq_ignore_ascii_case("x-forwarded-for") => + { + ForwardedHeader::XForwardedFor + } + Ok(value) => { + bp.build_warning( + ObjectType::Http.singleton(), + format!( + "INBUXA_HTTP_FORWARDED_HEADER is {value:?}; expected \"x-forwarded-for\" or \"forwarded\". X-Forwarded-For is read." + ), + ); + ForwardedHeader::XForwardedFor + } + Err(_) => ForwardedHeader::XForwardedFor, + }; + if use_permissive_cors { http_headers.push(( hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN, @@ -576,12 +604,23 @@ impl Http { cors_origins, use_forwarded: http.use_x_forwarded, trusted_proxies, + forwarded_header, redirect_root: http.redirect_root, basic_auth_everywhere, } } } +/// inbuxa: the forwarding header a trusted proxy writes. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum ForwardedHeader { + /// `X-Forwarded-For` (nginx, Caddy, Traefik, HAProxy `option forwardfor`) + #[default] + XForwardedFor, + /// RFC 7239 `Forwarded` (HAProxy `option forwarded`) + Forwarded, +} + // inbuxa: loopback and the private ranges, where a reverse proxy on the same // host, container network or LAN connects from. fn default_trusted_proxies() -> Vec { diff --git a/crates/http-proto/src/response.rs b/crates/http-proto/src/response.rs index b5e15197..f24a8387 100644 --- a/crates/http-proto/src/response.rs +++ b/crates/http-proto/src/response.rs @@ -276,7 +276,20 @@ impl HttpResponse { .boxed(), ), } - .unwrap() + .unwrap_or_else(|_| { + // inbuxa: a header that isn't a valid header value (a client's + // text copied into it) fails the request, not the connection + let mut response = hyper::Response::new( + Full::new(Bytes::new()) + .map_err(|never| match never {}) + .boxed(), + ); + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + response + .headers_mut() + .insert(header::CONTENT_LENGTH, HeaderValue::from_static("0")); + response + }) } pub fn body(&self) -> &HttpResponseBody { diff --git a/crates/http/src/auth/oauth/auth.rs b/crates/http/src/auth/oauth/auth.rs index ce488cd5..5f41f0cc 100644 --- a/crates/http/src/auth/oauth/auth.rs +++ b/crates/http/src/auth/oauth/auth.rs @@ -19,7 +19,7 @@ use common::{ CLIENT_ID_MAX_LEN, DEVICE_CODE_LEN, SUPPORTED_SCOPES, USER_CODE_ALPHABET, USER_CODE_LEN, client_id::{decode_client_id, scopes_to_mask}, - registration::redirect_uri_matches, + registration::{redirect_uri_matches, validate_redirect_uri}, }, }, }; @@ -232,6 +232,22 @@ impl OAuthApiHandler for Server { None => scope, }; + // inbuxa: with registration not required, a client nobody + // registered may only redirect where a web site can't listen: + // a loopback address or a private-use scheme (RFC 8252). A + // web application registers, so its redirect is checked. + let unregistered = stateless_client.is_none() + && !self.core.oauth.require_client_authentication + && !self.is_registered_client(&client_id).await?; + if unregistered + && (redirect_uri.starts_with("https://") + || validate_redirect_uri(&redirect_uri).is_err()) + { + return Err(trc::AuthEvent::Error.into_err().details( + "An unregistered client may only redirect to a loopback address or a private-use scheme.", + )); + } + // Validate Resource Indicators (RFC 8707) for resource in &resource { if !is_known_resource( @@ -275,7 +291,10 @@ impl OAuthApiHandler for Server { } }, None => { - if stateless_client.is_some() { + // inbuxa: a client nobody registered, accepted only + // because registration isn't required, proves itself + // with PKCE too + if stateless_client.is_some() || unregistered { return Err(trc::AuthEvent::Error.into_err().details( "A PKCE code_challenge with the S256 method is required.", )); diff --git a/crates/http/src/auth/oauth/registration.rs b/crates/http/src/auth/oauth/registration.rs index 310f2403..4931c10f 100644 --- a/crates/http/src/auth/oauth/registration.rs +++ b/crates/http/src/auth/oauth/registration.rs @@ -46,6 +46,11 @@ pub trait ClientRegistrationHandler: Sync + Send { session: HttpSessionData, ) -> impl Future> + Send; + fn is_registered_client( + &self, + client_id: &str, + ) -> impl Future> + Send; + fn validate_client_registration( &self, client_id: &str, @@ -216,6 +221,19 @@ impl ClientRegistrationHandler for Server { .into_http_response()) } + /// inbuxa: whether `client_id` is an `x:OAuthClient`. + async fn is_registered_client(&self, client_id: &str) -> trc::Result { + Ok(self + .registry() + .primary_key( + ObjectType::OAuthClient.into(), + Property::ClientId, + client_id.as_bytes().to_vec(), + ) + .await? + .is_some()) + } + async fn validate_client_registration( &self, client_id: &str, diff --git a/crates/http/src/client_ip.rs b/crates/http/src/client_ip.rs index e2309f9a..91903f42 100644 --- a/crates/http/src/client_ip.rs +++ b/crates/http/src/client_ip.rs @@ -10,8 +10,14 @@ //! client is the right-most address in the chain that no trusted proxy //! accounts for. Everything to its left was written by the client and can //! say anything. Forwarding headers are read only when the connection itself -//! comes from a trusted proxy. +//! comes from a trusted proxy, and only the one header that proxy writes: a +//! proxy appends to its own header and passes any other through as the client +//! sent it. +//! +//! A loopback address in a header is the loopback of whoever wrote it, so it +//! stands for this host only when every hop after it is loopback too. +use common::config::network::ForwardedHeader; use hyper::HeaderMap; use hyper::header::FORWARDED; use registry::types::ipmask::IpAddrOrMask; @@ -28,23 +34,36 @@ pub fn forwarded_client( peer: IpAddr, headers: &HeaderMap, trusted: &[IpAddrOrMask], + header: ForwardedHeader, ) -> Option { if !is_trusted_proxy(trusted, &peer) { return None; } - let chain = forwarded_chain(headers).or_else(|| x_forwarded_chain(headers))?; + // Never fall back from one header to the other: the one the proxy + // doesn't write is entirely the client's. + let chain = match header { + ForwardedHeader::XForwardedFor => x_forwarded_chain(headers), + ForwardedHeader::Forwarded => forwarded_chain(headers), + }?; let mut client = None; + let mut writer = peer.to_canonical(); for hop in chain.into_iter().rev() { // An address that can't be read ends the chain: what's left of it // can't be told apart from what the client wrote. let Some(ip) = hop else { break; }; + // Loopback as written by a proxy on another host is that host's, + // not ours: the client is as far as we can vouch for, the writer. + if ip.is_loopback() && !writer.is_loopback() { + return Some(writer); + } client = Some(ip); if !is_trusted_proxy(trusted, &ip) { break; } + writer = ip; } client } @@ -124,12 +143,24 @@ mod tests { .collect() } - fn client(peer: &str, headers: &[(&'static str, &str)]) -> Option { + fn client_with( + header: ForwardedHeader, + peer: &str, + headers: &[(&'static str, &str)], + ) -> Option { let mut map = HeaderMap::new(); for (name, value) in headers { map.append(*name, HeaderValue::from_str(value).unwrap()); } - forwarded_client(peer.parse().unwrap(), &map, &trusted()) + forwarded_client(peer.parse().unwrap(), &map, &trusted(), header) + } + + fn client(peer: &str, headers: &[(&'static str, &str)]) -> Option { + client_with(ForwardedHeader::XForwardedFor, peer, headers) + } + + fn client_fwd(peer: &str, headers: &[(&'static str, &str)]) -> Option { + client_with(ForwardedHeader::Forwarded, peer, headers) } fn ip(ip: &str) -> Option { @@ -176,7 +207,7 @@ mod tests { None ); assert_eq!( - client("192.0.2.10", &[("forwarded", "for=203.0.113.7")]), + client_fwd("192.0.2.10", &[("forwarded", "for=203.0.113.7")]), None ); } @@ -185,45 +216,83 @@ mod tests { fn forwarded_header() { let fwd = "forwarded"; assert_eq!( - client( + client_fwd( "127.0.0.1", &[(fwd, "for=198.51.100.1, for=203.0.113.7;proto=https")] ), ip("203.0.113.7") ); assert_eq!( - client( + client_fwd( "127.0.0.1", &[(fwd, r#"For="[2001:db8::7]:4711", for=10.0.0.5"#)] ), ip("2001:db8::7") ); assert_eq!( - client("127.0.0.1", &[(fwd, "for=203.0.113.7:8080")]), + client_fwd("127.0.0.1", &[(fwd, "for=203.0.113.7:8080")]), ip("203.0.113.7") ); // An obfuscated hop ends the chain assert_eq!( - client("127.0.0.1", &[(fwd, "for=_hidden, for=10.0.0.5")]), + client_fwd("127.0.0.1", &[(fwd, "for=_hidden, for=10.0.0.5")]), ip("10.0.0.5") ); - // Forwarded is preferred; X-Forwarded-For is the fallback + } + + #[test] + fn only_the_configured_header_is_read() { + let fwd = "forwarded"; + let xff = "x-forwarded-for"; + // X-Forwarded-For: a Forwarded header can only be the client's assert_eq!( client( "127.0.0.1", - &[ - (fwd, "for=203.0.113.7"), - ("x-forwarded-for", "198.51.100.1") - ] + &[(fwd, "for=192.0.2.66"), (xff, "203.0.113.7")] ), ip("203.0.113.7") ); + assert_eq!(client("127.0.0.1", &[(fwd, "for=127.0.0.1")]), None); + // Forwarded: an X-Forwarded-For header can only be the client's assert_eq!( - client( + client_fwd( "127.0.0.1", - &[(fwd, "proto=https"), ("x-forwarded-for", "198.51.100.1")] + &[(xff, "192.0.2.66"), (fwd, "for=203.0.113.7")] ), - ip("198.51.100.1") + ip("203.0.113.7") + ); + assert_eq!(client_fwd("127.0.0.1", &[(xff, "127.0.0.1")]), None); + assert_eq!( + client_fwd("127.0.0.1", &[(fwd, "proto=https"), (xff, "192.0.2.66")]), + None + ); + } + + #[test] + fn loopback_in_a_header_is_the_writer_s() { + let xff = "x-forwarded-for"; + // A client on the proxy's network can't claim to be this host + assert_eq!( + client("127.0.0.1", &[(xff, "127.0.0.1, 10.0.0.9")]), + ip("10.0.0.9") + ); + assert_eq!(client("10.0.0.2", &[(xff, "127.0.0.1")]), ip("10.0.0.2")); + assert_eq!(client("10.0.0.2", &[(xff, "::1")]), ip("10.0.0.2")); + assert_eq!( + client("127.0.0.1", &[(xff, "::ffff:127.0.0.1, 10.0.0.9")]), + ip("10.0.0.9") + ); + // A public client's claim never got that far anyway + assert_eq!( + client("127.0.0.1", &[(xff, "127.0.0.1, 203.0.113.7")]), + ip("203.0.113.7") + ); + // Loopback all the way: a client on this host, through a local proxy + assert_eq!(client("127.0.0.1", &[(xff, "127.0.0.1")]), ip("127.0.0.1")); + assert_eq!(client("::1", &[(xff, "::1")]), ip("::1")); + assert_eq!( + client_fwd("10.0.0.2", &[("forwarded", "for=127.0.0.1")]), + ip("10.0.0.2") ); } } diff --git a/crates/http/src/request.rs b/crates/http/src/request.rs index 6aa4c77c..12740569 100644 --- a/crates/http/src/request.rs +++ b/crates/http/src/request.rs @@ -183,6 +183,8 @@ impl ParseHttp for Server { .find(|(k, _)| k == "accept") .map(|(_, v)| v.into_owned()) }) + // inbuxa: only a type that can be a header + .filter(|v| hyper::header::HeaderValue::from_str(v).is_ok()) .unwrap_or("application/octet-stream".to_string()), blob, } @@ -726,6 +728,7 @@ async fn handle_session(inner: Arc, session: SessionDat session.remote_ip, req.headers(), &server.core.network.http.trusted_proxies, + server.core.network.http.forwarded_header, ) { trc::event!( Http(trc::HttpEvent::RequestUrl), diff --git a/crates/jmap/src/blob/download.rs b/crates/jmap/src/blob/download.rs index f41d8532..486a79be 100644 --- a/crates/jmap/src/blob/download.rs +++ b/crates/jmap/src/blob/download.rs @@ -12,9 +12,11 @@ use email::cache::email::MessageCacheAccess; use email::message::metadata::MessageMetadata; use groupware::cache::GroupwareCache; use registry::schema::enums::Permission; +use smtp::queue::Message; +use smtp::queue::spool::SmtpSpool; use std::future::Future; use store::ValueKey; -use store::write::{AlignedBytes, Archive}; +use store::write::{AlignedBytes, Archive, blob::BlobHolder}; use trc::AddContext; use types::acl::Acl; use types::blob::{BlobClass, BlobId}; @@ -103,61 +105,122 @@ impl BlobDownload for Server { blob_id: &BlobId, access_token: &AccessToken, ) -> trc::Result { - Ok( - (blob_id.class.is_superuser() && access_token.has_permission(Permission::FetchAnyBlob)) - || (self - .store() - .blob_has_access(&blob_id.hash, &blob_id.class) - .await - .caused_by(trc::location!())? - && match &blob_id.class { - BlobClass::Linked { - account_id, - collection, - document_id, - } => { - if access_token.reads_as_owner(*account_id) { - // inbuxa: AU-1.6: another account's blob - self.audit_foreign_access(access_token, *account_id, true) - .await; - true - } else { - match Collection::from(*collection) { - Collection::Email => self - .get_cached_messages(*account_id) - .await - .caused_by(trc::location!())? - .shared_messages(access_token, Acl::ReadItems) - .contains(*document_id), - collection @ (Collection::FileNode - | Collection::ContactCard - | Collection::CalendarEvent) => self - .fetch_dav_resources( - access_token.account_id(), - *account_id, - SyncCollection::from(collection), - ) - .await - .caused_by(trc::location!())? - // inbuxa: a calendar event's blob holds all of - // it, so a sharee gets none of a private or - // confidential event or one that hides its - // attendees - .shared_events( - access_token, - [Acl::ReadItems], - true, - EVENT_VIEW_PRIVATE | EVENT_VIEW_HIDE_ATTENDEES, - ) - .contains(*document_id), - _ => false, - } + Ok((blob_id.class.is_superuser() + && access_token.has_permission(Permission::FetchAnyBlob) + && self + .tenant_holds_blob(blob_id, access_token) + .await + .caused_by(trc::location!())?) + || (self + .store() + .blob_has_access(&blob_id.hash, &blob_id.class) + .await + .caused_by(trc::location!())? + && match &blob_id.class { + BlobClass::Linked { + account_id, + collection, + document_id, + } => { + if access_token.reads_as_owner(*account_id) { + // inbuxa: AU-1.6: another account's blob + self.audit_foreign_access(access_token, *account_id, true) + .await; + true + } else { + match Collection::from(*collection) { + Collection::Email => self + .get_cached_messages(*account_id) + .await + .caused_by(trc::location!())? + .shared_messages(access_token, Acl::ReadItems) + .contains(*document_id), + collection @ (Collection::FileNode + | Collection::ContactCard + | Collection::CalendarEvent) => self + .fetch_dav_resources( + access_token.account_id(), + *account_id, + SyncCollection::from(collection), + ) + .await + .caused_by(trc::location!())? + // inbuxa: a calendar event's blob holds all of + // it, so a sharee gets none of a private or + // confidential event or one that hides its + // attendees + .shared_events( + access_token, + [Acl::ReadItems], + true, + EVENT_VIEW_PRIVATE | EVENT_VIEW_HIDE_ATTENDEES, + ) + .contains(*document_id), + _ => false, } } - BlobClass::Reserved { account_id, .. } => { - access_token.is_member(*account_id) - } - }), - ) + } + BlobClass::Reserved { account_id, .. } => access_token.is_member(*account_id), + })) } } + +impl TenantBlobAccess for Server { + /// inbuxa: a superuser-class blob id names a blob by its hash alone, so + /// inside a tenant it reaches only blobs the tenant holds: one of its + /// accounts links the blob, or it is a queued message the tenant sees + /// (MT-5). Server-level administrators reach any blob. + async fn tenant_holds_blob( + &self, + blob_id: &BlobId, + access_token: &AccessToken, + ) -> trc::Result { + let Some(tenant_id) = access_token.tenant_id() else { + return Ok(true); + }; + let mut tenant_domains = None; + for holder in self.store().blob_holders(&blob_id.hash).await? { + match holder { + BlobHolder::Account(account_id) => { + if self + .try_account(account_id) + .await? + .is_some_and(|account| account.tenant_id() == Some(tenant_id)) + { + return Ok(true); + } + } + BlobHolder::QueuedMessage(queue_id) => { + let Some(archive) = self.read_message_archive(queue_id).await? else { + continue; + }; + let domains = match &tenant_domains { + Some(domains) => domains, + None => tenant_domains.insert( + inbuxa_features::tenancy::queue::tenant_domains( + self.registry(), + tenant_id, + ) + .await?, + ), + }; + if crate::registry::mapping::queued_message::tenant_sees_archived( + domains, + archive.unarchive::()?, + ) { + return Ok(true); + } + } + } + } + Ok(false) + } +} + +trait TenantBlobAccess: Sync + Send { + fn tenant_holds_blob( + &self, + blob_id: &BlobId, + access_token: &AccessToken, + ) -> impl Future> + Send; +} diff --git a/crates/jmap/src/registry/mapping/queued_message.rs b/crates/jmap/src/registry/mapping/queued_message.rs index 8f5073d9..1eb3f704 100644 --- a/crates/jmap/src/registry/mapping/queued_message.rs +++ b/crates/jmap/src/registry/mapping/queued_message.rs @@ -595,7 +595,7 @@ fn tenant_sees(domains: &AHashSet, message: &Message) -> bool { ) } -fn tenant_sees_archived(domains: &AHashSet, message: &ArchivedMessage) -> bool { +pub(crate) fn tenant_sees_archived(domains: &AHashSet, message: &ArchivedMessage) -> bool { inbuxa_features::tenancy::queue::sees( domains, message.recipients.iter().map(|rcpt| rcpt.address()), diff --git a/crates/store/src/write/blob.rs b/crates/store/src/write/blob.rs index fd64d190..082f8079 100644 --- a/crates/store/src/write/blob.rs +++ b/crates/store/src/write/blob.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use super::{BlobOp, Operation, ValueClass, ValueOp, key::DeserializeBigEndian, now}; @@ -21,6 +23,14 @@ use types::{ blob_hash::{BLOB_HASH_LEN, BlobHash}, }; +/// inbuxa: what keeps a blob: an account's document or upload, or a +/// queued message. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlobHolder { + Account(u32), + QueuedMessage(u64), +} + #[derive(Debug, PartialEq, Eq)] pub struct BlobQuota { pub bytes: usize, @@ -78,6 +88,62 @@ impl Store { self.key_exists(key).await } + /// inbuxa: everything that links `hash`, leaving out expired uploads. + pub async fn blob_holders( + &self, + hash: impl AsRef + Sync + Send, + ) -> trc::Result> { + const TEMP_LINK: usize = BLOB_HASH_LEN + U32_LEN + U64_LEN; + const DOC_LINK: usize = BLOB_HASH_LEN + U64_LEN + 1; + const ID_LINK: usize = BLOB_HASH_LEN + U64_LEN; + + let hash = hash.as_ref(); + let from_key = ValueKey { + account_id: 0, + collection: 0, + document_id: 0, + class: ValueClass::Blob(BlobOp::Commit { hash: hash.clone() }), + }; + let to_key = ValueKey { + account_id: u32::MAX, + collection: u8::MAX, + document_id: u32::MAX, + class: ValueClass::Blob(BlobOp::Link { + hash: hash.clone(), + to: BlobLink::Temporary { until: u64::MAX }, + }), + }; + let now = now(); + let mut holders = Vec::new(); + self.iterate( + IterateParams::new(from_key, to_key).ascending().no_values(), + |key, _| { + if key.get(0..BLOB_HASH_LEN) != Some(hash.as_slice()) { + return Ok(false); + } + match key.len() { + TEMP_LINK => { + if key.deserialize_be_u64(BLOB_HASH_LEN + U32_LEN)? > now { + holders + .push(BlobHolder::Account(key.deserialize_be_u32(BLOB_HASH_LEN)?)); + } + } + DOC_LINK => { + holders.push(BlobHolder::Account(key.deserialize_be_u32(BLOB_HASH_LEN)?)) + } + ID_LINK => holders.push(BlobHolder::QueuedMessage( + key.deserialize_be_u64(BLOB_HASH_LEN)?, + )), + _ => {} + } + Ok(true) + }, + ) + .await + .caused_by(trc::location!())?; + Ok(holders) + } + pub async fn purge_blobs_all_shards(&self, blob_store: BlobStore) -> trc::Result<()> { for shard_index in 0u8..=255 { self.purge_blobs(blob_store.clone(), shard_index).await?; diff --git a/docs/spec/features/multi-tenancy.md b/docs/spec/features/multi-tenancy.md index ab153fda..b758e274 100644 --- a/docs/spec/features/multi-tenancy.md +++ b/docs/spec/features/multi-tenancy.md @@ -213,6 +213,16 @@ Each requirement has an ID, and tests name the IDs they check. in a tenant it would break MT-1. A tenant administrator manages its own people's accounts and masks through its administrative permissions instead. + **Decision** (2026-11-17): `sysSieveSystemScriptCreate`, `…Update` and + `…Destroy` never take effect inside a tenant either. System Sieve scripts + run with the trusted functions, whose `exec` starts programs as the + server's user, so no tenant policy may hand them out. + **Decision** (2026-11-17): `fetchAnyBlob` stays in the default tenant role, + because a tenant administrator downloads queued messages with it. A + superuser-class blob id names a blob by its hash alone, so inside a tenant + it reaches only a blob the tenant holds: one an account of the tenant + links, or a queued message the tenant sees (MT-5). Server-level + administrators reach any blob. - **MT-16.** Changing a tenant's roles, permissions or quotas takes effect for its people without a restart and without them signing in again. The next request is judged by the new rules. **Decision**, a deliberate improvement: diff --git a/tests/src/system/oidc.rs b/tests/src/system/oidc.rs index 1dfd011d..5c654ebe 100644 --- a/tests/src/system/oidc.rs +++ b/tests/src/system/oidc.rs @@ -394,6 +394,50 @@ pub async fn test(test: &mut TestServer) { assert_ne!(status, 200, "PKCE method {method:?} must not be authorized"); } + // inbuxa: with registration not required, a client nobody registered + // gets a code only with PKCE (S256), and only for a loopback or + // private-use redirect (RFC 8252) + for require in [false, true] { + admin + .registry_update_setting( + OidcProvider { + require_client_registration: require, + ..Default::default() + }, + &[Property::RequireClientRegistration], + ) + .await; + admin.reload_settings().await; + if !require { + for (redirect, challenge, expected) in [ + ("com.example.app:/cb", None, false), + ("http://127.0.0.1:4711/cb", Some(PKCE_CHALLENGE), true), + ("com.example.app:/cb", Some(PKCE_CHALLENGE), true), + ("https://app.example.net/cb", Some(PKCE_CHALLENGE), false), + ] { + let (status, _) = post_login_raw(&LoginRequest::AuthCode { + account_name: "user@example.org".to_string(), + account_secret: "this is a very strong password".to_string(), + mfa_token: None, + client_id: "never-registered-app".to_string(), + redirect_uri: redirect.to_string().into(), + nonce: None, + scope: Some(PROFILE_SCOPE.to_string()), + code_challenge: challenge.map(str::to_string), + code_challenge_method: challenge.map(|_| "S256".to_string()), + state: None, + resource: vec![], + }) + .await; + assert_eq!( + status == 200, + expected, + "unregistered client, {redirect}, PKCE {challenge:?}: {status}" + ); + } + } + } + // Authenticate with the correct password, PKCE (S256), scope and a valid resource indicator let response = http .post::( diff --git a/tests/src/system/tenant.rs b/tests/src/system/tenant.rs index 5b190ec9..d37f6630 100644 --- a/tests/src/system/tenant.rs +++ b/tests/src/system/tenant.rs @@ -38,7 +38,10 @@ use serde_json::{Value, json}; use std::str::FromStr; use store::write::{BatchBuilder, ValueClass}; use trc::{Collector, MetricType}; -use types::id::Id; +use types::{ + blob::{BlobClass, BlobId}, + id::Id, +}; use utils::map::vec_map::VecMap; const SECRET_T_ADMIN: &str = "tenant t administrator passphrase"; @@ -568,6 +571,57 @@ pub async fn test(test: &mut TestServer) { "forbidden", "MT-1: no reach into another account's mail" ); + // System Sieve scripts can run programs as the server's user: no tenant + // policy, Merge or Replace, hands out writing them + let system_sieve = vec![ + Permission::SysSieveSystemScriptCreate, + Permission::SysSieveSystemScriptUpdate, + Permission::SysSieveSystemScriptDestroy, + ]; + admin + .registry_update_object( + ObjectType::Account, + p_user.id(), + json!({ + Property::Permissions: Permissions::Merge(PermissionsList { + enabled_permissions: Map::new(system_sieve.clone()), + disabled_permissions: Map::default(), + }) + }), + ) + .await; + for policy in [ + Permissions::Merge(PermissionsList { + enabled_permissions: Map::new(system_sieve.clone()), + disabled_permissions: Map::default(), + }), + Permissions::Replace(PermissionsList { + enabled_permissions: Map::new(system_sieve.clone()), + disabled_permissions: Map::default(), + }), + ] { + admin + .registry_update_object( + ObjectType::Tenant, + p_id, + json!({ Property::Permissions: policy }), + ) + .await; + let token = test.permissions_of(p_user.id()).await; + for permission in &system_sieve { + assert!( + !token.has_permission(*permission), + "system Sieve: {permission:?} granted inside a tenant" + ); + } + } + admin + .registry_update_object( + ObjectType::Account, + p_user.id(), + json!({ Property::Permissions: Permissions::Inherit }), + ) + .await; admin .registry_update_object( ObjectType::Tenant, @@ -610,6 +664,47 @@ pub async fn test(test: &mut TestServer) { "test 10" ); + // A tenant administrator's FetchAnyBlob reaches its own tenant's blobs + // only: a superuser-class blob id names a blob by its hash alone + assert!( + test.permissions_of(p_admin.id()) + .await + .has_permission(Permission::FetchAnyBlob), + "the default tenant role keeps FetchAnyBlob" + ); + let outside = s_user.upload_blob(b"server-level secret 7781").await; + let inside = p_user.upload_blob(b"tenant P attachment 7781").await; + for (blob, expected, what) in [(&outside, 404, "outside"), (&inside, 200, "inside")] { + let response = p_admin.download_any_blob(blob).await; + assert_eq!( + response.status, + expected, + "FetchAnyBlob, a blob {what} the tenant: {}", + String::from_utf8_lossy(&response.body) + ); + } + // Server-level administrators keep reaching every blob + assert_eq!(admin.download_any_blob(&outside).await.status, 200); + assert_eq!(admin.download_any_blob(&inside).await.status, 200); + + // A download type that can't be a header value: the default, not a panic + let response = p_user + .http_get_raw( + &format!( + "{}/jmap/download/{}/{inside}/a.bin?accept=text%2Fplain%0D%0AX-Evil%3A%201", + p_user.base_url(), + p_user.id_string() + ), + None, + ) + .await; + assert_eq!(response.status, 200, "download with a bad accept"); + assert_eq!( + response.headers.get("content-type").unwrap(), + "application/octet-stream" + ); + assert!(response.headers.get("x-evil").is_none()); + // Acceptance test 11: lowering a tenant's roles reaches a signed-in // administrator on its next request (MT-16) assert!(p_admin.can_list_accounts().await, "test 11"); @@ -771,9 +866,15 @@ pub async fn test(test: &mut TestServer) { ) .await; // The message is accepted and queued, and local delivery refuses it - // for now, so it waits in the queue (MT-19a) - lmtp.ingest("someone@remote.example.org", &["d3@d.example.org"], message) - .await; + // for now, so it waits in the queue (MT-19a). Its body differs, at the + // same size, so the queue alone holds its blob. + let queued_message = message.replace("0123456789", "9876543210"); + lmtp.ingest( + "someone@remote.example.org", + &["d3@d.example.org"], + &queued_message, + ) + .await; let mut reason = String::new(); for _ in 0..20 { tokio::time::sleep(std::time::Duration::from_millis(250)).await; @@ -812,6 +913,28 @@ pub async fn test(test: &mut TestServer) { .is_empty(), "MT-5" ); + // The recipient's tenant administrator downloads the queued message by + // the blob id the queue gives it; another tenant's can't + let blob_id = d_admin + .registry_get_many(ObjectType::QueuedMessage, queued.clone()) + .await + .list()[0]["blobId"] + .as_str() + .unwrap() + .to_string(); + for (account, expected) in [(&d_admin, 200), (&t_admin, 404)] { + let response = account + .http_get_raw( + &format!( + "{}/jmap/download/{}/{blob_id}/queued.eml", + account.base_url(), + account.id_string() + ), + None, + ) + .await; + assert_eq!(response.status, expected, "queued message download"); + } admin .registry_destroy(ObjectType::QueuedMessage, queued) .await; @@ -1022,6 +1145,44 @@ fn sorted(mut ids: Vec) -> Vec { ids } +impl Account { + /// Uploads `data`, returning its blob id. + async fn upload_blob(&self, data: &[u8]) -> String { + let response = self + .http_post_raw( + &format!("{}/jmap/upload/{}/", self.base_url(), self.id_string()), + "application/octet-stream", + data.to_vec(), + ) + .await; + assert_eq!( + response.status, + 200, + "{}", + String::from_utf8_lossy(&response.body) + ); + serde_json::from_slice::(&response.body).unwrap()["blobId"] + .as_str() + .unwrap() + .to_string() + } + + /// Downloads `blob_id`'s blob by its hash alone (a superuser-class id). + async fn download_any_blob(&self, blob_id: &str) -> crate::utils::jmap::RawResponse { + let hash = BlobId::from_base32(blob_id).unwrap().hash; + let any = BlobId::new(hash, BlobClass::default()); + self.http_get_raw( + &format!( + "{}/jmap/download/{}/{any}/blob.bin", + self.base_url(), + self.id_string() + ), + None, + ) + .await + } +} + impl TestServer { async fn permissions_of(&self, account_id: Id) -> common::auth::AccessToken { self.server