145145)
146146
147147_BENIGN_AR_VALUE_LABEL_PATTERN = re .compile (
148- r"^\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description|prompt)\s*:\s*" ,
148+ r"^\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description)\s*:\s*" ,
149+ re .IGNORECASE ,
150+ )
151+
152+ _BENIGN_AR_WARNING_INTRO_PATTERN = re .compile (r"^\s*(?:warning|note)\s*:\s*$" , re .IGNORECASE )
153+ _BENIGN_AR_CONTINUATION_LABEL_PATTERN = re .compile (
154+ r"^\s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description)\s*:\s*(?:[|>])?\s*$" ,
149155 re .IGNORECASE ,
150156)
151157
@@ -160,32 +166,62 @@ def _is_directly_instructive(context: str, matched_text: str) -> bool:
160166 return "do anything now" in matched_text .lower ()
161167
162168
163- def _is_quoted_or_labeled_benign_match (match_line : str , matched_text : str ) -> bool :
169+ def _is_quoted_or_labeled_benign_match (
170+ match_line : str ,
171+ matched_text : str ,
172+ previous_line : str | None = None ,
173+ ) -> bool :
164174 """Return True when a direct phrase appears only as quoted or declared reference text."""
165175 matched_text_lower = matched_text .lower ()
166176 match_line_lower = match_line .lower ()
167- if any (f"{ quote } { matched_text_lower } { quote } " in match_line_lower for quote in ('"' , "'" , "`" )):
177+ quoted_match = any (
178+ f"{ quote } { matched_text_lower } { quote } " in match_line_lower for quote in ('"' , "'" , "`" )
179+ )
180+ if quoted_match :
168181 return True
169182 if re .search (
170183 rf"\bthe\s+phrase\s+[\"'`]?{ re .escape (matched_text_lower )} [\"'`]?" ,
171184 match_line_lower ,
172185 ):
173186 return True
174- return bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line_lower ))
187+ if _BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line_lower ):
188+ return True
189+ if not previous_line :
190+ return False
191+ previous_line_lower = previous_line .lower ()
192+ if _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower ):
193+ return quoted_match or bool (re .search (r'^[\s"\']' , match_line ))
194+ if _BENIGN_AR_CONTINUATION_LABEL_PATTERN .search (previous_line_lower ):
195+ return bool (re .search (r"^\s+" , match_line )) or quoted_match
196+ return False
175197
176198
177- def _is_benign_ar_context (context : str , match_line : str , match : str ) -> bool :
199+ def _is_benign_ar_context (
200+ context : str ,
201+ match_line : str ,
202+ match : str ,
203+ previous_line : str | None = None ,
204+ ) -> bool :
178205 """Return True for high-confidence non-malicious prose patterns around AR matches."""
179- context_lower = context .lower ()
180206 match_line_lower = match_line .lower ()
181207 has_benign_marker = any (
182208 pattern .search (match_line_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
183209 )
184210 has_benign_marker = has_benign_marker or bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line ))
185- if re .search (r"\bwould\s+always\s+comply\b" , context_lower ):
211+ if previous_line :
212+ previous_line_lower = previous_line .lower ()
213+ has_benign_marker = has_benign_marker or bool (
214+ _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower )
215+ or _BENIGN_AR_CONTINUATION_LABEL_PATTERN .search (previous_line_lower )
216+ )
217+ if re .search (r"\bwould\s+always\s+comply\b" , match_line_lower ):
186218 return True
187219 if _is_directly_instructive (match_line_lower , match ):
188- return has_benign_marker and _is_quoted_or_labeled_benign_match (match_line , match )
220+ return has_benign_marker and _is_quoted_or_labeled_benign_match (
221+ match_line ,
222+ match ,
223+ previous_line = previous_line ,
224+ )
189225 return has_benign_marker
190226
191227
@@ -200,11 +236,17 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
200236 lines = content .splitlines ()
201237 line_num = get_line_number (content , match .start ())
202238 match_line = lines [line_num - 1 ] if lines else content
239+ previous_line = lines [line_num - 2 ] if line_num > 1 else None
203240 context = get_context (content , match .start (), context_lines = 3 )
204241 confidence = base_confidence
205242 if is_code_example (context ):
206243 confidence -= _EXAMPLE_PENALTY
207- if _is_benign_ar_context (context , match_line , match .group (0 )):
244+ if _is_benign_ar_context (
245+ context ,
246+ match_line ,
247+ match .group (0 ),
248+ previous_line = previous_line ,
249+ ):
208250 continue
209251 if confidence < _MIN_CONFIDENCE :
210252 continue
0 commit comments