#!/usr/bin/env python3 """Validate Reportist contract v1 from a YAML or JSON report.""" import argparse import json import math from pathlib import Path import sys try: from jsonschema import Draft202012Validator, FormatChecker import yaml except ImportError: raise SystemExit("Prepare test dependencies with: ./test.sh") SCHEMA = json.loads((Path(__file__).resolve().parents[1] / "schema/report.schema.json").read_text()) Draft202012Validator.check_schema(SCHEMA) VALIDATOR = Draft202012Validator(SCHEMA, format_checker=FormatChecker()) def unique_object(pairs): result = {} for key, value in pairs: if not isinstance(key, str): raise ValueError("mapping keys must be strings") if key in result: raise ValueError(f"duplicate mapping key: {key}") result[key] = value return result class ReportLoader(yaml.SafeLoader): """Safe YAML restricted to the JSON data model, without duplicate keys.""" def construct_mapping(loader, node): return unique_object((loader.construct_object(k), loader.construct_object(v)) for k, v in node.value) ReportLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping) def check_json_value(value, ancestors=None): ancestors = set() if ancestors is None else ancestors if value is None or isinstance(value, (str, bool, int)): return if isinstance(value, float): if not math.isfinite(value): raise ValueError("non-finite numbers are not supported") return if isinstance(value, (dict, list)): if id(value) in ancestors: raise ValueError("cyclic YAML aliases are not supported") ancestors.add(id(value)) if isinstance(value, dict): if any(not isinstance(key, str) for key in value): raise ValueError("mapping keys must be strings") children = value.values() else: children = value for child in children: check_json_value(child, ancestors) ancestors.remove(id(value)) return raise ValueError("report values must use JSON types; quote YAML dates and timestamps") def load_report(path): path = Path(path) text = path.read_text(encoding="utf-8") if path.suffix.lower() == ".json": document = json.loads(text, object_pairs_hook=unique_object) elif path.suffix.lower() in (".yaml", ".yml"): document = yaml.load(text, Loader=ReportLoader) else: raise ValueError("expected a .yaml, .yml, or .json report") check_json_value(document) return document def validate_document(document): check_json_value(document) errors = [] for error in VALIDATOR.iter_errors(document): path = "/" + "/".join(str(p).replace("~", "~0").replace("/", "~1") for p in error.absolute_path) # oneOf's headline is intentionally concise; its path locates the offending block. errors.append(f"{path}: {error.message}") if errors: return sorted(errors) ids = set() for index, finding in enumerate(document.get("findings", [])): if "id" in finding: if finding["id"] in ids: errors.append(f"/findings/{index}/id: duplicate finding ID") ids.add(finding["id"]) return errors def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("input", type=Path) args = parser.parse_args(argv) try: errors = validate_document(load_report(args.input)) except (OSError, ValueError, yaml.YAMLError, RecursionError) as error: print(f"error: {error}", file=sys.stderr) return 1 if errors: print("\n".join(errors), file=sys.stderr) return 1 print(f"Valid report v1: {args.input}") return 0 if __name__ == "__main__": sys.exit(main())