mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
feat: enhance filename sanitization to replace non-identifier characters
Signed-off-by: d9pouces <github@19pouces.net>
This commit is contained in:
+1
-1
@@ -361,6 +361,6 @@ def test_apply_sanitizes_source_name_with_slash(tmp_path) -> None:
|
||||
|
||||
AptModule().apply(context)
|
||||
|
||||
source_file = dst / "etc/apt/sources.list.d/.._.._evil.sources"
|
||||
source_file = dst / "etc/apt/sources.list.d/______evil.sources"
|
||||
assert source_file.exists()
|
||||
assert not (dst / "etc/apt/sources.list.d/evil.sources").exists()
|
||||
|
||||
@@ -67,7 +67,7 @@ def test_apply_sanitizes_certificate_name_with_slash(tmp_path) -> None:
|
||||
|
||||
CertificatesModule().apply(context)
|
||||
|
||||
local_cert = dst / "usr/local/share/ca-certificates/.._.._evil.crt"
|
||||
local_cert = dst / "usr/local/share/ca-certificates/______evil.crt"
|
||||
assert local_cert.read_bytes() == b"fake-cert-bytes"
|
||||
# The "/" must never be interpreted as a path separator: without
|
||||
# sanitizing, "../../evil" would have reshuffled the write up to
|
||||
|
||||
@@ -29,8 +29,25 @@ def test_sanitize_filename_leaves_plain_names_untouched() -> None:
|
||||
|
||||
|
||||
def test_sanitize_filename_replaces_forward_slash() -> None:
|
||||
assert sanitize_filename("../../etc/passwd") == ".._.._etc_passwd"
|
||||
assert sanitize_filename("../../etc/passwd") == "______etc_passwd"
|
||||
|
||||
|
||||
def test_sanitize_filename_replaces_backslash() -> None:
|
||||
assert sanitize_filename(r"a\b") == "a_b"
|
||||
|
||||
|
||||
def test_sanitize_filename_replaces_non_identifier_characters() -> None:
|
||||
assert sanitize_filename("My Root CA (v2).pem\n") == "My_Root_CA__v2__pem_"
|
||||
|
||||
|
||||
def test_sanitize_filename_keeps_letters_digits_underscore_and_dash() -> None:
|
||||
assert sanitize_filename("Inter_Diode-2026") == "Inter_Diode-2026"
|
||||
|
||||
|
||||
def test_sanitize_filename_never_returns_dot_names() -> None:
|
||||
assert sanitize_filename(".") == "_"
|
||||
assert sanitize_filename("..") == "__"
|
||||
|
||||
|
||||
def test_sanitize_filename_never_returns_empty_string() -> None:
|
||||
assert sanitize_filename("") == "_"
|
||||
|
||||
+1
-1
@@ -327,7 +327,7 @@ def test_apply_sanitizes_key_name_with_slash(tmp_path, monkeypatch) -> None:
|
||||
|
||||
YumModule().apply(context)
|
||||
|
||||
key_content = (dst / "etc/pki/rpm-gpg/RPM-GPG-KEY-.._.._evil-key").read_bytes()
|
||||
key_content = (dst / "etc/pki/rpm-gpg/RPM-GPG-KEY-______evil-key").read_bytes()
|
||||
assert key_content == b"fake-rpm-gpg-key-bytes"
|
||||
mock_run.assert_called_once()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user