Validate relay-directory changes in PR CI

The weekly automation validates a relay snapshot before it pushes its
branch, but that check lives inside the scheduled job. A change to the
reviewed relay directory that arrives as a pull request is validated by
nothing at all, which is the ground two relay-data PRs were closed on:
"PR CI doesn't run the validator, and untraceable relay-directory
updates aren't something we can merge on faith" (#1632, also #1673).

This runs the validator on a pull request touching relays/ or the
validator itself, against the base branch's reviewed copy, and publishes
the row count, unique-relay count and SHA-256 to the job summary. The
hash is the point: the relay automation already publishes one for every
snapshot it proposes, so a reviewer can now match a proposed file
against it instead of taking provenance on faith.

The gate deliberately runs the base branch's validator rather than the
pull request's copy of it. Otherwise a change that edits the data and
the validator together certifies itself -- dropping 86% of the directory
passes if the same commit relaxes MIN_BASELINE_FRACTION, and the summary
still reports "validated". The scheduled job validates from a main
checkout for the same reason. The pull request's own validator is still
exercised by the unit-test step.

A pull_request run executes the workflow definition from the pull
request's own head, so a pull request can always edit this file and
change its own gate. That path is visible in the diff, which is what
review is for; what the reviewed-validator gate closes is the quiet one.

Read-only and never pull_request_target: the job runs PR-authored code,
so it holds no write scope and no secrets. checkout is @v5 like the
other read-only PR workflows rather than SHA-pinned, since that pin
exists to protect the write-capable token in fetch_georelays.yml.

What it catches: malformed rows, out-of-range coordinates, insecure
hosts, conflicting duplicate relays, and snapshots that lose half or
double the baseline's relays. What it does not: a re-ordered file whose
rows are each valid still passes -- with a different hash, which the
summary makes visible.

