Download tasks/candidate-0390-security-appsec/tests/test_outputs.py from FineEnvs/MiMo-V2.6-RL-harbor-terminal: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal/resolve/5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0390-security-appsec/tests/test_outputs.py
- Command line
-
hf download hf://datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal@5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0390-security-appsec/tests/test_outputs.py
-
curl -L -o test_outputs.py https://huggingface.co/datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal/resolve/5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0390-security-appsec/tests/test_outputs.py
3.82 kB
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| import tempfile | |
| from pathlib import Path | |
| APP = Path("/app") if Path("/app/vendor/bandit").is_dir() else Path(__file__).resolve().parents[1] | |
| BANDIT = APP / "vendor" / "bandit" if (APP / "vendor/bandit").is_dir() else APP / "environment/vendor/bandit" | |
| FIXTURES = Path("/tests") / "fixtures" if Path("/tests/fixtures").is_dir() else APP / "tests/fixtures" | |
| def run_scan(*names): | |
| paths = [str(FIXTURES / name) for name in names] | |
| output = APP / "output.json" | |
| output.unlink(missing_ok=True) | |
| env = os.environ.copy() | |
| env["PYTHONPATH"] = str(BANDIT) | |
| completed = subprocess.run( | |
| [sys.executable, "-m", "bandit", "-q", "-f", "json", "-o", str(output), *paths], | |
| cwd=APP, | |
| env=env, | |
| text=True, | |
| capture_output=True, | |
| timeout=30, | |
| ) | |
| assert completed.returncode in (0, 1), completed.stderr | |
| assert completed.returncode != 2, completed.stderr | |
| assert output.is_file() and output.stat().st_size > 0 | |
| return json.loads(output.read_text()) | |
| def test_gate_output_schema_and_artifact(): | |
| data = run_scan("trojansource.py", "trojansource_latin1.py") | |
| assert set(data) == {"errors", "generated_at", "metrics", "results"} | |
| assert isinstance(data["results"], list) | |
| assert isinstance(data["metrics"], dict) | |
| assert data["errors"] == [] | |
| def test_core_trojan_finding_is_independently_located(): | |
| data = run_scan("trojansource.py") | |
| results = [item for item in data["results"] if item["test_id"] == "B613"] | |
| assert len(results) == 1 | |
| finding = results[0] | |
| source = (FIXTURES / "trojansource.py").read_text() | |
| bidi = next(character for character in source if character in "\u202a\u202b\u202c\u202d\u202e\u2066\u2067\u2068\u2069\u200f") | |
| expected_line = next(index for index, line in enumerate(source.splitlines(), 1) if bidi in line) | |
| expected_column = source.splitlines()[expected_line - 1].index(bidi) + 1 | |
| assert finding["line_number"] == expected_line | |
| assert finding["line_range"] == [expected_line] | |
| assert finding["col_offset"] == expected_column | |
| assert finding["issue_severity"] == "HIGH" | |
| assert finding["issue_confidence"] == "MEDIUM" | |
| assert finding["issue_cwe"]["id"] == 838 | |
| def test_core_generic_tester_preserves_and_falls_back_location(): | |
| sys.path.insert(0, str(BANDIT)) | |
| import bandit | |
| from bandit.core.tester import BanditTester | |
| class Metrics: | |
| def note_nosec(self): | |
| raise AssertionError("unexpected nosec") | |
| def note_skipped_test(self): | |
| raise AssertionError("unexpected skip") | |
| class TestSet: | |
| def __init__(self, tests): | |
| self.tests = tests | |
| def get_tests(self, checktype): | |
| return self.tests | |
| def explicit(context): | |
| result = bandit.Issue(bandit.MEDIUM, text="explicit", lineno=9, col_offset=2) | |
| result.linerange = [9] | |
| return result | |
| explicit._test_id = "X900" | |
| explicit._checks = ["File"] | |
| def fallback(context): | |
| return bandit.Issue(bandit.LOW, text="fallback") | |
| fallback._test_id = "X901" | |
| fallback._checks = ["File"] | |
| tester = BanditTester(TestSet([explicit, fallback]), False, {}, Metrics()) | |
| tester.run_tests({"filename": "sample.py", "file_data": "x", "lineno": 3, "linerange": [3, 4], "col_offset": 1}, "File") | |
| by_text = {item.text: item for item in tester.results} | |
| assert by_text["explicit"].linerange == [9] | |
| assert by_text["explicit"].lineno == 9 | |
| assert by_text["fallback"].linerange == [3, 4] | |
| assert by_text["fallback"].lineno == 3 | |
| def test_edge_latin1_is_analyzable_without_finding(): | |
| data = run_scan("trojansource_latin1.py") | |
| assert [item for item in data["results"] if item["test_id"] == "B613"] == [] | |
| assert data["errors"] == [] | |