A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(media-proxy): bound the relay body and drop the unread ttl (#2496)
Hampus150115cc0cb6
diff --git a/fluxer_media_proxy/src/config/mod.rs b/fluxer_media_proxy/src/config/mod.rsindex 322561d6e..663690191 100644--- a/fluxer_media_proxy/src/config/mod.rs+++ b/fluxer_media_proxy/src/config/mod.rs@@ -70,7 +70,6 @@ pub struct MediaServingConfig {pub struct UploadRelayConfig {pub(crate) secret: SecretBytes,pub max_body_bytes: u64,- pub token_ttl_secs: u64,pub s3_timeout_ms: u64,pub buffered_retry_max_bytes: u64,pub buffered_retry_total_bytes: u64,@@ -329,25 +328,30 @@ impl MediaServingConfig {impl UploadRelayConfig {fn load(env: &EnvMap, mode: DeploymentMode) -> anyhow::Result<Self> {+ let max_body_bytes = parse_u64(+ "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES",+ env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES"),+ 500 * 1024 * 1024,+ 1,+ 5 * 1024 * 1024 * 1024,+ )?;+ let spool_max_total_bytes = parse_u64(+ "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES",+ env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES"),+ 8 * 1024 * 1024 * 1024,+ 0,+ 256 * 1024 * 1024 * 1024,+ )?;+ anyhow::ensure!(+ max_body_bytes <= spool_max_total_bytes,+ "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES must not exceed FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES"+ );Ok(Self {secret: decode_upload_relay_secret(env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SECRET_BASE64"),mode,)?,- max_body_bytes: parse_u64(- "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES",- env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES"),- 500 * 1024 * 1024,- 1,- 5 * 1024 * 1024 * 1024,- )?,- token_ttl_secs: parse_u64(- "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_TOKEN_TTL_SECS",- env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_TOKEN_TTL_SECS"),- 3_600,- 1,- 7 * 24 * 60 * 60,- )?,+ max_body_bytes,s3_timeout_ms: parse_u64("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_S3_TIMEOUT_MS",env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_S3_TIMEOUT_MS"),@@ -380,13 +384,7 @@ impl UploadRelayConfig {64 * 1024,64 * 1024 * 1024,)?,- spool_max_total_bytes: parse_u64(- "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES",- env.get("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES"),- 8 * 1024 * 1024 * 1024,- 0,- 256 * 1024 * 1024 * 1024,- )?,+ spool_max_total_bytes,})}}diff --git a/fluxer_media_proxy/src/config/tests/mod.rs b/fluxer_media_proxy/src/config/tests/mod.rsindex 7a30034d1..8a74f8400 100644--- a/fluxer_media_proxy/src/config/tests/mod.rs+++ b/fluxer_media_proxy/src/config/tests/mod.rs@@ -114,6 +114,48 @@ fn parses_upload_relay_secret() {assert_eq!(&[7u8; 32][..], cfg.upload_relay.secret.expose());}+#[test]+fn rejects_a_body_limit_above_the_spool_budget() {+ let err = Config::load_from_iter([+ ("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret"),+ ("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES", "2"),+ ("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES", "1"),+ ])+ .unwrap_err();+ assert!(+ err.to_string()+ .contains("must not exceed FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES")+ );+}++#[test]+fn rejects_a_zero_spool_budget() {+ let err = Config::load_from_iter([+ ("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret"),+ ("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES", "0"),+ ])+ .unwrap_err();+ assert!(+ err.to_string()+ .contains("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES")+ );+}++#[test]+fn accepts_a_body_limit_equal_to_the_spool_budget() {+ let cfg = Config::load_from_iter([+ ("FLUXER_MEDIA_PROXY_SECRET_KEY", "secret"),+ ("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_MAX_BODY_BYTES", "4096"),+ (+ "FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES",+ "4096",+ ),+ ])+ .unwrap();+ assert_eq!(4096, cfg.upload_relay.max_body_bytes);+ assert_eq!(4096, cfg.upload_relay.spool_max_total_bytes);+}+#[test]fn rejects_invalid_mode_env() {let err = Config::load_from_iter([@@ -382,7 +424,6 @@ fn upload_relay_spool_and_bunny_ip_gate_keys_apply() {"FLUXER_MEDIA_PROXY_UPLOAD_RELAY_SPOOL_MAX_TOTAL_BYTES","1073741824",),- ("FLUXER_MEDIA_PROXY_UPLOAD_RELAY_TOKEN_TTL_SECS", "600"),("FLUXER_MEDIA_PROXY_BUNNY_IP_GATE_ENABLED", "yes"),("FLUXER_MEDIA_PROXY_BUNNY_IP_GATE_TRUSTED_PROXIES",@@ -398,7 +439,6 @@ fn upload_relay_spool_and_bunny_ip_gate_keys_apply() {);assert_eq!(2 << 20, cfg.upload_relay.spool_chunk_bytes);assert_eq!(1 << 30, cfg.upload_relay.spool_max_total_bytes);- assert_eq!(600, cfg.upload_relay.token_ttl_secs);assert!(cfg.bunny_ip_gate_enabled);assert_eq!(vec![diff --git a/fluxer_media_proxy/src/storage/tests/mod.rs b/fluxer_media_proxy/src/storage/tests/mod.rsindex a751258d6..7a05b5221 100644--- a/fluxer_media_proxy/src/storage/tests/mod.rs+++ b/fluxer_media_proxy/src/storage/tests/mod.rs@@ -68,7 +68,6 @@ fn test_config(root: &Path) -> Config {upload_relay: UploadRelayConfig {secret: SecretBytes::new(Vec::new()),max_body_bytes: 1024,- token_ttl_secs: 3600,s3_timeout_ms: 1000,buffered_retry_max_bytes: 0,buffered_retry_total_bytes: 0,