fix export case download with non-ascii names (#24100)
CI / AMD64 Build (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s

This commit is contained in:
Josh Hawkins
2026-08-26 14:42:34 -06:00
committed by GitHub
parent 5197881ef7
commit ca18b8dc13
2 changed files with 98 additions and 1 deletions
+20 -1
View File
@@ -9,6 +9,7 @@ import zipfile
from collections import deque
from collections.abc import Iterator
from pathlib import Path
from urllib.parse import quote
import psutil
from fastapi import APIRouter, Depends, Query, Request
@@ -453,6 +454,22 @@ def _stream_case_archive(exports: list[Export]) -> Iterator[bytes]:
yield from buffer.drain()
def _content_disposition(filename: str, ascii_fallback: str) -> str:
"""Build an attachment Content-Disposition that survives non-ASCII names.
Header values are encoded as latin-1, so a name outside that range cannot
go in filename at all. RFC 6266 handles this with a pair: a plain ASCII
filename for old clients, plus a percent-encoded UTF-8 filename* that
every current browser prefers.
"""
ascii_name = filename if filename.isascii() else ascii_fallback
return (
f'attachment; filename="{ascii_name}"; '
f"filename*=UTF-8''{quote(filename, safe='')}"
)
@router.get(
"/cases/{case_id}/download",
dependencies=[Depends(allow_any_authenticated())],
@@ -495,7 +512,9 @@ def download_export_case(
_stream_case_archive(exports),
media_type="application/zip",
headers={
"Content-Disposition": f'attachment; filename="{archive_base}.zip"',
"Content-Disposition": _content_disposition(
f"{archive_base}.zip", f"{case_id}.zip"
),
},
)