Skip to content

[K9VULN-4719] Allow rules to specify beforeAll and afterAll functions #695

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

Merged
merged 3 commits into from
Apr 28, 2025

Conversation

jasonforal
Copy link
Collaborator

@jasonforal jasonforal commented Apr 22, 2025

What problem are you trying to solve?

Some rules would be easier to write if there was a way to run code before any visit function is invoked or code after all visit functions are invoked.

This is useful when the rule's logic depends on the presence of absence of multiple conditions. For example, a C# rule might want to enforce that a type extending System.Exception implements all 3 required constructors, e.g. like so). A rule like this could capture all constructor nodes, set global booleans within the visit function if they match a specific pattern, and then after all nodes are visited, determine whether to report a violation or not.

Currently, one could hack together this functionality by incrementing an integer within each visit function and comparing it with the array length to determine whether it's the first or last invocation, but that's...awful.

What is your solution?

  • If a rule specifies a beforeAll function, call it before processing any query matches.
    • The function is passed a copy of the data in the query match bridge to prevent the rule code from inadvertently mutating the actual bridge's data.
  • If the rule specifies an afterAll function, call it after processing all query matches.

Alternatives considered

What the reviewer should know

  • This is marked as "experimental" in the code comments until we decide to officially support it.

@jasonforal jasonforal requested a review from a team as a code owner April 22, 2025 19:20
@jasonforal
Copy link
Collaborator Author

Re-requesting review for the (new) change that passes all query matches into the beforeAll function: 64527d5

@jasonforal jasonforal requested a review from juli1 April 22, 2025 23:58
@jasonforal jasonforal merged commit c4d8243 into main Apr 28, 2025
75 checks passed
@jasonforal jasonforal deleted the jf/K9VULN-4719 branch April 28, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants