Skip to content
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

Adds DependsOnTags field to Rule #533

Merged
merged 13 commits into from
Mar 7, 2023
Merged

Adds DependsOnTags field to Rule #533

merged 13 commits into from
Mar 7, 2023

Conversation

gfs
Copy link
Contributor

@gfs gfs commented Mar 2, 2023

Fix #531.

After processing of rules DependsOnTags will be checked, at that time, all matches whose rule have depends_on_tags which are not all present in the total set of UniqueTags gathered during the full run will be removed in iterative batches until no more rules have missing depends on tags.

Adds tests for new functionality and rule verifier will check to make sure all tags which are depended on are present in the rule set, and that any overridden rules have at least all the depends_on_tags of their overrider, since overridden rules are not tracked, and thus cannot be restored during the depends checking step, resulting in a potentially confusing interaction.

SemVer changes. Public properties in the some objects have been changed to IList from various Array or List.

Sample rules with new syntax (pulled from the new test cases)

One Rule Depends on tags from another rule

[
    {
        "id": "SA000005",
        "name": "Testing.Rules.DependsOnTags.OneWay",
        "tags": [
            "Dependant"
        ],
        "depends_on_tags": ["Dependee"],
        "severity": "Critical",
        "description": "This rule finds windows 2000",
        "patterns": [
            {
                "pattern": "windows 2000",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": "This rule depends on the Dependee tag"
    },
    {
        "id": "SA000006",
        "name": "Testing.Rules.DependsOnTags.OneWay",
        "tags": [
            "Dependee"
        ],
        "severity": "Critical",
        "description": "This rule finds linux",
        "patterns": [
            {
                "pattern": "linux",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": "This rule sets the dependee tag"
    }
]

Two Rules Depend on each other

[
    {
        "id": "SA000005",
        "name": "Testing.Rules.DependsOnTags.TwoWay",
        "tags": [
            "RuleOne"
        ],
        "depends_on_tags": ["RuleTwo"],
        "severity": "Critical",
        "description": "This rule finds windows 2000",
        "patterns": [
            {
                "pattern": "windows 2000",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": "This rule depends on the RuleTwo tag"
    },
    {
        "id": "SA000006",
        "name": "Testing.Rules.DependsOnTags.TwoWay",
        "tags": [
            "RuleTwo"
        ],
        "depends_on_tags": ["RuleOne"],
        "severity": "Critical",
        "description": "This rule finds linux",
        "patterns": [
            {
                "pattern": "linux",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": "This rule depends on the RuleOne tag"
    }
]

You can also chain depends on

[
    {
        "id": "SA000001",
        "name": "Testing.Rules.DependsOnTags.Chain.A",
        "tags": [
            "Category.A"
        ],
        "severity": "Critical",
        "description": "This rule finds A",
        "patterns": [
            {
                "pattern": "A",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": ""
    },
    {
        "id": "SA000002",
        "name": "Testing.Rules.DependsOnTags.Chain.B",
        "tags": [
            "Category.B"
        ],
        "depends_on_tags": ["Category.A"],
        "severity": "Critical",
        "description": "This rule finds B",
        "patterns": [
            {
                "pattern": "B",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": ""
    },
    {
        "id": "SA000003",
        "name": "Testing.Rules.DependsOnTags.Chain.C",
        "tags": [
            "Category.C"
        ],
        "depends_on_tags": ["Category.B"],
        "severity": "Critical",
        "description": "This rule finds C",
        "patterns": [
            {
                "pattern": "C",
                "type": "regex",
                "confidence": "High",
                "modifiers": [
                    "m"
                ],
                "scopes": [
                    "code"
                ]
            }
        ],
        "_comment": ""
    }
]

After processing of rules DependsOnTags will be checked and then matches whose matching rule have required tags which are not all present in the UniqueTags will be removed before returning results. Adds tests for new functionality and rule verifier will check to make sure all tags which are depended on are present in the rule set.

SemVer changes. Public properties in the Rule object have been changed to IList from a combination of Array and List.
@gfs gfs requested a review from daalcant March 2, 2023 00:31
@gfs gfs marked this pull request as ready for review March 2, 2023 20:30
gfs added 3 commits March 3, 2023 14:58
Change a few more List to IList.
Improve edge case in verification where if two rules shared the same id they would both receive the error message about depends_on_tags.
@jacobmsft
Copy link
Member

jacobmsft commented Mar 3, 2023 via email

gfs added 5 commits March 3, 2023 15:51
… the depends on of its overrider

This verification step prevents a potential issue if a ruleset contains Rule A, which is Overridden by Rule B which depends on TagX.

If RuleA and RuleB match, but TagX is not present, no results would be returned. This is because overrides are performed on a file by file basis, as the last step in processing each file. Depends on tags are checked after all files have been processed, and so the overridden matches are no longer tracked.
@gfs gfs merged commit 60f166d into main Mar 7, 2023
@gfs gfs deleted the gfs/DependsOn branch March 7, 2023 20:33
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.

Add capability for rules to depend on other rules (that may have applied to different files)
3 participants