From 9fb3cbd4fd6f5488ba37b705b6f9d445067b6559 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 6 Dec 2026 17:25:56 +0000 Subject: [PATCH] ci(lint): keep the job token away from renovate.json and pin image lines in place The renovate-config job clones in its own step and validates in a second step, so the job token is not in the environment that reads the pull request's renovate.json. check_bot_pr_diff.py now refuses a moved image line in every workflow whose pins a bot may move, lint.yml included, not only in image.yml and renovate.yml. Tests cover a moved lint.yml line, an added second Renovate line and a changed indentation. Refs: #188 Assisted-by: Claude Code --- .gitea/workflows/lint.yml | 13 +++++++++---- scripts/check_bot_pr_diff.py | 10 +++++----- tests/scripts/test_check_bot_pr_diff.py | 25 ++++++++++++++++++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index 2dc1d77..1558af5 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -157,8 +157,8 @@ jobs: container: image: renovate/renovate:44.161.0-full@sha256:ddbf02e44b542343c9df696b88fd953f04cc3ee5f771221805ff8efb26595416 steps: - # The image runs as a non-root user, so the clone goes to a directory it owns. - - name: Validate renovate.json + # The image runs as a non-root user, so the clone goes to the runner's temp directory. + - name: Checkout the commit under test env: GIT_CONFIG_COUNT: "1" GIT_CONFIG_KEY_0: http.extraheader @@ -167,10 +167,15 @@ jobs: REF: ${{ github.ref }} REPO_URL: ${{ github.server_url }}/${{ github.repository }} run: | - src="$(mktemp -d)" + src="${RUNNER_TEMP:-/tmp}/renovate-config" git clone "$REPO_URL" "$src" cd "$src" git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; } echo "Checked out $(git rev-parse HEAD)" - # --no-global: renovate.json is a repository config, not the bot's global one. + + # A step of its own, so the job token is not in the environment that reads renovate.json. + # --no-global: renovate.json is a repository config, not the bot's global one. + - name: Validate renovate.json + run: | + cd "${RUNNER_TEMP:-/tmp}/renovate-config" renovate-config-validator --strict --no-global renovate.json diff --git a/scripts/check_bot_pr_diff.py b/scripts/check_bot_pr_diff.py index d3d2676..44978a2 100644 --- a/scripts/check_bot_pr_diff.py +++ b/scripts/check_bot_pr_diff.py @@ -25,8 +25,8 @@ When starts with `renovate/`, the diff of ...HEAD may only `job_image: ubuntu:...@sha256:...` lines (the name stays); CI assigns the image versions and a human sets the matrix `base:`, so neither is the bot's to change; * .gitea/workflows/{lint,build,molecule,gitea-release-key}.yml: the version and digest of a - `git.rkdr.net/mortis/:@sha256:` pin (the name stays), including - the MOLECULE_IMAGE value; + `git.rkdr.net/mortis/:@sha256:` pin, in place (the name stays), + including the MOLECULE_IMAGE value; * .gitea/workflows/renovate.yml, and lint.yml's copy of it: the version and digest of the `image: renovate/renovate:-full@sha256:` line, in place; the name and the `-full` flavour stay (the job's report check needs the python3 that only -full ships). @@ -295,8 +295,8 @@ def parse_patch(text): def moved_problem(removed, added): """A reason when a changed line changes position, else None; both lists are (line number, line). - A `job_image:` pin moved to another matrix entry, or the Renovate image line moved out of its - `container:`, would change which job runs on it.""" + A `job_image:` pin moved to another matrix entry, or a workflow's image line moved to another + job, would change which job runs on it.""" if [n for n, unused in removed] != [n for n, unused in added]: return "moves or deletes a line (the changed lines must stay where they are)" return None @@ -347,7 +347,7 @@ def check(entries, patch_of, show=None): [line for unused, line in removed], [line for unused, line in added], ) - if not reason and path in (civ.MATRIX_FILE, RENOVATE_WORKFLOW): + if not reason and (path == civ.MATRIX_FILE or path in WORKFLOW_PINS): reason = moved_problem(removed, added) if reason: problems.append(f"{path}: {reason}") diff --git a/tests/scripts/test_check_bot_pr_diff.py b/tests/scripts/test_check_bot_pr_diff.py index 6d43fd3..e8ee19c 100644 --- a/tests/scripts/test_check_bot_pr_diff.py +++ b/tests/scripts/test_check_bot_pr_diff.py @@ -367,6 +367,7 @@ class RuleTest(unittest.TestCase): RENOVATE_NEW.replace(f"@{D2}", ""), RENOVATE_NEW.replace("44.170.2", "latest"), RENOVATE_NEW + " # evil", + RENOVATE_NEW.replace(" image:", " image:"), ): self.assertIsNotNone(self.problem(path, RENOVATE_OLD, new), (path, new)) @@ -627,16 +628,30 @@ class RepoTest(unittest.TestCase): self.assertEqual(code, 0, err) self.assertIn("2 file(s)", out) - def test_a_moved_renovate_image_line_is_refused(self): - path = ".gitea/workflows/renovate.yml" + def test_an_added_renovate_image_line_is_refused(self): + for path in (".gitea/workflows/renovate.yml", ".gitea/workflows/lint.yml"): + self.assertIsNotNone(cbd.file_problem(path, [], [RENOVATE_NEW]), path) + self.assertIsNotNone( + cbd.file_problem(path, [RENOVATE_OLD], [RENOVATE_NEW, RENOVATE_NEW]), + path, + ) + + def assert_moved_line_refused(self, path, anchor): self.edit(path, f"{RENOVATE_OLD}\n", "") - self.edit( - path, " run: renovate\n", f" run: renovate\n{RENOVATE_NEW}\n" - ) + self.edit(path, anchor, f"{anchor}{RENOVATE_NEW}\n") self.commit("bot") code, _unused, err = self.gate() self.assertEqual(code, 1, err) self.assertIn(path, err) + self.assertIn("moves or deletes a line", err) + + def test_a_moved_renovate_image_line_is_refused(self): + self.assert_moved_line_refused( + ".gitea/workflows/renovate.yml", " run: renovate\n" + ) + + def test_a_moved_image_line_in_lint_yml_is_refused(self): + self.assert_moved_line_refused(".gitea/workflows/lint.yml", "run: echo a\n") def bump_apm_cli(self): """The change Renovate makes for apm-cli 0.39.4, as uv 0.13.1 writes it."""