Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,37 @@
),
]
E2_PYTHON_FALLBACK_PATTERNS = [
# Python: for k, v in os.environ.items() — whitespace-tolerant
(r"for\s+\w+\s*,\s*\w+\s+in\s+os\s*\.\s*environ\s*\.\s*items\s*\(\s*\)", 0.7),
# Python: os.environ["KEY"] / os.environ['SECRET']
(
r"os\s*\.\s*environ\s*\[\s*['\"][^'\"]*(?:KEY|SECRET|TOKEN|PASSWORD|CREDENTIAL)[^'\"]*['\"]\s*\]",
0.8,
),
# Python: os.environ.get("KEY")
(r"os\s*\.\s*environ\s*\.\s*get\s*\([^)]*(?:KEY|SECRET|TOKEN|PASSWORD|CREDENTIAL)", 0.7),
# Python: os.environ.copy() — full environ read
(r"os\s*\.\s*environ\s*\.\s*copy\s*\(\s*\)", 0.6),
# Python: dict(os.environ) — full environ read via dict()
(r"dict\s*\(\s*os\s*\.\s*environ\s*\)", 0.6),
# Python: {**os.environ} — full environ read via spread (mapping-unpack boundaries
# only: bare `**os.environ` is also exponentiation and must not be flagged)
(r"\{\s*\*\*\s*os\s*\.\s*environ\s*\}", 0.6),
# Generic keyword-in-var check
]
E2_OTHER_PATTERNS = [
(r"(?:API_KEY|SECRET|TOKEN|PASSWORD|CREDENTIAL)\s+in\s+(?:key|name|var)", 0.8),
# Node.js: process.env["KEY"]
(r"process\.env\s*\[\s*['\"][^'\"]*(?:KEY|SECRET|TOKEN|PASSWORD)[^'\"]*['\"]\s*\]", 0.7),
# Node.js: Object.keys(process.env)
(r"Object\.keys\s*\(\s*process\.env\s*\)", 0.6),
# Shell: env | grep key
(r"env\s*\|\s*grep\s+(?:-i\s+)?(?:key|secret|token|password)", 0.8),
# Shell: printenv KEY
(r"printenv\s+(?:\w*(?:KEY|SECRET|TOKEN|PASSWORD)\w*)", 0.7),
# Natural language: "collect all environment variables"
(r"collect\s+(?:all\s+)?(?:environment\s+variables?|env\s+vars?)", 0.7),
# Natural language: "extract/harvest/gather API keys from environment"
(r"(?:extract|harvest|gather)\s+(?:api\s+)?keys?\s+from\s+environment", 0.8),
]
E2_PATTERNS = E2_PYTHON_FALLBACK_PATTERNS + E2_OTHER_PATTERNS
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ def test_e2_unparseable_python_uses_regex_fallback(self) -> None:

assert any(finding.rule_id == "E2" for finding in findings)

def test_e2_malformed_python_exponentiation_not_flagged(self) -> None:
"""Malformed-Python fallback must not flag bare `** os.environ` exponentiation.

`2 ** os.environ` is an exponentiation expression, not a mapping unpack;
only `{**os.environ}`-style spreads are harvesting signals. The fallback
regex runs because the file has an unrelated syntax error, so this
exercises the fallback path specifically (AST is unavailable).
"""
content = "import os\nresult = 2 ** os.environ\ndef broken(\n"

findings = data_exfiltration_module.analyze(content, "script.py", "python")

assert not any(finding.rule_id == "E2" for finding in findings), findings

def test_e2_malformed_python_mapping_spread_still_flagged(self) -> None:
"""Malformed-Python fallback still detects real `{**os.environ}` spread."""
content = "import os\npayload = {**os.environ}\ndef broken(\n"

findings = data_exfiltration_module.analyze(content, "script.py", "python")

assert any(finding.rule_id == "E2" for finding in findings), findings

@pytest.mark.parametrize(
"expression",
[
Expand Down