reject unsupported logic edge cases during rule loading#2913
reject unsupported logic edge cases during rule loading#2913akshat4703 wants to merge 2 commits intomandiant:masterfrom
Conversation
There was a problem hiding this comment.
Please add bug fixes, new features, breaking changes and anything else you think is worthwhile mentioning to the master (unreleased) section of CHANGELOG.md. If no CHANGELOG update is needed add the following to the PR description: [x] No CHANGELOG update needed
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the rule loading process by implementing strict validation for specific logic constructs. By rejecting problematic rule patterns at an early stage, it ensures that all loaded rules are compatible with the optimized matcher and indexer, thereby preventing potential ambiguities and incorrect behavior during rule evaluation. This change improves the robustness and reliability of the rule engine. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces validation to reject unsupported logic constructs during rule loading, specifically top-level not statements, nested not statements, and top-level count(...): 0 statements. The implementation adds a _lint_logic_edge_cases method that recursively checks rule statements. The changes are well-implemented and include corresponding tests and documentation updates. I have one suggestion to improve the readability of the new validation logic.
capa/rules/__init__.py
Outdated
| if isinstance(statement, ceng.Not): | ||
| if is_root: | ||
| raise InvalidRule("top level statement may not be a `not` statement") | ||
| if isinstance(statement.child, ceng.Not): | ||
| raise InvalidRule("nested `not` statements are not supported") | ||
|
|
||
| if is_root and isinstance(statement, ceng.Range) and statement.min == 0 and statement.max == 0: | ||
| raise InvalidRule("top level statement may not be `count(...): 0`") |
There was a problem hiding this comment.
For better readability and to more clearly separate the concerns of checking root-level statements versus nested statements, you could refactor this logic. Grouping all is_root checks together at the beginning makes the function's logic easier to follow.
| if isinstance(statement, ceng.Not): | |
| if is_root: | |
| raise InvalidRule("top level statement may not be a `not` statement") | |
| if isinstance(statement.child, ceng.Not): | |
| raise InvalidRule("nested `not` statements are not supported") | |
| if is_root and isinstance(statement, ceng.Range) and statement.min == 0 and statement.max == 0: | |
| raise InvalidRule("top level statement may not be `count(...): 0`") | |
| if is_root: | |
| if isinstance(statement, ceng.Not): | |
| raise InvalidRule("top level statement may not be a `not` statement") | |
| if isinstance(statement, ceng.Range) and statement.min == 0 and statement.max == 0: | |
| raise InvalidRule("top level statement may not be `count(...): 0`") | |
| if isinstance(statement, ceng.Not) and isinstance(statement.child, ceng.Not): | |
| raise InvalidRule("nested `not` statements are not supported") |
CHANGELOG updated or no update needed, thanks! 😄
this is an issue with the optimized matcher, not the underlying rules, so i don't want to tell users they did something wrong. perhaps we should fall back to the unoptimized matcher in these cases. |
|
@akshat4703 please open issues with feature requests or bug reports so we can discuss ideas before you open PRs. |
ok will keep this feedback for future |
Good callout. I updated the implementation to avoid rejecting these patterns or attributing the limitation to rule authors. Instead of failing at load time, rules that cannot produce a safe index selector ( Changes
Tests
These confirm correct behavior while preserving optimized matching for other rules. |
Description
Add core rule-validation checks to reject logic constructs that the optimized matcher/indexer cannot safely handle.
The following patterns are now rejected during rule loading:
notstatementsnotstatements (e.g.not: not: ...)count(...): 0statementsThese constructs can cause ambiguity or incorrect behavior in the optimized rule matcher and indexer. Rejecting them at load time ensures rules remain compatible with the matcher’s assumptions and prevents subtle evaluation issues.
Implementation Details
Rule._lint_logic_edge_cases()in__init__.py.Rule.from_dict()after rule statement construction.RuleSet.match()docstring to document that these constructs are now rejected during rule loading.test_rules.pycovering:invalid_top_level_not_statementinvalid_nested_not_statementinvalid_top_level_count_zero_statementValidation
Executed targeted tests for the new validation logic:
Result:
[x] No CHANGELOG update needed