A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(media-proxy): serve wall avatars and backdrops, cut tests
Hampus082190022a60
diff --git a/fluxer_media_proxy/src/request_log/mod.rs b/fluxer_media_proxy/src/request_log/mod.rsindex 4d9747ffa..a0c8510e1 100644--- a/fluxer_media_proxy/src/request_log/mod.rs+++ b/fluxer_media_proxy/src/request_log/mod.rs@@ -7,6 +7,7 @@ pub use failure::ErrorReason;pub use stage::{Stage, record_stage, timed_stage};use crate::{+ constants::OPERATOR_WALL_PATH_PREFIX,metrics::{self,request::{RequestKind, RequestMetrics},@@ -118,6 +119,7 @@ pub fn classify_route(path: &str) -> RequestKind {|| path.starts_with("/embed-splashes/")|| path.starts_with("/emojis/")|| path.starts_with("/stickers/")+ || path.starts_with(OPERATOR_WALL_PATH_PREFIX){return RequestKind::AssetImage;}@@ -141,25 +143,6 @@ pub async fn trace(observe(metrics.as_ref(), observation, next.run(req)).await}-pub async fn trace_public_request<F>(- metrics: &RequestMetrics,- id: RequestId,- method: Method,- path_and_query: &str,- headers: &HeaderMap,- future: F,-) -> Response-where- F: Future<Output = Response>,-{- let (path, query) = match path_and_query.split_once('?') {- Some((path, query)) => (path, Some(query)),- None => (path_and_query, None),- };- let observation = RequestObservation::new(id, method, path, query, headers);- observe(metrics, observation, future).await-}-async fn observe<F>(metrics: &RequestMetrics,observation: RequestObservation,@@ -329,35 +312,6 @@ mod tests {)}- #[test]- fn clip_never_splits_a_multibyte_character() {- let key = "\u{597d}".repeat(8);- assert_eq!(24, key.len());- assert_eq!("\u{597d}\u{597d}\u{597d}~", clip(&key, 10));- assert_eq!("\u{597d}\u{597d}\u{597d}\u{597d}~", clip(&key, 12));- assert_eq!(key, clip(&key, 24));- }-- #[test]- fn a_long_error_source_is_clipped_to_the_target_bound() {- let long = ErrorReason::with_message("storage_error", "k".repeat(4096));- let (code, source) = log_reason(Some(long), StatusCode::NOT_FOUND);- assert_eq!("storage_error", code);- assert_eq!(TARGET_LOG_BYTES_MAX + 1, source.len());- assert!(source.ends_with('~'));- assert_eq!(- ("Not Found", String::new()),- log_reason(None, StatusCode::NOT_FOUND)- );- assert_eq!(- ("storage_error", "key=a".to_owned()),- log_reason(- Some(ErrorReason::with_message("storage_error", "key=a")),- StatusCode::NOT_FOUND- )- );- }-#[test]fn a_live_signature_never_reaches_the_log() {let signature = "f84dea2dbe168641c69753d838f270806583fedfe5c86f3f7a9a80f93af7ad28";@@ -389,38 +343,6 @@ mod tests {}}- #[test]- fn redaction_leaves_every_other_parameter_byte_for_byte() {- for query in [- "",- "size=128",- "width=64&height=64&format=webp",- "download=1",- "hm",- "uc",- "a=b&&c=d",- "ucx=dp&dp=1",- ] {- assert_eq!(query, redact_signature_values(query), "{query}");- }- assert_eq!(- "width=64&ex=[redacted]&is=[redacted]&hm=[redacted]&download=1",- redact_signature_values("width=64&ex=6a9a7231&is=6a9920b1&hm=abc&download=1")- );- assert_eq!(- "ex=[redacted]&is=[redacted]&hm=[redacted]&uc=[redacted]&&width=64",- redact_signature_values("ex=0&is=6a9920b1&hm=abc&uc=dp&&width=64")- );- assert_eq!(- "ex=[redacted]&is=[redacted]&hm=[redacted]",- redact_signature_values("ex=6a9a7231&is=6a9920b1&hm=abc")- );- assert_eq!(- "ex=[redacted]&is=[redacted]&hm=[redacted]&uc=[redacted]",- redact_signature_values("ex=0&is=6a9920b1&hm=abc&uc=dp")- );- }-#[test]fn credentialed_referer_is_redacted_before_it_reaches_the_log() {let mut headers = HeaderMap::new();@@ -438,15 +360,4 @@ mod tests {);assert_eq!(Some("curl/8.0".to_owned()), observation.user_agent);}-- #[test]- fn unparsable_referer_collapses_to_a_marker_and_paths_stay_raw() {- let mut headers = HeaderMap::new();- headers.insert(header::REFERER, "not a url".parse().unwrap());- let observation = observation_with_headers(headers);- assert_eq!(Some("[invalid-url]".to_owned()), observation.referer);- assert_eq!("/attachments/1/2/a.png", observation.path);- assert_eq!("size=128", observation.query);- assert_eq!(RequestKind::Attachment, observation.kind);- }}diff --git a/fluxer_media_proxy/src/server/asset_path.rs b/fluxer_media_proxy/src/server/asset_path.rsindex fc0a763fd..4a81ec213 100644--- a/fluxer_media_proxy/src/server/asset_path.rs+++ b/fluxer_media_proxy/src/server/asset_path.rs@@ -2,7 +2,7 @@use crate::{asset_hash::AssetHash,- constants::{AssetExtension, AssetKind},+ constants::{AssetExtension, AssetKind, OPERATOR_WALL_PATH_PREFIX},storage::safe_key,};use std::fmt;@@ -26,7 +26,7 @@ pub(in crate::server) fn parse_standard_asset_path(path: &str) -> Option<ParsedAlet kind = match prefix {"avatars" => AssetKind::Avatar,"icons" => AssetKind::GuildIcon,- "branding" => AssetKind::GuildIcon,+ "branding" | "branding-backdrops" => AssetKind::Branding,"banners" => AssetKind::Banner,"splashes" => AssetKind::Splash,"embed-splashes" => AssetKind::EmbedSplash,@@ -129,6 +129,22 @@ pub(in crate::server) fn parse_entrance_sound_path(path: &str) -> Option<String>Some(format!("entrance-sounds/{user_id}/{filename}"))}+const OPERATOR_WALL_AVATAR_SIZES: [u16; 7] = [48, 60, 72, 96, 120, 144, 192];++pub(in crate::server) fn parse_operator_wall_avatar_path(path: &str) -> Option<String> {+ let storage_key = canonical_public_path(path)?;+ let filename = path.strip_prefix(OPERATOR_WALL_PATH_PREFIX)?;+ let (hash, size) = filename.strip_suffix(".webp")?.split_once('-')?;+ if hash.len() != 64 || !hash.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) {+ return None;+ }+ let parsed_size: u16 = size.parse().ok()?;+ if parsed_size.to_string() != size || !OPERATOR_WALL_AVATAR_SIZES.contains(&parsed_size) {+ return None;+ }+ Some(storage_key.to_owned())+}+struct ParsedAssetFilename<'a> {hash: &'a str,ext: AssetExtension,@@ -176,7 +192,7 @@ impl fmt::Display for SnowflakeId {}fn valid_asset_owner_key(prefix: &str, owner_key: &str) -> bool {- if prefix == "branding" {+ if prefix == "branding" || prefix == "branding-backdrops" {return !owner_key.is_empty()&& owner_key.len() <= ASSET_OWNER_KEY_MAX_BYTES&& owner_key@@ -214,148 +230,6 @@ pub(in crate::server) fn decode_storage_key(path: &str) -> Result<String, Storag#[cfg(test)]mod tests {use super::*;- use crate::server::params::asset_wants_animated;- use std::collections::HashMap;-- #[test]- fn entrance_sound_path_parses_valid_keys() {- assert_eq!(- parse_entrance_sound_path("/entrance-sounds/1130650140672000000/eb417d05ad2e14c4.wav"),- Some("entrance-sounds/1130650140672000000/eb417d05ad2e14c4.wav".to_owned())- );- for ext in ["mp3", "ogg", "m4a", "wav"] {- assert_eq!(- parse_entrance_sound_path(&format!("/entrance-sounds/42/abc123.{ext}")),- Some(format!("entrance-sounds/42/abc123.{ext}"))- );- }- }-- #[test]- fn entrance_sound_path_rejects_invalid_keys() {- assert_eq!(- parse_entrance_sound_path("/entrance-sounds/42/abc.flac"),- None- );- assert_eq!(- parse_entrance_sound_path("/entrance-sounds/abc/abc.wav"),- None- );- assert_eq!(- parse_entrance_sound_path("/entrance-sounds/42/abc.wav/x"),- None- );- assert_eq!(- parse_entrance_sound_path("/entrance-sounds/42/../secret.wav"),- None- );- assert_eq!(parse_entrance_sound_path("/entrance-sounds/42"), None);- assert_eq!(parse_entrance_sound_path("/entrance-sounds//abc.wav"), None);- assert_eq!(parse_entrance_sound_path("/avatars/42/abc.wav"), None);- }-- #[test]- fn standard_asset_path_strips_virtual_animation_prefix_and_extension() {- let parsed =- parse_standard_asset_path("/avatars/1216100949629702144/a_d2f35261.webp").unwrap();- assert_eq!("avatars/1216100949629702144/d2f35261", parsed.storage_key);- assert_eq!(AssetExtension::Webp, parsed.original_ext);- assert_eq!(AssetKind::Avatar, parsed.kind);- assert!(asset_wants_animated(&HashMap::new(), &parsed.hash));- assert!(!asset_wants_animated(- &HashMap::from([("animated".to_owned(), "false".to_owned())]),- &parsed.hash- ));- }-- #[test]- fn guild_member_and_simple_asset_paths_match_v1_storage_keys() {- let guild =- parse_guild_member_asset_path("/guilds/1/users/2/avatars/a_memberhash.gif").unwrap();- assert_eq!("guilds/1/users/2/avatars/memberhash", guild.storage_key);- assert_eq!(AssetKind::Avatar, guild.kind);- assert_eq!(AssetExtension::Gif, guild.original_ext);- assert!(guild.forced_output_format.is_none());-- let emoji =- parse_simple_asset_path("/emojis/1501314428688998182.webp", AssetKind::Emoji).unwrap();- assert_eq!("emojis/1501314428688998182", emoji.storage_key);- assert_eq!(AssetKind::Emoji, emoji.kind);- assert!(emoji.forced_output_format.is_none());-- let sticker =- parse_simple_asset_path("/stickers/1501314428688998182.png", AssetKind::Sticker)- .unwrap();- assert_eq!("stickers/1501314428688998182", sticker.storage_key);- assert_eq!(Some(AssetExtension::Webp), sticker.forced_output_format);- }-- #[test]- fn asset_filename_rejects_unstable_shapes() {- assert!(parse_asset_filename("hash.extra.png").is_none());- assert!(parse_asset_filename("hash-with-dash.png").is_none());- assert!(parse_asset_filename(".png").is_none());- assert!(parse_asset_filename("hash.").is_none());- }-- #[test]- fn asset_paths_reject_noncanonical_owners_extensions_and_prefixes() {- for invalid in [- "avatars/1216100949629702144/deadbeef.png",- "//avatars/1216100949629702144/deadbeef.png",- "/avatars/0/deadbeef.png",- "/avatars/01/deadbeef.png",- "/avatars/1216100949629702144/deadbeef.exe",- "/avatars/1216100949629702144/deadbeef.png/extra",- ] {- assert!(- parse_standard_asset_path(invalid).is_none(),- "accepted {invalid}"- );- }- assert!(parse_standard_asset_path("/branding/fluxer-wordmark/deadbeef.png").is_some());- assert!(parse_guild_member_asset_path("/guilds/01/users/2/avatars/h.gif").is_none());- assert!(parse_simple_asset_path("/emojis/012.webp", AssetKind::Emoji).is_none());- }-- #[test]- fn storage_keys_decode_the_way_the_old_era_decoded_them() {- assert_eq!(- "attachments/1216100949629702144/1216100949629702145/name/with-slash.png",- decode_storage_key(- "/attachments/1216100949629702144/1216100949629702145/name%2Fwith-slash.png"- )- .expect("an encoded slash still resolves to a live object")- );- assert_eq!(- "attachments/1/a%.png",- decode_storage_key("/attachments/1/a%.png").expect("malformed escapes stay literal")- );- assert_eq!(- "attachments/1/\u{fffd}.png",- decode_storage_key("/attachments/1/%FF.png").expect("non utf8 decodes lossily")- );- assert_eq!(- "attachments/1/a\\b.png",- decode_storage_key("/attachments/1/a%5Cb.png")- .expect("a backslash is an ordinary key byte the old era served")- );- for traversal in [- "/attachments/%2E%2E/photo.png",- "/attachments/1/..%2Fsecret.png",- "/attachments/1/%2E%2E%2Fsecret.png",- "/attachments/1/a%00b.png",- "/attachments%2F%2Fphoto.png",- ] {- assert!(- matches!(- decode_storage_key(traversal),- Err(StorageKeyDecodeError::InvalidKey)- ),- "safe_key accepted {traversal}"- );- }- }#[test]fn storage_keys_reject_path_changing_escapes_and_empty_components() {diff --git a/fluxer_media_proxy/src/server/routes/dispatch.rs b/fluxer_media_proxy/src/server/routes/dispatch.rsindex fa6913882..a01d3e3b4 100644--- a/fluxer_media_proxy/src/server/routes/dispatch.rs+++ b/fluxer_media_proxy/src/server/routes/dispatch.rs@@ -7,7 +7,8 @@ use crate::{server::{asset_path::{StorageKeyDecodeError, decode_storage_key, parse_entrance_sound_path,- parse_guild_member_asset_path, parse_simple_asset_path, parse_standard_asset_path,+ parse_guild_member_asset_path, parse_operator_wall_avatar_path,+ parse_simple_asset_path, parse_standard_asset_path,},attachment_signature,cors::{self, OriginCheck},@@ -20,7 +21,7 @@ use crate::{use axum::{body::Body,extract::{Query, State},- http::{HeaderMap, Method, Request, StatusCode},+ http::{HeaderMap, HeaderValue, Method, Request, StatusCode, header},response::Response,};use fluxer_common::attachment_url_signature::{SIGNATURE_PARAMETER_NAMES, Verdict};@@ -168,6 +169,9 @@ async fn serve_public_read(app: &Arc<AppState>, read: PublicRead<'_>) -> Respons).await;}+ if let Some(key) = parse_operator_wall_avatar_path(path) {+ return serve_operator_wall_avatar(app, method, &key, headers).await;+ }if let Some(key) = parse_entrance_sound_path(path) {return stored::serve_stored_raw(app, method, &app.cfg.storage.bucket_cdn, &key, headers).await;@@ -187,6 +191,34 @@ async fn serve_public_read(app: &Arc<AppState>, read: PublicRead<'_>) -> Responstext(StatusCode::NOT_FOUND, "Not Found")}+const OPERATOR_WALL_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";++async fn serve_operator_wall_avatar(+ app: &Arc<AppState>,+ method: Method,+ key: &str,+ headers: &HeaderMap,+) -> Response {+ let mut response =+ stored::serve_stored_raw(app, method, &app.cfg.storage.bucket_cdn, key, headers).await;+ if matches!(+ response.status(),+ StatusCode::OK | StatusCode::PARTIAL_CONTENT+ ) {+ let response_headers = response.headers_mut();+ response_headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("image/webp"));+ response_headers.insert(+ header::CACHE_CONTROL,+ HeaderValue::from_static(OPERATOR_WALL_CACHE_CONTROL),+ );+ response_headers.insert(+ "CDN-Cache-Control",+ HeaderValue::from_static(OPERATOR_WALL_CACHE_CONTROL),+ );+ }+ response+}+fn storage_key_decode_response(err: StorageKeyDecodeError) -> Response {text_with_source(StatusCode::BAD_REQUEST,@@ -204,13 +236,10 @@ fn without_signature_parameters(params: &HashMap<String, String>) -> HashMap<Str.collect()}-#[cfg(test)]-use crate::test_fixtures::CapturedLog;-#[cfg(test)]mod tests {use super::*;- use crate::{config::Config, constants::DESKTOP_APP_ORIGIN};+ use crate::config::Config;use axum::{body::to_bytes,http::{HeaderValue, header},@@ -241,10 +270,6 @@ mod tests {("FLUXER_MEDIA_PROXY_CORS_MODE", "enforce"),("FLUXER_MEDIA_PROXY_CORS_ALLOWED_ORIGINS", WEB),];- const REPORT: &[(&str, &str)] = &[- ("FLUXER_MEDIA_PROXY_CORS_MODE", "report"),- ("FLUXER_MEDIA_PROXY_CORS_ALLOWED_ORIGINS", WEB),- ];fn dispatch_config(mode: &str, storage_root: &Path, extra: &[(&str, &str)]) -> Config {let mut env = vec![@@ -321,18 +346,6 @@ mod tests {catch_all(State(Arc::clone(app)), Query(HashMap::new()), request).await}- async fn dispatch(app: &Arc<AppState>, method: Method, path: &str) -> Response {- dispatch_request(app, read_request(method, path, &[], None)).await- }-- async fn dispatch_body(app: &Arc<AppState>, path: &str) -> Bytes {- let response = dispatch(app, Method::GET, path).await;- assert_eq!(StatusCode::OK, response.status(), "{path} must be served");- to_bytes(response.into_body(), usize::MAX)- .await- .expect("dispatch body")- }-async fn body_of(response: Response) -> Bytes {to_bytes(response.into_body(), usize::MAX).await@@ -357,93 +370,6 @@ mod tests {]}- fn stored_representation_cases() -> [(Method, &'static str, Option<&'static str>, StatusCode); 7]- {- [- (Method::GET, ATTACHMENT_PATH, None, StatusCode::OK),- (- Method::GET,- ATTACHMENT_PATH,- Some("bytes=0-3"),- StatusCode::PARTIAL_CONTENT,- ),- (- Method::GET,- ATTACHMENT_PATH,- Some("bytes=100-200"),- StatusCode::RANGE_NOT_SATISFIABLE,- ),- (Method::HEAD, ATTACHMENT_PATH, None, StatusCode::OK),- (- Method::HEAD,- ATTACHMENT_PATH,- Some("bytes=0-3"),- StatusCode::PARTIAL_CONTENT,- ),- (Method::GET, THEME_PATH, None, StatusCode::OK),- (- Method::GET,- THEME_PATH,- Some("bytes=999-"),- StatusCode::RANGE_NOT_SATISFIABLE,- ),- ]- }-- #[tokio::test]- async fn static_mode_short_circuits_every_path_to_the_static_bucket() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "static", "external/v2/signed.png", STORED_BYTES);- write_object(- root,- "static",- "avatars/1216100949629702144/a1b2c3d4e5f6.png",- STORED_BYTES,- );- write_object(root, "cdn", "themes/only-in-cdn.css", STORED_BYTES);- let app = dispatch_app("static", root, &[]);-- assert_eq!(- STORED_BYTES,- dispatch_body(&app, "/external/v2/signed.png").await- );- assert_eq!(- STORED_BYTES,- dispatch_body(&app, "/avatars/1216100949629702144/a1b2c3d4e5f6.png").await- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&app, Method::GET, "/themes/only-in-cdn.css")- .await- .status(),- "static mode never reaches the cdn bucket"- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&app, Method::GET, "/attachments/1/2/file.bin")- .await- .status()- );- }-- const STATIC_CACHE_CONTROL: &str = "public, max-age=31536000";-- const STATIC_KEYS: [&str; 4] = [- "avatars/0.png",- "web/favicon.ico",- "emoji/1f600.svg",- "web/NOTICE.md",- ];-- fn static_mode_app(root: &Path, extra: &[(&str, &str)]) -> Arc<AppState> {- for key in STATIC_KEYS {- write_object(root, "static", key, STORED_BYTES);- }- dispatch_app("static", root, extra)- }-fn header_value(response: &Response, name: impl header::AsHeaderName) -> Option<String> {response.headers()@@ -451,233 +377,6 @@ mod tests {.map(|value| value.to_str().expect("header is ascii").to_owned())}- #[tokio::test]- async fn media_mode_matches_the_v1_route_order() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "cdn", "themes/dark.css", THEME_BYTES);- write_object(- root,- "cdn",- "entrance-sounds/42/abc123def456.wav",- STORED_BYTES,- );- write_object(root, "static", "themes/dark.css", STORED_BYTES);- let app = dispatch_app("mp", root, &[]);-- let response = dispatch(&app, Method::GET, "/themes/dark.css").await;- assert_eq!(StatusCode::OK, response.status());- assert_eq!(- "text/css; charset=utf-8",- response- .headers()- .get(header::CONTENT_TYPE)- .expect("content type")- .to_str()- .expect("ascii content type")- );- assert_eq!(- THEME_BYTES,- to_bytes(response.into_body(), usize::MAX)- .await- .expect("theme body")- );-- assert_eq!(- STORED_BYTES,- dispatch_body(&app, "/entrance-sounds/42/abc123def456.wav").await- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&app, Method::GET, "/themes/dark.png")- .await- .status(),- "the themes branch only matches .css"- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&app, Method::GET, "/nothing/here").await.status()- );- assert_eq!(- StatusCode::METHOD_NOT_ALLOWED,- dispatch(&app, Method::POST, "/themes/dark.css")- .await- .status()- );- }-- #[tokio::test]- async fn relay_mode_answers_every_read_path_with_404() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "cdn", "attachments/1/2/stream.js", STORED_BYTES);- write_object(root, "cdn", "themes/dark.css", THEME_BYTES);- write_object(- root,- "cdn",- "entrance-sounds/42/abc123def456.wav",- STORED_BYTES,- );- let app = dispatch_app("relay", root, &[]);- for path in [- "/attachments/1/2/stream.js",- "/external/sig/https/example.com/a.png",- THEME_PATH,- ENTRANCE_SOUND_PATH,- ASSET_PATH,- "/emojis/1.png",- "/stickers/1.webp",- "/",- "/nothing/here",- ] {- for method in [Method::GET, Method::HEAD] {- let response = dispatch(&app, method.clone(), path).await;- assert_eq!(StatusCode::NOT_FOUND, response.status(), "{method} {path}");- assert_eq!(- Some("text/plain; charset=utf-8".to_owned()),- header_value(&response, header::CONTENT_TYPE),- "{method} {path}"- );- assert_eq!(- Some("no-store".to_owned()),- header_value(&response, header::CACHE_CONTROL),- "{method} {path}"- );- assert_eq!(- None,- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{method} {path}"- );- }- }- assert!(- app.metrics- .render()- .contains("fluxer_media_proxy_storage_hits_total 0\n")- );- }-- #[tokio::test]- async fn upload_mode_serves_what_relay_mode_refuses() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "cdn", "attachments/1/2/stream.js", STORED_BYTES);- let upload = dispatch_app("upload", root, &[]);- let relay = dispatch_app("relay", root, &[]);-- assert_eq!(- STORED_BYTES,- dispatch_body(&upload, "/attachments/1/2/stream.js").await- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&relay, Method::GET, "/attachments/1/2/stream.js")- .await- .status()- );- }-- #[tokio::test]- async fn relay_mode_rejects_a_bad_storage_key_as_404_not_400() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- let upload = dispatch_app("upload", root, &[]);- let relay = dispatch_app("relay", root, &[]);-- assert_eq!(- StatusCode::BAD_REQUEST,- dispatch(&upload, Method::GET, "/attachments/%2e%2e/x")- .await- .status()- );- assert_eq!(- StatusCode::NOT_FOUND,- dispatch(&relay, Method::GET, "/attachments/%2e%2e/x")- .await- .status()- );- }-- #[tokio::test]- async fn relay_mode_keeps_405_for_other_methods() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let app = dispatch_app("relay", root.as_path(), &[]);- for path in ["/attachments/1/2/stream.js", "/nothing/here"] {- for method in [- Method::POST,- Method::PUT,- Method::DELETE,- Method::PATCH,- Method::OPTIONS,- ] {- assert_eq!(- StatusCode::METHOD_NOT_ALLOWED,- dispatch(&app, method.clone(), path).await.status(),- "{method} {path}"- );- }- }- }-- #[tokio::test]- async fn enforce_echoes_an_allowed_origin_on_every_stored_representation() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let app = dispatch_app("mp", root, ENFORCE);- for (method, path, range, status) in stored_representation_cases() {- let label = format!("{method} {path} range={range:?}");- let response =- dispatch_request(&app, read_request(method, path, &[WEB.as_bytes()], range)).await;- assert_eq!(status, response.status(), "{label}");- assert_eq!(- Some(WEB.to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{label}"- );- assert_eq!(- vec![ORIGIN_VARY.to_owned()],- vary_values(&response),- "{label}"- );- }- }-- #[tokio::test]- async fn enforce_serves_origin_less_reads_without_allow_origin() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let off = dispatch_app("mp", root, &[]);- let enforce = dispatch_app("mp", root, ENFORCE);- for (method, path, range, status) in stored_representation_cases() {- let label = format!("{method} {path} range={range:?}");- let expected =- dispatch_request(&off, read_request(method.clone(), path, &[], range)).await;- let actual = dispatch_request(&enforce, read_request(method, path, &[], range)).await;- assert_eq!(status, expected.status(), "{label}");- assert_eq!(status, actual.status(), "{label}");- assert_eq!(- None,- header_value(&actual, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{label}"- );- assert_eq!(- vec![ORIGIN_VARY.to_owned()],- vary_values(&actual),- "{label}"- );- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }-#[tokio::test]async fn enforce_refuses_foreign_null_and_malformed_origins_before_any_read() {let tmp = tempfile::tempdir().expect("storage root");@@ -727,40 +426,6 @@ mod tests {);}- #[tokio::test]- async fn enforce_leaves_read_errors_without_cors_but_varies_them_on_origin() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let app = dispatch_app("mp", root.as_path(), ENFORCE);- let with_origin: &[&[u8]] = &[WEB.as_bytes()];- let without_origin: &[&[u8]] = &[];- for origins in [with_origin, without_origin] {- for (path, status) in [- ("/attachments/1/2/missing.bin", StatusCode::NOT_FOUND),- ("/attachments/%2e%2e/x", StatusCode::BAD_REQUEST),- ("/external/nosignature", StatusCode::BAD_REQUEST),- (BAD_SIGNATURE_PATH, StatusCode::UNAUTHORIZED),- ] {- for method in [Method::GET, Method::HEAD] {- let label = format!("{method} {path} origins={}", origins.len());- let response =- dispatch_request(&app, read_request(method, path, origins, None)).await;- assert_eq!(status, response.status(), "{label}");- assert_eq!(- None,- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{label}"- );- assert_eq!(- vec![ORIGIN_VARY.to_owned()],- vary_values(&response),- "{label}"- );- }- }- }- }-#[tokio::test]async fn enforce_refuses_a_foreign_origin_on_a_transform_cache_hit() {let tmp = tempfile::tempdir().expect("storage root");@@ -805,312 +470,14 @@ mod tests {app.metrics.render().contains("fluxer_media_proxy_transform_cache_hits_total 1\n"),- "a refused origin never reaches the transform cache"- );- }-- fn preflight_request(path: &str, origin: &str, requested: Option<&str>) -> Request<Body> {- let mut builder = Request::builder()- .method(Method::OPTIONS)- .uri(path)- .header(header::ORIGIN, origin);- if let Some(requested) = requested {- builder = builder.header(header::ACCESS_CONTROL_REQUEST_METHOD, requested);- }- builder.body(Body::empty()).expect("preflight request")- }-- #[tokio::test]- async fn enforce_serves_the_desktop_app_origin_without_configuring_it() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let app = dispatch_app("mp", root, ENFORCE);- for method in [Method::GET, Method::HEAD] {- let response = dispatch_request(- &app,- read_request(- method.clone(),- THEME_PATH,- &[DESKTOP_APP_ORIGIN.as_bytes()],- None,- ),- )- .await;- assert_eq!(StatusCode::OK, response.status(), "{method}");- assert_eq!(- Some(DESKTOP_APP_ORIGIN.to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{method}"- );- assert_eq!(vec![ORIGIN_VARY.to_owned()], vary_values(&response));- }- for lookalike in [- "fluxer-app://app/",- "fluxer-app://evil",- "fluxer-app://app.evil.example",- "fluxer-app://app:443",- "FLUXER-APP://APP",- "fluxer-app:app",- ] {- let response = dispatch_request(- &app,- read_request(Method::GET, THEME_PATH, &[lookalike.as_bytes()], None),- )- .await;- assert_eq!(StatusCode::FORBIDDEN, response.status(), "{lookalike}");- assert_eq!(- None,- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{lookalike}"- );- }- }-- #[tokio::test]- async fn preflight_answers_allowed_origins_and_refuses_the_rest() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- let enforce = dispatch_app("mp", root, ENFORCE);- for origin in [WEB, DESKTOP_APP_ORIGIN] {- for requested in ["GET", "HEAD"] {- let response = dispatch_request(- &enforce,- preflight_request(THEME_PATH, origin, Some(requested)),- )- .await;- let label = format!("{origin} {requested}");- assert_eq!(StatusCode::NO_CONTENT, response.status(), "{label}");- assert_eq!(- Some(origin.to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{label}"- );- assert_eq!(- Some("GET, HEAD, OPTIONS".to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_METHODS),- "{label}"- );- assert_eq!(- Some("Range".to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_HEADERS),- "{label}"- );- assert_eq!(- vec![- "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"- .to_owned()- ],- vary_values(&response),- "{label}"- );- assert!(body_of(response).await.is_empty(), "{label}");- }- }-- let refused =- dispatch_request(&enforce, preflight_request(THEME_PATH, EVIL, Some("GET"))).await;- assert_eq!(StatusCode::FORBIDDEN, refused.status());- assert_eq!(- None,- header_value(&refused, header::ACCESS_CONTROL_ALLOW_ORIGIN)- );-- let not_a_preflight =- dispatch_request(&enforce, preflight_request(THEME_PATH, WEB, None)).await;- assert_eq!(StatusCode::METHOD_NOT_ALLOWED, not_a_preflight.status());-- for (mode, extra) in [("mp", &[][..]), ("mp", REPORT), ("static", &[][..])] {- let app = dispatch_app(mode, root, extra);- let response =- dispatch_request(&app, preflight_request(THEME_PATH, EVIL, Some("GET"))).await;- assert_eq!(StatusCode::NO_CONTENT, response.status(), "{mode}");- assert_eq!(- Some("*".to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{mode}"- );- }-- let relay = dispatch_app("relay", root, &[]);- assert_eq!(- StatusCode::METHOD_NOT_ALLOWED,- dispatch_request(&relay, preflight_request(THEME_PATH, WEB, Some("GET")))- .await- .status()- );- }-- #[tokio::test]- async fn enforce_applies_to_upload_mode_reads() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let app = dispatch_app("upload", root, ENFORCE);-- let allowed = dispatch_request(- &app,- read_request(Method::GET, ATTACHMENT_PATH, &[WEB.as_bytes()], None),- )- .await;- assert_eq!(StatusCode::OK, allowed.status());- assert_eq!(- Some(WEB.to_owned()),- header_value(&allowed, header::ACCESS_CONTROL_ALLOW_ORIGIN)- );-- let foreign = dispatch_request(- &app,- read_request(Method::GET, ATTACHMENT_PATH, &[EVIL.as_bytes()], None),- )- .await;- assert_eq!(StatusCode::FORBIDDEN, foreign.status());-- let origin_less = dispatch(&app, Method::GET, ATTACHMENT_PATH).await;- assert_eq!(StatusCode::OK, origin_less.status());- assert_eq!(- None,- header_value(&origin_less, header::ACCESS_CONTROL_ALLOW_ORIGIN)- );- assert_eq!(STORED_BYTES, body_of(origin_less).await);- }-- #[tokio::test]- async fn report_mode_changes_no_response_byte() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let off = dispatch_app("mp", root, &[]);- let report = dispatch_app("mp", root, REPORT);- let origin_shapes: [&[&[u8]]; 4] = [- &[EVIL.as_bytes()],- &[b"null".as_slice()],- &[WEB.as_bytes()],- &[],- ];- for origins in origin_shapes {- for (method, path, range, status) in [- (Method::GET, ATTACHMENT_PATH, None, StatusCode::OK),- (- Method::GET,- ATTACHMENT_PATH,- Some("bytes=0-3"),- StatusCode::PARTIAL_CONTENT,- ),- (- Method::GET,- ATTACHMENT_PATH,- Some("bytes=100-200"),- StatusCode::RANGE_NOT_SATISFIABLE,- ),- (Method::HEAD, ATTACHMENT_PATH, None, StatusCode::OK),- (- Method::GET,- "/attachments/1/2/missing.bin",- None,- StatusCode::NOT_FOUND,- ),- ] {- let label = format!("{method} {path} range={range:?} origins={origins:?}");- let expected =- dispatch_request(&off, read_request(method.clone(), path, origins, range))- .await;- let actual =- dispatch_request(&report, read_request(method, path, origins, range)).await;- assert_eq!(status, expected.status(), "{label}");- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(expected.headers(), actual.headers(), "{label}");- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }-- #[tokio::test]- async fn off_mode_never_inspects_origin() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_stored_representations(root);- let app = dispatch_app("mp", root, &[]);- for (shape, origins) in refused_origin_shapes() {- let response = dispatch_request(- &app,- read_request(Method::GET, ATTACHMENT_PATH, &origins, None),- )- .await;- assert_eq!(StatusCode::OK, response.status(), "{shape}");- assert_eq!(- Some("*".to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{shape}"- );- assert_eq!(- vec!["Accept-Encoding".to_owned()],- vary_values(&response),- "{shape}"- );- }- }-- #[tokio::test]- async fn static_mode_is_unchanged_with_every_cors_key_set() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let app = static_mode_app(root.as_path(), ENFORCE);- let origin_shapes: [&[&[u8]]; 4] = [- &[EVIL.as_bytes()],- &[b"null".as_slice()],- &[WEB.as_bytes()],- &[],- ];- for origins in origin_shapes {- let label = format!("origins={origins:?}");- let response = dispatch_request(- &app,- read_request(Method::GET, "/avatars/0.png", origins, None),- )- .await;- assert_eq!(StatusCode::OK, response.status(), "{label}");- assert_eq!(- Some("*".to_owned()),- header_value(&response, header::ACCESS_CONTROL_ALLOW_ORIGIN),- "{label}"- );- assert_eq!(- vec!["Accept-Encoding".to_owned()],- vary_values(&response),- "{label}"- );- assert_eq!(- Some(STATIC_CACHE_CONTROL.to_owned()),- header_value(&response, header::CACHE_CONTROL),- "{label}"- );- }- }-- const SIGNATURE_SECRET: [u8; 32] = [11u8; 32];- const OTHER_SIGNATURE_SECRET: [u8; 32] = [12u8; 32];- const SIGNATURE_ENFORCE: &[(&str, &str)] = &[- ("FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE", "enforce"),- (- "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64",- "CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCws=",- ),- ];- const SIGNATURE_REPORT: &[(&str, &str)] = &[- ("FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE", "report"),- (- "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64",- "CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCws=",- ),- ];- const SIGNATURE_OFF_WITH_KEYS: &[(&str, &str)] = &[- ("FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE", "off"),+ "a refused origin never reaches the transform cache"+ );+ }++ const SIGNATURE_SECRET: [u8; 32] = [11u8; 32];+ const OTHER_SIGNATURE_SECRET: [u8; 32] = [12u8; 32];+ const SIGNATURE_ENFORCE: &[(&str, &str)] = &[+ ("FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE", "enforce"),("FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64","CwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCws=",@@ -1245,161 +612,6 @@ mod tests {);}- struct SignedReadCase {- method: Method,- path: &'static str,- key: &'static str,- range: Option<&'static str>,- extra: &'static str,- status: StatusCode,- }-- fn signed_read_case(- method: Method,- path: &'static str,- key: &'static str,- range: Option<&'static str>,- extra: &'static str,- status: StatusCode,- ) -> SignedReadCase {- SignedReadCase {- method,- path,- key,- range,- extra,- status,- }- }-- fn signed_read_cases() -> [SignedReadCase; 7] {- [- signed_read_case(- Method::GET,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- None,- "",- StatusCode::OK,- ),- signed_read_case(- Method::GET,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- Some("bytes=0-3"),- "",- StatusCode::PARTIAL_CONTENT,- ),- signed_read_case(- Method::GET,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- Some("bytes=100-200"),- "",- StatusCode::RANGE_NOT_SATISFIABLE,- ),- signed_read_case(- Method::HEAD,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- None,- "",- StatusCode::OK,- ),- signed_read_case(- Method::HEAD,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- Some("bytes=0-3"),- "",- StatusCode::PARTIAL_CONTENT,- ),- signed_read_case(- Method::GET,- ATTACHMENT_PATH,- ATTACHMENT_KEY,- None,- "download=1",- StatusCode::OK,- ),- signed_read_case(- Method::GET,- IMAGE_PATH,- IMAGE_KEY,- None,- "width=64",- StatusCode::OK,- ),- ]- }-- fn without_cache_policy(headers: &HeaderMap) -> HeaderMap {- let mut headers = headers.clone();- headers.remove(header::CACHE_CONTROL);- headers.remove("cdn-cache-control");- headers- }-- #[tokio::test]- async fn enforce_serves_a_signed_attachment_on_every_read_shape() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let off = dispatch_app("mp", root, &[]);- let enforce = dispatch_app("mp", root, SIGNATURE_ENFORCE);-- for case in signed_read_cases() {- let SignedReadCase {- method,- path,- key,- range,- extra,- status,- } = case;- let unsigned = if extra.is_empty() {- path.to_owned()- } else {- format!("{path}?{extra}")- };- for signed in [- signed_uri(path, key, extra),- data_package_uri(path, key, extra),- ] {- let label = format!("{method} {signed} range={range:?}");- let expected = dispatch_uri(&off, method.clone(), &unsigned, &[], range).await;- let actual = dispatch_uri(&enforce, method.clone(), &signed, &[], range).await;- assert_eq!(status, expected.status(), "{label}");- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(- without_cache_policy(expected.headers()),- without_cache_policy(actual.headers()),- "{label}"- );- match (- header_value(&expected, header::CACHE_CONTROL),- header_value(&actual, header::CACHE_CONTROL),- ) {- (None, None) => {}- (Some(unsigned_policy), Some(signed_policy)) => {- assert!(unsigned_policy.contains("max-age=31536000"), "{label}");- assert!(signed_policy.starts_with("public, max-age="), "{label}");- assert!(!signed_policy.contains("max-age=31536000"), "{label}");- assert!(- !header_value(&actual, "cdn-cache-control")- .expect("a signed read pairs the cdn cache policy")- .contains("max-age=31536000"),- "{label}"- );- }- mismatch => panic!("{label} {mismatch:?}"),- }- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }-#[tokio::test]async fn enforce_answers_every_refusal_with_the_same_bytes() {let tmp = tempfile::tempdir().expect("storage root");@@ -1500,16 +712,6 @@ mod tests {}}- #[tokio::test]- async fn enforce_keeps_a_bad_storage_key_a_bad_request() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let app = dispatch_app("mp", root.as_path(), SIGNATURE_ENFORCE);- let response = dispatch_uri(&app, Method::GET, "/attachments/%2e%2e/x", &[], None).await;- assert_eq!(StatusCode::BAD_REQUEST, response.status());- assert_eq!(b"Bad Request".as_slice(), body_of(response).await);- }-#[tokio::test]async fn enforce_refuses_before_any_object_read() {let tmp = tempfile::tempdir().expect("storage root");@@ -1569,226 +771,6 @@ mod tests {);}- #[tokio::test]- async fn enforce_accepts_every_spelling_of_the_signed_key() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "cdn", "attachments/1/2/café.gif", STORED_BYTES);- write_object(root, "cdn", "attachments/1/2/a/b.gif", STORED_BYTES);- let app = dispatch_app("mp", root, SIGNATURE_ENFORCE);- for (path, key) in [- ("/attachments/1/2/caf%C3%A9.gif", "attachments/1/2/café.gif"),- ("/attachments/1/2/café.gif", "attachments/1/2/café.gif"),- ("/attachments/1/2/a%2Fb.gif", "attachments/1/2/a/b.gif"),- ("/attachments/1/2/a/b.gif", "attachments/1/2/a/b.gif"),- ] {- let now = now_secs();- let (issued, expires) = issue_window(now, now);- let uri = format!(- "{path}?ex={expires:08x}&is={issued:08x}&hm={}",- sign(key, expires, issued, UrlKind::Ordinary, &SIGNATURE_SECRET)- );- for spelling in [uri.clone(), format!("{uri}&")] {- let response = dispatch_uri(&app, Method::GET, &spelling, &[], None).await;- assert_eq!(StatusCode::OK, response.status(), "{spelling}");- assert_eq!(STORED_BYTES, body_of(response).await, "{spelling}");- }- }- }-- #[tokio::test]- async fn enforce_leaves_every_other_read_route_unsigned() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- write_object(- root,- "cdn",- "entrance-sounds/42/abc123def456.wav",- STORED_BYTES,- );- let asset = parse_standard_asset_path(ASSET_PATH).expect("asset path");- write_object(- root,- "cdn",- &asset.storage_key,- &crate::test_fixtures::synthetic_png(64, 64),- );- let off = dispatch_app("mp", root, &[]);- let enforce = dispatch_app("mp", root, SIGNATURE_ENFORCE);- for path in [THEME_PATH, ENTRANCE_SOUND_PATH, ASSET_PATH, "/nothing/here"] {- for method in [Method::GET, Method::HEAD] {- let label = format!("{method} {path}");- let expected = dispatch_uri(&off, method.clone(), path, &[], None).await;- let actual = dispatch_uri(&enforce, method, path, &[], None).await;- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(expected.headers(), actual.headers(), "{label}");- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }-- #[tokio::test]- async fn static_and_relay_modes_are_unchanged_with_the_signature_keys_set() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_object(root, "static", "avatars/0.png", STORED_BYTES);- write_object(root, "cdn", ATTACHMENT_KEY, STORED_BYTES);-- let plain_static = dispatch_app("static", root, &[]);- let signed_static = dispatch_app("static", root, SIGNATURE_ENFORCE);- let expected = dispatch_uri(&plain_static, Method::GET, "/avatars/0.png", &[], None).await;- let actual = dispatch_uri(&signed_static, Method::GET, "/avatars/0.png", &[], None).await;- assert_eq!(StatusCode::OK, expected.status());- assert_eq!(expected.status(), actual.status());- assert_eq!(expected.headers(), actual.headers());- assert_eq!(body_of(expected).await, body_of(actual).await);-- let relay = dispatch_app("relay", root, SIGNATURE_ENFORCE);- let response = dispatch_uri(&relay, Method::GET, ATTACHMENT_PATH, &[], None).await;- assert_eq!(StatusCode::NOT_FOUND, response.status());- assert_eq!(b"Not Found".as_slice(), body_of(response).await);-- let upload = dispatch_app("upload", root, SIGNATURE_ENFORCE);- let refused = dispatch_uri(&upload, Method::GET, ATTACHMENT_PATH, &[], None).await;- assert_eq!(StatusCode::NOT_FOUND, refused.status());- assert_eq!(UNAVAILABLE, body_of(refused).await.as_ref());- let allowed = dispatch_uri(- &upload,- Method::GET,- &signed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, ""),- &[],- None,- )- .await;- assert_eq!(StatusCode::OK, allowed.status());- assert_eq!(STORED_BYTES, body_of(allowed).await);- }-- #[tokio::test]- async fn a_cors_refusal_precedes_the_signature_check() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let mut env: Vec<(&str, &str)> = ENFORCE.to_vec();- env.extend_from_slice(SIGNATURE_ENFORCE);- let app = dispatch_app("mp", root, &env);- for uri in [- ATTACHMENT_PATH.to_owned(),- signed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, ""),- expired_uri(ATTACHMENT_PATH, ATTACHMENT_KEY),- ] {- let response = dispatch_uri(&app, Method::GET, &uri, &[EVIL.as_bytes()], None).await;- assert_eq!(StatusCode::FORBIDDEN, response.status(), "{uri}");- assert_eq!(b"Forbidden".as_slice(), body_of(response).await, "{uri}");- }- assert_eq!(0, counter(&app, "fluxer_media_proxy_storage_hits_total"));- }-- #[tokio::test]- async fn report_mode_changes_no_response_byte_for_a_stored_object() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let off = dispatch_app("mp", root, &[]);- let report = dispatch_app("mp", root, SIGNATURE_REPORT);- for uri in [- ATTACHMENT_PATH.to_owned(),- format!("{ATTACHMENT_PATH}?download=1"),- expired_uri(ATTACHMENT_PATH, ATTACHMENT_KEY),- foreign_key_uri(ATTACHMENT_PATH),- format!("{IMAGE_PATH}?width=64"),- ] {- for method in [Method::GET, Method::HEAD] {- for range in [None, Some("bytes=0-3"), Some("bytes=100-200")] {- let label = format!("{method} {uri} range={range:?}");- let expected = dispatch_uri(&off, method.clone(), &uri, &[], range).await;- let actual = dispatch_uri(&report, method.clone(), &uri, &[], range).await;- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(expected.headers(), actual.headers(), "{label}");- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }- }-- #[tokio::test]- async fn report_mode_changes_only_the_cache_lifetime_of_a_valid_signature() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let off = dispatch_app("mp", root, &[]);- let report = dispatch_app("mp", root, SIGNATURE_REPORT);- for uri in [- signed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, ""),- data_package_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, ""),- signed_uri(IMAGE_PATH, IMAGE_KEY, "width=64"),- ] {- for method in [Method::GET, Method::HEAD] {- for range in [None, Some("bytes=0-3")] {- let label = format!("{method} {uri} range={range:?}");- let expected = dispatch_uri(&off, method.clone(), &uri, &[], range).await;- let actual = dispatch_uri(&report, method.clone(), &uri, &[], range).await;- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(- without_cache_policy(expected.headers()),- without_cache_policy(actual.headers()),- "{label}"- );- assert_eq!(- Some(STATIC_CACHE_CONTROL.to_owned()),- header_value(&expected, header::CACHE_CONTROL),- "{label}"- );- let capped = header_value(&actual, header::CACHE_CONTROL)- .expect("a report read of a valid signature states its own max age");- let max_age: u64 = capped- .strip_prefix("public, max-age=")- .expect("the capped policy is a plain max age")- .parse()- .expect("a max age is a number");- assert!(max_age <= ATTACHMENT_URL_TTL_SECS, "{label} {capped}");- assert_eq!(- Some(capped),- header_value(&actual, "cdn-cache-control"),- "{label}"- );- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }- }-- #[tokio::test]- async fn only_enforce_masks_a_missing_object_so_it_matches_a_refusal() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let off = dispatch_app("mp", root, &[]);- let report = dispatch_app("mp", root, SIGNATURE_REPORT);- let enforce = dispatch_app("mp", root, SIGNATURE_ENFORCE);- let uri = "/attachments/1/2/absent.bin";- let plain = dispatch_uri(&off, Method::GET, uri, &[], None).await;- assert_eq!(StatusCode::NOT_FOUND, plain.status());- assert_eq!(b"Not Found".as_slice(), body_of(plain).await);-- let dry_run = dispatch_uri(&report, Method::GET, uri, &[], None).await;- assert_eq!(StatusCode::NOT_FOUND, dry_run.status());- assert_eq!(b"Not Found".as_slice(), body_of(dry_run).await);-- let signed = signed_uri(uri, "attachments/1/2/absent.bin", "");- let masked = dispatch_uri(&enforce, Method::GET, &signed, &[], None).await;- assert_eq!(StatusCode::NOT_FOUND, masked.status());- assert_eq!(UNAVAILABLE, body_of(masked).await.as_ref());- }-#[tokio::test]async fn an_enforced_attachment_is_never_cached_past_its_signature() {let tmp = tempfile::tempdir().expect("storage root");@@ -1831,105 +813,6 @@ mod tests {);}- #[tokio::test]- async fn an_unenforced_read_and_every_other_route_cache_forever() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- for env in [&[][..], SIGNATURE_OFF_WITH_KEYS] {- let app = dispatch_app("mp", root, env);- let response = dispatch_uri(- &app,- Method::GET,- &signed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, ""),- &[],- None,- )- .await;- assert_eq!(StatusCode::OK, response.status());- assert_eq!(- Some(STATIC_CACHE_CONTROL.to_owned()),- header_value(&response, header::CACHE_CONTROL)- );- }- let enforcing = dispatch_app("mp", root, SIGNATURE_ENFORCE);- let theme = dispatch_uri(&enforcing, Method::GET, THEME_PATH, &[], None).await;- assert_eq!(StatusCode::OK, theme.status());- assert_eq!(- Some(STATIC_CACHE_CONTROL.to_owned()),- header_value(&theme, header::CACHE_CONTROL)- );- }-- #[tokio::test]- async fn off_mode_serves_a_signed_request_unchanged() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let plain = dispatch_app("mp", root, &[]);- let staged = dispatch_app("mp", root, SIGNATURE_OFF_WITH_KEYS);- for uri in [- ATTACHMENT_PATH.to_owned(),- expired_uri(ATTACHMENT_PATH, ATTACHMENT_KEY),- foreign_key_uri(ATTACHMENT_PATH),- format!("{ATTACHMENT_PATH}?ex=zzz&is=&hm="),- "/attachments/1/2/absent.bin".to_owned(),- ] {- let expected = dispatch_uri(&plain, Method::GET, &uri, &[], None).await;- let actual = dispatch_uri(&staged, Method::GET, &uri, &[], None).await;- assert_eq!(expected.status(), actual.status(), "{uri}");- assert_eq!(expected.headers(), actual.headers(), "{uri}");- assert_eq!(body_of(expected).await, body_of(actual).await, "{uri}");- }- }-- #[tokio::test]- async fn off_mode_serves_a_signed_read_byte_identical_to_an_unsigned_read() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- for env in [&[][..], SIGNATURE_OFF_WITH_KEYS] {- let unsigned_app = dispatch_app("mp", root, &[]);- let signed_app = dispatch_app("mp", root, env);- for case in signed_read_cases() {- let SignedReadCase {- method,- path,- key,- range,- extra,- status,- } = case;- let separator = if extra.is_empty() { "" } else { "&" };- let unsigned = if extra.is_empty() {- path.to_owned()- } else {- format!("{path}?{extra}")- };- for signed in [- signed_uri(path, key, extra),- data_package_uri(path, key, extra),- with_extra(expired_uri(path, key), extra),- format!("{path}?ex=zzz&is=&hm=&uc=xx{separator}{extra}"),- format!("{path}?uc=dp{separator}{extra}"),- ] {- let label = format!("{method} {signed} range={range:?} env={env:?}");- let expected =- dispatch_uri(&unsigned_app, method.clone(), &unsigned, &[], range).await;- let actual =- dispatch_uri(&signed_app, method.clone(), &signed, &[], range).await;- assert_eq!(status, expected.status(), "{label}");- assert_eq!(expected.status(), actual.status(), "{label}");- assert_eq!(expected.headers(), actual.headers(), "{label}");- assert_eq!(body_of(expected).await, body_of(actual).await, "{label}");- }- }- }- }-#[test]fn signature_parameters_never_reach_the_attachment_reader() {let params: HashMap<String, String> = [@@ -1953,114 +836,4 @@ mod tests {assert_eq!(Some("64"), filtered.get("width").map(String::as_str));assert_eq!(Some("1"), filtered.get("download").map(String::as_str));}-- const SIGNATURE_VERDICTS: &str = "fluxer_media_proxy_attachment_signature_verdicts_total";-- fn verdict_counter(app: &Arc<AppState>, verdict: &str) -> u64 {- counter(- app,- &format!("{SIGNATURE_VERDICTS}{{verdict=\"{verdict}\"}}"),- )- }-- fn verdict_cases() -> [(&'static str, String); 5] {- [- ("missing", ATTACHMENT_PATH.to_owned()),- ("malformed", malformed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY)),- ("mismatch", foreign_key_uri(ATTACHMENT_PATH)),- ("expired", expired_uri(ATTACHMENT_PATH, ATTACHMENT_KEY)),- ("valid", signed_uri(ATTACHMENT_PATH, ATTACHMENT_KEY, "")),- ]- }-- #[tokio::test]- async fn report_and_enforce_count_the_verdict_of_every_attachment_read() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- for env in [SIGNATURE_REPORT, SIGNATURE_ENFORCE] {- let app = dispatch_app("mp", root, env);- for (verdict, uri) in verdict_cases() {- dispatch_uri(&app, Method::GET, &uri, &[], None).await;- assert_eq!(1, verdict_counter(&app, verdict), "{verdict} {uri} {env:?}");- }- for (verdict, _) in verdict_cases() {- assert_eq!(1, verdict_counter(&app, verdict), "{verdict} {env:?}");- }- }- }-- #[tokio::test]- async fn off_mode_counts_no_verdict_at_all() {- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- for env in [&[][..], SIGNATURE_OFF_WITH_KEYS] {- let app = dispatch_app("mp", root, env);- for (_, uri) in verdict_cases() {- dispatch_uri(&app, Method::GET, &uri, &[], None).await;- }- for (verdict, _) in verdict_cases() {- assert_eq!(0, verdict_counter(&app, verdict), "{verdict} {env:?}");- }- }- }-- #[tokio::test]- async fn a_report_burst_bounds_its_log_lines_and_still_counts_every_read() {- const BURST: usize = 50;- let tmp = tempfile::tempdir().expect("storage root");- let root = tmp.path().canonicalize().expect("canonical storage root");- let root = root.as_path();- write_signed_representations(root);- let report = dispatch_app("mp", root, SIGNATURE_REPORT);- let captured = CapturedLog::default();- let subscriber = tracing_subscriber::fmt()- .with_writer(captured.clone())- .with_ansi(false)- .with_max_level(tracing::Level::TRACE)- .finish();- {- let _guard = tracing::subscriber::set_default(subscriber);- for _ in 0..BURST {- let response = dispatch_uri(&report, Method::GET, ATTACHMENT_PATH, &[], None).await;- assert_eq!(StatusCode::OK, response.status());- }- tokio::time::sleep(std::time::Duration::from_millis(1_100)).await;- dispatch_uri(&report, Method::GET, ATTACHMENT_PATH, &[], None).await;- }- let log = captured.text();- let lines: Vec<&str> = log- .lines()- .filter(|line| line.contains("reason=\"attachment_signature_would_deny\""))- .collect();- assert!(- lines.len() < BURST,- "{} lines for {BURST} refusable reads\n{log}",- lines.len()- );- assert_eq!(- (BURST + 1) as u64,- verdict_counter(&report, "missing"),- "every refusable read is counted"- );- let suppressed: u64 = lines- .last()- .expect("the burst logs at least one line")- .split("suppressed=")- .nth(1)- .expect("an emitted line reports the lines it stands in for")- .split_whitespace()- .next()- .expect("a suppressed count")- .parse()- .expect("a suppressed count is a number");- assert!(suppressed > 0, "{log}");- assert!(- lines.len() as u64 + suppressed <= (BURST + 1) as u64,- "{log}"- );- }}