diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ef3947e..971f9a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -147,6 +147,16 @@ jobs: - name: Custom mods (no public listing to pin against) are reviewed run: python3 tests/test_custom_mods_reviewed.py + # The save itself must never be tracked. It was, on main, in #64 and + # #67: the droplet pulled it on 2026-10-11 and the live world was + # replaced by a local snapshot. Untracking it again in #68 is just as + # dangerous in principle, because dropping a path from the index + # deletes it from the working tree on the next checkout. Either way + # the save is the one thing on this project that cannot be restored + # from the repo, so it does not travel in the repo. + - name: data/world/ is not tracked (a deploy must not rewrite the save) + run: python3 tests/test_world_not_tracked.py + - name: No per-player data tracked in data/world/ (public repo) run: python3 tests/test_world_no_player_data.py diff --git a/gitops/deploy.sh b/gitops/deploy.sh index c05d5d3..880802a 100755 --- a/gitops/deploy.sh +++ b/gitops/deploy.sh @@ -115,6 +115,27 @@ if [ "${REQUIRE_CI:-true}" = "true" ]; then log "CI green (${succeeded}/${total}) for ${NEW_SHA:0:8}" fi +# A revision that touches data/world/ rewrites the live save, in either +# direction. Region files are binary, so a pull overwrites whatever players +# have built; and a revision that UNTRACKS them deletes the save outright, +# because dropping a path from the index deletes it from the working tree on +# the next checkout. Both have been on main already: the world was tracked in +# #64 and #67, deployed here on 2026-10-11, and untracked again in #68. +# +# This box holds the only real copy of the world, so it refuses rather than +# trusting main to stay correct. Loud (exit 1, every tick) on purpose: a main +# that would eat the save needs a human, not a quiet skip. +# +# Captured into a variable rather than piped into grep, for the same +# SIGPIPE-under-pipefail reason documented at kubejs_changed below. +world_touched=$(git diff --name-only "$OLD_SHA" "$NEW_SHA" -- data/world || true) +if [ -n "$world_touched" ]; then + log "REFUSING ${NEW_SHA:0:8}: it changes data/world/, the live save" + printf '%s\n' "$world_touched" | sed -n '1,10p' + log "data/world/ must stay untracked; see .gitignore and tests/test_world_not_tracked.py" + exit 1 +fi + gh_deployment_create "$NEW_SHA" # Back up before touching a running world. Restores are the real safety net; diff --git a/tests/test_world_no_player_data.py b/tests/test_world_no_player_data.py index 57edca9..2e76c05 100644 --- a/tests/test_world_no_player_data.py +++ b/tests/test_world_no_player_data.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 """No per-player data may ever be tracked in data/world/, in a PUBLIC repo. -data/world/ is tracked so contributors can pull a consistent world to develop -and test against. What must never be tracked alongside it is anything that -identifies or describes a specific real kid: exact inventory, position, +data/world/ is NOT tracked at all any more - tests/test_world_not_tracked.py +is what enforces that, and it is the check that matters. This one is the +second line of defence for the case where the world becomes tracked again +regardless: whatever else were to ship, nothing that identifies or +describes a specific real kid: exact inventory, position, playtime, or - as of the hub feature in hub.js - their saved home coordinates. See .gitignore's comments next to data/world/ for the full list and why each entry is there. diff --git a/tests/test_world_not_tracked.py b/tests/test_world_not_tracked.py new file mode 100644 index 0000000..10c6a2e --- /dev/null +++ b/tests/test_world_not_tracked.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""data/world/ must never be tracked: a git deploy must not rewrite the save. + +This is the tripwire for an incident that already happened. data/world/ was +tracked on main (PR #64, #67) so a hub built on the local test server could be +shipped to the droplet once. That worked, and then the mechanism stayed armed: +on 2026-10-11 the droplet pulled it and the live world was replaced by the +local snapshot, visible in the deployment statuses as the world seed changing +from 1691256543523180978 to 2945200692062874765. + +Tracking the save cannot be made safe, in either direction: + + - Region files are binary, so git cannot merge them. Whichever side is + deployed next silently destroys what the other side built. + - Untracking them is just as destructive. Dropping a path from the index + means the next checkout DELETES it from the working tree, so the very + commit that cleans this up can erase the live save. + - playerdata/, stats/ and advancements/ are the players' own save data: + inventories, positions, and the homes hub.js stores for them. + +Content that SHOULD ship through git lives outside data/world/: KubeJS scripts +and their configs, and the mod list in docker-compose.yml. + +Four independent checks, because they stop different mistakes. The .gitignore +rule stops an accidental "git add -A". The tracked-file check stops a +"git add -f", a merge of a branch that still tracks the world (every branch +cut before 2026-10-11 does), or anyone deleting the ignore rule. The last two +check the droplet's own refusal in gitops/deploy.sh, which is what protects +the save when a bad revision reaches main anyway - CI gates the repo, but only +the droplet gates the live world. +""" +import os +import shutil +import subprocess +import sys +import tempfile + +WORLD_PREFIX = "data/world/" +DEPLOY = "gitops/deploy.sh" + +# Any of these ignores the whole directory. Matched as a rule on its own line, +# so a mention inside a comment does not read as protection. +IGNORE_RULES = ("data/world/", "data/world", "/data/world/", "/data/world") + + +def git(args, cwd=None, check=True): + return subprocess.run( + ["git"] + args, cwd=cwd, capture_output=True, text=True, check=check + ).stdout + + +def tracked_world_files(): + return [f for f in git(["ls-files"]).splitlines() + if f.startswith(WORLD_PREFIX)] + + +def gitignore_has_rule(): + with open(".gitignore", encoding="utf-8") as f: + return any(line.strip() in IGNORE_RULES for line in f) + + +def deploy_guard_runs_before_pull(): + """The guard is worthless if it sits after the pull that does the damage.""" + with open(DEPLOY, encoding="utf-8") as f: + lines = f.read().splitlines() + guard = next((i for i, l in enumerate(lines) + if l.startswith("world_touched=")), None) + pull = next((i for i, l in enumerate(lines) + if "git pull --ff-only" in l), None) + if guard is None or pull is None: + return False, "guard=%s pull=%s (one of them is missing)" % (guard, pull) + if guard > pull: + return False, "guard is at line %d, after the pull at line %d" % ( + guard + 1, pull + 1) + return True, "guard at line %d, pull at line %d" % (guard + 1, pull + 1) + + +def detection_works_both_ways(): + """Prove the git command the guard relies on actually sees both cases. + + Built as a throwaway repo rather than asserting on the string, because the + thing that has to be true is the BEHAVIOUR: a revision that adds world + files and a revision that removes them must both be detected. The removal + direction is the one that deletes a live save, and it is the easy one to + forget to test. + """ + tmp = tempfile.mkdtemp(prefix="worldguard-") + try: + git(["init", "-q", tmp]) + git(["config", "user.email", "t@example.invalid"], cwd=tmp) + git(["config", "user.name", "test"], cwd=tmp) + + with open(os.path.join(tmp, "readme"), "w") as f: + f.write("code only") + git(["add", "-A"], cwd=tmp) + git(["commit", "-qm", "no world"], cwd=tmp) + base = git(["rev-parse", "HEAD"], cwd=tmp).strip() + + region = os.path.join(tmp, "data", "world", "region") + os.makedirs(region) + with open(os.path.join(region, "r.0.0.mca"), "w") as f: + f.write("binary-ish") + git(["add", "-A"], cwd=tmp) + git(["commit", "-qm", "track the world"], cwd=tmp) + tracked = git(["rev-parse", "HEAD"], cwd=tmp).strip() + + git(["rm", "-rq", "--cached", "data/world"], cwd=tmp) + git(["commit", "-qm", "untrack the world"], cwd=tmp) + untracked = git(["rev-parse", "HEAD"], cwd=tmp).strip() + + def guard_sees(old, new): + out = git(["diff", "--name-only", old, new, "--", "data/world"], + cwd=tmp) + return bool(out.strip()) + + return { + "adding world files is detected": guard_sees(base, tracked), + "untracking world files is detected": guard_sees(tracked, untracked), + "a code-only revision is NOT flagged": not guard_sees(base, base), + } + finally: + shutil.rmtree(tmp, ignore_errors=True) + + +def main() -> int: + failed = False + + world_files = tracked_world_files() + if world_files: + failed = True + print("FAIL: %d file(s) under %s are tracked." + % (len(world_files), WORLD_PREFIX)) + for f in world_files[:10]: + print(" " + f) + if len(world_files) > 10: + print(" ... and %d more" % (len(world_files) - 10)) + print("\n The live world is the only real one. Deploying a commit " + "that tracks") + print(" it overwrites what players built; a commit that untracks " + "it deletes") + print(" their save. Untrack with: git rm -r --cached data/world") + else: + print(" no tracked files under " + WORLD_PREFIX) + + if gitignore_has_rule(): + print(" .gitignore ignores " + WORLD_PREFIX) + else: + failed = True + print("FAIL: .gitignore has no rule ignoring " + WORLD_PREFIX) + print(" Without it, one 'git add -A' re-arms the whole problem.") + + ok, detail = deploy_guard_runs_before_pull() + if ok: + print(" " + DEPLOY + " refuses a world-touching revision (" + detail + ")") + else: + failed = True + print("FAIL: " + DEPLOY + " does not guard the save before pulling: " + + detail) + + for label, passed in detection_works_both_ways().items(): + if passed: + print(" " + label) + else: + failed = True + print("FAIL: " + label + " - it is not") + + if failed: + return 1 + print(" PASS: the live save cannot be rewritten by a deploy") + return 0 + + +if __name__ == "__main__": + sys.exit(main())