Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.8 KB
Raw Blame
"""Pin the two traversal classes the ``werkzeug~=3.1.6`` pin exists for.

``pyproject.toml`` keeps werkzeug for exactly two imports —
``security.safe_join`` behind ``PathValidator`` and ``utils.secure_filename``
behind ``sanitize_filename`` — and its comment says why the pin is narrow:

    The pin covers CVE-2026-27199 (safe_join accepting Windows device names in
    multi-segment paths) ... Do not reimplement against the stdlib without
    first adding Windows device-name and unicode NFKD-normalisation coverage;
    tests/security/ currently has none, so a regression in exactly the case
    this pin exists for would be silent.

That was accurate: the *defences* exist (`_has_encoded_traversal`,
`_has_unicode_traversal`), but nothing asserted on them, so a werkzeug
downgrade or a stdlib reimplementation would have passed CI. This file is that
missing coverage. It deliberately pins behaviour rather than implementation, so
it survives swapping werkzeug out — which is the stated eventual goal.
"""

import sys
import unicodedata

import pytest

from local_deep_research.security.filename_sanitizer import (
    UnsafeFilenameError,
    sanitize_filename,
)
from local_deep_research.security.path_validator import PathValidator


class TestUnicodeLookalikeTraversal:
    """Full-width and compatibility characters that NFKC-fold to './..'.

    A naive ``".." in text`` check misses these entirely: U+FF0E is not U+002E,
    so the substring test is False while the *normalised* string traverses.
    """

    @pytest.mark.parametrize(
        "hostile",
        [
            "../etc/passwd",  # fullwidth  ../etc/passwd
            "﹒﹒/etc/passwd",  # small full stop  ﹒﹒/
            "a/../../etc/passwd",
        ],
    )
    def test_fullwidth_dot_traversal_is_rejected(self, hostile, tmp_path):
        assert ".." not in hostile, (
            "this input must NOT contain literal '..' or it would prove "
            "nothing about unicode handling -- it would be caught by the "
            "plain substring check instead"
        )
        assert ".." in unicodedata.normalize("NFKC", hostile), (
            "input must fold to '..' under NFKC or it is not a look-alike"
        )

        with pytest.raises(ValueError):
            PathValidator.validate_safe_path(hostile, tmp_path)


class TestEncodedTraversal:
    """Percent-encoded '..' — single and double encoded.

    safe_join only sees literal segments, so an encoded form that some upstream
    layer might later decode has to be refused before it gets there.
    """

    @pytest.mark.parametrize(
        "hostile",
        [
            "%2e%2e/etc/passwd",
            "%2E%2E/etc/passwd",
            "%252e%252e/etc/passwd",  # double-encoded
            "a/%2e%2e/%2e%2e/etc/passwd",
        ],
    )
    def test_encoded_traversal_is_rejected(self, hostile, tmp_path):
        assert ".." not in hostile, (
            "input must not contain a literal '..' or the encoded-form check "
            "is not what rejected it"
        )
        with pytest.raises(ValueError):
            PathValidator.validate_safe_path(hostile, tmp_path)


class TestLiteralTraversalStillRejected:
    """The ordinary case, so the exotic tests above cannot be the only cover."""

    @pytest.mark.parametrize(
        "hostile",
        [
            "../etc/passwd",
            "a/../../etc/passwd",
            "a/b/../../../../etc/passwd",
        ],
    )
    def test_multi_segment_traversal_is_rejected(self, hostile, tmp_path):
        with pytest.raises(ValueError):
            PathValidator.validate_safe_path(hostile, tmp_path)

    def test_an_ordinary_relative_path_is_still_allowed(self, tmp_path):
        """Anti-vacuity: if everything were rejected the tests above pass for
        the wrong reason."""
        result = PathValidator.validate_safe_path("sub/file.json", tmp_path)
        assert result is not None
        assert str(result).startswith(str(tmp_path))


class TestWindowsDeviceNames:
    """The CVE's own subject, pinned honestly including its platform gate.

    werkzeug's device-name handling in ``secure_filename`` is gated on
    ``os.name == "nt"``, so on POSIX these pass through unchanged and that is
    correct, not a hole: 'CON' is an ordinary filename on Linux. Pinning the
    real, platform-dependent behaviour is the only assertion that is true on
    both platforms — asserting they are always stripped would fail on Linux,
    and asserting they always survive would fail on Windows.
    """

    DEVICE_NAMES = ["CON", "PRN", "AUX", "NUL", "COM1", "LPT1"]

    @pytest.mark.parametrize("name", DEVICE_NAMES)
    def test_device_name_handling_matches_the_platform(self, name):
        got = sanitize_filename(f"{name}.txt", allowed_extensions=(".txt",))
        if sys.platform.startswith("win"):
            assert got.startswith("_"), (
                f"on Windows werkzeug must prefix the reserved device name "
                f"{name!r}; got {got!r}"
            )
        else:
            assert got == f"{name}.txt", (
                f"on POSIX {name!r} is an ordinary filename and must survive "
                f"unchanged; got {got!r}"
            )

    def test_device_name_cannot_smuggle_traversal(self):
        """Whatever happens to the device name, the path part must not survive."""
        got = sanitize_filename("../../CON.txt", allowed_extensions=(".txt",))
        assert "/" not in got and ".." not in got, (
            f"sanitized filename still carries path structure: {got!r}"
        )


class TestSanitizerRejectsWhatItCannotMakeSafe:
    def test_a_name_that_sanitizes_to_nothing_raises(self):
        """Anti-vacuity for the class above: the sanitizer does refuse things,
        so 'it returned a string' is not automatic."""
        with pytest.raises(UnsafeFilenameError):
            sanitize_filename("../../..", allowed_extensions=(".txt",))