Datasets:
Download prepare_benchmarkjava.py from Zreby/security-code-curated-v1: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/datasets/Zreby/security-code-curated-v1/resolve/main/prepare_benchmarkjava.py
- Command line
-
hf download hf://datasets/Zreby/security-code-curated-v1/prepare_benchmarkjava.py
-
curl -L -o prepare_benchmarkjava.py https://huggingface.co/datasets/Zreby/security-code-curated-v1/resolve/main/prepare_benchmarkjava.py
12.5 kB
| #!/usr/bin/env python3 | |
| """Prepare a small, provenance-preserving OWASP BenchmarkJava v1.2 subset. | |
| Only the 22 pinned Java test cases below are copied, and each file's own GPLv2 | |
| notice is checked before it is included. No source code is compiled or executed. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import csv | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| REPO = "OWASP-Benchmark/BenchmarkJava" | |
| COMMIT = "10dfcc54ad02dcfcb358b0ba4a0fef8c919a6d17" | |
| VERSION = "v1.2" | |
| RAW = f"https://raw.githubusercontent.com/{REPO}/{COMMIT}/" | |
| LICENSE_PATH = "LICENSE" | |
| LICENSE_SHA256 = "c03cea027b4b40e4402fabd08557736727ec3d5bc54ad64ab6472de432198cad" | |
| EXPECTED_RESULTS_PATH = "expectedresults-1.2.csv" | |
| EXPECTED_RESULTS_SHA256 = "1809f6a690c6cf7dd6685df6ebe2eef8fe3ca93d19a7ca0ce45987ca4f5d78e1" | |
| EXPECTED_RESULTS_URL = RAW + EXPECTED_RESULTS_PATH | |
| LICENSE_OUTPUT = "OWASP-BENCHMARK-GPL-2.0.txt" | |
| # One expected-vulnerable and one expected-not-vulnerable case per v1.2 category. | |
| # Source hashes pin the exact file contents in addition to the repository commit. | |
| CASES = { | |
| "BenchmarkTest02613": ("cmdi", True, "78", "ee0f005015b5a8afa0a4e7af66b603bee30d88caccf4b7bc7b3967bab7923621"), | |
| "BenchmarkTest02714": ("cmdi", False, "78", "60fa63cb288b2c6103dd897272d7e918df2deced54974d2f59a94c39ad9c616c"), | |
| "BenchmarkTest02663": ("crypto", True, "327", "011b93cc1dddbdc90c846d4b2ad77c9c87141a6f0cd09c8bb1233fb5fd91a17f"), | |
| "BenchmarkTest02662": ("crypto", False, "327", "a5256d29eaa1f6038c56a944ed9d589e75fdd261661157f85d920298d123636d"), | |
| "BenchmarkTest02677": ("hash", True, "328", "5a7f56ba0dab2c57068f7aca2a8d60d96fc7523293808b08d8e58044b41b5c90"), | |
| "BenchmarkTest02678": ("hash", False, "328", "014c28bc6f5b933415766c634ab6d0cd2de58f90051e2053b623d32266900654"), | |
| "BenchmarkTest02472": ("ldapi", True, "90", "0e701ce82d7093d2655debe295bf364e44bab6298fa8c1eedab3a99d4a82f798"), | |
| "BenchmarkTest02572": ("ldapi", False, "90", "5d377a3d3ed7e15c55e93901f5c211b02831443a1401575f696db5066804315a"), | |
| "BenchmarkTest02569": ("pathtraver", True, "22", "a3dfe0cf46f8aa6f7e69f71f1df7117fe58466b35eb931d3c728dca9f69dd023"), | |
| "BenchmarkTest02669": ("pathtraver", False, "22", "2780eac2f1bf34aff43dca66e355917cbb823fa85595651b220c2ec1faebeddc"), | |
| "BenchmarkTest02710": ("securecookie", True, "614", "f30e31957b18d18cec7de465260bdb7e786f0e96b2f7dc50beab07223fd2f96b"), | |
| "BenchmarkTest02711": ("securecookie", False, "614", "65831fa3b1be50ad088972399c2ff54ef7fb84551c1c42c7a93545db00180b33"), | |
| "BenchmarkTest02656": ("sqli", True, "89", "bc933bf5913303a3526cbeab9714139334cafa6eebb9f5437a7a93f64dedda7b"), | |
| "BenchmarkTest02740": ("sqli", False, "89", "c6ae0ee13166dc706178828ae8d9bdbdd7461cedcd37b8f4ce35a764ac40aa59"), | |
| "BenchmarkTest02624": ("trustbound", True, "501", "f13bd9caf25b98242c80e830a4004f7a84e6af98f52c5df5c9ed65e75ea73641"), | |
| "BenchmarkTest02726": ("trustbound", False, "501", "4a3ed0b365445020289f03b038775cdb0af078dedc988736b56f8105b3592e4a"), | |
| "BenchmarkTest02708": ("weakrand", True, "330", "1e8e464b8db864639164081de466b2ab72ebb42c39a661efc15f5e97e2706c70"), | |
| "BenchmarkTest02721": ("weakrand", False, "330", "eddf1069da360f0a612164bed500520b271944188c0471792d784017263058ec"), | |
| "BenchmarkTest02189": ("xpathi", True, "643", "7dad2393712ed1520ae660080c8e09461999eee81ebd6cf04a182d5674033bfe"), | |
| "BenchmarkTest02457": ("xpathi", False, "643", "a46a076b44a193aebf8e413ddb82f3dc84437f65504aabfaca9acb70b842c7b3"), | |
| "BenchmarkTest02608": ("xss", True, "79", "7999e879497173727b3fa2e95fa287d8261e5da421f8050b46bf28d195ef0ed3"), | |
| "BenchmarkTest02712": ("xss", False, "79", "3de2c3635152c46252dbf1ad3bfd81f828b8a538f157198a09e921425a6dfa76"), | |
| } | |
| def sha256(data: bytes) -> str: | |
| return hashlib.sha256(data).hexdigest() | |
| def get_bytes(source_root: Path | None, path: str) -> bytes: | |
| if source_root is not None: | |
| return (source_root / path).read_bytes() | |
| try: | |
| import requests | |
| except ImportError as exc: # pragma: no cover - environment dependent | |
| raise RuntimeError("Install requests or pass --source-root with a pinned checkout") from exc | |
| response = requests.get(RAW + path, timeout=45) | |
| response.raise_for_status() | |
| return response.content | |
| def verify_hash(data: bytes, expected: str, what: str) -> None: | |
| actual = sha256(data) | |
| if actual != expected: | |
| raise ValueError(f"{what} SHA-256 mismatch: expected {expected}, got {actual}") | |
| def parse_expected_rows(csv_bytes: bytes) -> dict[str, dict[str, str]]: | |
| text = csv_bytes.decode("utf-8-sig") | |
| rows = [] | |
| for raw in csv.DictReader(text.splitlines()): | |
| rows.append({(key or "").strip(): (value or "").strip() for key, value in raw.items()}) | |
| mapped = {row["# test name"]: row for row in rows if row.get("# test name")} | |
| if len(rows) != 2740 or len(mapped) != 2740: | |
| raise ValueError(f"Unexpected v1.2 expected-results inventory: {len(rows)} rows") | |
| return mapped | |
| def check_sample_license(text: str, test_name: str) -> None: | |
| header = "\n".join(text.splitlines()[:20]) | |
| required = ( | |
| "part of the Open Web Application Security Project (OWASP) Benchmark Project", | |
| "you can redistribute it and/or modify it under the terms", | |
| "GNU General Public License", | |
| "version 2", | |
| ) | |
| missing = [fragment for fragment in required if fragment not in header] | |
| if missing: | |
| raise ValueError(f"{test_name} lacks required file-level GPLv2 language: {missing}") | |
| def make_record( | |
| test_name: str, | |
| spec: tuple[str, bool, str, str], | |
| code: bytes, | |
| label_row: dict[str, str], | |
| ) -> dict: | |
| category, expected_vulnerable, cwe, expected_hash = spec | |
| verify_hash(code, expected_hash, test_name) | |
| source_text = code.decode("utf-8") | |
| check_sample_license(source_text, test_name) | |
| if label_row.get("category") != category: | |
| raise ValueError(f"Category mismatch for {test_name}: {label_row.get('category')}") | |
| if label_row.get("real vulnerability", "").lower() != str(expected_vulnerable).lower(): | |
| raise ValueError(f"Expected-result mismatch for {test_name}") | |
| if label_row.get("cwe") != cwe: | |
| raise ValueError(f"CWE mismatch for {test_name}") | |
| relative_path = f"src/main/java/org/owasp/benchmark/testcode/{test_name}.java" | |
| source_url = RAW + relative_path | |
| expected_result_url = EXPECTED_RESULTS_URL | |
| label = { | |
| "classification": "benchmark_expected_vulnerable" if expected_vulnerable else "benchmark_expected_not_vulnerable", | |
| "expected_vulnerability": expected_vulnerable, | |
| "benchmark_category": category, | |
| "cwe": f"CWE-{cwe}", | |
| "label_basis": "OWASP BenchmarkJava v1.2 expectedresults-1.2.csv row for this exact test case.", | |
| "label_scope": ( | |
| "The expected result applies to this benchmark test case and its designated category. " | |
| "A false result means the benchmark does not expect this specific test case to trigger " | |
| "the designated vulnerability; it is not a general claim that the file or application is safe." | |
| ), | |
| } | |
| provenance = { | |
| "source_repo": REPO, | |
| "source_commit": COMMIT, | |
| "source_version": VERSION, | |
| "source_file": relative_path, | |
| "source_url": source_url, | |
| "source_sha256": sha256(code), | |
| "source_license": "GPL-2.0-only", | |
| "source_spdx": "GPL-2.0-only", | |
| "source_license_basis": "The selected Java source file itself carries an explicit OWASP Benchmark GPL version 2 redistribution/modification notice.", | |
| "source_license_file": f"LICENSES/{LICENSE_OUTPUT}", | |
| "source_license_file_url": RAW + LICENSE_PATH, | |
| "source_license_file_sha256": LICENSE_SHA256, | |
| "expected_results_file": EXPECTED_RESULTS_PATH, | |
| "expected_results_url": expected_result_url, | |
| "expected_results_sha256": EXPECTED_RESULTS_SHA256, | |
| "expected_result_category": category, | |
| "expected_result_vulnerable": expected_vulnerable, | |
| "expected_result_cwe": f"CWE-{cwe}", | |
| "ai_ml_terms_review": ( | |
| "No AI/ML-specific grant or restriction was located in the selected source header or GPL-2.0 text. " | |
| "The source permits copying and distribution subject to GPL-2.0 conditions; this is not a separate " | |
| "legal determination about model training or downstream model distribution." | |
| ), | |
| "selection_policy": ( | |
| "A small balanced review subset: one expected-vulnerable and one expected-not-vulnerable case " | |
| "from each of the 11 categories in the pinned v1.2 expected-results file. Cases were selected by " | |
| "explicit ID and included only after verifying their own GPLv2 header and exact source hash." | |
| ), | |
| "curation_transformation": ( | |
| "The full Java source file is copied verbatim with its original GPLv2 header. Labels are copied " | |
| "from the matching expected-results CSV fields; code is not modified, compiled, or executed." | |
| ), | |
| "curation_modified_at": "2026-11-24", | |
| } | |
| instruction = ( | |
| "Classify this OWASP BenchmarkJava v1.2 test case using the benchmark's expected-result label for its " | |
| "category. Return the expected vulnerability status and CWE, and cite relevant code evidence. Interpret " | |
| "a negative benchmark result only for the designated test case and category; do not claim that the whole " | |
| "file or application is generally secure." | |
| ) | |
| return { | |
| "id": f"owasp-benchmarkjava-{test_name.lower()}", | |
| "task_type": "classify_benchmark_security_case", | |
| "domain": "web_application_security", | |
| "language": "Java", | |
| "instruction": instruction, | |
| "input": source_text, | |
| "output": json.dumps(label, ensure_ascii=False, sort_keys=True), | |
| "split": "benchmarkjava_review", | |
| **provenance, | |
| } | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--source-root", type=Path, help="Local checkout at the pinned commit; avoids network access") | |
| parser.add_argument("--staging", type=Path, default=Path("staging")) | |
| parser.add_argument("--licenses", type=Path, default=Path("LICENSES")) | |
| args = parser.parse_args() | |
| license_bytes = get_bytes(args.source_root, LICENSE_PATH) | |
| verify_hash(license_bytes, LICENSE_SHA256, "BenchmarkJava GPLv2 license") | |
| if b"GNU GENERAL PUBLIC LICENSE" not in license_bytes or b"Version 2, June 1991" not in license_bytes: | |
| raise ValueError("The pinned upstream license is not the expected GPL version 2 text") | |
| expected_bytes = get_bytes(args.source_root, EXPECTED_RESULTS_PATH) | |
| verify_hash(expected_bytes, EXPECTED_RESULTS_SHA256, "BenchmarkJava v1.2 expected-results CSV") | |
| expected_rows = parse_expected_rows(expected_bytes) | |
| args.staging.mkdir(parents=True, exist_ok=True) | |
| args.licenses.mkdir(parents=True, exist_ok=True) | |
| (args.licenses / LICENSE_OUTPUT).write_bytes(license_bytes) | |
| records = [] | |
| for test_name, spec in sorted(CASES.items()): | |
| relative_path = f"src/main/java/org/owasp/benchmark/testcode/{test_name}.java" | |
| code = get_bytes(args.source_root, relative_path) | |
| if test_name not in expected_rows: | |
| raise ValueError(f"No expected-results row for {test_name}") | |
| records.append(make_record(test_name, spec, code, expected_rows[test_name])) | |
| if len(records) != 22: | |
| raise ValueError(f"Expected exactly 22 BenchmarkJava records, got {len(records)}") | |
| output = args.staging / "benchmarkjava_review_examples.jsonl" | |
| with output.open("w", encoding="utf-8", newline="\n") as handle: | |
| for record in records: | |
| handle.write(json.dumps(record, ensure_ascii=False, sort_keys=True) + "\n") | |
| print(json.dumps({ | |
| "source_repo": REPO, | |
| "source_commit": COMMIT, | |
| "source_version": VERSION, | |
| "records": len(records), | |
| "vulnerable": sum(json.loads(row["output"])["expected_vulnerability"] for row in records), | |
| "not_vulnerable": sum(not json.loads(row["output"])["expected_vulnerability"] for row in records), | |
| "categories": sorted({CASES[name][0] for name in CASES}), | |
| "staging_file": str(output), | |
| "license_file": str(args.licenses / LICENSE_OUTPUT), | |
| "source_hashes": {row["id"]: row["source_sha256"] for row in records}, | |
| }, ensure_ascii=False, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |