145145)
146146
147147_BENIGN_AR_VALUE_LABEL_PATTERN = re .compile (
148- r"(^|\n) \s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description|prompt)\s*:\s*" ,
148+ r"^ \s*(?:[-*]\s*)?(?:warnings?|disclaimers?|description|prompt)\s*:\s*" ,
149149 re .IGNORECASE ,
150150)
151151
@@ -160,29 +160,32 @@ def _is_directly_instructive(context: str, matched_text: str) -> bool:
160160 return "do anything now" in matched_text .lower ()
161161
162162
163- def _is_quoted_or_labeled_benign_match (context : str , matched_text : str ) -> bool :
163+ def _is_quoted_or_labeled_benign_match (match_line : str , matched_text : str ) -> bool :
164164 """Return True when a direct phrase appears only as quoted or declared reference text."""
165165 matched_text_lower = matched_text .lower ()
166- if any (f"{ quote } { matched_text_lower } { quote } " in context for quote in ('"' , "'" , "`" )):
166+ match_line_lower = match_line .lower ()
167+ if any (f"{ quote } { matched_text_lower } { quote } " in match_line_lower for quote in ('"' , "'" , "`" )):
167168 return True
168- if re .search (rf"\bthe\s+phrase\s+[\"'`]?{ re .escape (matched_text_lower )} [\"'`]?" , context ):
169+ if re .search (
170+ rf"\bthe\s+phrase\s+[\"'`]?{ re .escape (matched_text_lower )} [\"'`]?" ,
171+ match_line_lower ,
172+ ):
169173 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+ return bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line_lower ))
174175
175176
176- def _is_benign_ar_context (context : str , match : str ) -> bool :
177+ def _is_benign_ar_context (context : str , match_line : str , match : str ) -> bool :
177178 """Return True for high-confidence non-malicious prose patterns around AR matches."""
178179 context_lower = context .lower ()
180+ match_line_lower = match_line .lower ()
179181 has_benign_marker = any (
180- pattern .search (context_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
182+ pattern .search (match_line_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
181183 )
184+ has_benign_marker = has_benign_marker or bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line ))
182185 if re .search (r"\bwould\s+always\s+comply\b" , context_lower ):
183186 return True
184- if _is_directly_instructive (context_lower , match ):
185- return has_benign_marker and _is_quoted_or_labeled_benign_match (context_lower , match )
187+ if _is_directly_instructive (match_line_lower , match ):
188+ return has_benign_marker and _is_quoted_or_labeled_benign_match (match_line , match )
186189 return has_benign_marker
187190
188191
@@ -194,11 +197,14 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
194197 for rule_id , patterns in _RULES :
195198 for pattern , base_confidence in patterns :
196199 for match in re .finditer (pattern , content , re .IGNORECASE | re .MULTILINE ):
200+ lines = content .splitlines ()
201+ line_num = get_line_number (content , match .start ())
202+ match_line = lines [line_num - 1 ] if lines else content
197203 context = get_context (content , match .start (), context_lines = 3 )
198204 confidence = base_confidence
199205 if is_code_example (context ):
200206 confidence -= _EXAMPLE_PENALTY
201- if _is_benign_ar_context (context , match .group (0 )):
207+ if _is_benign_ar_context (context , match_line , match .group (0 )):
202208 continue
203209 if confidence < _MIN_CONFIDENCE :
204210 continue
@@ -209,7 +215,7 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
209215 severity = Severity .HIGH ,
210216 location = Location (
211217 file = file_path ,
212- start_line = get_line_number ( content , match . start ()) ,
218+ start_line = line_num ,
213219 ),
214220 confidence = round (confidence , 2 ),
215221 tags = tag ,
0 commit comments