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="<raw name>"`. 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''<percent-encoded>` 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 <noreply@anthropic.com>

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Amey Pawar
2026-11-19 11:13:09 +01:00
committed by GitHub
co-authored by Claude Opus 5.5 Ruben Fiszel
parent 089808f627
commit 52b7625bdf
2 changed files with 66 additions and 2 deletions
+65 -1
View File
@@ -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''<percent-encoded>`, 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}"
);
}
}
}
@@ -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())
},