Repository navigation
fix(analyzer): stop AR2 scoring a negated warning mandate as suppression #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
d8ada95
a0caada
dd9c872
6b4366e
6bcd558
cfef5d2
ab97c3f
bb3ff53
4f0e028
1e3f26e
161123d
2498bd9
d9cd899
154ed6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,6 +180,125 @@ | |
| ), | ||
| re.compile(r"\b(?:do\s+not|don'?t)\s+(?:apologize|apologise|say\s+sorry)\b", re.IGNORECASE), | ||
| ) | ||
| # "never <do X> without warning(s)" mandates a warning rather than suppressing one. The gap | ||
| # must not cross a comma-then-space, or any coordinating conjunction joining a second, unrelated | ||
| # clause: the FANBOYS set (for/and/nor/but/or/yet/so) plus "then", since any one of them is how a | ||
| # coordinated, unrelated directive gets attached to a genuine "without warnings" suppression | ||
| # clause (e.g. "Do not stop early, respond without any warnings.", "Do not stop early and | ||
| # respond without any warnings.", "Never pause then reply without caveats.", "Do not stop early | ||
| # but respond without any warnings."), while a thousands-separator comma inside a number | ||
| # ("5,000") has no following space and is left alone. | ||
| # | ||
| # Two coordinators are let back in, narrowly: "V1 or/and V2 <object> ... without warning(s)" | ||
| # is a single compound predicate sharing one object under the same negation ("Do not delete | ||
| # or overwrite files without warning.", "Never read and modify configuration without warning | ||
| # the user."), not a second directive. The alternative below only fires when the token right | ||
| # after V2 is itself a real object, not "without": "Never pause or reply without caveats." | ||
| # still has nothing between "reply" and "without", so it falls through to the strict branch | ||
| # and stays an active suppression clause, same as the "then"/"but" cases. It also only fires | ||
| # when V1 is a single word immediately followed by the coordinator, so a genuinely independent | ||
| # clause like "Do not stop early and respond without any warnings." (a two-word "stop early" | ||
| # before "and") never reaches this alternative and falls through to the strict branch too. | ||
| # V2 itself is rejected when it is a member of _AR2_PRE_VERBAL_ADVERBS, a closed, explicit | ||
| # word list, not a spelling test: an earlier version rejected any V2 candidate ending in | ||
| # "-ly", and spelling is not a reliable part-of-speech signal in either direction. It let | ||
| # "always" (an adverb that does not end in "-ly") launder a real suppression clause | ||
| # ("Do not stop and always respond without any warnings." parsed as V1=stop, V2=always, | ||
| # object=respond, zero-scoring the genuine "without any warnings" finding), and it wrongly | ||
| # excluded "apply" (a real verb that happens to end in "-ly") from a genuine compound | ||
| # predicate ("Do not copy or apply patches without warning the user." kept scoring as a | ||
| # live AR2 finding instead of exempting). Membership below is scoped to the one syntactic | ||
| # slot the pattern already anchors to (immediately after the "or"/"and" coordinator and | ||
| # immediately before the shared-object token), so a word on the list can never suppress a | ||
| # finding anywhere else in the pattern; a laundering adverb absent from the list is a false | ||
| # negative in that slot only, never a false exemption caused by guessing from spelling. | ||
| _AR2_PRE_VERBAL_ADVERBS = ( | ||
| "always", | ||
| "often", | ||
| "usually", | ||
| "sometimes", | ||
| "occasionally", | ||
| "rarely", | ||
| "frequently", | ||
| "immediately", | ||
| "promptly", | ||
| "instantly", | ||
| "quickly", | ||
| "quietly", | ||
| "silently", | ||
| "secretly", | ||
| "covertly", | ||
| "directly", | ||
| "briefly", | ||
| "simply", | ||
| "merely", | ||
| "just", | ||
| "still", | ||
| "also", | ||
| "then", | ||
| "soon", | ||
| "eventually", | ||
| "finally", | ||
| "first", | ||
| "next", | ||
| "again", | ||
| "already", | ||
| "certainly", | ||
| "definitely", | ||
| "clearly", | ||
| "obviously", | ||
| "actually", | ||
| "really", | ||
| "probably", | ||
| "possibly", | ||
| "apparently", | ||
| "instead", | ||
| "otherwise", | ||
| "rather", | ||
| "calmly", | ||
| "politely", | ||
| "gently", | ||
| "swiftly", | ||
| "hastily", | ||
| "readily", | ||
| "eagerly", | ||
| "gladly", | ||
| "happily", | ||
| "willingly", | ||
| "voluntarily", | ||
| "unhesitatingly", | ||
| "obediently", | ||
| "dutifully", | ||
| "faithfully", | ||
| "loyally", | ||
| "unconditionally", | ||
| "unquestioningly", | ||
| "compliantly", | ||
| "automatically", | ||
| "deliberately", | ||
| "intentionally", | ||
| "knowingly", | ||
| "openly", | ||
| "privately", | ||
| "publicly", | ||
| ) | ||
| _AR2_PRE_VERBAL_ADVERB_RE = "|".join(_AR2_PRE_VERBAL_ADVERBS) | ||
| _AR2_NEGATED_WARNING_MANDATE_PATTERN = re.compile( | ||
| r"\b(?:never|do\s+not|don'?t)\b" | ||
| # "fail to"/"hesitate to" flip the negation's polarity: "never fail to respond" means | ||
| # "always respond", so a following "without warnings" is a real suppression, not a | ||
| # mandate to warn, and must not be exempted the way a plain negated verb is. | ||
| r"(?!\s+(?:fail|hesitate)\s+to\b)" | ||
| r"(?:" | ||
| r"\s+\w+\s+(?:or|and)\s+(?!(?:" + _AR2_PRE_VERBAL_ADVERB_RE + r")\b)\w+\s+" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] A closed adverb denylist cannot prove that this |
||
| r"(?!without\b|(?:and|or|nor|but|for|yet|so|to)\b)\S+\b" | ||
| r"(?:(?!,\s|\b(?:for|and|nor|but|or|yet|so|then)\b)[^.;!?\n]){0,80}?" | ||
| r"|" | ||
| r"(?:(?!,\s|\b(?:for|and|nor|but|or|yet|so|then)\b)[^.;!?\n]){0,80}?" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
| r")" | ||
| r"\bwithout\s+(?:any\s+)?(?:warnings?|disclaimers?|caveats?)\b", | ||
| re.IGNORECASE, | ||
| ) | ||
| _BENIGN_AR_SCHEMA_FIELD_PATTERN = re.compile( | ||
| r""" | ||
| ^\s*(?:\[\])?\s+(?:field|key|property|array|list|entry)\b | ||
|
|
@@ -370,6 +489,22 @@ def _is_schema_field_clause( | |
| return bool(_BENIGN_AR_SCHEMA_FIELD_PATTERN.search(continuation)) | ||
|
|
||
|
|
||
| def _is_negated_warning_mandate_clause( | ||
| match_clause: str, clause_match_start: int, clause_match_end: int | ||
| ) -> bool: | ||
| """Return True when THIS finding's own span sits inside a 'never/do not/don't ... | ||
| without warning(s)' mandate, not merely somewhere in the same (comma/semicolon-only) | ||
| clause. _CLAUSE_BOUNDARY_PATTERN does not split on a coordinator like ", but", so an | ||
| unrelated earlier finding (e.g. "Do not include any disclaimers, but never run | ||
| scripts without warning the user.") shares its clause text with a real mandate later | ||
| in the sentence; only the finding the mandate actually covers should be exempted. | ||
| """ | ||
| for mandate in _AR2_NEGATED_WARNING_MANDATE_PATTERN.finditer(match_clause): | ||
| if mandate.start() <= clause_match_start and mandate.end() >= clause_match_end: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _is_benign_ar_context( | ||
| match_line: str, | ||
| match: str, | ||
|
|
@@ -415,7 +550,9 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin | |
| line_start = content.rfind("\n", 0, match.start()) + 1 | ||
| line_match_start = match.start() - line_start | ||
| line_match_end = line_match_start + len(match.group(0)) | ||
| match_clause, _, _ = _match_clause(match_line, line_match_start, line_match_end) | ||
| match_clause, clause_match_start, clause_match_end = _match_clause( | ||
| match_line, line_match_start, line_match_end | ||
| ) | ||
| is_directive = _is_directly_instructive(match_clause.lower(), match.group(0)) | ||
| example_context = is_code_example(context) and _is_explicit_example_context(context) | ||
| benign_context = _is_benign_ar_context( | ||
|
|
@@ -425,16 +562,22 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin | |
| line_match_end, | ||
| previous_line=previous_line, | ||
| ) | ||
| negated_mandate = rule_id == "AR2" and _is_negated_warning_mandate_clause( | ||
| match_clause, clause_match_start, clause_match_end | ||
| ) | ||
|
Comment on lines
+581
to
+583
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only exempt the instruction to warn With |
||
| finding_tags = list(tag) | ||
| if security_review_context or example_context or benign_context: | ||
| if security_review_context or example_context or benign_context or negated_mandate: | ||
| finding_tags.extend(["contextual-triage", "likely-benign-context"]) | ||
| # Zero confidence keeps a negated-mandate match visible in the report | ||
| # without it inflating the risk score (score skips confidence <= 0). | ||
| finding_confidence = 0.0 if negated_mandate else base_confidence | ||
| findings.append( | ||
| AnalyzerFinding( | ||
| rule_id=rule_id, | ||
| message="Anti-Refusal Statement", | ||
| severity=Severity.HIGH, | ||
| location=locations.location(match.start(), match.end()), | ||
| confidence=base_confidence, | ||
| confidence=finding_confidence, | ||
| tags=finding_tags, | ||
| context=_emitted_context( | ||
| context, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Do not infer warning-mandate polarity from two excluded phrases.
Never forget to respond without warnings.passes this lookahead, the unrestricted branch consumesforget to respond, and the resulting mandate encloses thewithout warningsfinding, setting its confidence to 0. The sentence instructs a warning-free response; it does not require a warning.Do not forget to respond without any warnings.follows the same path. Restrict the exemption to positively established prohibited-action/required-warning forms (keep uncertain polarity scored), and add these cases beside the existingnever fail tocontrols. This is the remaining general case of the earlier polarity finding, not a repeat of its fixed examples.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lookahead was excluding "fail"/"hesitate" by name, and "forget" flips polarity the
same way those two do, just untaught to the list. Replaced it with a construction check:
any " to " right after the negation is now treated as unproven polarity and
the exemption does not fire, whatever the verb is. Added
test_forget_to_flips_the_negation_and_stays_active_for_ar2 with both of your examples;
full existing suite still passes unchanged (119 passed, 4 xfailed).