Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions infra/github-backup/bin/ghb_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ def validate_state(state, source="state.json"):
same rule. A non-object top level invalidates the whole file as
a single named problem.

A per-repo "last_fetch" must be a numeric timestamp (never a
bool): fetch_mirrors does arithmetic on it, so a wrong-shape
value is dropped under a named problem (#6821) instead of
passing through to crash the fetch loop.

Bookkeeping keys written by the webhook drainer are state too,
under their own shapes: "drain_last::<repo>" is a numeric
timestamp and "drain_fail::<repo>" is an object. They are
Expand Down Expand Up @@ -459,6 +464,20 @@ def validate_state(state, source="state.json"):
f"expected a list — reset to []"
)
entry["fail_days"] = []
last_fetch = entry.get("last_fetch")
if (
not repo.startswith("drain_")
and last_fetch is not None
and (
not isinstance(last_fetch, (int, float))
or isinstance(last_fetch, bool)
)
):
problems.append(
f"{source}: 'last_fetch' for {repo!r} is "
f"{_shape(last_fetch)}, expected a number — dropped"
)
del entry["last_fetch"]
clean[repo] = entry
return clean, problems

Expand Down
49 changes: 49 additions & 0 deletions infra/github-backup/tests/test_fetch_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,54 @@ def fake_strftime(fmt):
self.assertIn("timed out", log.lower())


class FetchMirrorsLastFetchShapeTests(unittest.TestCase):
def test_wrong_shape_last_fetch_does_not_crash_the_loop(self):
# Oracle (#6821): a string last_fetch on one repo must not
# stop the daily fetch — the poisoned repo is fetched, the
# fresh repo coalesce-skips, state is rewritten, and the
# manifest is regenerated.
import time as time_mod

with tempfile.TemporaryDirectory(prefix="ghb-fetch-shape-") as root:
with open(os.path.join(root, "plan.json"), "w") as plan_file:
json.dump(
[{"repo": "owner/poison"}, {"repo": "owner/fresh"}],
plan_file,
)
initial_state = {
"owner/poison": {"last_fetch": "2026-11-01T00:00:00Z"},
"owner/fresh": {"last_fetch": time_mod.time()},
}
with open(os.path.join(root, "state.json"), "w") as state_file:
json.dump(initial_state, state_file)
success = SimpleNamespace(returncode=0, stdout="", stderr="")
with (
patch.object(fetch_mirrors, "load_config", return_value={}),
patch.object(fetch_mirrors, "backup_root", return_value=root),
patch.object(
fetch_mirrors,
"mirror_path",
side_effect=lambda _root, repo: f"/mirror/{repo}",
),
patch.object(fetch_mirrors, "valid_mirror", return_value=True),
patch.object(fetch_mirrors.manifest_mod, "build") as build_manifest,
patch.object(
fetch_mirrors.subprocess, "run", return_value=success
) as run,
contextlib.redirect_stdout(io.StringIO()),
contextlib.redirect_stderr(io.StringIO()) as err,
):
exit_code = fetch_mirrors.main(["fetch_mirrors.py"])
self.assertEqual(exit_code, 0)
self.assertIn("last_fetch", err.getvalue())
self.assertEqual(run.call_count, 1) # only the poisoned repo fetched
self.assertEqual(run.call_args_list[0].args[0][2], "/mirror/owner/poison")
build_manifest.assert_called_once_with(root)
with open(os.path.join(root, "state.json")) as state_file:
state = json.load(state_file)
self.assertIsInstance(state["owner/poison"]["last_fetch"], float)
self.assertIsInstance(state["owner/fresh"]["last_fetch"], float)


if __name__ == "__main__":
unittest.main()
45 changes: 45 additions & 0 deletions infra/github-backup/tests/test_plan_state_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ def test_drain_last_with_wrong_shape_is_named_and_dropped(self):
self.assertEqual(len(problems), 2)
self.assertIn("drain_last::a/b", problems[0])

def test_wrong_shape_last_fetch_is_named_and_dropped(self):
# Oracle (#6821): fetch_mirrors.main does arithmetic on
# last_fetch, so any non-number shape must surface as a named
# problem at validation and never reach the fetch loop.
for bad in ["2026-11-01T00:00:00Z", [1762000000], {"ts": 1}, True]:
clean, problems = ghb_common.validate_state(
{"a/b": {"last_fetch": bad, "fail_days": []}}
)
self.assertEqual(len(problems), 1, bad)
self.assertIn("last_fetch", problems[0])
self.assertIn("a/b", problems[0])
self.assertNotIn("last_fetch", clean["a/b"])
self.assertEqual(clean["a/b"]["fail_days"], [])

def test_drain_fail_entry_inner_fields_are_preserved(self):
# drain_fail::<repo> is a drainer bookkeeping object, preserved
# verbatim under its own shape (#6637) — its inner fields are
# not per-repo fetch state and are not last_fetch-validated.
entry = {"count": 2, "last_fetch": {}}
clean, problems = ghb_common.validate_state({"drain_fail::a/b": entry})
self.assertEqual(problems, [])
self.assertEqual(clean["drain_fail::a/b"], entry)

def test_numeric_last_fetch_passes_unchanged(self):
for good in [1762000000, 1762000000.5, 0]:
clean, problems = ghb_common.validate_state(
{"a/b": {"last_fetch": good}}
)
self.assertEqual(problems, [])
self.assertEqual(clean["a/b"]["last_fetch"], good)

def test_non_list_fail_days_is_reset_with_problem(self):
clean, problems = ghb_common.validate_state(
{"a/b": {"fail_days": "2026-01-01"}}
Expand Down Expand Up @@ -490,6 +521,20 @@ def state_oracle(state):
if days is not None and not isinstance(days, list):
entry["fail_days"] = []
problems += 1
# #6821: a wrong-shape per-repo last_fetch is dropped
# under a named problem; drain bookkeeping entries are
# preserved under their own shapes.
last_fetch = entry.get("last_fetch")
if (
not key.startswith("drain_")
and last_fetch is not None
and (
not isinstance(last_fetch, (int, float))
or isinstance(last_fetch, bool)
)
):
del entry["last_fetch"]
problems += 1
clean[key] = entry
return clean, problems

Expand Down
Loading