Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/stylesheets/navi.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ body {
/* h2: 2rem + accent underline per brand guide */
.md-typeset h2 {
font-size: 2rem;
padding-bottom: 0.75rem;
padding-bottom: 1.5rem;
border-bottom: 1px solid #2a2a2a;
}

Expand All @@ -56,7 +56,6 @@ body {
width: 40px;
height: 2px;
background: #7eb8a8;
margin-top: 0.75rem;
opacity: 0.6;
}

Expand Down Expand Up @@ -252,7 +251,6 @@ body {
:focus-visible {
outline: 3px solid #7eb8a8;
outline-offset: 2px;
border-radius: 3px;
}

/* --- Footer muted --- */
Expand Down
8 changes: 7 additions & 1 deletion src/navi_sanitize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import logging
from collections.abc import Callable
from importlib import metadata as importlib_metadata

from navi_sanitize._decode import decode_evasion
from navi_sanitize._pipeline import clean, walk
Expand All @@ -13,7 +14,12 @@

Escaper = Callable[[str], str]

__version__ = "0.2.1"
try:
__version__ = importlib_metadata.version("navi-sanitize")
except importlib_metadata.PackageNotFoundError:
# Fallback for cases where package metadata is not available (e.g. source checkout)
__version__ = "0.0.0"

__all__ = [
"Escaper",
"clean",
Expand Down
45 changes: 26 additions & 19 deletions tests/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
"../../"
)

hostile_text = st.text(alphabet=_HOSTILE_ALPHABET, min_size=0, max_size=200)
unicode_text = st.text(min_size=0, max_size=200)
safe_text = st.text(
HOSTILE_TEXT = st.text(alphabet=_HOSTILE_ALPHABET, min_size=0, max_size=200)
UNICODE_TEXT = st.text(min_size=0, max_size=200)
SAFE_TEXT = st.text(
alphabet="abcdefghijklmnopqrstuvwxyz0123456789 .-_",
min_size=0,
max_size=100,
Expand Down Expand Up @@ -86,7 +86,7 @@ def _nested_structure(leaf: st.SearchStrategy[object]) -> st.SearchStrategy[obje

walk_structure = _nested_structure(
st.one_of(
hostile_text,
HOSTILE_TEXT,
st.integers(min_value=-1000, max_value=1000),
st.none(),
st.booleans(),
Expand Down Expand Up @@ -129,41 +129,41 @@ def _collect_leaf_strings(data: object) -> list[str]:
class TestCleanProperties:
"""Property-based tests for clean() invariants."""

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_always_returns_str(self, text: str) -> None:
assert isinstance(clean(text), str)

@given(text=hostile_text)
@given(text=HOSTILE_TEXT)
@settings(max_examples=50)
def test_no_null_bytes(self, text: str) -> None:
assert "\x00" not in clean(text)

@given(text=hostile_text)
@given(text=HOSTILE_TEXT)
@settings(max_examples=50)
def test_no_invisible_characters(self, text: str) -> None:
assert not INVISIBLE_RE.search(clean(text))

@given(text=hostile_text)
@given(text=HOSTILE_TEXT)
@settings(max_examples=50)
def test_no_homoglyphs(self, text: str) -> None:
result = clean(text)
remaining = set(result) & set(HOMOGLYPH_MAP)
assert not remaining, f"Homoglyphs remain: {remaining!r}"

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_nfkc_stable(self, text: str) -> None:
result = clean(text)
assert unicodedata.normalize("NFKC", result) == result

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_idempotent(self, text: str) -> None:
first = clean(text)
assert clean(first) == first

@given(text=hostile_text)
@given(text=HOSTILE_TEXT)
@settings(max_examples=50)
def test_idempotent_hostile(self, text: str) -> None:
first = clean(text)
Expand Down Expand Up @@ -220,7 +220,14 @@ def test_handles_cycle(self) -> None:
a: dict[str, object] = {"val": "hello"}
a["self"] = a
result = walk(a)
assert result["val"] == "hello"
leaf = result["val"]
assert leaf == "hello"
# Leaf strings in cyclic structures should still satisfy clean() invariants.
assert "\x00" not in leaf
assert not INVISIBLE_RE.search(leaf)
remaining = set(leaf) & set(HOMOGLYPH_MAP)
assert not remaining, f"Homoglyph in walk output: {remaining!r}"
assert unicodedata.normalize("NFKC", leaf) == leaf
assert result["self"] is result


Expand All @@ -232,17 +239,17 @@ def test_handles_cycle(self) -> None:
class TestScriptDetectionProperties:
"""Property-based tests for script detection."""

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_returns_subset_of_known_scripts(self, text: str) -> None:
assert detect_scripts(text) <= KNOWN_SCRIPTS

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_is_mixed_consistent(self, text: str) -> None:
assert is_mixed_script(text) == (len(detect_scripts(text)) >= 2)

@given(text=safe_text)
@given(text=SAFE_TEXT)
@settings(max_examples=50)
def test_ascii_never_mixed(self, text: str) -> None:
assert not is_mixed_script(text)
Expand All @@ -256,13 +263,13 @@ def test_ascii_never_mixed(self, text: str) -> None:
class TestJinja2EscaperProperties:
"""Property-based tests for jinja2_escaper."""

@given(text=hostile_text)
@given(text=HOSTILE_TEXT)
@settings(max_examples=50)
def test_no_raw_delimiters(self, text: str) -> None:
result = jinja2_escaper(text)
assert not _JINJA2_DELIMITERS_RE.search(result)

@given(text=safe_text)
@given(text=SAFE_TEXT)
@settings(max_examples=50)
def test_safe_text_unchanged(self, text: str) -> None:
if not _JINJA2_DELIMITERS_RE.search(text):
Expand Down Expand Up @@ -308,13 +315,13 @@ def test_idempotent(self, text: str) -> None:
class TestDecodeEvasionProperties:
"""Property-based tests for decode_evasion."""

@given(text=unicode_text)
@given(text=UNICODE_TEXT)
@settings(max_examples=50)
def test_never_raises(self, text: str) -> None:
result = decode_evasion(text)
assert isinstance(result, str)

@given(text=safe_text)
@given(text=SAFE_TEXT)
@settings(max_examples=50)
def test_clean_text_unchanged(self, text: str) -> None:
if "%" not in text and "\\" not in text and "&" not in text:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def test_tuples_pass_through_by_reference(self) -> None:

data = {"key": ("n\u0430vi",)}
result = walk(data, max_depth=10)
assert result is not data
assert result["key"] == ("n\u0430vi",)

def test_never_crashes_on_any_depth(self) -> None:
Expand Down Expand Up @@ -338,8 +339,13 @@ def test_hostile_keys_and_values_simultaneously(self) -> None:
"\x00key": {"inner\u200b": ["\u0430", "\x00"]},
}
result = walk(data)
# Sanitized keys must exist
assert "name" in result
assert result["name"] == "value"
assert "key" in result
assert "inner" in result["key"]
assert result["key"]["inner"] == ["a", ""]
# Original hostile keys must be completely removed
assert "n\u0430me" not in result
assert "\x00key" not in result
assert "inner\u200b" not in result["key"]
Loading