144144 re .compile (r"\balways\s+comply\b" , re .IGNORECASE ),
145145)
146146
147+ _BENIGN_AR_VALUE_LABEL_PATTERN = re .compile (
148+ r"(^|\n)\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description|prompt)\s*:\s*" ,
149+ re .IGNORECASE ,
150+ )
151+
147152
148153def _is_directly_instructive (context : str , matched_text : str ) -> bool :
149154 """Return True when the match still looks like an active adversarial instruction."""
@@ -155,16 +160,30 @@ def _is_directly_instructive(context: str, matched_text: str) -> bool:
155160 return "do anything now" in matched_text .lower ()
156161
157162
163+ def _is_quoted_or_labeled_benign_match (context : str , matched_text : str ) -> bool :
164+ """Return True when a direct phrase appears only as quoted or declared reference text."""
165+ matched_text_lower = matched_text .lower ()
166+ if any (f"{ quote } { matched_text_lower } { quote } " in context for quote in ('"' , "'" , "`" )):
167+ return True
168+ if re .search (rf"\bthe\s+phrase\s+[\"'`]?{ re .escape (matched_text_lower )} [\"'`]?" , context ):
169+ return True
170+ return bool (
171+ _BENIGN_AR_VALUE_LABEL_PATTERN .search (context )
172+ and re .search (re .escape (matched_text_lower ), context )
173+ )
174+
175+
158176def _is_benign_ar_context (context : str , match : str ) -> bool :
159177 """Return True for high-confidence non-malicious prose patterns around AR matches."""
160178 context_lower = context .lower ()
161- if _is_directly_instructive (context_lower , match ):
162- return False
179+ has_benign_marker = any (
180+ pattern .search (context_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
181+ )
163182 if re .search (r"\bwould\s+always\s+comply\b" , context_lower ):
164183 return True
165- if any ( pattern . search ( context_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS ):
166- return True
167- return False
184+ if _is_directly_instructive ( context_lower , match ):
185+ return has_benign_marker and _is_quoted_or_labeled_benign_match ( context_lower , match )
186+ return has_benign_marker
168187
169188
170189def analyze (content : str , file_path : str , file_type : str ) -> list [AnalyzerFinding ]:
0 commit comments