ops/pii-hardware.sh 4.1 KiB 136 lines Raw
1 #!/usr/bin/env bash2 # Scan a public projection for host-specific hardware and operator facts.3 # The report contains rule ids and paths only; matched values are never4 # written. Patterns are assembled at runtime so this script does not match5 # itself.6 set -euo pipefail7 8 root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)9 report=10 quiet=011 paths=()12 binaries=()13 oci_dirs=()14 15 usage() {16     printf 'usage: %s [--report FILE] [--quiet] [--binary FILE] [--oci DIR] [PATH...]\n' "$0" >&217     exit 218 }19 20 while [ "$#" -gt 0 ]; do21     case $1 in22         --report) report=$2; shift 2 ;;23         --quiet) quiet=1; shift ;;24         --binary) binaries+=("$2"); shift 2 ;;25         --oci) oci_dirs+=("$2"); shift 2 ;;26         --help|-h) usage ;;27         --*) usage ;;28         *) paths+=("$1"); shift ;;29     esac30 done31 32 rules=(33     "gpu_sku|RT""X[ -]?(30|40|50)""[0-9]{2}|Ge""Force|Ra""deon R""X"34     "target_id|[12]x""30""60|[12]x""40""90|[12]x""50""90"35     "compute_cap|sm""(80|86|89|90|120)|gfx""(1030|1100|90a)"36     "vram_figure|118""00|122""88|245""76|125095""11680"37     "driver_version|615\.78\.08|620\.85\.02|580\.65\.06"38     "builder|root@""archiso"39     "hostname|seren""dipity"40     "home_path|/ho""me/[A-Za-z0-9._-]+"41     "operator_model|d1-2""b|ornith-1\\.5-9""b|flux-3-image-klei""n|musi""c3|parakeet-td""t|koko""ro|minimax-h""3|lfm-1\\.7""b|qwen3\\.8-27""b|qwen38-27""b|flux-de""v"42 )43 44 findings=$(mktemp)45 trap 'rm -f "$findings"' EXIT INT TERM46 scanned=047 48 scan_file() {49     local file=$1 label=$2 rule id pattern50     scanned=$((scanned + 1))51     for rule in "${rules[@]}"; do52         id=${rule%%|*}53         pattern=${rule#*|}54         if grep -aqE -- "$pattern" "$file" 2>/dev/null; then55             printf '%s\t%s\n' "$id" "$label" >>"$findings"56         fi57     done58 }59 60 scan_stream() {61     local label=$1 tmp62     tmp=$(mktemp)63     cat >"$tmp"64     scan_file "$tmp" "$label"65     rm -f "$tmp"66 }67 68 scan_archive() {69     local file=$1 member70     while IFS= read -r member; do71         tar -xOzf "$file" "$member" 2>/dev/null | scan_stream "$file:$member"72     done < <(tar -tzf "$file" 2>/dev/null)73 }74 75 scan_path() {76     local path=$1 file77     if [ -d "$path" ]; then78         while IFS= read -r -d '' file; do79             if gzip -t "$file" >/dev/null 2>&1 && tar -tzf "$file" >/dev/null 2>&1; then80                 scan_archive "$file"81             else82                 scan_file "$file" "$file"83             fi84         done < <(find "$path" -type f -print0 | sort -z)85     elif tar -tzf "$path" >/dev/null 2>&1; then86         scan_archive "$path"87     else88         scan_file "$path" "$path"89     fi90 }91 92 if [ "${#paths[@]}" -eq 0 ] && [ "${#binaries[@]}" -eq 0 ] && [ "${#oci_dirs[@]}" -eq 0 ]; then93     while IFS= read -r path; do94         [ -n "$path" ] && scan_file "$root/$path" "$path"95     done < <("$root/scripts/gate-public-tree.sh" --list)96 else97     for path in "${paths[@]}"; do scan_path "$path"; done98 fi99 for path in "${binaries[@]}"; do scan_file "$path" "$path"; done100 for path in "${oci_dirs[@]}"; do scan_path "$path"; done101 102 sort -u "$findings" -o "$findings"103 count=$(wc -l <"$findings" | tr -d ' ')104 json_escape() { printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'; }105 106 write_report() {107     local status=$1 first=1 id path108     {109         printf '{\n'110         printf '  "schema": "inf3r.hardware-gate/v1",\n'111         printf '  "scanner": "inf3r-hardware/1",\n'112         printf '  "status": "%s",\n' "$status"113         printf '  "scanned": %s,\n' "$scanned"114         printf '  "findings": %s,\n' "$count"115         printf '  "items": ['116         while IFS=$'\t' read -r id path; do117             [ -n "$id" ] || continue118             if [ "$first" -eq 1 ]; then first=0; else printf ','; fi119             printf '\n    {"rule": "%s", "path": "%s"}' "$(json_escape "$id")" "$(json_escape "$path")"120         done <"$findings"121         [ "$first" -eq 1 ] || printf '\n  '122         printf ']\n}\n'123     } >"$report"124 }125 126 if [ "$count" -eq 0 ]; then127     [ "$quiet" -eq 1 ] || printf 'hardware: ok (%s paths)\n' "$scanned"128     [ -n "$report" ] && write_report pass129     exit 0130 fi131 132 printf 'hardware: %s finding(s)\n' "$count" >&2133 cut -f1,2 "$findings" | sed 's/^/  /' >&2134 [ "$quiet" -eq 1 ] || true135 [ -n "$report" ] && write_report fail136 exit 1