Skip to content

Commit 5a97cc0

Browse files
test(injection): pin interleaved and distant-span redaction cases (#123)
1 parent 6371b23 commit 5a97cc0

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎sdk/tests/unit/scanners/test_scanners.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ def test_interleaved_zero_width_redacts_as_one_block(self):
101101
assert redacted.count("[BLOCKED:injection]") == 1
102102
assert redacted == "h[BLOCKED:injection]d"
103103

104+
def test_fully_interleaved_run_redacts_as_single_block(self):
105+
from unplug.config.policy import RedactionMode, ScanPolicy
106+
from unplug.core.redaction import apply_span_redactions
107+
108+
raw = "h\u200be\u200bl\u200bl\u200bo \u200bw\u200bo\u200br\u200bl\u200bd"
109+
findings = self.scanner.scan(_make_text(raw), self.ctx)
110+
invisible = [f for f in findings if f.subcategory == "invisible_text"]
111+
assert len(invisible) == 1
112+
redacted = apply_span_redactions(
113+
raw, findings, ScanPolicy(redaction_mode=RedactionMode.BLOCKED_TAGS)
114+
)
115+
assert redacted is not None
116+
assert redacted.count("[BLOCKED:injection]") == 1
117+
assert redacted == "h[BLOCKED:injection]d"
118+
119+
def test_distant_evasion_spans_kept_separate(self):
120+
from unplug.config.policy import RedactionMode, ScanPolicy
121+
from unplug.core.redaction import apply_span_redactions
122+
123+
raw = (
124+
"Zero width here\u200b and then a long stretch of perfectly ordinary "
125+
"English text that should survive redaction, ending with fullwidth \uff41\uff42\uff43"
126+
)
127+
findings = self.scanner.scan(_make_text(raw), self.ctx)
128+
invisible = [f for f in findings if f.subcategory == "invisible_text"]
129+
assert len(invisible) == 2
130+
assert (invisible[0].span_start, invisible[0].span_end) == (15, 16)
131+
assert (invisible[1].span_start, invisible[1].span_end) == (129, 132)
132+
redacted = apply_span_redactions(
133+
raw, findings, ScanPolicy(redaction_mode=RedactionMode.BLOCKED_TAGS)
134+
)
135+
assert redacted is not None
136+
assert redacted.count("[BLOCKED:injection]") == 2
137+
assert "perfectly ordinary English text that should survive redaction" in redacted
138+
104139
def test_cjk_text_not_invisible_text(self):
105140
text = _make_text("今天北京的天气很好,我想去公园散步。")
106141
findings = self.scanner.scan(text, self.ctx)

0 commit comments

Comments
 (0)