Deterministic sanitization for untrusted text: strips invisible and control characters, applies NFKC and replaces common homoglyphs. No dependencies.
Maintained. Last release 2 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install navi-sanitizenavi-sanitize
Deterministic sanitization of untrusted text for Python 3.12+, with no dependencies.
clean() removes null bytes and 492 invisible, formatting and control characters,
applies NFKC normalization, and replaces 66 curated homoglyphs (Cyrillic, Greek,
Armenian, Cherokee, Latin Extended and typographic lookalikes) with ASCII. An optional escaper then
prepares the result for one destination. It normalizes characters; it is not a
complete defense against prompt, template, HTML, SQL or path injection.
pip install navi-sanitize
from navi_sanitize import clean, path_escaper, walk
clean("Неllo Wоrld") # 'Hello World' (Cyrillic Н, о)
clean("pass\u200bword\x00") # 'password'
clean("full width") # 'full width'
clean("../../etc/passwd", escaper=path_escaper) # 'etc/passwd'
walk({"n\u0430me": ["te\u200bst"]}) # {'name': ['test']} (a new dict)
walk() sanitizes every string in nested dicts and lists, keys included, and returns
new containers. decode_evasion(), detect_scripts() and is_mixed_script() are
opt-in helpers that clean() never runs.
Limits
- Legitimate text changes. Mapped letters are replaced inside real words
(
clean("привет")gives the mixed-script'пpивeт'), ZWJ emoji sequences split, variation selectors and tag characters go, Arabic/Hebrew directional marks are removed, and NFKC folds compatibility forms. Apply it to fields where that is acceptable.