|
| 1 | +""" |
| 2 | +Security regression tests for CVE fixes. |
| 3 | +
|
| 4 | +Scans the codebase for common security anti-patterns: |
| 5 | +- Known vulnerable dependency version pins in requirements.txt |
| 6 | +- HTTP requests missing timeout parameters |
| 7 | +- HTTP requests with SSL verification disabled (verify=False) |
| 8 | +- Subprocess calls using shell=True without justification |
| 9 | +""" |
| 10 | + |
| 11 | +import ast |
| 12 | +import os |
| 13 | +import re |
| 14 | +from pathlib import Path |
| 15 | +from importlib.metadata import version as pkg_version |
| 16 | +from packaging.version import Version |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | + |
| 21 | +ROOT_DIR = Path(__file__).resolve().parent.parent |
| 22 | +PLUGIN_DIR = ROOT_DIR / "plugins" |
| 23 | + |
| 24 | +# Minimum safe versions for dependencies with known CVEs |
| 25 | +MINIMUM_SAFE_VERSIONS = { |
| 26 | + "pyasn1": "0.6.3", # CVE-2026-30922 |
| 27 | + "cryptography": "44.0.0", |
| 28 | + "jinja2": "3.1.4", |
| 29 | + "pyyaml": "6.0.1", |
| 30 | + "aiohttp": "3.10.11", |
| 31 | + "lxml": "5.3.0", |
| 32 | + "setuptools": "75.0.0", |
| 33 | +} |
| 34 | + |
| 35 | +# Files where shell=True is explicitly acceptable (agent payloads that must |
| 36 | +# execute arbitrary commands by design). |
| 37 | +SHELL_TRUE_ALLOWLIST = { |
| 38 | + "ragdoll.py", |
| 39 | + "manx.py", |
| 40 | + "sandcat.go", |
| 41 | +} |
| 42 | + |
| 43 | +# Files/directories to skip entirely during scanning |
| 44 | +SKIP_DIRS = { |
| 45 | + ".git", "__pycache__", "node_modules", ".eggs", "build", "dist", |
| 46 | + "magma", # frontend JS plugin |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +def _iter_python_files(*search_roots): |
| 51 | + """Yield all .py files under the given roots, skipping irrelevant dirs.""" |
| 52 | + for root in search_roots: |
| 53 | + root = Path(root) |
| 54 | + if not root.exists(): |
| 55 | + continue |
| 56 | + for dirpath, dirnames, filenames in os.walk(root): |
| 57 | + dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS] |
| 58 | + for fn in filenames: |
| 59 | + if fn.endswith(".py"): |
| 60 | + yield Path(dirpath) / fn |
| 61 | + |
| 62 | + |
| 63 | +def _parse_requirements(req_file): |
| 64 | + """Parse a requirements.txt into a dict of {package_name: version_spec}.""" |
| 65 | + reqs = {} |
| 66 | + with open(req_file) as f: |
| 67 | + for line in f: |
| 68 | + line = line.strip().split("#")[0].strip() |
| 69 | + if not line or line.startswith("-"): |
| 70 | + continue |
| 71 | + # Match package==version, package~=version, package>=version |
| 72 | + m = re.match(r"^([A-Za-z0-9_-]+)\s*([~>=<!]+)\s*([^\s;]+)", line) |
| 73 | + if m: |
| 74 | + reqs[m.group(1).lower()] = (m.group(2), m.group(3)) |
| 75 | + return reqs |
| 76 | + |
| 77 | + |
| 78 | +def _find_requests_calls_without_timeout(filepath): |
| 79 | + """Use AST to find requests.get/post/put/patch/delete/head calls missing timeout.""" |
| 80 | + issues = [] |
| 81 | + try: |
| 82 | + source = filepath.read_text(encoding="utf-8", errors="ignore") |
| 83 | + tree = ast.parse(source, filename=str(filepath)) |
| 84 | + except SyntaxError: |
| 85 | + return issues |
| 86 | + |
| 87 | + for node in ast.walk(tree): |
| 88 | + if not isinstance(node, ast.Call): |
| 89 | + continue |
| 90 | + func = node.func |
| 91 | + |
| 92 | + # Match requests.get(...), requests.post(...), etc. |
| 93 | + if isinstance(func, ast.Attribute) and func.attr in ( |
| 94 | + "get", "post", "put", "patch", "delete", "head", "request" |
| 95 | + ): |
| 96 | + # Check if the value is 'requests' or an alias |
| 97 | + if isinstance(func.value, ast.Name) and func.value.id == "requests": |
| 98 | + kwarg_names = [kw.arg for kw in node.keywords] |
| 99 | + if "timeout" not in kwarg_names: |
| 100 | + issues.append((node.lineno, f"requests.{func.attr}() missing timeout")) |
| 101 | + return issues |
| 102 | + |
| 103 | + |
| 104 | +def _find_verify_false(filepath): |
| 105 | + """Find requests calls with verify=False.""" |
| 106 | + issues = [] |
| 107 | + try: |
| 108 | + source = filepath.read_text(encoding="utf-8", errors="ignore") |
| 109 | + tree = ast.parse(source, filename=str(filepath)) |
| 110 | + except SyntaxError: |
| 111 | + return issues |
| 112 | + |
| 113 | + for node in ast.walk(tree): |
| 114 | + if not isinstance(node, ast.Call): |
| 115 | + continue |
| 116 | + for kw in node.keywords: |
| 117 | + if kw.arg == "verify" and isinstance(kw.value, ast.Constant) and kw.value.value is False: |
| 118 | + issues.append((node.lineno, "verify=False disables SSL certificate verification")) |
| 119 | + return issues |
| 120 | + |
| 121 | + |
| 122 | +def _find_shell_true(filepath): |
| 123 | + """Find subprocess calls with shell=True.""" |
| 124 | + issues = [] |
| 125 | + try: |
| 126 | + source = filepath.read_text(encoding="utf-8", errors="ignore") |
| 127 | + tree = ast.parse(source, filename=str(filepath)) |
| 128 | + except SyntaxError: |
| 129 | + return issues |
| 130 | + |
| 131 | + for node in ast.walk(tree): |
| 132 | + if not isinstance(node, ast.Call): |
| 133 | + continue |
| 134 | + for kw in node.keywords: |
| 135 | + if kw.arg == "shell" and isinstance(kw.value, ast.Constant) and kw.value.value is True: |
| 136 | + if filepath.name not in SHELL_TRUE_ALLOWLIST: |
| 137 | + issues.append((node.lineno, "subprocess call with shell=True")) |
| 138 | + return issues |
| 139 | + |
| 140 | + |
| 141 | +# --------------------------------------------------------------------------- |
| 142 | +# Test: dependency minimum versions in requirements.txt |
| 143 | +# --------------------------------------------------------------------------- |
| 144 | +class TestDependencyVersions: |
| 145 | + """Ensure requirements.txt does not pin known-vulnerable versions.""" |
| 146 | + |
| 147 | + @pytest.fixture(autouse=True) |
| 148 | + def _load_requirements(self): |
| 149 | + self.req_file = ROOT_DIR / "requirements.txt" |
| 150 | + assert self.req_file.exists(), "requirements.txt not found" |
| 151 | + self.reqs = _parse_requirements(self.req_file) |
| 152 | + |
| 153 | + @pytest.mark.parametrize("pkg,min_ver", list(MINIMUM_SAFE_VERSIONS.items())) |
| 154 | + def test_minimum_version_not_pinned_below_safe(self, pkg, min_ver): |
| 155 | + """Check that requirements.txt does not pin a package below the minimum safe version.""" |
| 156 | + if pkg.lower() not in self.reqs: |
| 157 | + pytest.skip(f"{pkg} not in requirements.txt") |
| 158 | + op, ver_str = self.reqs[pkg.lower()] |
| 159 | + pinned = Version(ver_str) |
| 160 | + minimum = Version(min_ver) |
| 161 | + |
| 162 | + if op == "==": |
| 163 | + assert pinned >= minimum, ( |
| 164 | + f"{pkg}=={ver_str} is pinned below minimum safe version {min_ver}" |
| 165 | + ) |
| 166 | + elif op == "~=": |
| 167 | + # ~= means compatible release; the pinned version itself must be >= minimum |
| 168 | + assert pinned >= minimum, ( |
| 169 | + f"{pkg}~={ver_str} allows versions below minimum safe {min_ver}" |
| 170 | + ) |
| 171 | + |
| 172 | + def test_pyasn1_not_vulnerable(self): |
| 173 | + """Regression test for CVE-2026-30922: pyasn1 must be >= 0.6.3.""" |
| 174 | + if "pyasn1" not in self.reqs: |
| 175 | + pytest.skip("pyasn1 not in requirements.txt") |
| 176 | + op, ver_str = self.reqs["pyasn1"] |
| 177 | + pinned = Version(ver_str) |
| 178 | + assert pinned >= Version("0.6.3"), ( |
| 179 | + f"pyasn1 {op}{ver_str} is vulnerable (CVE-2026-30922). Upgrade to >=0.6.3" |
| 180 | + ) |
| 181 | + |
| 182 | + |
| 183 | +# --------------------------------------------------------------------------- |
| 184 | +# Test: requests calls in plugin code must have timeout |
| 185 | +# --------------------------------------------------------------------------- |
| 186 | +class TestRequestsTimeout: |
| 187 | + """All requests.get/post calls in plugin Python code must include a timeout parameter.""" |
| 188 | + |
| 189 | + def test_stockpile_steganography_has_timeout(self): |
| 190 | + """Regression: steganography.py requests calls must have timeout.""" |
| 191 | + path = PLUGIN_DIR / "stockpile" / "app" / "obfuscators" / "steganography.py" |
| 192 | + if not path.exists(): |
| 193 | + pytest.skip("steganography.py not found") |
| 194 | + issues = _find_requests_calls_without_timeout(path) |
| 195 | + assert not issues, f"Missing timeout in {path}: {issues}" |
| 196 | + |
| 197 | + def test_stockpile_ragdoll_has_timeout(self): |
| 198 | + """Regression: ragdoll.py requests calls must have timeout.""" |
| 199 | + path = PLUGIN_DIR / "stockpile" / "payloads" / "ragdoll.py" |
| 200 | + if not path.exists(): |
| 201 | + pytest.skip("ragdoll.py not found") |
| 202 | + issues = _find_requests_calls_without_timeout(path) |
| 203 | + assert not issues, f"Missing timeout in {path}: {issues}" |
| 204 | + |
| 205 | + def test_response_elasticat_has_timeout(self): |
| 206 | + """Regression: elasticat.py requests calls must have timeout.""" |
| 207 | + path = PLUGIN_DIR / "response" / "payloads" / "elasticat.py" |
| 208 | + if not path.exists(): |
| 209 | + pytest.skip("elasticat.py not found") |
| 210 | + issues = _find_requests_calls_without_timeout(path) |
| 211 | + assert not issues, f"Missing timeout in {path}: {issues}" |
| 212 | + |
| 213 | + def test_all_plugin_requests_have_timeout(self): |
| 214 | + """Scan all plugin Python files for requests calls without timeout.""" |
| 215 | + all_issues = [] |
| 216 | + for filepath in _iter_python_files(PLUGIN_DIR): |
| 217 | + issues = _find_requests_calls_without_timeout(filepath) |
| 218 | + if issues: |
| 219 | + for lineno, msg in issues: |
| 220 | + all_issues.append(f"{filepath.relative_to(ROOT_DIR)}:{lineno} - {msg}") |
| 221 | + |
| 222 | + if all_issues: |
| 223 | + # Report as warning rather than hard fail since some may be intentional |
| 224 | + pytest.xfail( |
| 225 | + f"Found {len(all_issues)} requests call(s) without timeout:\n" |
| 226 | + + "\n".join(all_issues[:20]) |
| 227 | + ) |
| 228 | + |
| 229 | + |
| 230 | +# --------------------------------------------------------------------------- |
| 231 | +# Test: no verify=False in plugin code |
| 232 | +# --------------------------------------------------------------------------- |
| 233 | +class TestNoVerifyFalse: |
| 234 | + """No requests calls should use verify=False.""" |
| 235 | + |
| 236 | + def test_stockpile_steganography_no_verify_false(self): |
| 237 | + """Regression: steganography.py must use verify=True.""" |
| 238 | + path = PLUGIN_DIR / "stockpile" / "app" / "obfuscators" / "steganography.py" |
| 239 | + if not path.exists(): |
| 240 | + pytest.skip("steganography.py not found") |
| 241 | + issues = _find_verify_false(path) |
| 242 | + assert not issues, f"verify=False found in {path}: {issues}" |
| 243 | + |
| 244 | + def test_all_plugins_no_verify_false(self): |
| 245 | + """Scan all plugin Python files for verify=False.""" |
| 246 | + all_issues = [] |
| 247 | + for filepath in _iter_python_files(PLUGIN_DIR): |
| 248 | + issues = _find_verify_false(filepath) |
| 249 | + if issues: |
| 250 | + for lineno, msg in issues: |
| 251 | + all_issues.append(f"{filepath.relative_to(ROOT_DIR)}:{lineno} - {msg}") |
| 252 | + |
| 253 | + if all_issues: |
| 254 | + pytest.xfail( |
| 255 | + f"Found {len(all_issues)} verify=False occurrence(s):\n" |
| 256 | + + "\n".join(all_issues[:20]) |
| 257 | + ) |
| 258 | + |
| 259 | + |
| 260 | +# --------------------------------------------------------------------------- |
| 261 | +# Test: shell=True usage audit |
| 262 | +# --------------------------------------------------------------------------- |
| 263 | +class TestNoShellTrue: |
| 264 | + """Subprocess calls should avoid shell=True unless in allowlisted agent payloads.""" |
| 265 | + |
| 266 | + def test_core_code_no_shell_true(self): |
| 267 | + """Scan core caldera code (not plugins) for shell=True.""" |
| 268 | + core_dirs = [ROOT_DIR / "app"] |
| 269 | + all_issues = [] |
| 270 | + for filepath in _iter_python_files(*core_dirs): |
| 271 | + issues = _find_shell_true(filepath) |
| 272 | + if issues: |
| 273 | + for lineno, msg in issues: |
| 274 | + all_issues.append(f"{filepath.relative_to(ROOT_DIR)}:{lineno} - {msg}") |
| 275 | + |
| 276 | + if all_issues: |
| 277 | + pytest.xfail( |
| 278 | + f"Found {len(all_issues)} shell=True occurrence(s) in core code:\n" |
| 279 | + + "\n".join(all_issues[:20]) |
| 280 | + ) |
| 281 | + |
| 282 | + def test_plugin_code_no_unexpected_shell_true(self): |
| 283 | + """Scan plugin code for shell=True outside of known agent payloads.""" |
| 284 | + all_issues = [] |
| 285 | + for filepath in _iter_python_files(PLUGIN_DIR): |
| 286 | + issues = _find_shell_true(filepath) |
| 287 | + if issues: |
| 288 | + for lineno, msg in issues: |
| 289 | + all_issues.append(f"{filepath.relative_to(ROOT_DIR)}:{lineno} - {msg}") |
| 290 | + |
| 291 | + if all_issues: |
| 292 | + pytest.xfail( |
| 293 | + f"Found {len(all_issues)} shell=True occurrence(s) in plugin code " |
| 294 | + f"(outside allowlist):\n" + "\n".join(all_issues[:20]) |
| 295 | + ) |
0 commit comments