Skip to content

Commit b393f6f

Browse files
test(picklescan): cover invoked source refresh (#1224)
1 parent 9c36efb commit b393f6f

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

packages/modelaudit-picklescan/tests/test_call_graph_import_statements.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,37 @@ def test_scan_bytes_refreshes_call_graph_after_source_rewrite(
18321832
assert _has_critical_call_graph_finding(dangerous_report, module_name, "invoke", "os.system")
18331833

18341834

1835+
def test_scan_bytes_refreshes_invoked_import_fallback_after_source_rewrite(
1836+
tmp_path: Path,
1837+
monkeypatch: pytest.MonkeyPatch,
1838+
) -> None:
1839+
module_dir = tmp_path / "modules"
1840+
module_dir.mkdir()
1841+
module_name = "modelaudit_tp_rewritten_invoked_import_source"
1842+
module_path = module_dir / f"{module_name}.py"
1843+
module_path.write_text("def invoke(command):\n return command\n", encoding="utf-8")
1844+
monkeypatch.syspath_prepend(str(module_dir))
1845+
importlib.invalidate_caches()
1846+
_clear_call_graph_caches()
1847+
payload = _global_call_payload(module_name, "invoke", _unicode_operand("echo rewritten"))
1848+
1849+
try:
1850+
safe_report = scan_bytes(payload, source="rewritten-invoked-import-safe.pkl")
1851+
1852+
module_path.write_text(
1853+
"def invoke(command):\n import modelaudit_tp_invoked_import_dependency\n return command\n",
1854+
encoding="utf-8",
1855+
)
1856+
importlib.invalidate_caches()
1857+
dangerous_report = scan_bytes(payload, source="rewritten-invoked-import-dangerous.pkl")
1858+
finally:
1859+
_clear_call_graph_caches()
1860+
1861+
assert safe_report.verdict == SafetyVerdict.CLEAN
1862+
assert dangerous_report.verdict == SafetyVerdict.MALICIOUS
1863+
assert _has_critical_call_graph_finding(dangerous_report, module_name, "invoke", "builtins.__import__")
1864+
1865+
18351866
def test_call_graph_propagates_wrapper_import_execution_fallbacks() -> None:
18361867
calls = call_graph._calls_for_function("platform.mac_ver") or ()
18371868

0 commit comments

Comments
 (0)