#!/usr/bin/env bash set -euo pipefail usage(){ echo "Usage: $0 INPUT [--evidence-root DIR]" >&2; exit 2; }; fail(){ echo "error: $*" >&2; exit 1; } sha256_file(){ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1"|awk '{print $1}'; elif command -v sha256 >/dev/null 2>&1; then sha256 -q "$1"; else openssl dgst -sha256 "$1"|awk '{print $NF}'; fi; } input=${1:-}; [[ -n "$input" ]]||usage; shift; evidence_root= while (($#)); do case $1 in --evidence-root) evidence_root=$2;shift 2;;*) usage;;esac; done jq -e ' type=="object" and .schema_version==1 and (.metadata|type)=="object" and (.metadata.producer.name|type)=="string" and (.metadata.producer.version|type)=="string" and (.metadata.source.artifact|type)=="string" and (.metadata.source.sha256|test("^[a-f0-9]{64}$")) and (.collection.sections|type)=="object" and (.observations|type)=="array" and (.findings|type)=="array" and (([.observations[].id]|length)==([.observations[].id]|unique|length)) and (([.findings[].id]|length)==([.findings[].id]|unique|length)) and ([.observations[].section] - ([.collection.sections|keys[]]))==[] and (([.observations[].id]) as $ids | all(.findings[].evidence[]?; $ids|index(.)!=null)) and (if .collection.status=="complete" then (.collection.sections|length)>0 and all(.collection.sections[];.status=="ok" and (.truncated|not)) else true end) ' "$input" >/dev/null || fail "invalid diagnostic v1: $input" if [[ -n "$evidence_root" ]]; then artifact=$(jq -r '.metadata.source.artifact' "$input") [[ "$artifact" != /* && "$artifact" != *..* && "$artifact" != *\\* ]] || fail "source artifact escapes evidence root" source=$(realpath "$evidence_root/$artifact"); root=$(realpath "$evidence_root") [[ "$source" == "$root"/* && -f "$source" ]] || fail "source artifact escapes evidence root" expected=$(jq -r '.metadata.source.sha256' "$input"); actual=$(sha256_file "$source") [[ "$actual" == "$expected" ]] || fail "evidence checksum differs" jq -e --slurpfile raw "$source" ' $raw[0] as $raw | all(.observations[]; . as $observation | (.source_pointer|split("/")[1:]|map(gsub("~1";"/")|gsub("~0";"~")|if test("^[0-9]+$") then tonumber else . end)) as $path | (try (($raw|getpath($path)) == $observation.value) catch false)) ' "$input" >/dev/null || fail "observation differs from referenced raw evidence" fi echo "Valid diagnostic v1: $input"