Skip to content

Commit 17a4a9d

Browse files
fix(P2): add bidi control character detection (CVE-2021-42574 / Trojan Source)
P2_PATTERNS was missing Unicode bidi override/embedding/isolate characters (U+202A-U+202E, U+2066-U+2069) that can be used to hide malicious instructions from human code review while the LLM sees and executes them. Add the range with confidence 0.85 (higher than plain zero-width chars because bidi controls have almost no legitimate use in AI skill content). Closes #39 Signed-off-by: Lalit Shrotriya <[email protected]>
1 parent a5092dd commit 17a4a9d

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_prompt_injection.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
(r"<!--.*?(?:system|instructions?|ignore|POST|GET|send|transmit).*?-->", 0.7),
5252
(r"\[//\]:\s*#\s*\(.*?(?:system|instructions?|ignore|POST|GET|send|transmit).*?\)", 0.8),
5353
(r"[\u200b\u200c\u200d\u2060\ufeff]", 0.6),
54+
(r"[\u202a-\u202e\u2066-\u2069]", 0.85),
5455
(r"data:text/plain;base64,[A-Za-z0-9+/=]{50,}", 0.7),
5556
]
5657
# P3: Exfiltration Commands

‎tests/nodes/analyzers/test_static_patterns.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ def test_p2_hidden_comment_produces_finding(self):
6666
assert len(findings) >= 1
6767
assert any(f.rule_id == "P2" for f in findings)
6868

69+
def test_p2_bidi_control_chars_produce_finding(self):
70+
"""Bidi control characters (Trojan Source CVE-2021-42574) yield P2."""
71+
# ‮ is RIGHT-TO-LEFT OVERRIDE — a bidi control character
72+
state = {
73+
"components": ["SKILL.md"],
74+
"file_cache": {
75+
"SKILL.md": "Normal text‮ evil hidden content‬",
76+
},
77+
}
78+
findings = static_runner.run_static_patterns(state, [prompt_injection_module])
79+
assert len(findings) >= 1
80+
assert any(f.rule_id == "P2" for f in findings)
81+
82+
def test_p2_bidi_rlo_edge_cases(self):
83+
"""Bidi override variants ‪-‮ and ⁦-⁩ all yield P2."""
84+
bidi_chars = ["‪", "‫", "‬", "‭", "‮", "⁦", "⁧", "⁨", "⁩"]
85+
for ch in bidi_chars:
86+
state = {
87+
"components": ["skill.md"],
88+
"file_cache": {"skill.md": f"text{ch}more"},
89+
}
90+
findings = static_runner.run_static_patterns(state, [prompt_injection_module])
91+
p2 = [f for f in findings if f.rule_id == "P2"]
92+
assert len(p2) >= 1, f"Expected P2 for bidi char U+{ord(ch):04X}"
93+
6994
def test_safe_content_no_p1_p2(self):
7095
"""Safe content does not produce P1/P2."""
7196
state = {

0 commit comments

Comments
 (0)