Skip to content

Commit

Permalink
BST-11883: recommended rules (#49)
Browse files Browse the repository at this point in the history
* add support for recommended flag
  • Loading branch information
JonathanSerafini authored Jul 30, 2024
1 parent 7c3690a commit 91ede50
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions boostsec/registry_validator/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class RuleSchema(BaseModel):
categories: list[str]
ref: AnyHttpUrl

recommended: bool = False

class Config:
"""Config."""

Expand Down
4 changes: 4 additions & 0 deletions boostsec/registry_validator/upload_rules_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def load_scanners(scanners_path: Path, updated_ns: set[str]) -> list[ScannerName

rules_db_yaml = yaml.safe_load(rules_path.read_text())
rules = RulesDbSchema.parse_obj(rules_db_yaml)

scanners.append(
ScannerNamespace(
namespace=namespace,
Expand Down Expand Up @@ -260,6 +261,7 @@ def upload_rules_db(
"name": rule.name,
"prettyName": rule.pretty_name,
"ref": rule.ref,
"recommended": rule.recommended,
}
for rule in rules.values()
],
Expand Down Expand Up @@ -294,10 +296,12 @@ def main(
scanners = load_scanners(config.scanners_path, updated_ns)
server_scanners = load_scanners(config.server_side_scanners_path, updated_ns)
scanners = scanners + server_scanners

rules_realm = load_rules_realm(config.rules_realm_path, updated_ns)
namespace_cache = make_namespace_cache(scanners, rules_realm, server_scanners)
scanners_to_update = get_updated_scanners(scanners, namespace_cache)


if len(scanners_to_update) == 0:
print("No module rules to update.")
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:
name: my-rule-1
pretty_name: My rule 1
ref: "http://my.link.com"
recommended: true
my-rule-2:
categories:
- ALL
Expand All @@ -17,3 +18,4 @@ rules:
name: my-rule-2
pretty_name: My rule 2
ref: "http://my.link.com"

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:
name: my-rule-1
pretty_name: My rule 1
ref: "http://my.link.com"
recommended: True
my-rule-2:
categories:
- ALL
Expand All @@ -17,3 +18,4 @@ rules:
name: my-rule-2
pretty_name: My rule 2
ref: "http://my.link.com"
recommended: False
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:
name: my-rule-1
pretty_name: My rule 1
ref: "http://my.link.com"
recommended: True
my-rule-2:
categories:
- ALL
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_upload_rules_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def test_main_simple_scanner(
"name": "my-rule-1",
"prettyName": "My rule 1",
"ref": "http://my.link.com",
"recommended": True,
},
{
"categories": ["ALL", "category-2"],
Expand All @@ -117,7 +118,9 @@ def test_main_simple_scanner(
"name": "my-rule-2",
"prettyName": "My rule 2",
"ref": "http://my.link.com",
"recommended": False,
},

],
}
}
Expand Down Expand Up @@ -188,6 +191,7 @@ def test_main_only_import(
"name": "CWE-1004",
"prettyName": "CWE-1004: Sensitive Cookie Without 'HttpOnly' Flag",
"ref": "https://cwe.mitre.org/data/definitions/1004.html",
"recommended": False,
},
{
"categories": ["ALL", "boost-hardened"],
Expand All @@ -200,6 +204,7 @@ def test_main_only_import(
"Homoglyphs Presented to User"
),
"ref": "https://cwe.mitre.org/data/definitions/1007.html",
"recommended": False,
},
{
"categories": ["ALL", "category-1"],
Expand All @@ -209,6 +214,7 @@ def test_main_only_import(
"name": "my-rule-1",
"prettyName": "My rule 1",
"ref": "http://my.link.com",
"recommended": True,
},
{
"categories": ["ALL", "category-2"],
Expand All @@ -218,6 +224,7 @@ def test_main_only_import(
"name": "my-rule-2",
"prettyName": "My rule 2",
"ref": "http://my.link.com",
"recommended": False,
},
{
"categories": ["ALL", "boost-hardened"],
Expand All @@ -229,6 +236,7 @@ def test_main_only_import(
"CWE-UNKNOWN - Original rule did not map to a known CWE rule"
),
"ref": "https://cwe.mitre.org/",
"recommended": False,
},
],
}
Expand Down Expand Up @@ -291,6 +299,7 @@ def test_main_rule_update_trigger_upload(
"name": "CWE-1004",
"prettyName": "CWE-1004: Sensitive Cookie Without 'HttpOnly' Flag",
"ref": "https://cwe.mitre.org/data/definitions/1004.html",
"recommended": False,
},
{
"categories": ["ALL", "boost-hardened"],
Expand All @@ -303,6 +312,7 @@ def test_main_rule_update_trigger_upload(
"Homoglyphs Presented to User"
),
"ref": "https://cwe.mitre.org/data/definitions/1007.html",
"recommended": False,
},
{
"categories": ["ALL", "category-1"],
Expand All @@ -312,6 +322,7 @@ def test_main_rule_update_trigger_upload(
"name": "my-rule-1",
"prettyName": "My rule 1",
"ref": "http://my.link.com",
"recommended": True,
},
{
"categories": ["ALL", "category-2"],
Expand All @@ -321,6 +332,7 @@ def test_main_rule_update_trigger_upload(
"name": "my-rule-2",
"prettyName": "My rule 2",
"ref": "http://my.link.com",
"recommended": False,
},
{
"categories": ["ALL", "boost-hardened"],
Expand All @@ -332,6 +344,7 @@ def test_main_rule_update_trigger_upload(
"CWE-UNKNOWN - Original rule did not map to a known CWE rule"
),
"ref": "https://cwe.mitre.org/",
"recommended": False,
},
],
}
Expand Down Expand Up @@ -386,6 +399,7 @@ def test_main_rule_import_overload(
"name": "CWE-1004",
"prettyName": "CWE-1004: Overload",
"ref": "https://cwe.mitre.org/data/definitions/1004.html",
"recommended": False,
},
{
"categories": ["ALL", "boost-hardened"],
Expand All @@ -398,6 +412,7 @@ def test_main_rule_import_overload(
"of Homoglyphs Presented to User"
),
"ref": "https://cwe.mitre.org/data/definitions/1007.html",
"recommended": False,
},
{
"categories": ["ALL"],
Expand All @@ -407,6 +422,7 @@ def test_main_rule_import_overload(
"name": "CWE-OVERLOAD",
"prettyName": "CWE-OVERLOAD - Overload",
"ref": "https://cwe.mitre.org/",
"recommended": False,
},
],
}
Expand Down Expand Up @@ -465,6 +481,7 @@ def test_main_with_placeholder(
"name": "my-rule-1",
"prettyName": "My rule 1",
"ref": f"{doc_url}/a/b/c",
"recommended": False,
},
{
"categories": ["ALL", "category-2"],
Expand All @@ -474,6 +491,7 @@ def test_main_with_placeholder(
"name": "my-rule-2",
"prettyName": "My rule 2",
"ref": f"{doc_url}/d/e/f",
"recommended": False,
},
],
},
Expand Down
1 change: 1 addition & 0 deletions tests/unit/scanner/test_upload_rules_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def has_auth_token(request: Any) -> bool:
"name": rule.name,
"prettyName": rule.pretty_name,
"ref": rule.ref,
"recommended": rule.recommended
}
for rule in rules
],
Expand Down

0 comments on commit 91ede50

Please sign in to comment.