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.
Files changed (41) hide show
  1. git_security_sentinel/__init__.py +74 -0
  2. git_security_sentinel/__main__.py +9 -0
  3. git_security_sentinel/ai/__init__.py +17 -0
  4. git_security_sentinel/ai/cost.py +34 -0
  5. git_security_sentinel/ai/prompts.py +81 -0
  6. git_security_sentinel/ai_cli.py +884 -0
  7. git_security_sentinel/cli.py +937 -0
  8. git_security_sentinel/config/__init__.py +16 -0
  9. git_security_sentinel/config/loader.py +245 -0
  10. git_security_sentinel/core/__init__.py +42 -0
  11. git_security_sentinel/core/diff_parser.py +93 -0
  12. git_security_sentinel/core/models.py +111 -0
  13. git_security_sentinel/core/scanner.py +360 -0
  14. git_security_sentinel/hooks/__init__.py +8 -0
  15. git_security_sentinel/hooks/bootstrap.py +84 -0
  16. git_security_sentinel/hooks/installer.py +103 -0
  17. git_security_sentinel/output/__init__.py +18 -0
  18. git_security_sentinel/output/sarif.py +179 -0
  19. git_security_sentinel/py.typed +0 -0
  20. git_security_sentinel/redaction/__init__.py +15 -0
  21. git_security_sentinel/redaction/redactor.py +313 -0
  22. git_security_sentinel/rules/__init__.py +29 -0
  23. git_security_sentinel/rules/auth.py +103 -0
  24. git_security_sentinel/rules/catalog.py +63 -0
  25. git_security_sentinel/rules/cloud.py +132 -0
  26. git_security_sentinel/rules/compliance.py +54 -0
  27. git_security_sentinel/rules/database.py +87 -0
  28. git_security_sentinel/rules/insecure_code.py +130 -0
  29. git_security_sentinel/rules/sensitive_files.py +48 -0
  30. git_security_sentinel/suppression/__init__.py +31 -0
  31. git_security_sentinel/suppression/baseline.py +133 -0
  32. git_security_sentinel/suppression/comment_filter.py +99 -0
  33. git_security_sentinel/suppression/ignore_filter.py +154 -0
  34. git_security_sentinel/update/__init__.py +27 -0
  35. git_security_sentinel/update/checker.py +189 -0
  36. git_security_sentinel-1.2.0.dist-info/METADATA +583 -0
  37. git_security_sentinel-1.2.0.dist-info/RECORD +41 -0
  38. git_security_sentinel-1.2.0.dist-info/WHEEL +5 -0
  39. git_security_sentinel-1.2.0.dist-info/entry_points.txt +4 -0
  40. git_security_sentinel-1.2.0.dist-info/licenses/LICENSE +21 -0
  41. git_security_sentinel-1.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,179 @@
