Skip to content

Commit

Permalink
Removal of the Cwe2 dependency (#499)
Browse files Browse the repository at this point in the history
This change removes the dependency on Cwe2 and replaces with its own
class.

This seems to improve performance some. Seems to cut about a half second
of time to analyze cpython.

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored May 30, 2024
1 parent ddcb3ce commit d934cf3
Show file tree
Hide file tree
Showing 50 changed files with 111 additions and 54 deletions.
61 changes: 61 additions & 0 deletions precli/core/cwe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2024 Secure Sauce LLC


class Cwe:
def __init__(self, id: int):
self._id = id

@property
def id(self) -> int:
"""CWE ID."""
return self._id

@property
def name(self) -> str:
"""Name of the CWE."""
match self._id:
case 94:
return (
"Improper Control of Generation of Code ('Code Injection')"
)
case 208:
return "Observable Timing Discrepancy"
case 214:
return (
"Invocation of Process Using Visible Sensitive Information"
)
case 295:
return "Improper Certificate Validation"
case 319:
return "Cleartext Transmission of Sensitive Information"
case 326:
return "Inadequate Encryption Strength"
case 327:
return "Use of a Broken or Risky Cryptographic Algorithm"
case 328:
return "Use of Weak Hash"
case 330:
return "Use of Insufficiently Random Values"
case 377:
return "Insecure Temporary File"
case 502:
return "Deserialization of Untrusted Data"
case 598:
return "Use of GET Request Method With Sensitive Query Strings"
case 614:
return (
"Sensitive Cookie in HTTPS Session Without 'Secure' "
"Attribute"
)
case 703:
return "Improper Check or Handling of Exceptional Conditions"
case 1327:
return "Binding to an Unrestricted IP Address"
case 1333:
return "Inefficient Regular Expression Complexity"

return self._id

def url(self) -> str:
"""URL of the CWE."""
return f"https://cwe.mitre.org/data/definitions/{self._id}.html"
2 changes: 1 addition & 1 deletion precli/renderers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def create_rule_if_needed(
properties={
"tags": [
"security",
f"external/cwe/cwe-{rule.cwe.cwe_id}",
f"external/cwe/cwe-{rule.cwe.id}",
],
"security-severity": (rule.default_config.level.to_severity()),
},
Expand Down
9 changes: 3 additions & 6 deletions precli/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
from abc import ABC
from typing import Self

from cwe2.database import Database
from cwe2.weakness import Weakness

from precli.core.config import Config
from precli.core.cwe import Cwe
from precli.core.fix import Fix
from precli.core.location import Location


class Rule(ABC):
_rules = {}
_cwedb = Database()

def __init__(
self,
Expand Down Expand Up @@ -41,7 +38,7 @@ def __init__(
except ValueError:
start = 0
self._full_descr = description[start:]
self._cwe = Rule._cwedb.get(cwe_id)
self._cwe = Cwe(cwe_id)
self._message = message
self._wildcards = wildcards
self._config = Config() if not config else config
Expand Down Expand Up @@ -107,7 +104,7 @@ def enabled(self, enabled):
self._enabled = enabled

@property
def cwe(self) -> Weakness:
def cwe(self) -> Cwe:
"""CWE weakness object for this rule."""
return self._cwe

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cwe2==2.0.0
Pygments==2.18.0
rich==13.7.1
tree-sitter==0.21.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "327"
assert rule.cwe.id == 327

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/go/stdlib/crypto/test_crypto_weak_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "328"
assert rule.cwe.id == 328

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/go/stdlib/crypto/test_crypto_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "326"
assert rule.cwe.id == 326

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "614"
assert rule.cwe.id == 614

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "328"
assert rule.cwe.id == 328

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "326"
assert rule.cwe.id == 326

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "338"
assert rule.cwe.id == 338

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "327"
assert rule.cwe.id == 327

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "614"
assert rule.cwe.id == 614

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "214"
assert rule.cwe.id == 214

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/python/stdlib/assert/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "703"
assert rule.cwe.id == 703

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "328"
assert rule.cwe.id == 328

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "319"
assert rule.cwe.id == 319

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "295"
assert rule.cwe.id == 295

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "330"
assert rule.cwe.id == 330

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "328"
assert rule.cwe.id == 328

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "208"
assert rule.cwe.id == 208

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/python/stdlib/hmac/test_hmac_weak_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "328"
assert rule.cwe.id == 328

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/python/stdlib/hmac/test_hmac_weak_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "326"
assert rule.cwe.id == 326

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "1327"
assert rule.cwe.id == 1327

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "598"
assert rule.cwe.id == 598

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "319"
assert rule.cwe.id == 319

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "295"
assert rule.cwe.id == 295

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/python/stdlib/json/test_json_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is False
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "502"
assert rule.cwe.id == 502

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "94"
assert rule.cwe.id == 94

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "502"
assert rule.cwe.id == 502

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "319"
assert rule.cwe.id == 319

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "295"
assert rule.cwe.id == 295

@pytest.mark.parametrize(
"filename",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/rules/python/stdlib/pickle/test_pickle_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "502"
assert rule.cwe.id == 502

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.ERROR
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "319"
assert rule.cwe.id == 319

@pytest.mark.parametrize(
"filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_rule_meta(self):
assert rule.default_config.enabled is True
assert rule.default_config.level == Level.WARNING
assert rule.default_config.rank == -1.0
assert rule.cwe.cwe_id == "295"
assert rule.cwe.id == 295

@pytest.mark.parametrize(
"filename",
Expand Down
Loading

0 comments on commit d934cf3

Please sign in to comment.