git-security-sentinel 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- git_security_sentinel/__init__.py +74 -0
- git_security_sentinel/__main__.py +9 -0
- git_security_sentinel/ai/__init__.py +17 -0
- git_security_sentinel/ai/cost.py +34 -0
- git_security_sentinel/ai/prompts.py +81 -0
- git_security_sentinel/ai_cli.py +884 -0
- git_security_sentinel/cli.py +937 -0
- git_security_sentinel/config/__init__.py +16 -0
- git_security_sentinel/config/loader.py +245 -0
- git_security_sentinel/core/__init__.py +42 -0
- git_security_sentinel/core/diff_parser.py +93 -0
- git_security_sentinel/core/models.py +111 -0
- git_security_sentinel/core/scanner.py +360 -0
- git_security_sentinel/hooks/__init__.py +8 -0
- git_security_sentinel/hooks/bootstrap.py +84 -0
- git_security_sentinel/hooks/installer.py +103 -0
- git_security_sentinel/output/__init__.py +18 -0
- git_security_sentinel/output/sarif.py +179 -0
- git_security_sentinel/py.typed +0 -0
- git_security_sentinel/redaction/__init__.py +15 -0
- git_security_sentinel/redaction/redactor.py +313 -0
- git_security_sentinel/rules/__init__.py +29 -0
- git_security_sentinel/rules/auth.py +103 -0
- git_security_sentinel/rules/catalog.py +63 -0
- git_security_sentinel/rules/cloud.py +132 -0
- git_security_sentinel/rules/compliance.py +54 -0
- git_security_sentinel/rules/database.py +87 -0
- git_security_sentinel/rules/insecure_code.py +130 -0
- git_security_sentinel/rules/sensitive_files.py +48 -0
- git_security_sentinel/suppression/__init__.py +31 -0
- git_security_sentinel/suppression/baseline.py +133 -0
- git_security_sentinel/suppression/comment_filter.py +99 -0
- git_security_sentinel/suppression/ignore_filter.py +154 -0
- git_security_sentinel/update/__init__.py +27 -0
- git_security_sentinel/update/checker.py +189 -0
- git_security_sentinel-1.2.0.dist-info/METADATA +583 -0
- git_security_sentinel-1.2.0.dist-info/RECORD +41 -0
- git_security_sentinel-1.2.0.dist-info/WHEEL +5 -0
- git_security_sentinel-1.2.0.dist-info/entry_points.txt +4 -0
- git_security_sentinel-1.2.0.dist-info/licenses/LICENSE +21 -0
- git_security_sentinel-1.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Detection Rules Catalog.
|
|
3
|
+
Central registry and lookup for all 47 static detection rules across 5 canonical categories.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Dict, List, Optional
|
|
7
|
+
|
|
8
|
+
from git_security_sentinel.core.models import Rule
|
|
9
|
+
from git_security_sentinel.rules.auth import AUTH_RULES
|
|
10
|
+
from git_security_sentinel.rules.cloud import CLOUD_RULES
|
|
11
|
+
from git_security_sentinel.rules.compliance import COMPLIANCE_RULES
|
|
12
|
+
from git_security_sentinel.rules.database import DATABASE_RULES
|
|
13
|
+
from git_security_sentinel.rules.insecure_code import INSECURE_CODE_RULES
|
|
14
|
+
from git_security_sentinel.rules.sensitive_files import SENSITIVE_FILES_RULES
|
|
15
|
+
|
|
16
|
+
# Ensure backwards-compatible dict-like access on Rule instances
|
|
17
|
+
if not hasattr(Rule, "__getitem__"):
|
|
18
|
+
Rule.__getitem__ = lambda self, key: getattr(self, key)
|
|
19
|
+
if not hasattr(Rule, "get"):
|
|
20
|
+
Rule.get = lambda self, key, default=None: getattr(self, key, default)
|
|
21
|
+
|
|
22
|
+
# Master list of all 47 detection rules
|
|
23
|
+
ALL_RULES: List[Rule] = (
|
|
24
|
+
CLOUD_RULES
|
|
25
|
+
+ AUTH_RULES
|
|
26
|
+
+ DATABASE_RULES
|
|
27
|
+
+ SENSITIVE_FILES_RULES
|
|
28
|
+
+ INSECURE_CODE_RULES
|
|
29
|
+
+ COMPLIANCE_RULES
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
# Internal lookup index by full ID and suffix alias
|
|
33
|
+
_RULES_BY_ID: Dict[str, Rule] = {}
|
|
34
|
+
for _r in ALL_RULES:
|
|
35
|
+
_RULES_BY_ID[_r.rule_id] = _r
|
|
36
|
+
_suffix = _r.rule_id.split(".")[-1]
|
|
37
|
+
if _suffix not in _RULES_BY_ID:
|
|
38
|
+
_RULES_BY_ID[_suffix] = _r
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def get_all_rules() -> List[Rule]:
|
|
42
|
+
"""Returns a list of all active detection rules (47 rules total)."""
|
|
43
|
+
return list(ALL_RULES)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_rule_by_id(rule_id: str) -> Optional[Rule]:
|
|
47
|
+
"""
|
|
48
|
+
Lookup rule by canonical ID (e.g. 'secret.aws_access_key')
|
|
49
|
+
or short alias (e.g. 'aws_access_key').
|
|
50
|
+
"""
|
|
51
|
+
if not rule_id:
|
|
52
|
+
return None
|
|
53
|
+
# Exact lookup
|
|
54
|
+
if rule_id in _RULES_BY_ID:
|
|
55
|
+
return _RULES_BY_ID[rule_id]
|
|
56
|
+
# Suffix or case-insensitive lookup
|
|
57
|
+
cleaned = rule_id.strip().lower()
|
|
58
|
+
for r in ALL_RULES:
|
|
59
|
+
if r.rule_id.lower() == cleaned:
|
|
60
|
+
return r
|
|
61
|
+
if r.rule_id.split(".")[-1].lower() == cleaned:
|
|
62
|
+
return r
|
|
63
|
+
return None
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Cloud Provider Keys Detection Rules.
|
|
3
|
+
Covers AWS, Stripe, Twilio, SendGrid, Mailgun, Mailchimp, Square, Shopify, Heroku, Datadog, New Relic.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
from git_security_sentinel.core.models import Rule
|
|
10
|
+
|
|
11
|
+
CLOUD_RULES: List[Rule] = [
|
|
12
|
+
Rule(
|
|
13
|
+
rule_id="secret.aws_access_key",
|
|
14
|
+
category="secrets",
|
|
15
|
+
severity="critical",
|
|
16
|
+
confidence="high",
|
|
17
|
+
pattern=re.compile(r"\b((?:AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16})\b"),
|
|
18
|
+
message="Potential AWS Access Key ID detected.",
|
|
19
|
+
recommendation="Never hardcode AWS keys. Use environment variables (AWS_ACCESS_KEY_ID), IAM roles, or AWS Secrets Manager.",
|
|
20
|
+
),
|
|
21
|
+
Rule(
|
|
22
|
+
rule_id="secret.aws_secret_key",
|
|
23
|
+
category="secrets",
|
|
24
|
+
severity="critical",
|
|
25
|
+
confidence="high",
|
|
26
|
+
pattern=re.compile(
|
|
27
|
+
r"""(?i)(?:aws_secret_access_key|aws_secret_key|aws_secret)\s*[:=]\s*['"]?([A-Za-z0-9/+=]{40})['"]?"""
|
|
28
|
+
),
|
|
29
|
+
message="Potential AWS Secret Access Key assignment detected.",
|
|
30
|
+
recommendation="Rotate immediately if active. Use AWS IAM roles, AWS Secrets Manager, or vault solutions.",
|
|
31
|
+
),
|
|
32
|
+
Rule(
|
|
33
|
+
rule_id="secret.stripe_key",
|
|
34
|
+
category="secrets",
|
|
35
|
+
severity="critical",
|
|
36
|
+
confidence="high",
|
|
37
|
+
pattern=re.compile(r"\b((?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,99})\b"),
|
|
38
|
+
message="Potential Stripe Secret or Restricted API Key detected.",
|
|
39
|
+
recommendation="Never expose Stripe secret/restricted keys. Rotate key in Stripe dashboard and store in environment variables.",
|
|
40
|
+
),
|
|
41
|
+
Rule(
|
|
42
|
+
rule_id="secret.twilio_key",
|
|
43
|
+
category="secrets",
|
|
44
|
+
severity="critical",
|
|
45
|
+
confidence="high",
|
|
46
|
+
pattern=re.compile(r"\b(AC[0-9a-fA-F]{32}|SK[0-9a-fA-F]{32})\b"),
|
|
47
|
+
message="Potential Twilio Account SID or API Key detected.",
|
|
48
|
+
recommendation="Never commit Twilio account credentials. Use environment variables (TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN).",
|
|
49
|
+
),
|
|
50
|
+
Rule(
|
|
51
|
+
rule_id="secret.sendgrid_key",
|
|
52
|
+
category="secrets",
|
|
53
|
+
severity="critical",
|
|
54
|
+
confidence="high",
|
|
55
|
+
pattern=re.compile(r"\b(SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43})\b"),
|
|
56
|
+
message="Potential SendGrid API Key detected.",
|
|
57
|
+
recommendation="Rotate SendGrid API key immediately in SendGrid Console > Settings > API Keys.",
|
|
58
|
+
),
|
|
59
|
+
Rule(
|
|
60
|
+
rule_id="secret.mailgun_key",
|
|
61
|
+
category="secrets",
|
|
62
|
+
severity="critical",
|
|
63
|
+
confidence="high",
|
|
64
|
+
pattern=re.compile(r"\b(key-[0-9a-zA-Z]{32})\b"),
|
|
65
|
+
message="Potential Mailgun API Key detected.",
|
|
66
|
+
recommendation="Rotate Mailgun API key in Mailgun Dashboard > Settings > API Security.",
|
|
67
|
+
),
|
|
68
|
+
Rule(
|
|
69
|
+
rule_id="secret.mailchimp_key",
|
|
70
|
+
category="secrets",
|
|
71
|
+
severity="high",
|
|
72
|
+
confidence="high",
|
|
73
|
+
pattern=re.compile(r"\b([0-9a-f]{32}-us[0-9]{1,2})\b"),
|
|
74
|
+
message="Potential Mailchimp API Key detected.",
|
|
75
|
+
recommendation="Revoke Mailchimp API key in Account & Billing > Extras > API keys.",
|
|
76
|
+
),
|
|
77
|
+
Rule(
|
|
78
|
+
rule_id="secret.square_token",
|
|
79
|
+
category="secrets",
|
|
80
|
+
severity="critical",
|
|
81
|
+
confidence="high",
|
|
82
|
+
pattern=re.compile(
|
|
83
|
+
r"\b(sq0atp-[0-9A-Za-z\-_]{22}|sq0csp-[0-9A-Za-z\-_]{43}|EAAA[a-zA-Z0-9]{60})\b"
|
|
84
|
+
),
|
|
85
|
+
message="Potential Square Access Token or Secret detected.",
|
|
86
|
+
recommendation="Revoke Square credentials in the Square Developer Dashboard.",
|
|
87
|
+
),
|
|
88
|
+
Rule(
|
|
89
|
+
rule_id="secret.shopify_token",
|
|
90
|
+
category="secrets",
|
|
91
|
+
severity="critical",
|
|
92
|
+
confidence="high",
|
|
93
|
+
pattern=re.compile(
|
|
94
|
+
r"\b(shpat_[a-fA-F0-9]{32}|shpca_[a-fA-F0-9]{32}|shppa_[a-fA-F0-9]{32})\b"
|
|
95
|
+
),
|
|
96
|
+
message="Potential Shopify Access Token detected.",
|
|
97
|
+
recommendation="Rotate Shopify app credentials in Shopify Partner Dashboard or Store Admin.",
|
|
98
|
+
),
|
|
99
|
+
Rule(
|
|
100
|
+
rule_id="secret.heroku_key",
|
|
101
|
+
category="secrets",
|
|
102
|
+
severity="critical",
|
|
103
|
+
confidence="medium",
|
|
104
|
+
pattern=re.compile(
|
|
105
|
+
r"""(?i)(?:heroku[a-z0-9_\-\.]*)\s*[:=]\s*['"]?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})['"]?"""
|
|
106
|
+
),
|
|
107
|
+
message="Potential Heroku API Key detected.",
|
|
108
|
+
recommendation="Regenerate Heroku API token via Heroku CLI (heroku authorizations:create) or Account Settings.",
|
|
109
|
+
),
|
|
110
|
+
Rule(
|
|
111
|
+
rule_id="secret.datadog_key",
|
|
112
|
+
category="secrets",
|
|
113
|
+
severity="critical",
|
|
114
|
+
confidence="medium",
|
|
115
|
+
pattern=re.compile(
|
|
116
|
+
r"""(?i)(?:datadog|dd)[\-_]?(?:api|app)[\-_]?key\s*[:=]\s*['"]?([a-f0-9]{32,40})['"]?"""
|
|
117
|
+
),
|
|
118
|
+
message="Potential Datadog API or Application Key detected.",
|
|
119
|
+
recommendation="Revoke and rotate Datadog API keys in Organization Settings > API Keys.",
|
|
120
|
+
),
|
|
121
|
+
Rule(
|
|
122
|
+
rule_id="secret.newrelic_key",
|
|
123
|
+
category="secrets",
|
|
124
|
+
severity="critical",
|
|
125
|
+
confidence="high",
|
|
126
|
+
pattern=re.compile(
|
|
127
|
+
r"""(?i)\b(NRAK-[A-Z0-9]{27}|NRII-[A-Za-z0-9_-]{32})\b|(?:newrelic|new_relic)[\-_]?(?:license|api)?[\-_]?key\s*[:=]\s*['"]?([a-f0-9]{40})['"]?"""
|
|
128
|
+
),
|
|
129
|
+
message="Potential New Relic Ingest or API Key detected.",
|
|
130
|
+
recommendation="Rotate New Relic API/license keys in New Relic API Keys UI.",
|
|
131
|
+
),
|
|
132
|
+
]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Compliance, Hygiene & Generic Credentials Detection Rules.
|
|
3
|
+
Covers cryptographic private key headers, generic credentials, security debt markers, and root license.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
from git_security_sentinel.core.models import Rule
|
|
10
|
+
|
|
11
|
+
COMPLIANCE_RULES: List[Rule] = [
|
|
12
|
+
Rule(
|
|
13
|
+
rule_id="secret.private_key",
|
|
14
|
+
category="secrets",
|
|
15
|
+
severity="critical",
|
|
16
|
+
confidence="high",
|
|
17
|
+
pattern=re.compile(
|
|
18
|
+
r"-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP )?PRIVATE KEY(?: BLOCK)?-----"
|
|
19
|
+
),
|
|
20
|
+
message="Cryptographic private key header detected.",
|
|
21
|
+
recommendation="Never commit private keys to source control. Add the key file to .gitignore and rotate the keypair.",
|
|
22
|
+
),
|
|
23
|
+
Rule(
|
|
24
|
+
rule_id="secret.generic_credential",
|
|
25
|
+
category="secrets",
|
|
26
|
+
severity="high",
|
|
27
|
+
confidence="medium",
|
|
28
|
+
pattern=re.compile(
|
|
29
|
+
r"""(?i)(?:password|passwd|api_key|apikey|secret_key|client_secret|auth_token|access_token)\s*[:=]\s*['"]((?!\[REDACTED:)[^'"\r\n]{8,128})['"]"""
|
|
30
|
+
),
|
|
31
|
+
message="Potential hardcoded credential or secret assignment detected.",
|
|
32
|
+
recommendation="Extract credentials into a .env file or cloud secret store. Load via environment variables at runtime.",
|
|
33
|
+
),
|
|
34
|
+
Rule(
|
|
35
|
+
rule_id="compliance.security_todo",
|
|
36
|
+
category="compliance",
|
|
37
|
+
severity="low",
|
|
38
|
+
confidence="high",
|
|
39
|
+
pattern=re.compile(
|
|
40
|
+
r"""(?i)(?:#|//|/\*)\s*(?:TODO|FIXME|HACK|XXX)\s*[:\s].*(?:security|vulnerab|cwe|cve|auth|token|credential|sanitize|exploit|leak)"""
|
|
41
|
+
),
|
|
42
|
+
message="Security debt marker (TODO/FIXME/HACK) detected in comments.",
|
|
43
|
+
recommendation="Resolve security debt items prior to merging into production branches.",
|
|
44
|
+
),
|
|
45
|
+
Rule(
|
|
46
|
+
rule_id="compliance.root_license",
|
|
47
|
+
category="compliance",
|
|
48
|
+
severity="low",
|
|
49
|
+
confidence="high",
|
|
50
|
+
pattern=re.compile(r"^LICENSE.*"),
|
|
51
|
+
message="Root open-source license file missing.",
|
|
52
|
+
recommendation="Add a LICENSE file to the repository root.",
|
|
53
|
+
),
|
|
54
|
+
]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Database and Connection Strings Detection Rules.
|
|
3
|
+
Covers MongoDB, PostgreSQL, MySQL, Redis, AMQP, Firebase config, GCP Service Account.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
from git_security_sentinel.core.models import Rule
|
|
10
|
+
|
|
11
|
+
DATABASE_RULES: List[Rule] = [
|
|
12
|
+
Rule(
|
|
13
|
+
rule_id="secret.mongodb_uri",
|
|
14
|
+
category="secrets",
|
|
15
|
+
severity="critical",
|
|
16
|
+
confidence="high",
|
|
17
|
+
pattern=re.compile(
|
|
18
|
+
r"""\b(mongodb(?:\+srv)?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b"""
|
|
19
|
+
),
|
|
20
|
+
message="MongoDB connection URI with embedded credentials detected.",
|
|
21
|
+
recommendation="Use environment variables for database credentials and connection strings.",
|
|
22
|
+
),
|
|
23
|
+
Rule(
|
|
24
|
+
rule_id="secret.postgres_uri",
|
|
25
|
+
category="secrets",
|
|
26
|
+
severity="critical",
|
|
27
|
+
confidence="high",
|
|
28
|
+
pattern=re.compile(
|
|
29
|
+
r"""\b(postgres(?:ql)?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b"""
|
|
30
|
+
),
|
|
31
|
+
message="PostgreSQL connection URI with embedded credentials detected.",
|
|
32
|
+
recommendation="Store PostgreSQL credentials in environment variables or use IAM database authentication.",
|
|
33
|
+
),
|
|
34
|
+
Rule(
|
|
35
|
+
rule_id="secret.mysql_uri",
|
|
36
|
+
category="secrets",
|
|
37
|
+
severity="critical",
|
|
38
|
+
confidence="high",
|
|
39
|
+
pattern=re.compile(
|
|
40
|
+
r"""\b(mysql://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b"""
|
|
41
|
+
),
|
|
42
|
+
message="MySQL connection URI with embedded credentials detected.",
|
|
43
|
+
recommendation="Store MySQL credentials in environment variables or vault secret storage.",
|
|
44
|
+
),
|
|
45
|
+
Rule(
|
|
46
|
+
rule_id="secret.redis_uri",
|
|
47
|
+
category="secrets",
|
|
48
|
+
severity="high",
|
|
49
|
+
confidence="high",
|
|
50
|
+
pattern=re.compile(
|
|
51
|
+
r"""\b(rediss?://(?:[^:\s]*:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b"""
|
|
52
|
+
),
|
|
53
|
+
message="Redis connection URI with embedded password/auth token detected.",
|
|
54
|
+
recommendation="Load Redis password from environment variable REDIS_PASSWORD at runtime.",
|
|
55
|
+
),
|
|
56
|
+
Rule(
|
|
57
|
+
rule_id="secret.amqp_uri",
|
|
58
|
+
category="secrets",
|
|
59
|
+
severity="high",
|
|
60
|
+
confidence="high",
|
|
61
|
+
pattern=re.compile(
|
|
62
|
+
r"""\b(amqps?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b"""
|
|
63
|
+
),
|
|
64
|
+
message="AMQP/RabbitMQ connection URI with embedded credentials detected.",
|
|
65
|
+
recommendation="Store message broker credentials in environment variables or configuration files outside git.",
|
|
66
|
+
),
|
|
67
|
+
Rule(
|
|
68
|
+
rule_id="secret.firebase_key",
|
|
69
|
+
category="secrets",
|
|
70
|
+
severity="critical",
|
|
71
|
+
confidence="high",
|
|
72
|
+
pattern=re.compile(r"\b(AAAA[a-zA-Z0-9_-]{7}:[a-zA-Z0-9_-]{140})\b"),
|
|
73
|
+
message="Firebase Cloud Messaging (FCM) legacy server key detected.",
|
|
74
|
+
recommendation="Migrate from legacy FCM server key to HTTP v1 API using Google Cloud service account credentials.",
|
|
75
|
+
),
|
|
76
|
+
Rule(
|
|
77
|
+
rule_id="secret.gcp_service_account",
|
|
78
|
+
category="secrets",
|
|
79
|
+
severity="critical",
|
|
80
|
+
confidence="high",
|
|
81
|
+
pattern=re.compile(
|
|
82
|
+
r"""(?i)"type"\s*:\s*"service_account"|"private_key_id"\s*:\s*"[a-f0-9]{40}"|[a-z0-9\-]+@[a-z0-9\-]+\.iam\.gserviceaccount\.com"""
|
|
83
|
+
),
|
|
84
|
+
message="GCP Service Account private key or identity definition detected.",
|
|
85
|
+
recommendation="Delete leaked service account key in GCP IAM & Admin > Service Accounts > Keys and recreate.",
|
|
86
|
+
),
|
|
87
|
+
]
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Insecure Code Patterns & CWEs Detection Rules.
|
|
3
|
+
Covers Shell injection, pickle, unsafe YAML, SQL injection, XXE, temp credentials,
|
|
4
|
+
debug flags, wildcard CORS, unverified SSL, eval execution, and localhost endpoints.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
from typing import List
|
|
9
|
+
|
|
10
|
+
from git_security_sentinel.core.models import Rule
|
|
11
|
+
|
|
12
|
+
INSECURE_CODE_RULES: List[Rule] = [
|
|
13
|
+
Rule(
|
|
14
|
+
rule_id="insecure_code.shell_injection",
|
|
15
|
+
category="insecure_code",
|
|
16
|
+
severity="high",
|
|
17
|
+
confidence="medium",
|
|
18
|
+
pattern=re.compile(
|
|
19
|
+
r"\b(?:subprocess\.(?:Popen|run|call|check_call|check_output)\s*\([^)]*shell\s*=\s*(?:True|1)|os\.system\s*\([^)]*\)|os\.popen\s*\([^)]*\))"
|
|
20
|
+
),
|
|
21
|
+
message="Subprocess invocation with shell=True or os.system() detected (potential command injection CWE-78).",
|
|
22
|
+
recommendation="Avoid shell=True. Pass arguments as a list of strings and sanitize inputs.",
|
|
23
|
+
),
|
|
24
|
+
Rule(
|
|
25
|
+
rule_id="insecure_code.pickle_deserialization",
|
|
26
|
+
category="insecure_code",
|
|
27
|
+
severity="critical",
|
|
28
|
+
confidence="high",
|
|
29
|
+
pattern=re.compile(r"\b(?:pickle|_pickle|cPickle)\.(?:loads?|Unpickler)\s*\("),
|
|
30
|
+
message="Unsafe object deserialization using pickle detected (CWE-502).",
|
|
31
|
+
recommendation="Never deserialize untrusted data with pickle. Use json, msgpack, or protobuf instead.",
|
|
32
|
+
),
|
|
33
|
+
Rule(
|
|
34
|
+
rule_id="insecure_code.unsafe_yaml",
|
|
35
|
+
category="insecure_code",
|
|
36
|
+
severity="high",
|
|
37
|
+
confidence="high",
|
|
38
|
+
pattern=re.compile(
|
|
39
|
+
r"\byaml\.(?:load|load_all)\s*\((?![^)]*Loader\s*=\s*(?:yaml\.)?SafeLoader\b)|\byaml\.unsafe_load\s*\("
|
|
40
|
+
),
|
|
41
|
+
message="Unsafe YAML deserialization without SafeLoader detected (CWE-502).",
|
|
42
|
+
recommendation="Use yaml.safe_load() or specify Loader=yaml.SafeLoader when loading YAML data.",
|
|
43
|
+
),
|
|
44
|
+
Rule(
|
|
45
|
+
rule_id="insecure_code.sql_concatenation",
|
|
46
|
+
category="insecure_code",
|
|
47
|
+
severity="high",
|
|
48
|
+
confidence="medium",
|
|
49
|
+
pattern=re.compile(
|
|
50
|
+
r"""(?i)(?:execute|cursor\.execute|session\.execute)\s*\(\s*(?:f['"]|['"].*?%|['"].*?\+\s*|[a-z0-9_]+\s*\+\s*['"]).*(?:SELECT|INSERT|UPDATE|DELETE|DROP|ALTER|WHERE)\b"""
|
|
51
|
+
),
|
|
52
|
+
message="Potential SQL Injection via string formatting or concatenation (CWE-89).",
|
|
53
|
+
recommendation="Use parameterized queries (e.g. cursor.execute('SELECT * FROM t WHERE id = %s', (id,))) or an ORM.",
|
|
54
|
+
),
|
|
55
|
+
Rule(
|
|
56
|
+
rule_id="insecure_code.xxe_injection",
|
|
57
|
+
category="insecure_code",
|
|
58
|
+
severity="high",
|
|
59
|
+
confidence="medium",
|
|
60
|
+
pattern=re.compile(
|
|
61
|
+
r"""(?i)\b(?:xml\.etree|xml\.sax|lxml\.etree|xml\.dom\.minidom)\b.*(?:parse|fromstring)\s*\(|resolve_entities\s*=\s*True|XMLParser\s*\([^)]*resolve_entities\s*=\s*True"""
|
|
62
|
+
),
|
|
63
|
+
message="XML parser with external entity resolution or standard parser without entity protection detected (CWE-611).",
|
|
64
|
+
recommendation="Use defusedxml or disable external entity resolution (resolve_entities=False).",
|
|
65
|
+
),
|
|
66
|
+
Rule(
|
|
67
|
+
rule_id="insecure_code.temp_credential",
|
|
68
|
+
category="insecure_code",
|
|
69
|
+
severity="high",
|
|
70
|
+
confidence="medium",
|
|
71
|
+
pattern=re.compile(
|
|
72
|
+
r"""(?i)(?:temp_password|temporary_password|test_credential|staging_token|mock_secret)\s*[:=]\s*['"]([^'"\r\n]{8,64})['"]"""
|
|
73
|
+
),
|
|
74
|
+
message="Hardcoded temporary or staging credential detected in source code.",
|
|
75
|
+
recommendation="Inject temporary credentials via environment variables and keep secrets out of source.",
|
|
76
|
+
),
|
|
77
|
+
Rule(
|
|
78
|
+
rule_id="insecure_code.debug_enabled",
|
|
79
|
+
category="insecure_code",
|
|
80
|
+
severity="medium",
|
|
81
|
+
confidence="high",
|
|
82
|
+
pattern=re.compile(
|
|
83
|
+
r"(?i)\b(?:DEBUG\s*=\s*True|app\.run\([^)]*debug\s*=\s*True|settings\.DEBUG\s*=\s*True)\b"
|
|
84
|
+
),
|
|
85
|
+
message="Application debug mode enabled (CWE-215).",
|
|
86
|
+
recommendation="Ensure DEBUG is set to False in production environments.",
|
|
87
|
+
),
|
|
88
|
+
Rule(
|
|
89
|
+
rule_id="insecure_code.wildcard_cors",
|
|
90
|
+
category="insecure_code",
|
|
91
|
+
severity="medium",
|
|
92
|
+
confidence="high",
|
|
93
|
+
pattern=re.compile(
|
|
94
|
+
r"""(?i)(?:Access-Control-Allow-Origin['"]?\s*[:=,]\s*['"]?\*['"]?|allow_origins\s*=\s*\[\s*['"]\*['"]\s*\]|CORS\([^)]*origins\s*=\s*['"]\*['"])"""
|
|
95
|
+
),
|
|
96
|
+
message="Permissive wildcard CORS header ('*') detected (CWE-942).",
|
|
97
|
+
recommendation="Explicitly specify trusted origin domains instead of wildcard '*' for CORS.",
|
|
98
|
+
),
|
|
99
|
+
Rule(
|
|
100
|
+
rule_id="insecure_code.unverified_ssl",
|
|
101
|
+
category="insecure_code",
|
|
102
|
+
severity="high",
|
|
103
|
+
confidence="high",
|
|
104
|
+
pattern=re.compile(
|
|
105
|
+
r"""(?i)\b(?:verify\s*=\s*False|ssl\._create_unverified_context|NODE_TLS_REJECT_UNAUTHORIZED\s*=\s*['"]?0['"]?|check_hostname\s*=\s*False)\b"""
|
|
106
|
+
),
|
|
107
|
+
message="Disabled SSL/TLS certificate verification detected (CWE-295).",
|
|
108
|
+
recommendation="Enable SSL/TLS certificate verification (verify=True) to prevent Man-in-the-Middle (MITM) attacks.",
|
|
109
|
+
),
|
|
110
|
+
Rule(
|
|
111
|
+
rule_id="risk.eval_execution",
|
|
112
|
+
category="insecure_code",
|
|
113
|
+
severity="medium",
|
|
114
|
+
confidence="medium",
|
|
115
|
+
pattern=re.compile(r"\b(?:eval|exec)\s*\((?!\s*['\"][^'\"]*['\"]\s*\))"),
|
|
116
|
+
message="Potentially unsafe dynamic code execution using eval() or exec().",
|
|
117
|
+
recommendation="Avoid eval()/exec() with dynamic or user-influenced data to prevent arbitrary code execution (CWE-95). Use ast.literal_eval() or safe parser alternatives.",
|
|
118
|
+
),
|
|
119
|
+
Rule(
|
|
120
|
+
rule_id="risk.localhost_endpoint",
|
|
121
|
+
category="configuration",
|
|
122
|
+
severity="low",
|
|
123
|
+
confidence="high",
|
|
124
|
+
pattern=re.compile(
|
|
125
|
+
r"""https?://(?:localhost|127\.0\.0\.1|0\.0\.0\.0)(?::[0-9]{1,5})?(?:/[^\s'"]*)?"""
|
|
126
|
+
),
|
|
127
|
+
message="Hardcoded local loopback URL endpoint detected in code.",
|
|
128
|
+
recommendation="Ensure development endpoints are parameterized via environment variables before deploying to production.",
|
|
129
|
+
),
|
|
130
|
+
]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Sensitive Committed Files Detection Rules.
|
|
3
|
+
Covers .env files, id_rsa, id_ed25519, and *.pem/key certificates.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
from git_security_sentinel.core.models import Rule
|
|
10
|
+
|
|
11
|
+
SENSITIVE_FILES_RULES: List[Rule] = [
|
|
12
|
+
Rule(
|
|
13
|
+
rule_id="file.env_file",
|
|
14
|
+
category="sensitive_files",
|
|
15
|
+
severity="critical",
|
|
16
|
+
confidence="high",
|
|
17
|
+
pattern=re.compile(r"""(?i)(?:^|/)\.env(?:\.(?!example|template|sample|dist)[a-zA-Z0-9_\-]+)?$"""),
|
|
18
|
+
message="Committed .env environment variable configuration file detected.",
|
|
19
|
+
recommendation="Add .env files to .gitignore and use .env.example with dummy values for documentation.",
|
|
20
|
+
),
|
|
21
|
+
Rule(
|
|
22
|
+
rule_id="file.ssh_rsa_key",
|
|
23
|
+
category="sensitive_files",
|
|
24
|
+
severity="critical",
|
|
25
|
+
confidence="high",
|
|
26
|
+
pattern=re.compile(r"(?i)(?:^|/)id_rsa$"),
|
|
27
|
+
message="Committed SSH RSA private key file detected.",
|
|
28
|
+
recommendation="Remove id_rsa from git history immediately and regenerate SSH keypairs.",
|
|
29
|
+
),
|
|
30
|
+
Rule(
|
|
31
|
+
rule_id="file.ssh_ed25519_key",
|
|
32
|
+
category="sensitive_files",
|
|
33
|
+
severity="critical",
|
|
34
|
+
confidence="high",
|
|
35
|
+
pattern=re.compile(r"(?i)(?:^|/)id_ed25519$"),
|
|
36
|
+
message="Committed SSH Ed25519 private key file detected.",
|
|
37
|
+
recommendation="Remove id_ed25519 from git history immediately and regenerate SSH keypairs.",
|
|
38
|
+
),
|
|
39
|
+
Rule(
|
|
40
|
+
rule_id="file.pem_key",
|
|
41
|
+
category="sensitive_files",
|
|
42
|
+
severity="critical",
|
|
43
|
+
confidence="high",
|
|
44
|
+
pattern=re.compile(r"(?i)(?:^|/).+\.(?:pem|pkcs12|pfx|p12|key)$"),
|
|
45
|
+
message="Committed cryptographic certificate/key file detected.",
|
|
46
|
+
recommendation="Never commit .pem, .key, or PKCS#12 bundle files to version control.",
|
|
47
|
+
),
|
|
48
|
+
]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Suppression Package.
|
|
3
|
+
Provides line-level comment suppression, gitignore-style path filtering,
|
|
4
|
+
and persistent baseline filtering.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from git_security_sentinel.suppression.comment_filter import (
|
|
8
|
+
SUPPRESSION_REGEX,
|
|
9
|
+
is_rule_suppressed,
|
|
10
|
+
normalize_rule_identifier,
|
|
11
|
+
parse_inline_suppression,
|
|
12
|
+
)
|
|
13
|
+
from git_security_sentinel.suppression.ignore_filter import (
|
|
14
|
+
PathFilter,
|
|
15
|
+
load_sentinelignore,
|
|
16
|
+
)
|
|
17
|
+
from git_security_sentinel.suppression.baseline import (
|
|
18
|
+
BaselineFilter,
|
|
19
|
+
compute_fingerprint,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"SUPPRESSION_REGEX",
|
|
24
|
+
"parse_inline_suppression",
|
|
25
|
+
"is_rule_suppressed",
|
|
26
|
+
"normalize_rule_identifier",
|
|
27
|
+
"PathFilter",
|
|
28
|
+
"load_sentinelignore",
|
|
29
|
+
"BaselineFilter",
|
|
30
|
+
"compute_fingerprint",
|
|
31
|
+
]
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Git Security Sentinel - Baseline File Filtering and Generation.
|
|
3
|
+
Suppresses pre-existing technical security debt across repositories using
|
|
4
|
+
stable deterministic SHA-256 fingerprints resistant to line shifts.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import hashlib
|
|
8
|
+
import json
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional, Set
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def compute_fingerprint(rule_id: str, file_path: str, evidence: str) -> str:
|
|
14
|
+
"""
|
|
15
|
+
Computes a deterministic, line-shift-resistant SHA-256 fingerprint for a finding.
|
|
16
|
+
|
|
17
|
+
Path separators are normalized to '/', leading slashes removed, case normalized to lower.
|
|
18
|
+
Evidence whitespace is normalized to single spaces.
|
|
19
|
+
Line numbers are intentionally omitted so line shifts above findings do not invalidate them.
|
|
20
|
+
"""
|
|
21
|
+
norm_path = file_path.replace("\\", "/").strip().lower()
|
|
22
|
+
if norm_path.startswith("./"):
|
|
23
|
+
norm_path = norm_path[2:]
|
|
24
|
+
norm_path = norm_path.lstrip("/")
|
|
25
|
+
|
|
26
|
+
norm_evidence = " ".join(evidence.strip().split())
|
|
27
|
+
key = f"{rule_id}:{norm_path}:{norm_evidence}"
|
|
28
|
+
return hashlib.sha256(key.encode("utf-8")).hexdigest()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class BaselineFilter:
|
|
32
|
+
"""
|
|
33
|
+
Manages loading, matching, and generating .sentinel.baseline JSON files.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
compute_fingerprint = staticmethod(compute_fingerprint)
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
baseline_path: Optional[Path] = None,
|
|
41
|
+
fingerprints: Optional[Set[str]] = None,
|
|
42
|
+
):
|
|
43
|
+
self.baseline_path = Path(baseline_path) if baseline_path else None
|
|
44
|
+
if fingerprints is not None:
|
|
45
|
+
self.fingerprints: Set[str] = set(fingerprints)
|
|
46
|
+
elif self.baseline_path and self.baseline_path.is_file():
|
|
47
|
+
self.fingerprints = self.load_baseline(self.baseline_path)
|
|
48
|
+
else:
|
|
49
|
+
self.fingerprints = set()
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def load_baseline(cls, path: Path) -> Set[str]:
|
|
53
|
+
"""
|
|
54
|
+
Reads a baseline JSON file and extracts the set of suppressed fingerprints.
|
|
55
|
+
"""
|
|
56
|
+
p = Path(path)
|
|
57
|
+
if not p.is_file():
|
|
58
|
+
return set()
|
|
59
|
+
try:
|
|
60
|
+
data = json.loads(p.read_text(encoding="utf-8"))
|
|
61
|
+
fps = set()
|
|
62
|
+
for item in data.get("findings", []):
|
|
63
|
+
if isinstance(item, dict) and "fingerprint" in item:
|
|
64
|
+
fps.add(item["fingerprint"])
|
|
65
|
+
return fps
|
|
66
|
+
except Exception:
|
|
67
|
+
return set()
|
|
68
|
+
|
|
69
|
+
def is_suppressed(self, finding: Any) -> bool:
|
|
70
|
+
"""
|
|
71
|
+
Determines if a finding matches a suppressed fingerprint in the baseline.
|
|
72
|
+
"""
|
|
73
|
+
if not self.fingerprints:
|
|
74
|
+
return False
|
|
75
|
+
|
|
76
|
+
if hasattr(finding, "fingerprint") and finding.fingerprint:
|
|
77
|
+
return finding.fingerprint in self.fingerprints
|
|
78
|
+
|
|
79
|
+
rule_id = getattr(finding, "rule_id", None) or (
|
|
80
|
+
finding.get("rule_id") if isinstance(finding, dict) else ""
|
|
81
|
+
)
|
|
82
|
+
file_path = getattr(finding, "file", None) or (
|
|
83
|
+
finding.get("file") if isinstance(finding, dict) else ""
|
|
84
|
+
)
|
|
85
|
+
evidence = getattr(finding, "evidence", None) or (
|
|
86
|
+
finding.get("evidence") if isinstance(finding, dict) else ""
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
fp = compute_fingerprint(rule_id, file_path, evidence)
|
|
90
|
+
return fp in self.fingerprints
|
|
91
|
+
|
|
92
|
+
@classmethod
|
|
93
|
+
def create_baseline(cls, path: Path, findings: List[Any]) -> None:
|
|
94
|
+
"""
|
|
95
|
+
Generates and writes a .sentinel.baseline JSON file from a list of findings.
|
|
96
|
+
"""
|
|
97
|
+
p = Path(path)
|
|
98
|
+
entries = []
|
|
99
|
+
for f in findings:
|
|
100
|
+
rule_id = getattr(f, "rule_id", None) or (
|
|
101
|
+
f.get("rule_id") if isinstance(f, dict) else ""
|
|
102
|
+
)
|
|
103
|
+
file_path = getattr(f, "file", None) or (
|
|
104
|
+
f.get("file") if isinstance(f, dict) else ""
|
|
105
|
+
)
|
|
106
|
+
line = getattr(f, "line", None) or (
|
|
107
|
+
f.get("line") if isinstance(f, dict) else 1
|
|
108
|
+
)
|
|
109
|
+
evidence = getattr(f, "evidence", None) or (
|
|
110
|
+
f.get("evidence") if isinstance(f, dict) else ""
|
|
111
|
+
)
|
|
112
|
+
fp = getattr(f, "fingerprint", None) or compute_fingerprint(
|
|
113
|
+
rule_id, file_path, evidence
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
entries.append(
|
|
117
|
+
{
|
|
118
|
+
"fingerprint": fp,
|
|
119
|
+
"rule_id": rule_id,
|
|
120
|
+
"file": file_path.replace("\\", "/"),
|
|
121
|
+
"line": line,
|
|
122
|
+
"evidence": evidence,
|
|
123
|
+
}
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
data = {
|
|
127
|
+
"format_version": "1.0",
|
|
128
|
+
"generator": "git-security-sentinel 1.2.0",
|
|
129
|
+
"findings": entries,
|
|
130
|
+
}
|
|
131
|
+
if p.parent and not p.parent.exists():
|
|
132
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
133
|
+
p.write_text(json.dumps(data, indent=2), encoding="utf-8")
|