From 6b4ecf1c2014a50f89eccc8d4d7afaf3088b2d0b Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:23:57 -0700 Subject: [PATCH 1/6] TLS: stop trusting the distrusted emSign intermediates rustls-webpki 0.103.15, webpki-roots 1.0.9 and webpki-root-certs 1.0.9 accept certificate paths through emSign intermediates whose private keys are now public (RUSTSEC-2026-0381, -0382, -0383; CVE-2026-111943). That reaches every outbound TLS connection that uses the bundled roots. They are now 0.103.17, 1.0.12 and 1.0.12. Only these three crates move in Cargo.lock. Reported privately in a security review. --- Cargo.lock | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d819f0c3..c9caea01 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]] @@ -3489,7 +3489,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -5178,7 +5178,7 @@ dependencies = [ "tokio-util", "twox-hash 2.1.5", "url", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -6963,7 +6963,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams 0.4.2", "web-sys", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", ] [[package]] @@ -7353,9 +7353,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", @@ -9799,9 +9799,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", ] @@ -9812,14 +9812,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", ] @@ -10447,7 +10447,7 @@ dependencies = [ "tokio", "tokio-util", "tracing", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", "x509-parser", "zenoh-buffers", "zenoh-codec", @@ -10531,7 +10531,7 @@ dependencies = [ "tokio-rustls", "tokio-util", "tracing", - "webpki-roots 1.0.9", + "webpki-roots 1.0.12", "x509-parser", "zenoh-config", "zenoh-core", -- 2.54.0 From 8228a813650a42ed9c119106bba4cf7dda0727c7 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:24:05 -0700 Subject: [PATCH 2/6] HTTP: read only the forwarding header the proxy writes With forwarding headers trusted, the client address came from RFC 7239 Forwarded, or X-Forwarded-For when there was none. nginx, Caddy and Traefik write only X-Forwarded-For and pass a client's own Forwarded header through unchanged, so a client behind them could name its own address. That address drives rate limits, bans, IP rules and the audit log, and a loopback address skipped endpoint restrictions, the contact form's rate limit and the migration run token's loopback check. Only one header is read now: X-Forwarded-For, or Forwarded with INBUXA_HTTP_FORWARDED_HEADER=forwarded. 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; otherwise the hop that wrote it is the client. That also stops a client on a trusted private network from claiming loopback through X-Forwarded-For. Test: client_ip's new tests fail on the old chain logic and pass now. Reported privately in a security review. --- crates/common/src/config/network.rs | 39 +++++++++++ crates/http/src/client_ip.rs | 103 +++++++++++++++++++++++----- crates/http/src/request.rs | 1 + 3 files changed, 126 insertions(+), 17 deletions(-) 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/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 36b98df3..aee30d32 100644 --- a/crates/http/src/request.rs +++ b/crates/http/src/request.rs @@ -742,6 +742,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), -- 2.54.0 From c2ebfac4a926cc77561c251233da38d635bd353e Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:25:16 -0700 Subject: [PATCH 3/6] Tenants: no tenant policy grants writing system Sieve scripts System Sieve scripts run with the trusted functions, whose exec starts any program as the server's user. The tenant ceiling hard-denied impersonation, legal holds and the deliverability lists, but not sysSieveSystemScriptCreate, Update or Destroy, so a server administrator's Merge or Replace policy on a tenant could hand them to the tenant's administrators. Tenant roles don't include them by default, and a tenant administrator can't grant them alone. The ceiling now refuses all three inside a tenant, whatever the policy (multi-tenancy spec, MT-15). Test: the tenant suite gives a tenant user all three through both a Merge and a Replace tenant policy. On main the user holds sysSieveSystemScriptCreate; now none of the three. Reported privately in a security review. --- crates/common/src/auth/permissions.rs | 6 ++++ docs/spec/features/multi-tenancy.md | 4 +++ tests/src/system/tenant.rs | 51 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/crates/common/src/auth/permissions.rs b/crates/common/src/auth/permissions.rs index 2155c257..60e60d63 100644 --- a/crates/common/src/auth/permissions.rs +++ b/crates/common/src/auth/permissions.rs @@ -123,6 +123,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/docs/spec/features/multi-tenancy.md b/docs/spec/features/multi-tenancy.md index ab153fda..c7a508ff 100644 --- a/docs/spec/features/multi-tenancy.md +++ b/docs/spec/features/multi-tenancy.md @@ -213,6 +213,10 @@ 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. - **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/tenant.rs b/tests/src/system/tenant.rs index 2079e788..ae3b2eb8 100644 --- a/tests/src/system/tenant.rs +++ b/tests/src/system/tenant.rs @@ -587,6 +587,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, -- 2.54.0 From 57ca03c53010decd0e29127ebef6f1421eaeb761 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:30:03 -0700 Subject: [PATCH 4/6] Tenants: fetchAnyBlob reaches only blobs the tenant holds A superuser-class blob id names a blob by its hash alone, and fetchAnyBlob, in the default tenant administrator role, downloaded any such blob. The check never looked at the tenant, so a tenant administrator who learned a hash could download that blob from another tenant's mail. Inside a tenant, fetchAnyBlob now reaches a blob only when the tenant holds it: an account of the tenant links it, or it is a queued message the tenant sees (MT-5). Server-level administrators reach any blob as before. The permission stays in the tenant role, since queue downloads use it, and roles already stored need no change. Store::blob_holders lists what links a hash. Test: in the tenant suite a tenant administrator downloads, by hash, a blob of a user outside the tenant: 200 on main, 404 now. The tenant's own blobs and its queued messages still download, and another tenant's administrator can't fetch that queued message. With the queue check turned off, the queued download fails, so the test covers that path. Reported privately in a security review. --- crates/jmap/src/blob/download.rs | 173 ++++++++++++------ .../src/registry/mapping/queued_message.rs | 2 +- crates/store/src/write/blob.rs | 66 +++++++ docs/spec/features/multi-tenancy.md | 6 + tests/src/system/tenant.rs | 100 +++++++++- 5 files changed, 287 insertions(+), 60 deletions(-) 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 c7a508ff..b758e274 100644 --- a/docs/spec/features/multi-tenancy.md +++ b/docs/spec/features/multi-tenancy.md @@ -217,6 +217,12 @@ Each requirement has an ID, and tests name the IDs they check. `…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/tenant.rs b/tests/src/system/tenant.rs index ae3b2eb8..6e3eebfe 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"; @@ -680,6 +683,29 @@ 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); + // 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"); @@ -841,9 +867,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; @@ -882,6 +914,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; @@ -1092,6 +1146,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 -- 2.54.0 From fc582c8cabaf352bd14c2789d2909da3224d8659 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:30:11 -0700 Subject: [PATCH 5/6] HTTP: a download type that can't be a header no longer panics /jmap/download takes the Content-Type from its accept parameter. A value that isn't a valid header value, such as one with an encoded CR/LF, failed the response builder, and HttpResponse::build unwrapped the error and panicked, ending that connection. A signed-in user with a blob to download could do it; the server kept running. The accept value is used only when it is a valid header value, and otherwise the download is application/octet-stream. build() answers 500 instead of panicking when any header is invalid. Test: in the tenant suite, accept=text/plain%0D%0AX-Evil:1 panicked at response.rs on main; now it downloads as application/octet-stream with no injected header. Reported privately in a security review. --- crates/http-proto/src/response.rs | 15 ++++++++++++++- crates/http/src/request.rs | 2 ++ tests/src/system/tenant.rs | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) 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/request.rs b/crates/http/src/request.rs index aee30d32..3f064330 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, } diff --git a/tests/src/system/tenant.rs b/tests/src/system/tenant.rs index 6e3eebfe..48483179 100644 --- a/tests/src/system/tenant.rs +++ b/tests/src/system/tenant.rs @@ -706,6 +706,24 @@ pub async fn test(test: &mut TestServer) { 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"); -- 2.54.0 From e81b4022f462783c04356838d968063d9044b2c1 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Tue, 17 Nov 2026 07:30:11 -0700 Subject: [PATCH 6/6] OAuth: unregistered clients need PKCE and a native-app redirect With requireClientRegistration off (it's on by default), the authorization endpoint accepted any client id with any redirect URI and no PKCE. A made-up client could send people to a sign-in page that returned their authorization code to any web site. A client nobody registered now has to send an S256 code_challenge, as stateless clients already do, and may only redirect to a loopback address or a private-use scheme (RFC 8252), where a web site can't listen. Desktop and mobile mail apps keep working; a web application has to be registered. Registered clients are unchanged. Test: in the system suite, with registration not required, an unregistered client got a code without PKCE on main. Now it needs PKCE and a loopback or private-use redirect; an https redirect is refused. Reported privately in a security review. --- crates/http/src/auth/oauth/auth.rs | 23 ++++++++++- crates/http/src/auth/oauth/registration.rs | 18 +++++++++ tests/src/system/oidc.rs | 44 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) 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/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::( -- 2.54.0