The accompanying test asserts the job's security-relevant shape as text,
following the precedent in test_fetch_georelays_workflow.py: the
trigger, the absent write scope, --output never pointed at the tracked
CSV, and that the gate reads the validator from the base branch.
This commit is contained in:
Jozef Koval committed 2026-09-15 23:15:30 +02:00
1 parent 9b84b36122
commit d627b9fc2c
2 files changed
+168

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
name: Validate GeoRelay Data
# The weekly automation validates a relay snapshot before pushing its branch,
# but that check lives inside the scheduled job: a relay-directory change that
# arrives as a pull request is otherwise reviewed on faith. This runs the same
# validator against the reviewed baseline and publishes the resulting hash, so
# a proposed snapshot can be matched against the one the automation published.
on:
pull_request:
paths:
- "relays/online_relays_gps.csv"
- "scripts/validate_georelays.py"
- "scripts/tests/**"
- ".github/workflows/validate_georelays.yml"
# This job runs PR-authored code. Read-only, no secrets, and never
# pull_request_target.
permissions:
contents: read
# A superseded run has nothing left to report; the sibling PR workflows do not
# set this, so drop it if the repo prefers them uniform.
concurrency:
group: validate-georelays-${{ github.ref }}
cancel-in-progress: true
jobs:
validate-relay-directory:
name: Validate relay directory
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout pull request
# @v5 matches the sibling read-only PR workflows. fetch_georelays.yml
# pins a SHA for a reason that does not apply here — it protects the
# code that receives that job's write-capable token; this job has none.
uses: actions/checkout@v5
with:
persist-credentials: false
- name: Test GeoRelay validator
# Exercises the pull request's own validator and workflow tests. The
# gate below deliberately does not use that copy.
run: python3 -m unittest discover -s scripts/tests -p "test_*.py" -v
- name: Validate relay directory against the reviewed baseline
run: |
set -euo pipefail
# actions/checkout gives a pull_request run the merge ref, not the
# base branch, so the reviewed copy has to be fetched explicitly.
git fetch --no-tags --depth=1 origin "+refs/heads/${GITHUB_BASE_REF}:refs/remotes/origin/${GITHUB_BASE_REF}"
git show "origin/${GITHUB_BASE_REF}:relays/online_relays_gps.csv" > "$RUNNER_TEMP/baseline.csv"
# Gate with the reviewed validator rather than the pull request's copy,
# or a change that edits data and validator together certifies itself:
# dropping 86% of the directory passes if the same commit relaxes
# MIN_BASELINE_FRACTION. The scheduled job validates from a main
# checkout for the same reason. Stdlib-only, so a copy runs standalone.
git show "origin/${GITHUB_BASE_REF}:scripts/validate_georelays.py" > "$RUNNER_TEMP/reviewed-validator.py"
# --output is required and receives a verbatim copy of --input. It
# stays in RUNNER_TEMP so validation can never write to the tracked
# file it is checking.
python3 "$RUNNER_TEMP/reviewed-validator.py" \
--input relays/online_relays_gps.csv \
--baseline "$RUNNER_TEMP/baseline.csv" \
--output "$RUNNER_TEMP/validated-copy.csv" \
--github-output "$RUNNER_TEMP/summary.txt"
# Publish the hash so a reviewer can match a proposed snapshot against
# the one the automation published, instead of taking it on faith.
{
echo "### GeoRelay validation"
echo
sed 's/^/- /' "$RUNNER_TEMP/summary.txt"
} >> "$GITHUB_STEP_SUMMARY"
@@ -0,0 +1,90 @@
import re
from pathlib import Path
import unittest
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
WORKFLOW_PATH = REPOSITORY_ROOT / ".github/workflows/validate_georelays.yml"
RELAY_DATA_PATH = "relays/online_relays_gps.csv"
VALIDATOR_PATH = "scripts/validate_georelays.py"
class ValidateGeoRelaysWorkflowTests(unittest.TestCase):
"""The relay directory is reviewed data: a PR that changes it must be
checked by the same validator the weekly automation runs, and must publish
the hash a reviewer needs to match a proposed snapshot against the
automation's own published one."""
@classmethod
def setUpClass(cls) -> None:
cls.workflow = WORKFLOW_PATH.read_text(encoding="utf-8")
# Comments explain why a construct is absent and would otherwise trip
# the assertions looking for it, so directives are checked without them.
cls.directives = "\n".join(
line for line in cls.workflow.splitlines() if not line.lstrip().startswith("#")
)
def test_runs_on_untrusted_pull_request_trigger(self) -> None:
self.assertIn("pull_request:", self.directives)
# pull_request_target would run PR-authored code with repository secrets.
self.assertNotIn("pull_request_target", self.directives)
def test_job_holds_no_write_scope(self) -> None:
self.assertIn("permissions:\n contents: read", self.directives)
write_scope = re.search(r"^\s*[\w-]+:\s*write\s*$", self.directives, re.MULTILINE)
self.assertIsNone(write_scope, "a job running PR-authored code must not hold a write scope")
def test_checkout_does_not_persist_credentials(self) -> None:
self.assertIn("uses: actions/checkout@", self.directives)
self.assertIn("persist-credentials: false", self.directives)
def test_relay_data_changes_trigger_the_job(self) -> None:
paths_block = re.search(r"paths:\n((?:\s+- .+\n)+)", self.directives)
self.assertIsNotNone(paths_block)
self.assertIn(RELAY_DATA_PATH, paths_block.group(1))
self.assertIn(VALIDATOR_PATH, paths_block.group(1))
def test_validates_against_the_base_branch_copy(self) -> None:
required_fragments = [
"python3 -m unittest discover -s scripts/tests -p \"test_*.py\" -v",
f"--input {RELAY_DATA_PATH}",
"python3 \"$RUNNER_TEMP/reviewed-validator.py\"",
"--baseline \"$RUNNER_TEMP/baseline.csv\"",
f"git show \"origin/${{GITHUB_BASE_REF}}:{RELAY_DATA_PATH}\"",
"--github-output",
"$GITHUB_STEP_SUMMARY",
]
for fragment in required_fragments:
with self.subTest(fragment=fragment):
self.assertIn(fragment, self.directives)
def test_gate_runs_the_reviewed_validator_not_the_pull_request_copy(self) -> None:
# A change that edits the relay data and the validator together would
# otherwise certify itself: dropping most of the directory passes if the
# same commit relaxes a threshold. The gate must read the base branch's
# validator; the PR's copy is exercised by the unit-test step instead.
self.assertIn(
f'git show "origin/${{GITHUB_BASE_REF}}:{VALIDATOR_PATH}"',
self.directives,
)
self.assertIsNone(
re.search(rf"python3\s+\S*{re.escape(VALIDATOR_PATH)}", self.directives),
"the gate must not run the pull request's own validator, however the path is spelled",
)
def test_base_ref_is_fetched_before_it_is_read(self) -> None:
# actions/checkout gives a pull_request run the merge ref, so the base
# branch is not present unless the job fetches it first.
fetched = self.directives.index('git fetch --no-tags --depth=1 origin "+refs/heads/${GITHUB_BASE_REF}')
read = self.directives.index('git show "origin/${GITHUB_BASE_REF}')
self.assertLess(fetched, read)
def test_validation_cannot_write_to_the_reviewed_file(self) -> None:
# --output is required and receives a verbatim copy of --input. Pointed
# at the tracked file, a validation run would write to reviewed data.
self.assertIn("--output \"$RUNNER_TEMP/", self.directives)
self.assertNotIn(f"--output {RELAY_DATA_PATH}", self.directives)
if __name__ == "__main__":
unittest.main()