Merge pull request #670 from santhreal/fix/safe-filename-strip-controls

fix(partners): strip control chars in safe_filename
This commit is contained in:
Bingxi Zhao
2026-07-23 22:30:23 +08:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@@ -30,11 +30,13 @@ def timestamp() -> str:
_UNSAFE_CHARS = re.compile(r'[<>:"/\\|?*]')
_CONTROL_CHARS = re.compile(r"[\x00-\x1f\x7f]")
def safe_filename(name: str) -> str:
"""Replace unsafe path characters with underscores."""
return _UNSAFE_CHARS.sub("_", name).strip()
"""Replace unsafe path / control characters; ``.`` / ``..`` become empty."""
cleaned = _CONTROL_CHARS.sub("", _UNSAFE_CHARS.sub("_", name or "")).strip().strip(".")
return cleaned
def split_message(content: str, max_len: int = 2000) -> list[str]:

View File

@@ -0,0 +1,23 @@
"""Regression tests for partners.helpers.safe_filename."""
from __future__ import annotations
from pathlib import Path
from deeptutor.partners.helpers import safe_filename
def test_safe_filename_strips_embedded_null_byte() -> None:
cleaned = safe_filename("photo\x00.png")
assert "\x00" not in cleaned
assert cleaned.endswith(".png")
path = Path("/tmp") / f"abc_{cleaned}"
path.write_bytes(b"x")
assert path.read_bytes() == b"x"
path.unlink()
def test_safe_filename_strips_newlines_and_dot_names() -> None:
assert "\n" not in safe_filename("a\nb.png")
assert safe_filename(".") == ""
assert safe_filename("..") == ""