Files
filecodebox/tests/test_download_filename.py
orion 41519afb6a ✨ feat: 整合 2.8.5 后端存储、安全与运行优化
* 禁用 OSS 非必需校验尾部,改用精确会话前缀逐对象清理
* 保护上传终态与分享事务,清理失败保留会话并支持安全重试
* 统一下载响应头,兼容旧下载器并防止文件名注入
* 限流改用单调时钟并限制 IP 记录,缓存模板与静态资源策略
* 统一 ORM 初始化与异常处理,新增查询索引及 SQLite 性能参数
* 固定 uvloop/httptools,统一 Docker keep-alive 与内存回收配置
* 同步版本与升级文档,补充 OSS、清理、缓存及数据库回归验证
2026-10-29 16:37:14 +08:00

50 lines
1.8 KiB
Python

"""Issue #531: keep a legacy filename alongside the RFC 5987 UTF-8 name."""
import re
from types import SimpleNamespace
from urllib.parse import quote, unquote
import pytest
from apps.base import local_share, views
from apps.base.models import FileCodes
from core.settings import settings
from core.storage import build_attachment_headers
def legacy_filename(disposition):
match = re.search(r'(?:^|;\s*)filename="([^"]+)"', disposition)
assert match is not None, "legacy download clients cannot read filename*"
return match.group(1)
@pytest.mark.parametrize(
("filename", "fallback"),
[
("report.pdf", "report.pdf"),
("my archive.tar.gz", "my archive.tar.gz"),
("报告.pdf", "__.pdf"),
("图像 🖼.png", "__ _.png"),
("", "download"),
],
)
def test_attachment_has_legacy_and_utf8_filenames(filename, fallback):
headers = build_attachment_headers(filename, 42)
disposition = headers["Content-Disposition"]
assert disposition.startswith("attachment;")
assert legacy_filename(disposition) == fallback
assert disposition.endswith("filename*=UTF-8''" + quote(filename, safe=""))
assert headers["Content-Length"] == "42"
assert disposition.isascii()
def test_legacy_fallback_cannot_inject_parameters_headers_or_paths():
filename = '../bad\\name"; inline;\r\nX-Evil: true\x00%20.html'
disposition = build_attachment_headers(filename)["Content-Disposition"]
fallback = legacy_filename(disposition)
assert re.fullmatch(r"[A-Za-z0-9._ -]+", fallback)
assert not fallback.startswith(".")
assert fallback.endswith(".html")
assert "\r" not in disposition and "\n" not in disposition and "\x00" not in disposition
assert len(disposition.split(";")) == 3
assert unquote(disposition.split("filename*=UTF-8''", 1)[1]) == filename