From 52b7625bdff32147fb187289c37db821ee4e0b7c Mon Sep 17 00:00:00 2001 From: Amey Pawar <138877912+ameyypawar@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:43:09 +0530 Subject: [PATCH] fix(api): encode non-ASCII filenames in S3 Content-Disposition (#9634) * fix(api): encode non-ASCII filenames in S3 Content-Disposition Multipart S3 uploads stored Content-Disposition as `inline; filename=""`. For a non-ASCII file name (e.g. a Chinese name) the stored header is not valid ASCII, so on download the object store fails to read the attribute back (`HeaderValue::to_str`) and returns a 500: "Content-Disposition header contained non UTF-8 characters". Encode non-ASCII names with the RFC 6266 / RFC 5987 `filename*=UTF-8''` form, which is pure ASCII and is decoded back to the original name by clients. ASCII names are unchanged. Addresses #7456 * fix: send an ASCII filename fallback and escape quotes in Content-Disposition, also for HTTP route static assets Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Ruben Fiszel Co-authored-by: Claude Opus 5.5 --- backend/windmill-api/src/args.rs | 66 ++++++++++++++++++- .../windmill-api/src/triggers/http/handler.rs | 2 +- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/backend/windmill-api/src/args.rs b/backend/windmill-api/src/args.rs index 569df56ece..bea546ee0c 100644 --- a/backend/windmill-api/src/args.rs +++ b/backend/windmill-api/src/args.rs @@ -69,6 +69,40 @@ pub struct WebhookArgs { pub metadata: WebhookArgsMetadata, } +/// `inline` `Content-Disposition` value naming `filename`, always pure ASCII. +/// +/// Raw non-ASCII bytes in the header are stored as-is by the object store, which +/// then fails to read the attribute back (`HeaderValue::to_str`), turning every +/// download of the object into a 500. A name that is not printable ASCII is +/// therefore sent as RFC 6266 `filename*=UTF-8''`, after a +/// `filename="..."` fallback where each such character is replaced by `_`. +#[cfg_attr(not(feature = "parquet"), allow(dead_code))] +pub(crate) fn content_disposition_inline(filename: &str) -> String { + let mut fallback = String::with_capacity(filename.len()); + let mut lossy = false; + for c in filename.chars() { + match c { + '"' | '\\' => { + fallback.push('\\'); + fallback.push(c); + } + ' '..='~' => fallback.push(c), + _ => { + fallback.push('_'); + lossy = true; + } + } + } + if lossy { + format!( + "inline; filename=\"{fallback}\"; filename*=UTF-8''{}", + urlencoding::encode(filename) + ) + } else { + format!("inline; filename=\"{fallback}\"") + } +} + // capture // @@ -173,7 +207,7 @@ impl RawWebhookArgs { ( Attribute::ContentDisposition, if let Some(filename) = filename { - format!("inline; filename=\"{}\"", filename) + content_disposition_inline(&filename) } else { "inline".to_string() }, @@ -958,4 +992,34 @@ mod tests { // A name used by both a text and a file part keeps both, in body order. assert_eq!(get("mixed"), r#"["a",{"s3":"k4"}]"#); } + + #[test] + fn test_content_disposition_inline() { + assert_eq!( + content_disposition_inline("report 2024.md"), + r#"inline; filename="report 2024.md""# + ); + assert_eq!( + content_disposition_inline(r#"a"b\c.txt"#), + r#"inline; filename="a\"b\\c.txt""# + ); + assert_eq!( + content_disposition_inline("这是测试.md"), + r#"inline; filename="____.md"; filename*=UTF-8''%E8%BF%99%E6%98%AF%E6%B5%8B%E8%AF%95.md"# + ); + assert_eq!( + content_disposition_inline("a\r\nb\"'*.txt"), + r#"inline; filename="a__b\"'*.txt"; filename*=UTF-8''a%0D%0Ab%22%27%2A.txt"# + ); + for name in ["这是测试.md", "Москва \"x\".pdf", "a\nb", ""] { + let v = content_disposition_inline(name); + assert!( + axum::http::HeaderValue::from_str(&v) + .unwrap() + .to_str() + .is_ok(), + "{v}" + ); + } + } } diff --git a/backend/windmill-api/src/triggers/http/handler.rs b/backend/windmill-api/src/triggers/http/handler.rs index a2637e391d..402cc28696 100644 --- a/backend/windmill-api/src/triggers/http/handler.rs +++ b/backend/windmill-api/src/triggers/http/handler.rs @@ -708,7 +708,7 @@ async fn route_job( .unwrap_or("inline".parse().unwrap()) }, |filename| { - format!("inline; filename=\"{}\"", filename) + crate::args::content_disposition_inline(filename) .parse() .unwrap_or("inline".parse().unwrap()) },