121121_EXAMPLE_PENALTY = 0.4
122122_MIN_CONFIDENCE = 0.5
123123
124- _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS = (
125- re .compile (r"\b(json|schema|errors?\[\])\b" , re .IGNORECASE ),
126- )
127-
128124_AR_DIRECT_INTENT_PATTERNS = (
129125 re .compile (r"\byou\s+(?:must|will|should|can|cannot|can'?t|are|were)\b" , re .IGNORECASE ),
130126 re .compile (r"\bfrom\s+now\s+on\b" , re .IGNORECASE ),
175171 r"\b(?:json|output)\s+schema\b|\berrors?\[\]\b" ,
176172 re .IGNORECASE ,
177173)
178-
179- _BENIGN_AR_VALUE_LABEL_PATTERN = re .compile (
180- r"^\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description)\s*:\s*" ,
181- re .IGNORECASE ,
182- )
183-
184174_BENIGN_AR_WARNING_INTRO_PATTERN = re .compile (r"^\s*(?:warning|note)\s*:\s*$" , re .IGNORECASE )
185- _BENIGN_AR_CONTINUATION_LABEL_PATTERN = re .compile (
186- r"^\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description)\s*:\s*(?:[|>])?\s*$" ,
187- re .IGNORECASE ,
188- )
189- _BENIGN_AR_DECLARATION_INTRO_PATTERN = re .compile (
190- r"^\s*(?:deny-?list|tool)\s+declaration\s*:\s*$" ,
191- re .IGNORECASE ,
192- )
193- _BENIGN_AR_DECLARATION_INLINE_PATTERN = re .compile (
194- r"^\s*(?:deny-?list|tool)\s+declaration\s*:\s*" ,
175+ _BENIGN_AR_DENYLIST_DECLARATION_PATTERN = re .compile (
176+ r"^\s*deny-?list\s+declaration\s*:\s*(?:[|>])?\s*$" ,
195177 re .IGNORECASE ,
196178)
197- _BENIGN_AR_TOOL_FIELD_PATTERN = re .compile (r"^\s*tool\s*:\s*\S" , re .IGNORECASE )
198- _BENIGN_AR_DESCRIPTION_FIELD_PATTERN = re .compile (r"^\s*description\s*:\s*" , re .IGNORECASE )
199179_DIRECTIVE_DOCUMENTATION_LABEL_PATTERN = re .compile (r"^\s*documentation\s*:\s*" , re .IGNORECASE )
200180_DOCUMENTATION_HEADING_PATTERN = re .compile (r"^\s*documentation\s*:\s*$" , re .IGNORECASE )
201181_BENIGN_AR_FIXTURE_INTRO_PATTERN = re .compile (
206186 r"(?:```|example:|for example|e\.g\.|such as|# warning:|# note:|\*\*warning\*\*|\*\*note\*\*|// ✅|// ❌|// good:|// bad:|// correct:|// incorrect:|// wrong:)" ,
207187 re .IGNORECASE ,
208188)
189+ _CLAUSE_BOUNDARY_PATTERN = re .compile (r"[.;!?]" )
190+ _LIVE_RESPONSE_OBJECT_PATTERN = re .compile (
191+ r"\b(?:answer|response|reply|assistant|user)\b" ,
192+ re .IGNORECASE ,
193+ )
194+ _DEFENSIVE_AR_CONTEXT_PATTERN = re .compile (
195+ r"(?:^\s*(?:warning|note)\s*:|\b(?:malicious|example|payload|attack|defensive|deny-?list|fixture|the\s+phrase)\b)" ,
196+ re .IGNORECASE ,
197+ )
198+ _BENIGN_AR_NARRATIVE_PREFIX_PATTERN = re .compile (r"\bwould\s+$" , re .IGNORECASE )
209199
210200
211201def _is_directly_instructive (context : str , matched_text : str ) -> bool :
212202 """Return True when the match still looks like an active adversarial instruction."""
213203 context_lower = context .lower ()
214204 matched_text_lower = matched_text .lower ()
215- if re .search (r"\bwould\s+always\s+comply\b" , context_lower ):
216- return False
217205 if any (pattern .search (context_lower ) for pattern in _AR_DIRECT_INTENT_PATTERNS ):
218206 return True
219207 if any (pattern .search (context_lower ) for pattern in _AR2_DIRECT_INTENT_PATTERNS ):
220- if _AR2_LIVE_SUPPRESSION_PATTERN .search (context_lower ):
221- return not _BENIGN_AR_SCHEMA_FIELD_PATTERN .search (context_lower )
222208 return True
223209 return "do anything now" in matched_text_lower
224210
@@ -228,6 +214,30 @@ def _is_explicit_example_context(context: str) -> bool:
228214 return bool (_EXPLICIT_EXAMPLE_CONTEXT_PATTERN .search (context ))
229215
230216
217+ def _match_clause_bounds (match_line : str , match_start : int , match_end : int ) -> tuple [int , int ]:
218+ """Return the semantically local clause around a match on one line."""
219+ clause_start = 0
220+ for boundary in _CLAUSE_BOUNDARY_PATTERN .finditer (match_line ):
221+ if boundary .start () >= match_start :
222+ break
223+ clause_start = boundary .end ()
224+ clause_end = len (match_line )
225+ boundary_match = _CLAUSE_BOUNDARY_PATTERN .search (match_line , match_end )
226+ if boundary_match :
227+ clause_end = boundary_match .start ()
228+ return clause_start , clause_end
229+
230+
231+ def _match_clause (match_line : str , match_start : int , match_end : int ) -> tuple [str , int , int ]:
232+ """Return the clause text and the match offsets within that clause."""
233+ clause_start , clause_end = _match_clause_bounds (match_line , match_start , match_end )
234+ return (
235+ match_line [clause_start :clause_end ],
236+ match_start - clause_start ,
237+ match_end - clause_start ,
238+ )
239+
240+
231241def _emitted_context (
232242 context : str ,
233243 match_line : str ,
@@ -247,110 +257,86 @@ def _emitted_context(
247257 return context
248258
249259
250- def _has_benign_continuation_source (
251- previous_line : str ,
252- earlier_line : str | None = None ,
253- ) -> bool :
254- """Return True when a continuation label belongs to an explicit benign scaffold."""
255- previous_line_lower = previous_line .lower ()
256- if not _BENIGN_AR_CONTINUATION_LABEL_PATTERN .search (previous_line_lower ):
257- return False
258- if not earlier_line :
259- return False
260- earlier_line_lower = earlier_line .lower ()
261- return bool (
262- _BENIGN_AR_DECLARATION_INTRO_PATTERN .search (earlier_line_lower )
263- or _BENIGN_AR_TOOL_FIELD_PATTERN .search (earlier_line_lower )
264- )
265-
266-
267- def _is_quoted_or_labeled_benign_match (
268- match_line : str ,
269- matched_text : str ,
270- previous_line : str | None = None ,
271- earlier_line : str | None = None ,
272- ) -> bool :
273- """Return True when a direct phrase appears only as quoted or declared reference text."""
260+ def _is_quoted_match (match_line : str , matched_text : str ) -> bool :
261+ """Return True when the matched phrase is quoted on the same line."""
274262 matched_text_lower = matched_text .lower ()
275263 match_line_lower = match_line .lower ()
276- quoted_match = any (
264+ if any (
277265 re .search (
278266 rf"{ re .escape (quote )} [^{ re .escape (quote )} \n]*{ re .escape (matched_text_lower )} [^{ re .escape (quote )} \n]*{ re .escape (quote )} " ,
279267 match_line_lower ,
280268 )
281269 for quote in ('"' , "'" , "`" )
282- )
283- if quoted_match :
270+ ):
284271 return True
285272 if re .search (
286273 rf"\bthe\s+phrase\b.*?[\"'`][^\"'`\n]*{ re .escape (matched_text_lower )} [^\"'`\n]*[\"'`]" ,
287274 match_line_lower ,
288275 ):
289276 return True
290- if _BENIGN_AR_DECLARATION_INLINE_PATTERN .search (match_line_lower ):
277+ return False
278+
279+
280+ def _has_explicit_defensive_context (
281+ match_line : str ,
282+ previous_line : str | None = None ,
283+ ) -> bool :
284+ """Return True when quoted text is clearly framed as defensive prose."""
285+ if _DEFENSIVE_AR_CONTEXT_PATTERN .search (match_line ):
291286 return True
292287 if not previous_line :
293288 return False
294- previous_line_lower = previous_line .lower ()
295- if _BENIGN_AR_TOOL_FIELD_PATTERN .search (
296- previous_line_lower
297- ) and _BENIGN_AR_DESCRIPTION_FIELD_PATTERN .search (match_line_lower ):
289+ if _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line ):
298290 return True
299- if _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower ):
300- return quoted_match
301- if _has_benign_continuation_source (previous_line , earlier_line = earlier_line ):
302- return bool (re .search (r"^\s+" , match_line )) or quoted_match
303- if _BENIGN_AR_DECLARATION_INTRO_PATTERN .search (previous_line_lower ):
304- return bool (re .search (r"^\s*(?:[-*]\s*)?\S" , match_line ))
305- if _BENIGN_AR_FIXTURE_INTRO_PATTERN .search (previous_line_lower ):
306- return quoted_match
307- return False
291+ if _BENIGN_AR_DENYLIST_DECLARATION_PATTERN .search (previous_line ):
292+ return True
293+ return bool (_BENIGN_AR_FIXTURE_INTRO_PATTERN .search (previous_line ))
294+
295+
296+ def _is_match_local_narrative_clause (
297+ match_clause : str ,
298+ clause_match_start : int ,
299+ ) -> bool :
300+ """Return True when the current match is part of a narrative clause, not a directive."""
301+ return bool (_BENIGN_AR_NARRATIVE_PREFIX_PATTERN .search (match_clause [:clause_match_start ]))
302+
303+
304+ def _is_schema_field_clause (
305+ match_clause : str ,
306+ clause_match_end : int ,
307+ matched_text : str ,
308+ ) -> bool :
309+ """Return True when an AR2 warning-suppression phrase targets schema fields."""
310+ if not _AR2_LIVE_SUPPRESSION_PATTERN .search (matched_text ):
311+ return False
312+ clause_tail = match_clause [clause_match_end :]
313+ if not _BENIGN_AR_SCHEMA_FIELD_PATTERN .search (clause_tail ):
314+ return False
315+ return not _LIVE_RESPONSE_OBJECT_PATTERN .search (clause_tail )
308316
309317
310318def _is_benign_ar_context (
311- context : str ,
312319 match_line : str ,
313320 match : str ,
321+ line_match_start : int ,
322+ line_match_end : int ,
314323 previous_line : str | None = None ,
315- earlier_line : str | None = None ,
316324) -> bool :
317- """Return True for high-confidence non-malicious prose patterns around AR matches."""
318- match_line_lower = match_line .lower ()
319- has_technical_benign_marker = any (
320- pattern .search (match_line_lower ) for pattern in _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS
321- )
322- has_inline_role_marker = bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line ))
323- has_inline_role_marker = has_inline_role_marker or bool (
324- _BENIGN_AR_DECLARATION_INLINE_PATTERN .search (match_line )
325+ """Return True for high-confidence non-malicious prose patterns around one match span."""
326+ match_clause , clause_match_start , clause_match_end = _match_clause (
327+ match_line ,
328+ line_match_start ,
329+ line_match_end ,
325330 )
326- has_previous_line_benign_marker = False
327- if previous_line :
328- previous_line_lower = previous_line .lower ()
329- has_previous_line_benign_marker = bool (
330- _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower )
331- or _has_benign_continuation_source (previous_line , earlier_line = earlier_line )
332- or _BENIGN_AR_DECLARATION_INTRO_PATTERN .search (previous_line_lower )
333- or _BENIGN_AR_FIXTURE_INTRO_PATTERN .search (previous_line_lower )
334- or (
335- _BENIGN_AR_TOOL_FIELD_PATTERN .search (previous_line_lower )
336- and _BENIGN_AR_DESCRIPTION_FIELD_PATTERN .search (match_line )
337- )
338- )
339- if re .search (r"\bwould\s+always\s+comply\b" , match_line_lower ):
331+ match_clause_lower = match_clause .lower ()
332+ if _is_match_local_narrative_clause (match_clause_lower , clause_match_start ):
333+ return True
334+ if _is_schema_field_clause (match_clause_lower , clause_match_end , match .lower ()):
340335 return True
341- role_labeled_match = _is_quoted_or_labeled_benign_match (
336+ return _is_quoted_match ( match_line , match ) and _has_explicit_defensive_context (
342337 match_line ,
343- match ,
344338 previous_line = previous_line ,
345- earlier_line = earlier_line ,
346339 )
347- if _is_directly_instructive (match_line_lower , match ):
348- return (has_inline_role_marker or has_previous_line_benign_marker ) and role_labeled_match
349- if has_technical_benign_marker :
350- return True
351- if has_inline_role_marker :
352- return role_labeled_match
353- return has_previous_line_benign_marker and role_labeled_match
354340
355341
356342def analyze (content : str , file_path : str , file_type : str ) -> list [AnalyzerFinding ]:
@@ -365,27 +351,28 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
365351 line_num = get_line_number (content , match .start ())
366352 match_line = lines [line_num - 1 ] if lines else content
367353 previous_line = lines [line_num - 2 ] if line_num > 1 else None
368- earlier_line = lines [line_num - 3 ] if line_num > 2 else None
369354 context = get_context (content , match .start (), context_lines = 3 )
370- is_directive = _is_directly_instructive (match_line .lower (), match .group (0 ))
355+ line_start = content .rfind ("\n " , 0 , match .start ()) + 1
356+ line_match_start = match .start () - line_start
357+ line_match_end = line_match_start + len (match .group (0 ))
358+ match_clause , _ , _ = _match_clause (match_line , line_match_start , line_match_end )
359+ is_directive = _is_directly_instructive (match_clause .lower (), match .group (0 ))
371360 confidence = base_confidence
372361 if (
373362 is_code_example (context )
374363 and _is_explicit_example_context (context )
375- and not _is_quoted_or_labeled_benign_match (
364+ and not _is_quoted_match (
376365 match_line ,
377366 match .group (0 ),
378- previous_line = previous_line ,
379- earlier_line = earlier_line ,
380367 )
381368 ):
382369 confidence -= _EXAMPLE_PENALTY
383370 if _is_benign_ar_context (
384- context ,
385371 match_line ,
386372 match .group (0 ),
373+ line_match_start ,
374+ line_match_end ,
387375 previous_line = previous_line ,
388- earlier_line = earlier_line ,
389376 ):
390377 continue
391378 if confidence < _MIN_CONFIDENCE :
0 commit comments