|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | +# Repair only a finding's policy file, after its canonical targets exist. |
| 4 | +set -euo pipefail |
| 5 | +REPO_PATH="${1:?Usage: $0 <repo-path> <finding-json>}" |
| 6 | +FINDING_JSON="${2:?Missing finding JSON file}" |
| 7 | +ruby - "$REPO_PATH" "$FINDING_JSON" <<'RUBY' |
| 8 | +require 'json' |
| 9 | +require 'pathname' |
| 10 | +
|
| 11 | +root = File.realpath(ARGV.fetch(0)) |
| 12 | +finding = JSON.parse(File.read(ARGV.fetch(1))) |
| 13 | +relative = finding['file'] || finding.dig('location', 'file') |
| 14 | +abort 'Finding must name a policy file' unless relative.is_a?(String) && !relative.empty? |
| 15 | +path = Pathname.new(relative) |
| 16 | +abort 'Finding path must be repository-relative' if path.absolute? || path.each_filename.include?('..') |
| 17 | +allowed = %w[Justfile justfile].include?(relative) || |
| 18 | + (path.dirname.to_s == '.github/workflows' && %w[.yml .yaml].include?(path.extname)) || |
| 19 | + (%w[scripts .githooks].include?(path.dirname.to_s) && path.extname == '.sh') |
| 20 | +target = File.join(root, relative) |
| 21 | +unless allowed && !File.symlink?(target) && File.realpath(target).start_with?(root + '/') |
| 22 | + abort 'Refusing a path outside supported repository policy files' |
| 23 | +end |
| 24 | +pattern = %r{\.machine_readable/(?:6a2/)?(STATE|META|ECOSYSTEM|AGENTIC|NEUROSYM|PLAYBOOK|ANCHOR)\.a2ml} |
| 25 | +content = File.read(target) |
| 26 | +names = content.scan(pattern).flatten.uniq |
| 27 | +names.each do |name| |
| 28 | + canonical = File.join(root, '.machine_readable/descriptiles', name + '.a2ml') |
| 29 | + unless !File.symlink?(canonical) && File.file?(canonical) && File.realpath(canonical).start_with?(root + '/') |
| 30 | + abort "Canonical descriptile missing or unsafe: #{name}; reconcile files before repairing policy" |
| 31 | + end |
| 32 | +end |
| 33 | +updated = content.gsub(pattern) { ".machine_readable/descriptiles/#{$1}.a2ml" } |
| 34 | +File.write(target, updated) unless updated == content |
| 35 | +puts "#{relative}: #{names.length} descriptile reference target(s) reconciled" |
| 36 | +RUBY |
0 commit comments