AdithyaSK's picture
AdithyaSK HF Staff
MiMo-V2.6-RL Terminal as Harbor tasks (part 3)
52ba303 verified
Raw History Blame Contribute Delete
5.21 kB
import ast
import hashlib
import json
import os
import subprocess
import sys
from pathlib import Path
APP = Path("/app")
SOURCE = APP / "vendor/bandit"
PUBLIC_FIXTURE = SOURCE / "workspace/yaml_cases.py"
PRIVATE_FIXTURE = Path("/tests/fixtures/private_yaml_cases.py")
ARTIFACT = APP / "output.json"
EXPECTED = json.loads((Path(__file__).parent / "fixtures/expected.json").read_text())
PROTECTED = json.loads(
(Path(__file__).parent / "fixtures/protected_hashes.json").read_text()
)
def runtime_env():
env = os.environ.copy()
env["PYTHONPATH"] = f"{APP / 'runtime_compat'}:{SOURCE}"
return env
def run_scan(fixture, artifact):
artifact.unlink(missing_ok=True)
completed = subprocess.run(
[
"python3",
"-m",
"bandit",
"-q",
"-f",
"json",
"-t",
"B506",
"-o",
str(artifact),
str(fixture),
],
cwd=SOURCE,
env=runtime_env(),
check=False,
timeout=30,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
assert completed.returncode == 1, completed.stderr
assert artifact.is_file()
return json.loads(artifact.read_text())
def expected_lines(fixture):
tree = ast.parse(fixture.read_text())
lines = []
for node in ast.walk(tree):
if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Attribute):
continue
if not isinstance(node.func.value, ast.Name):
continue
if node.func.value.id != "yaml" or node.func.attr != "load":
continue
positional = node.args[1] if len(node.args) > 1 else None
positional_name = (
positional.attr
if isinstance(positional, ast.Attribute)
else positional.id
if isinstance(positional, ast.Name)
else None
)
keyword = next((item.value for item in node.keywords if item.arg == "Loader"), None)
keyword_name = (
keyword.attr
if isinstance(keyword, ast.Attribute)
else keyword.id
if isinstance(keyword, ast.Name)
else None
)
if positional_name not in {"SafeLoader", "CSafeLoader"} and keyword_name not in {
"SafeLoader",
"CSafeLoader",
}:
lines.append(node.lineno)
return sorted(lines)
def result_rows(report):
return sorted(report["results"], key=lambda row: row["line_number"])
def sha256(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def test_gate_artifact_schema():
report = json.loads(ARTIFACT.read_text())
rows = result_rows(report)
assert set(report) == {"errors", "generated_at", "metrics", "results"}
assert report["errors"] == []
assert [row["line_number"] for row in rows] == EXPECTED["unsafe_lines"]
assert all(row["filename"].endswith("workspace/yaml_cases.py") for row in rows)
assert all(set(row) >= {"line_range", "test_id", "issue_severity", "issue_confidence"} for row in rows)
def test_gate_source_tree_and_runtime_protected():
assert (SOURCE / "bandit/core/context.py").is_file()
assert (SOURCE / "bandit/plugins/yaml_load.py").is_file()
assert PUBLIC_FIXTURE.read_text().count("yaml.load") == 9
for relative_path, expected_hash in PROTECTED.items():
assert sha256(APP / relative_path) == expected_hash
def test_core_context_argument_semantics():
sys.path[:0] = [str(APP / "runtime_compat"), str(SOURCE)]
from bandit.core.context import Context
calls = ast.parse(
"f(value, yaml.SafeLoader)\n"
"f(value, SafeLoader)\n"
"f(value, 'literal')\n"
).body
assert Context({"call": calls[0].value}).get_call_arg_at_position(1) == "SafeLoader"
assert Context({"call": calls[1].value}).get_call_arg_at_position(1) == "SafeLoader"
assert Context({"call": calls[2].value}).get_call_arg_at_position(1) == "literal"
assert Context({"call": calls[2].value}).get_call_arg_at_position(4) is None
def test_core_private_cli_finding_lines(tmp_path):
report = run_scan(PRIVATE_FIXTURE, tmp_path / "private.json")
rows = result_rows(report)
assert [row["line_number"] for row in rows] == expected_lines(PRIVATE_FIXTURE)
assert len(rows) == 5
assert all(row["test_id"] == EXPECTED["test_id"] for row in rows)
assert all(row["issue_severity"] == EXPECTED["severity"] for row in rows)
assert all(row["issue_confidence"] == EXPECTED["confidence"] for row in rows)
def test_edge_safe_loader_forms_suppressed(tmp_path):
report = run_scan(PRIVATE_FIXTURE, tmp_path / "safe.json")
found_lines = {row["line_number"] for row in report["results"]}
safe_lines = set(range(6, 12))
assert found_lines.isdisjoint(safe_lines)
assert len(found_lines) == 5
def test_edge_unsafe_loader_forms_remain(tmp_path):
report = run_scan(PRIVATE_FIXTURE, tmp_path / "unsafe.json")
found_lines = {row["line_number"] for row in report["results"]}
assert found_lines == set(expected_lines(PRIVATE_FIXTURE))
assert found_lines == {15, 16, 17, 18, 19}