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,99 @@
1
+ """
2
+ Git Security Sentinel - Inline Comment Suppression Filter.
3
+ Supports line-level suppression directives (# nosec, # sentinel:ignore, //, --)
4
+ with optional rule-specific identifiers and aliases.
5
+ """
6
+
7
+ import re
8
+ from typing import Optional, Set, Tuple
9
+
10
+ # Matches comment markers (#, //, --) followed by nosec or sentinel:ignore
11
+ # and optional rule identifiers up to end of line.
12
+ SUPPRESSION_REGEX = re.compile(
13
+ r"""(?i)(?:#|//|--)\s*(?:nosec|sentinel:ignore)(?:[:\s]+([A-Za-z0-9_\-.,;\s]+))?\s*$"""
14
+ )
15
+
16
+
17
+ def normalize_rule_identifier(rule_id: str) -> str:
18
+ """
19
+ Normalizes rule identifiers to lowercase with hyphens.
20
+ E.g., 'secret.aws_access_key' -> 'secret-aws-access-key'
21
+ 'aws-access-key' -> 'aws-access-key'
22
+ """
23
+ return rule_id.strip().lower().replace("_", "-").replace(".", "-")
24
+
25
+
26
+ def parse_inline_suppression(line_content: str) -> Optional[Tuple[bool, Set[str]]]:
27
+ """
28
+ Analyzes line content for inline suppression directives.
29
+
30
+ Returns:
31
+ None: No suppression directive found.
32
+ (True, set()): Blanket suppression (all rules on this line suppressed).
33
+ (False, {rules}): Specific rules suppressed on this line.
34
+ """
35
+ line = line_content.rstrip("\r\n")
36
+ match = SUPPRESSION_REGEX.search(line)
37
+ if not match:
38
+ return None
39
+
40
+ raw_rules = match.group(1)
41
+ if not raw_rules or not raw_rules.strip():
42
+ return (True, set())
43
+
44
+ # Parse comma, semicolon, or space-separated rule list
45
+ tokens = re.split(r"[,;\s]+", raw_rules)
46
+ rules = {normalize_rule_identifier(t) for t in tokens if t.strip()}
47
+
48
+ if not rules:
49
+ return (True, set())
50
+
51
+ return (False, rules)
52
+
53
+
54
+ def is_rule_suppressed(rule_id: str, suppression: Optional[Tuple[bool, Set[str]]]) -> bool:
55
+ """
56
+ Determines whether a specific rule_id is suppressed given parsed suppression directives.
57
+
58
+ Normalizes rule identifiers so case, '.', '_', and '-' are treated interchangeably.
59
+ Supports exact match, rule alias match (e.g. 'aws-access-key' matches 'secret.aws_access_key'),
60
+ category prefix match (e.g. 'secret' matches 'secret.aws_access_key'), and reverse alias matching.
61
+ Requires strict segment/boundary matching to prevent unbounded suffix overmatching
62
+ (e.g. 'en' does NOT match 'secret.github_token', and 'key' does NOT match 'secret.stripe_key').
63
+ """
64
+ if not suppression:
65
+ return False
66
+
67
+ is_blanket, rules = suppression
68
+ if is_blanket:
69
+ return True
70
+
71
+ norm_id = normalize_rule_identifier(rule_id)
72
+
73
+ # Determine rule name alias without category prefix (e.g. 'aws-access-key' from 'secret.aws_access_key')
74
+ rule_name_alias = None
75
+ if "." in rule_id:
76
+ rule_name_alias = normalize_rule_identifier(rule_id.split(".", 1)[1])
77
+ else:
78
+ for cat in ("secret-", "file-", "insecure-code-", "risk-", "compliance-"):
79
+ if norm_id.startswith(cat):
80
+ rule_name_alias = norm_id[len(cat):]
81
+ break
82
+
83
+ for raw_r in rules:
84
+ r = normalize_rule_identifier(raw_r)
85
+ if norm_id == r:
86
+ return True
87
+ # Exact rule alias match (e.g. 'aws-access-key' matches 'secret.aws_access_key')
88
+ if rule_name_alias and r == rule_name_alias:
89
+ return True
90
+ # Category prefix match (e.g. 'secret' matches 'secret-aws-access-key')
91
+ if norm_id.startswith(r + "-"):
92
+ return True
93
+ # Reverse alias matching: directive contains full ID while rule_id was short alias
94
+ if rule_name_alias and (r.endswith("." + rule_name_alias) or r.endswith("-" + rule_name_alias)):
95
+ return True
96
+ if r.endswith("." + norm_id) or any(r == f"{c}{norm_id}" for c in ("secret-", "file-", "insecure-code-", "risk-", "compliance-")):
97
+ return True
98
+
99
+ return False
@@ -0,0 +1,154 @@
1
+ """
2
+ Git Security Sentinel - Path Exclusions and .sentinelignore Filter.
3
+ Pure Python standard library implementation of gitignore-style glob matching.
4
+ """
5
+
6
+ from pathlib import Path
7
+ import re
8
+ from typing import List, Optional, Pattern, Tuple
9
+
10
+
11
+ class PathFilter:
12
+ """
13
+ Gitignore-style path filter using Python standard library regex.
14
+ Supports glob patterns (*, **, ?), root anchoring (/), bare directory matching,
15
+ negation (!), and comment stripping. Rules are evaluated sequentially.
16
+ """
17
+
18
+ def __init__(self, patterns: Optional[List[str]] = None):
19
+ self.patterns: List[str] = patterns or []
20
+ self._rules: List[Tuple[bool, Pattern]] = []
21
+ self._regexes: List[Pattern] = []
22
+ for pat in self.patterns:
23
+ rule = self._parse_rule(pat)
24
+ if rule is not None:
25
+ self._rules.append(rule)
26
+ self._regexes.append(rule[1])
27
+
28
+ @classmethod
29
+ def _parse_rule(cls, pattern: str) -> Optional[Tuple[bool, Pattern]]:
30
+ """
31
+ Parses a pattern into an (is_negative, compiled_regex) tuple.
32
+ Returns None for comments and empty lines.
33
+ """
34
+ p = pattern.strip().replace("\\", "/")
35
+ if not p or p.startswith("#"):
36
+ return None
37
+
38
+ is_negative = False
39
+ if p.startswith("!"):
40
+ is_negative = True
41
+ p = p[1:].strip()
42
+ if not p:
43
+ return None
44
+ elif p.startswith(r"\!"):
45
+ p = p[1:]
46
+
47
+ rx = cls._pattern_to_regex(p)
48
+ if rx is None:
49
+ return None
50
+ return (is_negative, rx)
51
+
52
+ @staticmethod
53
+ def _pattern_to_regex(pattern: str) -> Optional[Pattern]:
54
+ """
55
+ Converts a gitignore-style pattern into a compiled regex.
56
+ Supports bare directories, trailing slashes, globs (*, **, ?), and root anchoring.
57
+ Returns None for comments and empty lines.
58
+ """
59
+ p = pattern.strip().replace("\\", "/")
60
+ if not p or p.startswith("#"):
61
+ return None
62
+
63
+ if p.startswith("!"):
64
+ p = p[1:].strip()
65
+ if not p:
66
+ return None
67
+ elif p.startswith(r"\!"):
68
+ p = p[1:]
69
+
70
+ anchored = p.startswith("/")
71
+ if anchored:
72
+ p = p[1:]
73
+
74
+ is_dir_pattern = False
75
+ if p.endswith("/"):
76
+ is_dir_pattern = True
77
+ p = p[:-1]
78
+ elif "*" not in p and "?" not in p:
79
+ last_segment = p.split("/")[-1]
80
+ if "." not in last_segment:
81
+ is_dir_pattern = True
82
+
83
+ i = 0
84
+ n = len(p)
85
+ res = []
86
+ while i < n:
87
+ c = p[i]
88
+ if c == "*":
89
+ if i + 1 < n and p[i + 1] == "*":
90
+ i += 2
91
+ if i < n and p[i] == "/":
92
+ i += 1
93
+ res.append("(?:.*/)?")
94
+ else:
95
+ res.append(".*")
96
+ else:
97
+ i += 1
98
+ res.append("[^/]*")
99
+ elif c == "?":
100
+ i += 1
101
+ res.append("[^/]")
102
+ elif c in r"\.+^$()[]{}|":
103
+ i += 1
104
+ res.append("\\" + c)
105
+ else:
106
+ i += 1
107
+ res.append(c)
108
+
109
+ regex_str = "".join(res)
110
+ suffix = "(?:/.*)?$" if is_dir_pattern else "$"
111
+ if anchored:
112
+ return re.compile(f"^{regex_str}{suffix}")
113
+ return re.compile(f"(?:^|/){regex_str}{suffix}")
114
+
115
+ def is_ignored(self, path: str) -> bool:
116
+ """
117
+ Checks whether the given path matches any ignore pattern,
118
+ evaluating rules sequentially in order of appearance.
119
+ A pattern starting with '!' un-ignores matching paths.
120
+ """
121
+ norm_path = path.replace("\\", "/").strip().lstrip("/")
122
+ if norm_path.startswith("./"):
123
+ norm_path = norm_path[2:]
124
+
125
+ ignored = False
126
+ for is_negative, rx in self._rules:
127
+ if rx.search(norm_path) is not None:
128
+ ignored = not is_negative
129
+ return ignored
130
+
131
+ def should_ignore(self, path: str) -> bool:
132
+ """Alias for is_ignored."""
133
+ return self.is_ignored(path)
134
+
135
+
136
+ def load_sentinelignore(repo_root: Path) -> List[str]:
137
+ """
138
+ Loads ignore patterns from .sentinelignore at the repository root if present.
139
+ """
140
+ root = Path(repo_root)
141
+ ignore_file = root / ".sentinelignore"
142
+ if not ignore_file.is_file():
143
+ return []
144
+
145
+ try:
146
+ content = ignore_file.read_text(encoding="utf-8", errors="replace")
147
+ patterns = []
148
+ for line in content.splitlines():
149
+ line = line.strip()
150
+ if line and not line.startswith("#"):
151
+ patterns.append(line)
152
+ return patterns
153
+ except Exception:
154
+ return []
@@ -0,0 +1,27 @@
1
+ """
2
+ Git Security Sentinel - PyPI Update Checker and Self-Upgrade.
3
+ """
4
+
5
+ from git_security_sentinel.update.checker import (
6
+ CACHE_EXPIRY_SEC,
7
+ DEFAULT_PYPI_JSON_URL,
8
+ PACKAGE_NAME,
9
+ check_for_pypi_update,
10
+ get_cache_file_path,
11
+ print_update_banner,
12
+ pypi_json_url,
13
+ PYPI_JSON_URL,
14
+ run_self_upgrade,
15
+ )
16
+
17
+ __all__ = [
18
+ "CACHE_EXPIRY_SEC",
19
+ "DEFAULT_PYPI_JSON_URL",
20
+ "PACKAGE_NAME",
21
+ "check_for_pypi_update",
22
+ "get_cache_file_path",
23
+ "print_update_banner",
24
+ "pypi_json_url",
25
+ "PYPI_JSON_URL",
26
+ "run_self_upgrade",
27
+ ]
@@ -0,0 +1,189 @@
1
+ """
2
+ Git Security Sentinel - PyPI Self-Update and Cached Update Checker.
3
+ Zero external dependencies.
4
+ """
5
+
6
+ import hashlib
7
+ import json
8
+ import os
9
+ from pathlib import Path
10
+ import re
11
+ import subprocess
12
+ import sys
13
+ import time
14
+ from typing import Optional, Tuple
15
+ import urllib.error
16
+ import urllib.parse
17
+ import urllib.request
18
+
19
+ PACKAGE_NAME = "git-security-sentinel"
20
+ DEFAULT_PYPI_JSON_URL = f"https://pypi.org/pypi/{PACKAGE_NAME}/json"
21
+ PYPI_JSON_URL = DEFAULT_PYPI_JSON_URL # backwards-compatible alias
22
+ CACHE_EXPIRY_SEC = 86400 # 24 hours
23
+
24
+ _PRE_STAGES = {"dev": -1, "a": 0, "alpha": 0, "b": 1, "beta": 1, "rc": 2, "c": 2, "post": 4}
25
+ _FINAL_STAGE = 3
26
+
27
+
28
+ def pypi_json_url(index_url: Optional[str] = None) -> str:
29
+ """Resolve the JSON metadata URL for PyPI, TestPyPI, or a custom index."""
30
+ if not index_url:
31
+ return DEFAULT_PYPI_JSON_URL
32
+ cleaned = index_url.strip().rstrip("/")
33
+ if "/pypi/" in cleaned and cleaned.endswith("/json"):
34
+ return cleaned
35
+ parsed = urllib.parse.urlparse(cleaned)
36
+ if parsed.scheme and parsed.netloc:
37
+ root = f"{parsed.scheme}://{parsed.netloc}"
38
+ else:
39
+ root = "https://pypi.org"
40
+ return f"{root}/pypi/{PACKAGE_NAME}/json"
41
+
42
+
43
+ def get_cache_file_path(index_url: Optional[str] = None) -> Path:
44
+ """Returns the platform-specific cache path, isolated per package index."""
45
+ if os.name == "nt":
46
+ app_data = os.environ.get("LOCALAPPDATA")
47
+ base = Path(app_data) if app_data else (Path.home() / "AppData" / "Local")
48
+ else:
49
+ xdg_cache = os.environ.get("XDG_CACHE_HOME")
50
+ base = Path(xdg_cache) if xdg_cache else (Path.home() / ".cache")
51
+
52
+ cache_dir = base / "git-security-sentinel"
53
+ try:
54
+ cache_dir.mkdir(parents=True, exist_ok=True)
55
+ except Exception:
56
+ pass
57
+ if index_url:
58
+ host = urllib.parse.urlparse(index_url).netloc or index_url
59
+ suffix = hashlib.sha1(host.encode("utf-8")).hexdigest()[:10]
60
+ return cache_dir / f"update_check_{suffix}.json"
61
+ return cache_dir / "update_check.json"
62
+
63
+
64
+ def _parse_version(value: str) -> Tuple[Tuple[int, ...], int, int]:
65
+ """Parse a simple PEP 440-style version into (release, pre-stage, stage-number)."""
66
+ match = re.match(
67
+ r"^\s*(\d+(?:\.\d+)*)[-_.]?(dev|a|alpha|b|beta|rc|c|post)[-_.]?(\d*)",
68
+ value.strip().lower(),
69
+ )
70
+ if not match:
71
+ match = re.match(r"^\s*(\d+(?:\.\d+)*)", value.strip().lower())
72
+ if not match:
73
+ return (0,), _FINAL_STAGE, 0
74
+ release = tuple(int(piece) for piece in match.group(1).split("."))
75
+ release = release + (0,) * (4 - len(release)) if len(release) < 4 else release
76
+ stage_name = match.group(2) if len(match.groups()) > 2 else None
77
+ stage = _PRE_STAGES.get(stage_name, _FINAL_STAGE) if stage_name else _FINAL_STAGE
78
+ number = int(match.group(3) or 0) if len(match.groups()) > 2 else 0
79
+ return release, stage, number
80
+
81
+
82
+ def _is_newer_version(latest: str, current: str) -> bool:
83
+ """Compare versions, treating pre-releases as older than the final release."""
84
+ try:
85
+ return _parse_version(latest) > _parse_version(current)
86
+ except Exception:
87
+ return False
88
+
89
+
90
+ def check_for_pypi_update(
91
+ current_version: str,
92
+ no_check: bool = False,
93
+ index_url: Optional[str] = None,
94
+ ) -> Optional[str]:
95
+ """
96
+ Checks if a newer version of git-security-sentinel is available on the package index.
97
+ Caches results for 24 hours to prevent repeated network queries.
98
+ Never blocks or crashes on network failure.
99
+
100
+ Returns:
101
+ Optional[str]: latest_version string if an update is available, else None.
102
+ """
103
+ if no_check:
104
+ return None
105
+
106
+ # Bypass in CI environments and if disabled via environment variable
107
+ if (
108
+ os.environ.get("CI") == "true"
109
+ or os.environ.get("GITHUB_ACTIONS") == "true"
110
+ or os.environ.get("SENTINEL_NO_UPDATE_CHECK") == "1"
111
+ ):
112
+ return None
113
+
114
+ cache_file = get_cache_file_path(index_url)
115
+ now = time.time()
116
+ latest_version: Optional[str] = None
117
+
118
+ if cache_file.is_file():
119
+ try:
120
+ data = json.loads(cache_file.read_text(encoding="utf-8"))
121
+ last_checked = float(data.get("last_checked", 0))
122
+ if now - last_checked < CACHE_EXPIRY_SEC:
123
+ latest_version = data.get("latest_version")
124
+ except Exception:
125
+ pass
126
+
127
+ if latest_version is None:
128
+ try:
129
+ req = urllib.request.Request(
130
+ pypi_json_url(index_url),
131
+ headers={"User-Agent": f"{PACKAGE_NAME}/{current_version}"},
132
+ )
133
+ with urllib.request.urlopen(req, timeout=1.5) as resp:
134
+ if resp.status == 200:
135
+ payload = json.loads(resp.read().decode("utf-8"))
136
+ latest_version = payload.get("info", {}).get("version")
137
+ try:
138
+ cache_file.write_text(
139
+ json.dumps({
140
+ "last_checked": now,
141
+ "latest_version": latest_version,
142
+ }),
143
+ encoding="utf-8",
144
+ )
145
+ except Exception:
146
+ pass
147
+ except Exception:
148
+ return None
149
+
150
+ if latest_version and _is_newer_version(latest_version, current_version):
151
+ return latest_version
152
+
153
+ return None
154
+
155
+
156
+ def print_update_banner(current_version: str, latest_version: str) -> None:
157
+ """
158
+ Prints a non-intrusive update notification banner strictly to sys.stderr
159
+ so standard output (JSON / SARIF) remains pipeable and valid.
160
+ """
161
+ msg = (
162
+ "\n" + "=" * 62 + "\n"
163
+ f" Update available: {current_version} -> {latest_version:<26}\n"
164
+ " Run 'git scan --update' to update to the latest release.\n"
165
+ + "=" * 62 + "\n"
166
+ )
167
+ sys.stderr.write(msg)
168
+ sys.stderr.flush()
169
+
170
+
171
+ def run_self_upgrade(index_url: Optional[str] = None) -> int:
172
+ """
173
+ Executes pip self-upgrade for git-security-sentinel.
174
+ An optional package index URL supports TestPyPI and private mirrors.
175
+ Returns pip process exit code.
176
+ """
177
+ print(f"[*] Upgrading {PACKAGE_NAME} via pip...")
178
+ cmd = [sys.executable, "-m", "pip", "install", "--upgrade", PACKAGE_NAME]
179
+ if index_url:
180
+ cmd.extend(["--index-url", index_url])
181
+ res = subprocess.run(cmd, check=False)
182
+ if res.returncode == 0:
183
+ print(f"[OK] {PACKAGE_NAME} upgraded successfully.")
184
+ else:
185
+ hint = f"pip install --upgrade {PACKAGE_NAME}"
186
+ if index_url:
187
+ hint += f" --index-url {index_url}"
188
+ print(f"[ERROR] Upgrade failed with exit code {res.returncode}. Please run: {hint}")
189
+ return res.returncode