A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(app-proxy): allow instance endpoints in the csp (#3524)
Hampus0eb4e1733ede
diff --git a/fluxer_app_proxy/src/branding_images.rs b/fluxer_app_proxy/src/branding_images.rsindex 2a1f40b32..2cb626103 100644--- a/fluxer_app_proxy/src/branding_images.rs+++ b/fluxer_app_proxy/src/branding_images.rs@@ -1,6 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-lateruse crate::config::{AppProxyConfig, HttpEndpoint};+use crate::csp::InstanceCspSources;use axum::http::header;use reqwest::Url;use std::sync::Arc;@@ -15,6 +16,15 @@ const BRANDING_IMAGES: [(&str, &str); 5] = [("wordmark_url", "FLUXER_APP_WORDMARK_URL"),("favicon_url", "FLUXER_APP_FAVICON_URL"),];+const CONNECT_ENDPOINTS: [&str; 7] = [+ "api",+ "api_client",+ "api_public",+ "gateway",+ "media",+ "upload_relay",+ "static_cdn",+];const PRODUCT_NAME: (&str, &str) = ("product_name", "FLUXER_APP_PRODUCT_NAME");const ICON_URL: (&str, &str) = ("icon_url", "FLUXER_APP_ICON_URL");const FAVICON_URL: (&str, &str) = ("favicon_url", "FLUXER_APP_FAVICON_URL");@@ -33,9 +43,45 @@ wherepub fn snapshot_from_discovery(discovery: &serde_json::Value) -> Option<BrandingSnapshot> {let branding = discovery.pointer("/app_public/branding")?.as_object()?;- Some(snapshot_from_fields(|(key, _)| {- branding.get(key)?.as_str().map(str::to_owned)- }))+ Some(BrandingSnapshot {+ instance_sources: instance_sources_from_discovery(discovery),+ ..snapshot_from_fields(|(key, _)| branding.get(key)?.as_str().map(str::to_owned))+ })+}++fn instance_sources_from_discovery(discovery: &serde_json::Value) -> InstanceCspSources {+ let endpoint = |key: &str| {+ discovery+ .pointer(&format!("/endpoints/{key}"))+ .and_then(serde_json::Value::as_str)+ .map(str::trim)+ .filter(|value| !value.is_empty())+ };+ let http_origin = |key: &'static str| {+ endpoint(key).and_then(|value| HttpEndpoint::origin_of(key, value).ok())+ };+ let mut connect_origins: Vec<String> = Vec::new();+ for key in CONNECT_ENDPOINTS {+ let Some(origin) = endpoint(key).and_then(connect_origin) else {+ continue;+ };+ if !connect_origins.contains(&origin) {+ connect_origins.push(origin);+ }+ }+ InstanceCspSources {+ static_cdn_endpoint: http_origin("static_cdn"),+ media_endpoint: http_origin("media"),+ connect_origins,+ }+}++fn connect_origin(value: &str) -> Option<String> {+ let url = Url::parse(value).ok()?;+ if !matches!(url.scheme(), "http" | "https" | "ws" | "wss") || url.host_str().is_none() {+ return None;+ }+ Some(url.origin().ascii_serialization())}fn snapshot_from_fields(@@ -50,6 +96,7 @@ fn snapshot_from_fields(let favicon_url = non_empty(field(FAVICON_URL)).or_else(|| icon_url.clone());BrandingSnapshot {image_origins: image_origins.into(),+ instance_sources: InstanceCspSources::default(),product_name: non_empty(field(PRODUCT_NAME)).map(Into::into),icon_url: icon_url.as_deref().and_then(absolute_url).map(Into::into),favicon_url: favicon_url@@ -137,6 +184,7 @@ async fn fetch_discovery(#[derive(Clone, Debug, Default, PartialEq, Eq)]pub struct BrandingSnapshot {pub image_origins: Arc<[HttpEndpoint]>,+ pub instance_sources: InstanceCspSources,pub product_name: Option<Arc<str>>,pub icon_url: Option<Arc<str>>,pub favicon_url: Option<Arc<str>>,@@ -163,6 +211,10 @@ impl InstanceBranding {self.0.borrow().image_origins.clone()}+ pub fn instance_sources(&self) -> InstanceCspSources {+ self.0.borrow().instance_sources.clone()+ }+pub fn snapshot(&self) -> Arc<BrandingSnapshot> {self.0.borrow().clone()}@@ -191,6 +243,7 @@ impl InstanceBranding {.collect();tracing::info!(?origins,+ instance_origins = ?snapshot.instance_sources.connect_origins,product_name = snapshot.product_name.as_deref(),icon_url = snapshot.icon_url.as_deref(),favicon_url = snapshot.favicon_url.as_deref(),@@ -339,6 +392,46 @@ mod tests {);}+ #[test]+ fn discovery_endpoints_yield_the_instance_origins_the_page_must_reach() {+ let discovery = serde_json::json!({+ "endpoints": {+ "api": "https://chat.example.com/api",+ "api_client": "https://chat.example.com/api",+ "gateway": "wss://chat.example.com/gateway",+ "media": "https://chat.example.com/media",+ "static_cdn": "https://static.example.com",+ "marketing": "https://www.example.com",+ },+ "app_public": {"branding": {}},+ });+ let sources = snapshot_from_discovery(&discovery)+ .unwrap()+ .instance_sources;+ assert_eq!(+ sources.connect_origins,+ vec![+ "https://chat.example.com",+ "wss://chat.example.com",+ "https://static.example.com",+ ]+ );+ assert_eq!(+ sources+ .media_endpoint+ .as_ref()+ .map(HttpEndpoint::csp_origin),+ Some("https://chat.example.com")+ );+ assert_eq!(+ sources+ .static_cdn_endpoint+ .as_ref()+ .map(HttpEndpoint::csp_origin),+ Some("https://static.example.com")+ );+ }+#[test]fn a_document_without_branding_is_not_a_discovery_snapshot() {assert!(snapshot_from_discovery(&serde_json::json!({"api_code_version": "v1"})).is_none());diff --git a/fluxer_app_proxy/src/csp.rs b/fluxer_app_proxy/src/csp.rsindex fdfbf5f01..da3bea0cf 100644--- a/fluxer_app_proxy/src/csp.rs+++ b/fluxer_app_proxy/src/csp.rs@@ -80,6 +80,13 @@ fn has_src_attribute(attributes: &str) -> bool {})}+#[derive(Clone, Debug, Default, PartialEq, Eq)]+pub struct InstanceCspSources {+ pub static_cdn_endpoint: Option<HttpEndpoint>,+ pub media_endpoint: Option<HttpEndpoint>,+ pub connect_origins: Vec<String>,+}+#[derive(Clone, Debug, Default)]pub struct RuntimeCspSources {pub static_cdn_endpoint: Option<HttpEndpoint>,@@ -206,6 +213,7 @@ impl CompiledCspPolicy {&[InlineScriptHash::of("")],configured_sources,&[],+ &InstanceCspSources::default(),)).map_err(CspCompileError::InvalidSpaPolicy)?;Ok(Self {@@ -223,12 +231,14 @@ impl CompiledCspPolicy {&self,script_hashes: &[InlineScriptHash],branding_image_origins: &[HttpEndpoint],+ instance_sources: &InstanceCspSources,) -> CspHeaderValues {let enforced = HeaderValue::from_str(&build_csp(&self.config,script_hashes,&self.runtime_sources,branding_image_origins,+ instance_sources,)).expect("every CSP source is a validated keyword, scheme, ASCII origin, or base64 hash, so a \@@ -246,18 +256,27 @@ fn build_csp(script_hashes: &[InlineScriptHash],runtime_sources: &RuntimeCspSources,branding_image_origins: &[HttpEndpoint],+ instance_sources: &InstanceCspSources,) -> String {build_csp_directives(config,Some(script_hashes),runtime_sources,branding_image_origins,+ instance_sources,).join("; ")}fn build_asset_csp(config: &CspConfig, runtime_sources: &RuntimeCspSources) -> String {- build_csp_directives(config, None, runtime_sources, &[]).join("; ")+ build_csp_directives(+ config,+ None,+ runtime_sources,+ &[],+ &InstanceCspSources::default(),+ )+ .join("; ")}fn build_csp_directives(@@ -265,6 +284,7 @@ fn build_csp_directives(script_hashes: Option<&[InlineScriptHash]>,runtime_sources: &RuntimeCspSources,branding_image_origins: &[HttpEndpoint],+ instance_sources: &InstanceCspSources,) -> Vec<String> {let mut directives = Vec::with_capacity(14);@@ -281,17 +301,17 @@ fn build_csp_directives(script.splice(1..1, hashes.iter().map(|hash| hash.as_source().to_owned()));}extend_from(&mut script, &config.extra_script_src, SCRIPT_SOURCES);- extend_runtime_sources(&mut script, runtime_sources, true, false);+ extend_runtime_sources(&mut script, runtime_sources, instance_sources, true, false);directives.push(format!("script-src {}", script.join(" ")));let mut style = vec!["'self'".to_owned(), "'unsafe-inline'".to_owned()];extend_from(&mut style, &config.extra_style_src, STYLE_SOURCES);- extend_runtime_sources(&mut style, runtime_sources, true, true);+ extend_runtime_sources(&mut style, runtime_sources, instance_sources, true, true);directives.push(format!("style-src {}", style.join(" ")));let mut img = vec!["'self'".to_owned(), "blob:".to_owned(), "data:".to_owned()];extend_from(&mut img, &config.extra_img_src, IMAGE_SOURCES);- extend_runtime_sources(&mut img, runtime_sources, true, true);+ extend_runtime_sources(&mut img, runtime_sources, instance_sources, true, true);for origin in branding_image_origins {push_endpoint_source(&mut img, Some(origin));}@@ -299,18 +319,23 @@ fn build_csp_directives(let mut media = vec!["'self'".to_owned(), "blob:".to_owned()];extend_from(&mut media, &config.extra_media_src, MEDIA_SOURCES);- extend_runtime_sources(&mut media, runtime_sources, true, true);+ extend_runtime_sources(&mut media, runtime_sources, instance_sources, true, true);directives.push(format!("media-src {}", media.join(" ")));let mut font = vec!["'self'".to_owned(), "data:".to_owned()];extend_from(&mut font, &config.extra_font_src, FONT_SOURCES);- extend_runtime_sources(&mut font, runtime_sources, true, true);+ extend_runtime_sources(&mut font, runtime_sources, instance_sources, true, true);directives.push(format!("font-src {}", font.join(" ")));let mut connect = vec!["'self'".to_owned(), "blob:".to_owned(), "data:".to_owned()];extend_from(&mut connect, &config.extra_connect_src, CONNECT_SOURCES);- extend_runtime_sources(&mut connect, runtime_sources, true, true);+ extend_runtime_sources(&mut connect, runtime_sources, instance_sources, true, true);extend_runtime_s3_sources(&mut connect, runtime_sources);+ for origin in &instance_sources.connect_origins {+ if !connect.contains(origin) {+ connect.push(origin.clone());+ }+ }directives.push(format!("connect-src {}", connect.join(" ")));let mut frame = vec!["'self'".to_owned()];@@ -319,12 +344,18 @@ fn build_csp_directives(let mut worker = vec!["'self'".to_owned(), "blob:".to_owned()];extend_from(&mut worker, &config.extra_worker_src, WORKER_SOURCES);- extend_runtime_sources(&mut worker, runtime_sources, true, false);+ extend_runtime_sources(&mut worker, runtime_sources, instance_sources, true, false);directives.push(format!("worker-src {}", worker.join(" ")));let mut manifest = vec!["'self'".to_owned()];extend_from(&mut manifest, &config.extra_manifest_src, MANIFEST_SOURCES);- extend_runtime_sources(&mut manifest, runtime_sources, true, false);+ extend_runtime_sources(+ &mut manifest,+ runtime_sources,+ instance_sources,+ true,+ false,+ );directives.push(format!("manifest-src {}", manifest.join(" ")));directives.push("object-src 'none'".to_owned());@@ -341,14 +372,17 @@ fn build_csp_directives(fn extend_runtime_sources(target: &mut Vec<String>,runtime_sources: &RuntimeCspSources,+ instance_sources: &InstanceCspSources,include_static: bool,include_media: bool,) {if include_static {push_endpoint_source(target, runtime_sources.static_cdn_endpoint.as_ref());+ push_endpoint_source(target, instance_sources.static_cdn_endpoint.as_ref());}if include_media {push_endpoint_source(target, runtime_sources.media_endpoint.as_ref());+ push_endpoint_source(target, instance_sources.media_endpoint.as_ref());}}@@ -448,7 +482,13 @@ mod tests {#[test]fn build_csp_includes_required_directives() {let config = default_csp_config();- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );assert!(csp.contains("default-src"));assert!(csp.contains("script-src"));assert!(csp.contains("style-src"));@@ -467,7 +507,13 @@ mod tests {#[test]fn build_csp_allows_the_apex_domain_for_images_and_media() {let config = default_csp_config();- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );for name in ["img-src ", "media-src "] {let directive = csp.split("; ")@@ -485,7 +531,13 @@ mod tests {#[test]fn build_csp_allows_blob_connections_for_camera_background_media() {let config = default_csp_config();- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );let connect = csp.split("; ").find(|directive| directive.starts_with("connect-src "))@@ -520,6 +572,7 @@ mod tests {&[hash_of("first()"), hash_of("second()")],&runtime_sources(),&[],+ &InstanceCspSources::default(),);let script = csp.split("; ")@@ -548,6 +601,7 @@ mod tests {&[hash_of("boot()")],&runtime_sources(),&[],+ &InstanceCspSources::default(),);assert!(!csp.contains("hcaptcha"));assert!(!csp.contains("challenges.cloudflare.com"));@@ -556,7 +610,13 @@ mod tests {#[test]fn csp_no_double_spaces_or_trailing_semicolons() {let config = default_csp_config();- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );assert!(!csp.contains(" "), "CSP contains double spaces");assert!(!csp.ends_with(';'), "CSP ends with semicolon");assert!(!csp.ends_with("; "), "CSP ends with semicolon+space");@@ -574,14 +634,26 @@ mod tests {),..Default::default()};- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );assert!(csp.contains("report-uri https://example.com/csp-report"));}#[test]fn build_csp_excludes_report_uri_when_none() {let config = default_csp_config();- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources(), &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources(),+ &[],+ &InstanceCspSources::default(),+ );assert!(!csp.contains("report-uri"));}@@ -593,7 +665,13 @@ mod tests {media_endpoint: Some(endpoint("https://media.example.test")),..Default::default()};- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources, &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources,+ &[],+ &InstanceCspSources::default(),+ );assert!(csp.contains("style-src 'self' 'unsafe-inline'"));assert!(csp.contains("https://static.example.test"));assert!(csp.contains("https://media.example.test"));@@ -635,7 +713,13 @@ mod tests {..Default::default()};- let csp = build_csp(&config, &[hash_of("boot()")], &runtime_sources, &[]);+ let csp = build_csp(+ &config,+ &[hash_of("boot()")],+ &runtime_sources,+ &[],+ &InstanceCspSources::default(),+ );assert!(csp.contains("http://localhost:3900"));assert!(csp.contains("http://fluxer-uploads.localhost:3900"));@@ -673,7 +757,7 @@ mod tests {};let policy = CompiledCspPolicy::compile(default_csp_config(), &configured).unwrap();- let headers = policy.spa_headers(&[hash_of("boot()")], &[]);+ let headers = policy.spa_headers(&[hash_of("boot()")], &[], &InstanceCspSources::default());let header = headers.enforced.to_str().unwrap();assert!(header.contains(hash_of("boot()").as_source()));@@ -689,7 +773,7 @@ mod tests {config.media_endpoint = Some(endpoint("https://media.configured.test/"));let policy = CompiledCspPolicy::from_config(&config).unwrap();- let headers = policy.spa_headers(&[hash_of("boot()")], &[]);+ let headers = policy.spa_headers(&[hash_of("boot()")], &[], &InstanceCspSources::default());let header = headers.enforced.to_str().unwrap();for directive in ["img-src",@@ -740,8 +824,18 @@ mod tests {);let hashes = [hash_of("boot()")];assert_eq!(- policy.spa_headers(&hashes, &[]).enforced.to_str().unwrap(),- build_csp(&config, &hashes, &sources, &[])+ policy+ .spa_headers(&hashes, &[], &InstanceCspSources::default())+ .enforced+ .to_str()+ .unwrap(),+ build_csp(+ &config,+ &hashes,+ &sources,+ &[],+ &InstanceCspSources::default()+ ));}@@ -753,7 +847,11 @@ mod tests {endpoint("http://assets.example.test:8443"),];- let headers = policy.spa_headers(&[hash_of("boot()")], &branding);+ let headers = policy.spa_headers(+ &[hash_of("boot()")],+ &branding,+ &InstanceCspSources::default(),+ );let header = headers.enforced.to_str().unwrap();for directive in header.split("; ") {@@ -780,6 +878,43 @@ mod tests {);}+ #[test]+ fn discovered_instance_origins_are_granted_to_the_page() {+ let policy = CompiledCspPolicy::compile(default_csp_config(), &runtime_sources()).unwrap();+ let instance = InstanceCspSources {+ static_cdn_endpoint: None,+ media_endpoint: Some(endpoint("https://chat.example.com/media")),+ connect_origins: vec![+ "https://chat.example.com".to_owned(),+ "wss://chat.example.com".to_owned(),+ ],+ };++ let headers = policy.spa_headers(&[hash_of("boot()")], &[], &instance);+ let header = headers.enforced.to_str().unwrap();+ let directive = |name: &str| {+ header+ .split("; ")+ .find(|directive| directive.starts_with(&format!("{name} ")))+ .unwrap()+ .split(' ')+ .collect::<Vec<_>>()+ };++ assert!(directive("connect-src").contains(&"wss://chat.example.com"));+ assert!(directive("connect-src").contains(&"https://chat.example.com"));+ assert!(directive("img-src").contains(&"https://chat.example.com"));+ assert!(directive("media-src").contains(&"https://chat.example.com"));+ assert!(!directive("script-src").contains(&"https://chat.example.com"));+ assert!(+ !policy+ .asset_header()+ .to_str()+ .unwrap()+ .contains("chat.example.com")+ );+ }+#[test]fn a_branding_image_origin_the_policy_already_grants_is_not_repeated() {let configured = RuntimeCspSources {@@ -791,6 +926,7 @@ mod tests {let headers = policy.spa_headers(&[hash_of("boot()")],&[endpoint("https://media.example.test")],+ &InstanceCspSources::default(),);let header = headers.enforced.to_str().unwrap();let img = headerdiff --git a/fluxer_app_proxy/src/routes/spa_index.rs b/fluxer_app_proxy/src/routes/spa_index.rsindex 130f30fe1..af5434854 100644--- a/fluxer_app_proxy/src/routes/spa_index.rs+++ b/fluxer_app_proxy/src/routes/spa_index.rs@@ -226,6 +226,7 @@ async fn serve_spa_index(state: &AppState, headers: &HeaderMap, request_path: &slet csp = state.csp.spa_headers(&inline_script_hashes(&html),&state.branding.image_origins(),+ &state.branding.instance_sources(),);build_spa_response(html.into_boxed_str(), &csp, should_bust_dev_assets)}@@ -1050,6 +1051,7 @@ mod tests {icon_url: Some(Arc::from("https://example.test/icon.png")),favicon_url: Some(Arc::from("https://example.test/favicon.png")),theme_color: Some(Arc::from("#7b2ce4")),+ ..BrandingSnapshot::default()}}diff --git a/fluxer_docs/src/content/docs/operator/configuration.mdx b/fluxer_docs/src/content/docs/operator/configuration.mdxindex e03a8f72a..a083cfcad 100644--- a/fluxer_docs/src/content/docs/operator/configuration.mdx+++ b/fluxer_docs/src/content/docs/operator/configuration.mdx@@ -1696,7 +1696,7 @@ Defaults to the crate version. The reported build of `admin` and `app-proxy`. `B`FLUXER_CSP_EXTRA_DEFAULT_SRC`, `FLUXER_CSP_EXTRA_CONNECT_SRC`, `FLUXER_CSP_EXTRA_IMG_SRC`, `FLUXER_CSP_EXTRA_MEDIA_SRC`, `FLUXER_CSP_EXTRA_FONT_SRC`, `FLUXER_CSP_EXTRA_SCRIPT_SRC`, `FLUXER_CSP_EXTRA_STYLE_SRC`, `FLUXER_CSP_EXTRA_FRAME_SRC`, `FLUXER_CSP_EXTRA_WORKER_SRC`, and `FLUXER_CSP_EXTRA_MANIFEST_SRC` take one or more sources separated by commas, spaces, tabs, or newlines. Blank entries and sources the directive already lists are dropped. `FLUXER_CSP_REPORT_URI` sets a single `report-uri` value.-`object-src`, `base-uri`, and `frame-ancestors` are fixed and have no override. `app-proxy` adds the static CDN endpoint and the media endpoint from its environment. The self-hosted image also adds the origin of each branding image to `img-src`. At start it takes them from `FLUXER_APP_ICON_URL`, `FLUXER_APP_SYMBOL_URL`, `FLUXER_APP_LOGO_URL`, `FLUXER_APP_WORDMARK_URL` and `FLUXER_APP_FAVICON_URL`. It then polls the discovery document every `DISCOVERY_REFRESH_INTERVAL_MS`, where a value saved in the admin dashboard wins. A dashboard change reaches the header within a minute by default with no restart. A stack on one hostname needs no extra sources. The usual reason to set one is a voice server on another hostname, which needs its WebSocket origin in `FLUXER_CSP_EXTRA_CONNECT_SRC`. [Voice media does not use the proxy](/operator/reverse-proxy/#voice-media-does-not-use-the-proxy) has that line in place.+`object-src`, `base-uri`, and `frame-ancestors` are fixed and have no override. `app-proxy` adds the static CDN endpoint and the media endpoint from its environment. The self-hosted image also adds the origin of each branding image to `img-src`. At start it takes them from `FLUXER_APP_ICON_URL`, `FLUXER_APP_SYMBOL_URL`, `FLUXER_APP_LOGO_URL`, `FLUXER_APP_WORDMARK_URL` and `FLUXER_APP_FAVICON_URL`. It then polls the discovery document every `DISCOVERY_REFRESH_INTERVAL_MS`, where a value saved in the admin dashboard wins. A dashboard change reaches the header within a minute by default with no restart. From the same document it adds the origins of the API, gateway, media, upload relay and static CDN endpoints, so a web app served on a second hostname through `FLUXER_APP_ORIGIN_ALIASES` can still reach them. A stack on one hostname needs no extra sources. The usual reason to set one is a voice server on another hostname, which needs its WebSocket origin in `FLUXER_CSP_EXTRA_CONNECT_SRC`. [Voice media does not use the proxy](/operator/reverse-proxy/#voice-media-does-not-use-the-proxy) has that line in place.A front proxy must not add a [Content-Security-Policy of its own](/operator/reverse-proxy/#send-no-content-security-policy-of-its-own).