1
+ """
2
+ Git Security Sentinel - SARIF 2.1.0 Output Formatter.
3
+ Strictly adheres to OASIS SARIF 2.1.0 schema and GitHub Advanced Security specifications.
4
+ Zero third-party dependencies.
5
+ """
6
+
7
+ from typing import Any, Dict, List, Optional
8
+
9
+ from git_security_sentinel.core.models import VERSION
10
+ from git_security_sentinel.redaction.redactor import redact_text
11
+
12
+ SARIF_SCHEMA_URI = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"
13
+ SARIF_VERSION = "2.1.0"
14
+
15
+ SEVERITY_LEVEL_MAP: Dict[str, str] = {
16
+ "critical": "error",
17
+ "high": "error",
18
+ "medium": "warning",
19
+ "low": "note",
20
+ "info": "note",
21
+ }
22
+
23
+
24
+ def generate_sarif_report(
25
+ report_data: Any,
26
+ rules_catalog: Optional[List[Any]] = None,
27
+ ) -> Dict[str, Any]:
28
+ """
29
+ Converts internal scan report (ScanReport object or dict) to standard SARIF 2.1.0 format.
30
+ Ensures strict schema compliance and complete secret redaction.
31
+ """
32
+ if hasattr(report_data, "to_dict"):
33
+ report_dict = report_data.to_dict()
34
+ elif isinstance(report_data, dict):
35
+ report_dict = report_data
36
+ else:
37
+ report_dict = {}
38
+
39
+ if rules_catalog is None:
40
+ try:
41
+ from git_security_sentinel.rules.catalog import get_all_rules
42
+ rules_catalog = get_all_rules()
43
+ except Exception:
44
+ rules_catalog = []
45
+
46
+ # Build driver rules list
47
+ rules_dict: Dict[str, int] = {}
48
+ driver_rules: List[Dict[str, Any]] = []
49
+
50
+ for idx, rule in enumerate(rules_catalog):
51
+ r_id = getattr(rule, "rule_id", None) or (rule.get("rule_id", "") if isinstance(rule, dict) else "")
52
+ r_cat = getattr(rule, "category", None) or (rule.get("category", "secrets") if isinstance(rule, dict) else "secrets")
53
+ r_sev = getattr(rule, "severity", None) or (rule.get("severity", "high") if isinstance(rule, dict) else "high")
54
+ r_conf = getattr(rule, "confidence", None) or (rule.get("confidence", "high") if isinstance(rule, dict) else "high")
55
+ r_msg = getattr(rule, "message", None) or (rule.get("message", "Security rule finding") if isinstance(rule, dict) else "Security rule finding")
56
+ r_rec = getattr(rule, "recommendation", None) or (rule.get("recommendation", "Remediate finding safely.") if isinstance(rule, dict) else "Remediate finding safely.")
57
+
58
+ level = SEVERITY_LEVEL_MAP.get(str(r_sev).lower(), "warning")
59
+ rule_name = r_id.replace(".", "_").replace("-", "_")
60
+
61
+ rule_obj = {
62
+ "id": r_id,
63
+ "name": rule_name,
64
+ "shortDescription": {"text": str(r_msg)},
65
+ "fullDescription": {"text": str(r_msg)},
66
+ "defaultConfiguration": {"level": level},
67
+ "help": {
68
+ "text": str(r_rec),
69
+ "markdown": f"**Remediation**: {r_rec}",
70
+ },
71
+ "properties": {
72
+ "category": str(r_cat),
73
+ "severity": str(r_sev),
74
+ "confidence": str(r_conf),
75
+ "tags": ["security", str(r_cat)],
76
+ },
77
+ }
78
+ driver_rules.append(rule_obj)
79
+ rules_dict[r_id] = idx
80
+
81
+ # Build results list
82
+ findings = report_dict.get("findings", [])
83
+ results: List[Dict[str, Any]] = []
84
+
85
+ for f in findings:
86
+ r_id = f.get("rule_id", "unknown") if isinstance(f, dict) else getattr(f, "rule_id", "unknown")
87
+ sev = f.get("severity", "high") if isinstance(f, dict) else getattr(f, "severity", "high")
88
+ level = SEVERITY_LEVEL_MAP.get(str(sev).lower(), "warning")
89
+
90
+ # Map to ruleIndex if present
91
+ if r_id in rules_dict:
92
+ rule_idx = rules_dict[r_id]
93
+ else:
94
+ # Dynamically append rule if not in catalogue
95
+ rule_idx = len(driver_rules)
96
+ driver_rules.append({
97
+ "id": r_id,
98
+ "name": r_id.replace(".", "_").replace("-", "_"),
99
+ "shortDescription": {"text": "Security rule finding"},
100
+ "fullDescription": {"text": "Security rule finding"},
101
+ "defaultConfiguration": {"level": level},
102
+ "help": {"text": "Remediate finding safely.", "markdown": "**Remediation**: Remediate finding safely."},
103
+ "properties": {"category": "secrets", "severity": str(sev), "confidence": "high", "tags": ["security"]},
104
+ })
105
+ rules_dict[r_id] = rule_idx
106
+
107
+ raw_file = f.get("file", "") if isinstance(f, dict) else getattr(f, "file", "")
108
+ norm_file = str(raw_file).replace("\\", "/").lstrip("/")
109
+
110
+ raw_line = f.get("line", 1) if isinstance(f, dict) else getattr(f, "line", 1)
111
+ try:
112
+ line_num = max(1, int(raw_line))
113
+ except (ValueError, TypeError):
114
+ line_num = 1
115
+
116
+ raw_ev = f.get("evidence", "") if isinstance(f, dict) else getattr(f, "evidence", "")
117
+ raw_msg = f.get("message", "") if isinstance(f, dict) else getattr(f, "message", "")
118
+ raw_rec = f.get("recommendation", "") if isinstance(f, dict) else getattr(f, "recommendation", "")
119
+ raw_cat = f.get("category", "secrets") if isinstance(f, dict) else getattr(f, "category", "secrets")
120
+
121
+ # PRIVACY GUARANTEE: Redact any sensitive secret values from all text fields
122
+ redacted_evidence = redact_text(str(raw_ev))
123
+ redacted_msg = redact_text(str(raw_msg))
124
+
125
+ if redacted_evidence and redacted_evidence not in redacted_msg:
126
+ full_msg = f"{redacted_msg} Evidence: {redacted_evidence}"
127
+ else:
128
+ full_msg = redacted_msg
129
+
130
+ result_obj = {
131
+ "ruleId": r_id,
132
+ "ruleIndex": rule_idx,
133
+ "level": level,
134
+ "message": {
135
+ "text": full_msg,
136
+ },
137
+ "locations": [
138
+ {
139
+ "physicalLocation": {
140
+ "artifactLocation": {
141
+ "uri": norm_file,
142
+ "uriBaseId": "%SRCROOT%",
143
+ },
144
+ "region": {
145
+ "startLine": line_num,
146
+ "startColumn": 1,
147
+ "snippet": {
148
+ "text": redacted_evidence,
149
+ },
150
+ },
151
+ }
152
+ }
153
+ ],
154
+ "properties": {
155
+ "recommendation": str(raw_rec),
156
+ "category": str(raw_cat),
157
+ },
158
+ }
159
+ results.append(result_obj)
160
+
161
+ driver_version = str(report_dict.get("version", VERSION))
162
+
163
+ return {
164
+ "$schema": SARIF_SCHEMA_URI,
165
+ "version": SARIF_VERSION,
166
+ "runs": [
167
+ {
168
+ "tool": {
169
+ "driver": {
170
+ "name": "Git Security Sentinel",
171
+ "semanticVersion": driver_version,
172
+ "informationUri": "https://github.com/INSVikrant54/Git-Security-Sentinel",
173
+ "rules": driver_rules,
174
+ }
175
+ },
176
+ "results": results,
177
+ }
178
+ ],
179
+ }
File without changes
@@ -0,0 +1,15 @@
1
+ """
2
+ Git Security Sentinel - Redaction Package.
3
+ """
4
+
5
+ from git_security_sentinel.redaction.redactor import (
6
+ IGNORE_PLACEHOLDERS,
7
+ is_benign_generic_value,
8
+ redact_text,
9
+ )
10
+
11
+ __all__ = [
12
+ "IGNORE_PLACEHOLDERS",
13
+ "is_benign_generic_value",
14
+ "redact_text",
15
+ ]
@@ -0,0 +1,313 @@
1
+ """
2
+ Git Security Sentinel - Redaction Engine.
3
+ Deterministically sanitizes sensitive tokens, credentials, and secrets
4
+ from diff lines, output reports, and AI review prompts.
5
+ """
6
+
7
+ import re
8
+ from typing import Set
9
+
10
+ # Common benign strings / placeholders to ignore for generic credential rules
11
+ IGNORE_PLACEHOLDERS: Set[str] = {
12
+ "your_api_key",
13
+ "your_key_here",
14
+ "your_secret_here",
15
+ "your_password",
16
+ "your_api_key_here",
17
+ "change_me",
18
+ "changeme",
19
+ "placeholder",
20
+ "test_secret",
21
+ "example_key",
22
+ "dummy_password",
23
+ "sample_token",
24
+ "xxxx",
25
+ "xxxxxx",
26
+ "********",
27
+ "undefined",
28
+ "process.env",
29
+ "os.environ",
30
+ "${",
31
+ "{{",
32
+ "true",
33
+ "false",
34
+ "null",
35
+ "none",
36
+ }
37
+
38
+
39
+ def is_benign_generic_value(val: str) -> bool:
40
+ """Check if matched generic credential value is a common placeholder or template."""
41
+ clean = val.strip().lower()
42
+ if clean in IGNORE_PLACEHOLDERS:
43
+ return True
44
+ if any(clean.startswith(prefix) for prefix in ("your_", "example_", "test_")):
45
+ return True
46
+ if any(marker in clean for marker in ("${", "{{", "process.env", "os.environ", "os.getenv")):
47
+ return True
48
+ # If it contains only identical repeating characters (e.g. 'xxxxxxxx')
49
+ if len(set(clean)) <= 2:
50
+ return True
51
+ return False
52
+
53
+
54
+ def redact_text(line: str) -> str:
55
+ """
56
+ Deterministically redacts detected secrets within a line before reporting or saving diffs.
57
+ Returns the sanitized line with sensitive tokens replaced with [REDACTED:<TYPE>].
58
+ Ensures negative lookahead protection prevents generic credential rules from clobbering
59
+ previously redacted vendor tokens.
60
+ """
61
+ sanitized = line
62
+
63
+ # Step 1: Multi-line / Cryptographic Blocks
64
+ sanitized = re.sub(
65
+ r"-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP )?PRIVATE KEY(?: BLOCK)?-----(?:.*?-----END [A-Z ]+-----|[^\r\n]*)",
66
+ "[REDACTED:PRIVATE_KEY]",
67
+ sanitized,
68
+ flags=re.DOTALL,
69
+ )
70
+
71
+ # Step 2: Multi-segment Structured Tokens
72
+ # JSON Web Token (JWT)
73
+ sanitized = re.sub(
74
+ r"\b(eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,})\b",
75
+ "[REDACTED:JWT_TOKEN]",
76
+ sanitized,
77
+ )
78
+
79
+ # SendGrid API Key
80
+ sanitized = re.sub(
81
+ r"\b(SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43})\b",
82
+ "[REDACTED:SENDGRID_KEY]",
83
+ sanitized,
84
+ )
85
+
86
+ # Firebase FCM Server Key
87
+ sanitized = re.sub(
88
+ r"\b(AAAA[a-zA-Z0-9_-]{7}:[a-zA-Z0-9_-]{140})\b",
89
+ "[REDACTED:FIREBASE_KEY]",
90
+ sanitized,
91
+ )
92
+
93
+ # Discord Bot Token
94
+ sanitized = re.sub(
95
+ r"\b([MN][A-Za-z\d]{23,26}\.[A-Za-z\d\-_]{6}\.[A-Za-z\d\-_]{27,38})\b",
96
+ "[REDACTED:DISCORD_TOKEN]",
97
+ sanitized,
98
+ )
99
+
100
+ # Telegram Bot API Token
101
+ sanitized = re.sub(
102
+ r"\b([0-9]{8,10}:[a-zA-Z0-9_-]{35})\b",
103
+ "[REDACTED:TELEGRAM_TOKEN]",
104
+ sanitized,
105
+ )
106
+
107
+ # Mailchimp API Key
108
+ sanitized = re.sub(
109
+ r"\b([0-9a-f]{32}-us[0-9]{1,2})\b",
110
+ "[REDACTED:MAILCHIMP_KEY]",
111
+ sanitized,
112
+ )
113
+
114
+ # Step 3: Vendor Prefixed Tokens
115
+ # AWS Access Key ID
116
+ sanitized = re.sub(
117
+ r"\b((?:AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16})\b",
118
+ "[REDACTED:AWS_KEY_ID]",
119
+ sanitized,
120
+ )
121
+
122
+ # GitHub Tokens (PAT, OAuth, fine-grained)
123
+ sanitized = re.sub(
124
+ r"\b((?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,255}|github_pat_[A-Za-z0-9_]{82})\b",
125
+ "[REDACTED:GITHUB_TOKEN]",
126
+ sanitized,
127
+ )
128
+
129
+ # Slack Tokens
130
+ sanitized = re.sub(
131
+ r"\b(xox[baprs]-[0-9A-Za-z]{10,48}-[0-9A-Za-z]{10,48}-[0-9A-Za-z]{24,48}|xox[baprs]-[0-9A-Za-z]{10,48})\b",
132
+ "[REDACTED:SLACK_TOKEN]",
133
+ sanitized,
134
+ )
135
+
136
+ # Stripe Secret or Restricted Key
137
+ sanitized = re.sub(
138
+ r"\b((?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,99})\b",
139
+ "[REDACTED:STRIPE_KEY]",
140
+ sanitized,
141
+ )
142
+
143
+ # Twilio Account SID or API Key
144
+ sanitized = re.sub(
145
+ r"\b(AC[0-9a-fA-F]{32}|SK[0-9a-fA-F]{32})\b",
146
+ "[REDACTED:TWILIO_KEY]",
147
+ sanitized,
148
+ )
149
+
150
+ # Mailgun API Key
151
+ sanitized = re.sub(
152
+ r"\b(key-[0-9a-zA-Z]{32})\b",
153
+ "[REDACTED:MAILGUN_KEY]",
154
+ sanitized,
155
+ )
156
+
157
+ # Square Access Token / Secret
158
+ sanitized = re.sub(
159
+ r"\b(sq0atp-[0-9A-Za-z\-_]{22}|sq0csp-[0-9A-Za-z\-_]{43}|EAAA[a-zA-Z0-9]{60})\b",
160
+ "[REDACTED:SQUARE_TOKEN]",
161
+ sanitized,
162
+ )
163
+
164
+ # Shopify Access Token
165
+ sanitized = re.sub(
166
+ r"\b(shpat_[a-fA-F0-9]{32}|shpca_[a-fA-F0-9]{32}|shppa_[a-fA-F0-9]{32})\b",
167
+ "[REDACTED:SHOPIFY_TOKEN]",
168
+ sanitized,
169
+ )
170
+
171
+ # npm Access Token
172
+ sanitized = re.sub(
173
+ r"\b(npm_[A-Za-z0-9]{36})\b",
174
+ "[REDACTED:NPM_TOKEN]",
175
+ sanitized,
176
+ )
177
+
178
+ # PyPI Upload Token
179
+ sanitized = re.sub(
180
+ r"\b(pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,1000})\b",
181
+ "[REDACTED:PYPI_TOKEN]",
182
+ sanitized,
183
+ )
184
+
185
+ # New Relic Keys (NRAK, NRII, or hex license key)
186
+ sanitized = re.sub(
187
+ r"(?i)\b(NRAK-[A-Z0-9]{27}|NRII-[A-Za-z0-9_-]{32})\b",
188
+ "[REDACTED:NEWRELIC_KEY]",
189
+ sanitized,
190
+ )
191
+ sanitized = re.sub(
192
+ r"""(?i)((?:newrelic|new_relic)[\-_]?(?:license|api)?[\-_]?key\s*[:=]\s*['"]?)[a-f0-9]{40}(['"]?)""",
193
+ r"\1[REDACTED:NEWRELIC_KEY]\2",
194
+ sanitized,
195
+ )
196
+
197
+ # Step 4: Connection Strings with Embedded Credentials
198
+ # MongoDB URI
199
+ sanitized = re.sub(
200
+ r"""\b(mongodb(?:\+srv)?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b""",
201
+ "[REDACTED:MONGODB_URI]",
202
+ sanitized,
203
+ )
204
+
205
+ # PostgreSQL URI
206
+ sanitized = re.sub(
207
+ r"""\b(postgres(?:ql)?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b""",
208
+ "[REDACTED:POSTGRES_URI]",
209
+ sanitized,
210
+ )
211
+
212
+ # MySQL URI
213
+ sanitized = re.sub(
214
+ r"""\b(mysql://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b""",
215
+ "[REDACTED:MYSQL_URI]",
216
+ sanitized,
217
+ )
218
+
219
+ # Redis URI
220
+ sanitized = re.sub(
221
+ r"""\b(rediss?://(?:[^:\s]*:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b""",
222
+ "[REDACTED:REDIS_URI]",
223
+ sanitized,
224
+ )
225
+
226
+ # AMQP / RabbitMQ URI
227
+ sanitized = re.sub(
228
+ r"""\b(amqps?://(?:[^:\s]+:[^@\s]+@)[a-zA-Z0-9.-]+(?::\d+)?(?:/[^\s'"]*)?)\b""",
229
+ "[REDACTED:AMQP_URI]",
230
+ sanitized,
231
+ )
232
+
233
+ # Basic Auth in URL
234
+ sanitized = re.sub(
235
+ r"""(?i)(https?://(?![^:\s]+:[^@\s]+@(?:localhost|127\.0\.0\.1)))(?:[a-zA-Z0-9_\-\.%]+):(?:[a-zA-Z0-9_\-\.%]+)@""",
236
+ r"\1[REDACTED:BASIC_AUTH]@",
237
+ sanitized,
238
+ )
239
+
240
+ # Basic Auth in Header
241
+ sanitized = re.sub(
242
+ r"""(?i)(\bAuthorization\s*:\s*Basic\s+)[A-Za-z0-9+/=]{16,}""",
243
+ r"\1[REDACTED:BASIC_AUTH]",
244
+ sanitized,
245
+ )
246
+
247
+ # Bearer Token in Header
248
+ sanitized = re.sub(
249
+ r"""(?i)(\bBearer\s+['"]?)(?!\[REDACTED:)[A-Za-z0-9_\-\.+=]{20,}(['"]?)""",
250
+ r"\1[REDACTED:BEARER_TOKEN]\2",
251
+ sanitized,
252
+ )
253
+
254
+ # GCP Service Account Private Key ID
255
+ sanitized = re.sub(
256
+ r"""("private_key_id"\s*:\s*")[a-f0-9]{40}(")""",
257
+ r"\1[REDACTED:GCP_SERVICE_ACCOUNT]\2",
258
+ sanitized,
259
+ )
260
+
261
+ # Step 5: Assignment-based Secrets
262
+ # AWS Secret Access Key assignment
263
+ sanitized = re.sub(
264
+ r"""(?i)((?:aws_secret_access_key|aws_secret_key|aws_secret)\s*[:=]\s*['"]?)[A-Za-z0-9/+=]{40}(['"]?)""",
265
+ r"\1[REDACTED:AWS_SECRET]\2",
266
+ sanitized,
267
+ )
268
+
269
+ # Heroku API Key
270
+ sanitized = re.sub(
271
+ 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}(['"]?)""",
272
+ r"\1[REDACTED:HEROKU_KEY]\2",
273
+ sanitized,
274
+ )
275
+
276
+ # Datadog API/App Key
277
+ sanitized = re.sub(
278
+ r"""(?i)((?:datadog|dd)[\-_]?(?:api|app)[\-_]?key\s*[:=]\s*['"]?)[a-f0-9]{32,40}(['"]?)""",
279
+ r"\1[REDACTED:DATADOG_KEY]\2",
280
+ sanitized,
281
+ )
282
+
283
+ # Hardcoded Temporary Credentials
284
+ def redact_temp_match(m: re.Match) -> str:
285
+ prefix = m.group(1)
286
+ val = m.group(2)
287
+ suffix = m.group(3)
288
+ if is_benign_generic_value(val) or val.startswith("[REDACTED:"):
289
+ return m.group(0)
290
+ return f"{prefix}[REDACTED:TEMP_CREDENTIAL]{suffix}"
291
+
292
+ sanitized = re.sub(
293
+ r"""(?i)((?:temp_password|temporary_password|test_credential|staging_token|mock_secret)\s*[:=]\s*['"])((?!\[REDACTED:)[^'"\r\n]{8,64})(['"])""",
294
+ redact_temp_match,
295
+ sanitized,
296
+ )
297
+
298
+ # Step 6: Generic Credential Catch-All (with Negative Lookahead Protection)
299
+ def redact_generic_match(m: re.Match) -> str:
300
+ prefix = m.group(1)
301
+ val = m.group(2)
302
+ suffix = m.group(3)
303
+ if is_benign_generic_value(val) or val.startswith("[REDACTED:"):
304
+ return m.group(0)
305
+ return f"{prefix}[REDACTED:GENERIC_CREDENTIAL]{suffix}"
306
+
307
+ sanitized = re.sub(
308
+ r"""(?i)((?:password|passwd|api_key|apikey|secret_key|client_secret|auth_token|access_token)\s*[:=]\s*['"])((?!\[REDACTED:)[^'"\r\n]{8,128})(['"])""",
309
+ redact_generic_match,
310
+ sanitized,
311
+ )
312
+
313
+ return sanitized
@@ -0,0 +1,29 @@
1
+ """
2
+ Git Security Sentinel - Detection Rules Package.
3
+ """
4
+
5
+ from git_security_sentinel.core.models import Rule
6
+ from git_security_sentinel.rules.catalog import (
7
+ ALL_RULES,
8
+ get_all_rules,
9
+ get_rule_by_id,
10
+ )
11
+ from git_security_sentinel.rules.cloud import CLOUD_RULES
12
+ from git_security_sentinel.rules.auth import AUTH_RULES
13
+ from git_security_sentinel.rules.database import DATABASE_RULES
14
+ from git_security_sentinel.rules.sensitive_files import SENSITIVE_FILES_RULES
15
+ from git_security_sentinel.rules.insecure_code import INSECURE_CODE_RULES
16
+ from git_security_sentinel.rules.compliance import COMPLIANCE_RULES
17
+
18
+ __all__ = [
19
+ "ALL_RULES",
20
+ "AUTH_RULES",
21
+ "CLOUD_RULES",
22
+ "COMPLIANCE_RULES",
23
+ "DATABASE_RULES",
24
+ "INSECURE_CODE_RULES",
25
+ "Rule",
26
+ "SENSITIVE_FILES_RULES",
27
+ "get_all_rules",
28
+ "get_rule_by_id",
29
+ ]
@@ -0,0 +1,103 @@
1
+ """
2
+ Git Security Sentinel - Authentication Tokens Detection Rules.
3
+ Covers GitHub PAT/OAuth, Slack, JWT, Bearer tokens, Basic Auth, npm, PyPI, Discord bot, Telegram bot.
4
+ """
5
+
6
+ import re
7
+ from typing import List
8
+
9
+ from git_security_sentinel.core.models import Rule
10
+
11
+ AUTH_RULES: List[Rule] = [
12
+ Rule(
13
+ rule_id="secret.github_token",
14
+ category="secrets",
15
+ severity="critical",
16
+ confidence="high",
17
+ pattern=re.compile(
18
+ r"\b((?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,255}|github_pat_[A-Za-z0-9_]{82})\b"
19
+ ),
20
+ message="Potential GitHub personal access or OAuth token detected.",
21
+ recommendation="Revoke and rotate the GitHub token immediately in GitHub Settings > Developer settings.",
22
+ ),
23
+ Rule(
24
+ rule_id="secret.slack_token",
25
+ category="secrets",
26
+ severity="high",
27
+ confidence="high",
28
+ pattern=re.compile(
29
+ r"\b(xox[baprs]-[0-9A-Za-z]{10,48}-[0-9A-Za-z]{10,48}-[0-9A-Za-z]{24,48}|xox[baprs]-[0-9A-Za-z]{10,48})\b"
30
+ ),
31
+ message="Potential Slack API bot or user token detected.",
32
+ recommendation="Revoke the Slack token via the Slack API admin dashboard and use secure secret injection.",
33
+ ),
34
+ Rule(
35
+ rule_id="secret.jwt_token",
36
+ category="secrets",
37
+ severity="high",
38
+ confidence="high",
39
+ pattern=re.compile(
40
+ r"\b(eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,})\b"
41
+ ),
42
+ message="Potential hardcoded JSON Web Token (JWT) detected.",
43
+ recommendation="Never hardcode JWT tokens. Issue tokens dynamically and verify signature at runtime.",
44
+ ),
45
+ Rule(
46
+ rule_id="secret.bearer_token",
47
+ category="secrets",
48
+ severity="high",
49
+ confidence="medium",
50
+ pattern=re.compile(r"""(?i)\bBearer\s+['"]?([A-Za-z0-9_\-\.+=]{20,})['"]?"""),
51
+ message="Potential hardcoded Bearer token detected in HTTP authorization header or configuration.",
52
+ recommendation="Inject bearer tokens via secure runtime environment variables or vault secrets.",
53
+ ),
54
+ Rule(
55
+ rule_id="secret.basic_auth",
56
+ category="secrets",
57
+ severity="high",
58
+ confidence="high",
59
+ pattern=re.compile(
60
+ r"""(?i)https?://(?![^:\s]+:[^@\s]+@(?:localhost|127\.0\.0\.1))([a-zA-Z0-9_\-\.%]+):([a-zA-Z0-9_\-\.%]+)@|\bAuthorization\s*:\s*Basic\s+([A-Za-z0-9+/=]{16,})"""
61
+ ),
62
+ message="Cleartext Basic Authentication credentials embedded in URL or header detected.",
63
+ recommendation="Remove cleartext credentials from URLs and headers. Use credential helpers or secret injection.",
64
+ ),
65
+ Rule(
66
+ rule_id="secret.npm_token",
67
+ category="secrets",
68
+ severity="critical",
69
+ confidence="high",
70
+ pattern=re.compile(r"\b(npm_[A-Za-z0-9]{36})\b"),
71
+ message="Potential npm access token detected.",
72
+ recommendation="Revoke npm access token immediately with 'npm token revoke' and regenerate in npmjs.com.",
73
+ ),
74
+ Rule(
75
+ rule_id="secret.pypi_token",
76
+ category="secrets",
77
+ severity="critical",
78
+ confidence="high",
79
+ pattern=re.compile(r"\b(pypi-AgEIcHlwaS5vcmc[A-Za-z0-9\-_]{50,1000})\b"),
80
+ message="Potential PyPI API upload token detected.",
81
+ recommendation="Revoke PyPI token immediately in PyPI Account Settings > API tokens.",
82
+ ),
83
+ Rule(
84
+ rule_id="secret.discord_bot_token",
85
+ category="secrets",
86
+ severity="critical",
87
+ confidence="high",
88
+ pattern=re.compile(
89
+ r"\b([MN][A-Za-z\d]{23,26}\.[A-Za-z\d\-_]{6}\.[A-Za-z\d\-_]{27,38})\b"
90
+ ),
91
+ message="Potential Discord Bot authentication token detected.",
92
+ recommendation="Reset Discord bot token immediately in the Discord Developer Portal.",
93
+ ),
94
+ Rule(
95
+ rule_id="secret.telegram_bot_token",
96
+ category="secrets",
97
+ severity="critical",
98
+ confidence="high",
99
+ pattern=re.compile(r"\b([0-9]{8,10}:[a-zA-Z0-9_-]{35})\b"),
100
+ message="Potential Telegram Bot API HTTP token detected.",
101
+ recommendation="Revoke Telegram bot token via BotFather with /revoke.",
102
+ ),
103
+ ]