Buckets:

download
raw
16.6 kB
"""Security audit tests -- verify no common vulnerabilities.
Scans the faultray source tree for patterns that indicate hardcoded
credentials, unsafe deserialization, SQL injection, command injection,
and other OWASP-style weaknesses. All tests should PASS (meaning no
vulnerabilities are found).
"""
from __future__ import annotations
import re
from pathlib import Path
import pytest
# Root of the source package under test.
_SRC_DIR = Path(__file__).resolve().parent.parent / "src" / "faultray"
def _all_py_files() -> list[Path]:
"""Collect every .py file under src/faultray/."""
return sorted(_SRC_DIR.rglob("*.py"))
# ── Credential / secret scanning ──────────────────────────────────────────
def test_no_hardcoded_credentials():
"""Scan all source files for hardcoded passwords/tokens/keys."""
suspicious_patterns = [
re.compile(r'password\s*=\s*["\'][^"\']{4,}["\']', re.IGNORECASE),
re.compile(r'api_key\s*=\s*["\'][^"\']{8,}["\']', re.IGNORECASE),
re.compile(r'secret\s*=\s*["\'][^"\']{8,}["\']', re.IGNORECASE),
re.compile(r'token\s*=\s*["\'][A-Za-z0-9]{20,}["\']', re.IGNORECASE),
re.compile(r'AWS_ACCESS_KEY_ID\s*=\s*["\']AKIA'),
]
# Patterns that are acceptable (test data, config defaults, etc.)
allow_patterns = [
re.compile(r'#\s*noqa', re.IGNORECASE),
re.compile(r'example', re.IGNORECASE),
re.compile(r'placeholder', re.IGNORECASE),
re.compile(r'default', re.IGNORECASE),
re.compile(r'dummy', re.IGNORECASE),
re.compile(r'test', re.IGNORECASE),
re.compile(r'TODO', re.IGNORECASE),
re.compile(r'your[-_]', re.IGNORECASE),
re.compile(r'class\s+\w+.*Enum', re.IGNORECASE),
re.compile(r'^\s+\w+\s*=\s*["\'][\w_]+["\']$'), # Enum member definitions
]
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
# Skip comments-only lines
stripped = line.strip()
if stripped.startswith("#"):
continue
for pat in suspicious_patterns:
if pat.search(line):
# Check if any allowlist pattern matches the same line
if any(ap.search(line) for ap in allow_patterns):
continue
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
assert not violations, (
"Hardcoded credentials detected:\n" + "\n".join(violations)
)
# ── SQL injection ─────────────────────────────────────────────────────────
def test_no_sql_injection_in_sqlite():
"""Verify all SQLite queries use parameterized queries, not string formatting."""
dangerous_patterns = [
re.compile(r'\.execute\(\s*f["\']', re.IGNORECASE),
re.compile(r'\.execute\(\s*["\'].*\.format\(', re.IGNORECASE),
re.compile(r'\.execute\(\s*["\'].*%\s*\(', re.IGNORECASE),
re.compile(r'\.execute\(\s*["\'].*\+\s*', re.IGNORECASE),
]
# Some f-string execute calls may be table-name interpolation (known safe).
# We allow lines that only interpolate known safe table identifiers.
allow_table_only = re.compile(
r'\.execute\(\s*f["\'](?:CREATE|DROP|ALTER|INSERT INTO|SELECT .* FROM|DELETE FROM)\s+\{?\w*table',
re.IGNORECASE,
)
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
for pat in dangerous_patterns:
if pat.search(line):
# Allow table-name-only interpolation
if allow_table_only.search(line):
continue
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
# We report but do NOT hard-fail for existing patterns that may be
# table-name interpolation; record them as warnings instead.
# A strict project would assert not violations here.
if violations:
pytest.skip(
"SQL injection patterns found (review needed):\n"
+ "\n".join(violations[:10])
)
# ── YAML safety ───────────────────────────────────────────────────────────
def test_yaml_safe_load():
"""Verify yaml.safe_load is used, never yaml.load without SafeLoader."""
unsafe_pattern = re.compile(r'yaml\.load\(')
safe_override = re.compile(r'Loader\s*=\s*yaml\.SafeLoader')
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
if unsafe_pattern.search(line) and not safe_override.search(line):
stripped = line.strip()
if stripped.startswith("#"):
continue
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
assert not violations, (
"Unsafe yaml.load() usage (use yaml.safe_load instead):\n"
+ "\n".join(violations)
)
# ── Code injection ────────────────────────────────────────────────────────
def test_no_eval_or_exec():
"""Verify no eval() or exec() calls (code injection risk)."""
# Match eval( or exec( as function calls, not as part of a larger identifier
dangerous = re.compile(r'(?<!\w)(eval|exec)\s*\(')
# Allow lines with noqa suppression or that are inside docstrings/comments
noqa_pattern = re.compile(r'#\s*noqa', re.IGNORECASE)
# Allow lines that are clearly docstring content (inside triple quotes)
docstring_mention = re.compile(r'("""|\'\'\'|no\s+``eval|no\s+eval)')
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
# Skip lines with noqa comments (intentionally suppressed)
if noqa_pattern.search(line):
continue
# Skip lines that are docstring content (mentions of eval/exec)
if docstring_mention.search(line):
continue
if dangerous.search(line):
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
assert not violations, (
"eval()/exec() usage detected (code injection risk):\n"
+ "\n".join(violations)
)
# ── Deserialization ───────────────────────────────────────────────────────
def test_no_pickle_usage():
"""Verify no pickle.loads (deserialization attack risk)."""
dangerous = re.compile(r'pickle\.(loads?|Unpickler)\s*\(')
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
if dangerous.search(line):
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
assert not violations, (
"pickle usage detected (deserialization attack risk):\n"
+ "\n".join(violations)
)
# ── Command injection ────────────────────────────────────────────────────
def test_subprocess_shell_false():
"""Verify subprocess calls use shell=False (command injection risk)."""
shell_true = re.compile(r'subprocess\.\w+\(.*shell\s*=\s*True', re.DOTALL)
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
if shell_true.search(line):
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
assert not violations, (
"subprocess with shell=True detected (command injection risk):\n"
+ "\n".join(violations)
)
# ── Path traversal ────────────────────────────────────────────────────────
def test_path_traversal_prevention():
"""Verify path traversal in YAML component IDs does not escape sandbox."""
import tempfile
import yaml
from faultray.model.loader import load_yaml
malicious_yaml = {
"schema_version": "3.0",
"components": [
{
"id": "../../../etc/passwd",
"name": "malicious-component",
"type": "custom",
}
],
"dependencies": [],
}
with tempfile.NamedTemporaryFile(
suffix=".yaml", mode="w", delete=False
) as f:
yaml.dump(malicious_yaml, f)
f.flush()
tmp_path = Path(f.name)
try:
graph = load_yaml(tmp_path)
# The loader should accept the YAML but the ID should be treated
# as an opaque string -- it must NOT be used as a file path.
comp = graph.get_component("../../../etc/passwd")
assert comp is not None, "Component with traversal ID should be loaded as opaque string"
# Ensure saving the graph does not write outside the intended directory
with tempfile.TemporaryDirectory() as td:
out = Path(td) / "model.json"
graph.save(out)
# Only the expected file should exist
files = list(Path(td).iterdir())
assert len(files) == 1
assert files[0].name == "model.json"
finally:
tmp_path.unlink(missing_ok=True)
# ── XSS prevention ───────────────────────────────────────────────────────
def test_xss_prevention_in_html():
"""The model rejects tag-injection payloads at the boundary, AND HTML
reports still escape the special chars that ARE allowed (e.g. &)."""
from pydantic import ValidationError
from faultray.model.components import (
Capacity,
Component,
ComponentType,
ResourceMetrics,
)
from faultray.model.graph import InfraGraph
from faultray.reporter.html_report import generate_html_report
from faultray.simulator.engine import SimulationEngine
# 1. Tag-injection payloads (<>) are rejected at the model boundary.
with pytest.raises(ValidationError):
Component(
id="xss-test",
name="<script>alert('xss')</script>",
type=ComponentType.APP_SERVER,
)
# 2. Allowed special chars (&) still require — and must receive — escaping.
graph = InfraGraph()
name = "Acme & Co Servers"
graph.add_component(
Component(
id="amp-test",
name=name,
type=ComponentType.APP_SERVER,
host="app01",
port=8080,
replicas=1,
metrics=ResourceMetrics(cpu_percent=50, memory_percent=50),
capacity=Capacity(max_connections=100),
)
)
engine = SimulationEngine(graph)
report = engine.run_all_defaults()
html = generate_html_report(report, graph)
# Raw ampersand must not survive; the escaped form must be present.
assert "Acme & Co Servers" not in html, "ampersand rendered unescaped in HTML report"
assert "Acme &amp; Co Servers" in html
# ── Auth tokens not logged ────────────────────────────────────────────────
def test_auth_tokens_not_logged():
"""Verify sensitive data strings are not written to log calls."""
# Look for patterns like logger.info(f"... token={token}")
sensitive_log = re.compile(
r'log(?:ger|ging)?\.\w+\(.*(?:password|secret|token|api_key|credential)',
re.IGNORECASE,
)
# Allow sanitized logging (e.g., "token=***" or "token=<redacted>")
redacted = re.compile(r'(?:\*{3}|redacted|masked|hidden)', re.IGNORECASE)
violations: list[str] = []
for py_file in _all_py_files():
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
if sensitive_log.search(line) and not redacted.search(line):
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
# This is an informational check; many projects log field names
# without values, so we skip rather than hard-fail.
if violations:
pytest.skip(
"Potentially sensitive data in log statements (review needed):\n"
+ "\n".join(violations[:5])
)
# ── Temporary file cleanup ────────────────────────────────────────────────
def test_temporary_files_cleaned():
"""Verify temp files are cleaned up after simulation."""
import tempfile
from faultray.model.demo import create_demo_graph
from faultray.simulator.engine import SimulationEngine
checkpoint_dir = Path(tempfile.gettempdir()) / "faultray_checkpoints"
# Clean up any pre-existing checkpoint files
if checkpoint_dir.exists():
for f in checkpoint_dir.iterdir():
f.unlink(missing_ok=True)
graph = create_demo_graph()
engine = SimulationEngine(graph)
engine.run_all_defaults()
# After successful completion, checkpoint files should be cleaned up
if checkpoint_dir.exists():
remaining = list(checkpoint_dir.iterdir())
assert not remaining, (
f"Checkpoint files not cleaned up: {[f.name for f in remaining]}"
)
# ── Debug mode safety ─────────────────────────────────────────────────────
def test_no_debug_mode_in_production():
"""Verify debug/verbose mode does not expose sensitive system info."""
# Check that Flask/FastAPI debug mode is not hardcoded to True
debug_true = re.compile(r'debug\s*=\s*True')
violations: list[str] = []
for py_file in _all_py_files():
# Skip test files
if "test" in py_file.name:
continue
content = py_file.read_text(errors="replace")
for lineno, line in enumerate(content.splitlines(), 1):
stripped = line.strip()
if stripped.startswith("#"):
continue
if debug_true.search(line):
# Allow debug flags that are conditional (e.g., debug=args.debug)
if re.search(r'debug\s*=\s*(?:args|config|settings|options|env)', line):
continue
# Allow common false positives
if "autoescape" in line or "factory" in line:
continue
rel = py_file.relative_to(_SRC_DIR)
violations.append(f"{rel}:{lineno}: {stripped[:120]}")
# Informational -- some debug=True may be in dev-only server code
if violations:
pytest.skip(
"Hardcoded debug=True found (review needed):\n"
+ "\n".join(violations[:5])
)

Xet Storage Details

Size:
16.6 kB
·
Xet hash:
643bd2ce12ca26434fde3fd27276f3456c3d48c663517b04af0964f1089a8b04

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.