From e45b92551aba06dc37f6315e3174ceead132e4a6 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sat, 21 Nov 2026 18:52:22 +0000 Subject: [PATCH 1/2] ci: lint workflows with zizmor Add zizmor (v1.32.0, pinned by SHA) as a pre-commit hook over .gitea/workflows, offline at its default level; it fails on pull_request_target and on ${{ }} expanded into run:. The ci image bakes and smoke-tests the new hook environment, so its version moves to 0.2.32. image.yml drops `packages: write` and the job-token probe: Gitea 28 refuses the job token for organisation packages, and the latest build used the org push token. One probe of that token now fails fast. Refs #145 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG --- .gitea/workflows/image.yml | 61 +++++++++---------------- .pre-commit-config.yaml | 12 +++++ CONTRIBUTING.md | 3 +- docs/images/ci.md | 2 +- images/ci/Dockerfile | 4 +- tests/scripts/test_check_bot_pr_diff.py | 8 ++++ 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/.gitea/workflows/image.yml b/.gitea/workflows/image.yml index 55c792b..a9175ad 100644 --- a/.gitea/workflows/image.yml +++ b/.gitea/workflows/image.yml @@ -22,12 +22,12 @@ name: Images # same Ubuntu release as the FROM lines of the image's Dockerfile (check_image_versions.py # pins enforces it; run 2529 broke a 24.04 build in a 26.04 job container: GLIBC_2.42 # not found). -# - Credentials: the job token is tried first (permissions: packages: write). Gitea 28 -# refuses it for an organisation-owned package (HTTP 401 on a write probe, run 2237: -# the Actions user is not an org member, services/context/package.go, TODO ActionUser -# permission check). Only if a write probe is refused are the org secrets -# PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN used. The log names the method that worked, -# never a value. +# - Credentials: the push uses the org secrets PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN only. +# The job token cannot write an organisation-owned package on Gitea 28 (HTTP 401 on a +# write probe: the Actions user is not an org member), so the workflow asks for no +# packages permission. One write probe checks the token before anything is built and +# fails the job when it is refused. Revisit when a Gitea release lets the job token +# write organisation packages. # - Layers: every layer stays far below Cloudflare's 100 MB request-body limit (see the # Dockerfile). No kaniko cache: a tiny, rarely rebuilt image gains little, and cache # layers would be pushed through the same Cloudflare-limited path. @@ -48,7 +48,6 @@ on: # yamllint disable-line rule:truthy permissions: contents: read - packages: write # One image build at a time; a later run must not cancel a push in progress. concurrency: @@ -88,7 +87,7 @@ jobs: dockerfile: Dockerfile job_image: ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db - name: ci - version: 0.2.31 + version: 0.2.32 subpath: "" dockerfile: images/ci/Dockerfile job_image: ubuntu:26.04@sha256:d66b366dac61b575bee2aef07e9c26e8070096fbe84a3d4d3eb0da069b5d76db @@ -133,15 +132,13 @@ jobs: # Its CA bundle (layers[1]) and executor (layers[9], both 0-based) are fetched by content digest and # verified with sha256sum. # - # The credential: the job token first; the org secrets only after a write probe was - # refused. Credentials reach commands only through the environment and curl's stdin + # The credential: the org push token, checked by one write probe before the build. + # Credentials reach commands only through the environment and curl's stdin # config, never the argument list or the log. Writes /kaniko/.docker/config.json. - name: Prepare kaniko and select the registry credential env: KANIKO_CA_LAYER_SHA256: 5ecab5922e6a5124fab96e8cb404b8cf93e8a96716af4fbbea2a9221b1216f86 KANIKO_EXECUTOR_LAYER_SHA256: 49c71aa9e7197af5dcf12811395f182f831c7e714ba94241908c955dd1649ca7 - JOB_USER: ${{ github.actor }} - JOB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PAT_USER: ${{ secrets.PACKAGE_PUSH_USER }} PAT_TOKEN: ${{ secrets.PACKAGE_PUSH_TOKEN }} SERVER_URL: ${{ github.server_url }} @@ -196,33 +193,19 @@ jobs: (umask 077; printf '{"auths":{"%s":{"auth":"%s"}}}\n' "$registry" "$auth" > /kaniko/.docker/config.json) } - code="$(probe "$JOB_USER" "$JOB_TOKEN")" - if [ "$code" = "202" ]; then - write_config "$JOB_USER" "$JOB_TOKEN" - method="job-token" - elif [ "$code" = "401" ] || [ "$code" = "403" ]; then - echo "The Actions job token was refused for a package write (HTTP $code)." - if [ -z "$PAT_USER" ] || [ -z "$PAT_TOKEN" ]; then - echo "::error::The job token cannot push to the organisation's packages, and the org Actions" \ - "secrets PACKAGE_PUSH_USER and PACKAGE_PUSH_TOKEN are not set. Create a bot user with write" \ - "access to the mortis packages and a personal access token with the write:package scope," \ - "and store them as those two org secrets." - exit 1 - fi - code="$(probe "$PAT_USER" "$PAT_TOKEN")" - if [ "$code" != "202" ]; then - echo "::error::The PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN credential was refused too" \ - "(HTTP $code). Check the token's write:package scope and the bot user's access to the org." - exit 1 - fi - write_config "$PAT_USER" "$PAT_TOKEN" - method="org-secrets (PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN)" - else - echo "::error::Unexpected HTTP status $code from the registry write probe; not trying another credential." + if [ -z "$PAT_USER" ] || [ -z "$PAT_TOKEN" ]; then + echo "::error::The org Actions secrets PACKAGE_PUSH_USER and PACKAGE_PUSH_TOKEN are not set." \ + "Create a bot user with write access to the mortis packages and a personal access token" \ + "with the write:package scope, and store them as those two org secrets." exit 1 fi - echo "$method" > /kaniko/credential-method - echo "Registry credential in use: $method" + code="$(probe "$PAT_USER" "$PAT_TOKEN")" + if [ "$code" != "202" ]; then + echo "::error::The PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN credential was refused (HTTP $code)." \ + "Check the token's write:package scope and the bot user's access to the org." + exit 1 + fi + write_config "$PAT_USER" "$PAT_TOKEN" # Gitea cannot make a tag immutable, so look the version tag up first: when it # exists the build must not move it (the build step reads this marker). @@ -307,8 +290,8 @@ jobs: if [ -n "$SUBPATH" ]; then ctx+=(--context-sub-path "$SUBPATH"); fi # # When it finishes the executor has replaced the job container's filesystem with - # the built image and removed /kaniko, so nothing written there (a --digest-file, - # the credential method) can be read back. Take the digest from its + # the built image and removed /kaniko, so nothing written there (a --digest-file) + # can be read back. Take the digest from its # "Pushed @sha256:" log line into a variable of this shell, which # needs only builtins by then. digest="" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 705b17e..13bb724 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -63,6 +63,18 @@ repos: files: ^\.gitea/workflows/.*\.ya?ml$ stages: ['pre-commit'] + # zizmor finds security problems in workflows (pull_request_target, ${{ }} expanded into + # run:). Its own files: pattern only matches .github/, so point it at .gitea/workflows. + # --offline: no network audits, so a run needs no token and gives the same result everywhere. + # Overriding args drops the hook's default, so --no-progress is repeated here. + - repo: https://github.com/zizmorcore/zizmor-pre-commit + rev: 06a39750ec31860bb44f2acf67182c40016cd1e7 # frozen: v1.32.0 + hooks: + - id: zizmor + args: [--no-progress, --offline] + files: ^\.gitea/workflows/[^/]+\.ya?ml$ + stages: ['pre-commit'] + # ansible-lint and the changelog lint run from PATH (language: system): their # versions are the pins in requirements-dev.txt, installed into a virtualenv by # CI and locally. ansible-lint is the slowest hook, so fail_fast stops the run diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2b3eb59..6ff524b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,8 +72,7 @@ outside contributions: - Protect `main`: pull request required, with the `Lint` and `Build` checks. Molecule has no path filter on pull requests (the job always starts and chooses its scenarios itself), so it can be made a required check as well; that is the operator's decision. -- The image workflow (`.gitea/workflows/image.yml`) tries the Actions job token first; - Gitea 28 refuses it for an organisation's packages, so it pushes with the organisation secrets +- The image workflow (`.gitea/workflows/image.yml`) pushes with the organisation secrets `PACKAGE_PUSH_USER` and `PACKAGE_PUSH_TOKEN` (a bot user with write access to the organisation's packages and a token with only the `write:package` scope). Keep those the only credentials stored at organisation level. diff --git a/docs/images/ci.md b/docs/images/ci.md index 634da65..cf62414 100644 --- a/docs/images/ci.md +++ b/docs/images/ci.md @@ -13,7 +13,7 @@ Ubuntu 26.04 with: `requirements.yml`: `community.general` and `ansible.posix` at the exact versions CI tests against, so `ansible-lint --offline` resolves their modules instead of mocking them. - **Every pre-commit hook environment** of `.pre-commit-config.yaml` in `/opt/pre-commit` - (`PRE_COMMIT_HOME`): conventional-pre-commit, gitleaks, yamllint, ruff, shellcheck-py, pre-commit-hooks and actionlint. The Go + (`PRE_COMMIT_HOME`): conventional-pre-commit, gitleaks, yamllint, ruff, shellcheck-py, pre-commit-hooks, actionlint and zizmor. The Go hooks are prebuilt; their toolchains, and the binaries of those repositories that no hook runs, are removed to keep layers small. The local hooks (`reuse-lint`, `antsibull-changelog-lint`, `ansible-lint`) are `language: system`: they run from `/opt/venv`, which is why diff --git a/images/ci/Dockerfile b/images/ci/Dockerfile index 13ab44a..7ee7b0b 100644 --- a/images/ci/Dockerfile +++ b/images/ci/Dockerfile @@ -135,9 +135,9 @@ RUN set -eu; \ printf -- 'print("ok")\n' > ok.py; \ printf -- '#!/bin/sh\necho ok\n' > ok.sh; \ printf -- '---\nself-hosted-runner:\n labels:\n - kamino-docker\n' > .gitea/actionlint.yaml; \ - printf -- '---\n"on": push\njobs:\n t:\n runs-on: kamino-docker\n steps:\n - run: echo ok\n' > .gitea/workflows/t.yml; \ + printf -- '---\n"on": push\npermissions: {}\njobs:\n t:\n runs-on: kamino-docker\n steps:\n - run: echo ok\n' > .gitea/workflows/t.yml; \ git add ok.yaml ok.py ok.sh .gitea/actionlint.yaml .gitea/workflows/t.yml; \ - smoke() { for h in yamllint gitleaks actionlint ruff-check ruff-format shellcheck; do /opt/venv/bin/pre-commit run "$h" --all-files; done; }; \ + smoke() { for h in yamllint gitleaks actionlint zizmor ruff-check ruff-format shellcheck; do /opt/venv/bin/pre-commit run "$h" --all-files; done; }; \ smoke; \ rm -rf /opt/pre-commit/repo*/golangenv-*/.go /root/.cache/go-build; \ find /opt/pre-commit/repo*/golangenv-*/bin -mindepth 1 -maxdepth 1 -type f \ diff --git a/tests/scripts/test_check_bot_pr_diff.py b/tests/scripts/test_check_bot_pr_diff.py index cacedc3..b9fecfa 100644 --- a/tests/scripts/test_check_bot_pr_diff.py +++ b/tests/scripts/test_check_bot_pr_diff.py @@ -319,6 +319,14 @@ class RuleTest(unittest.TestCase): cbd.file_problem(".pre-commit-config.yaml", [" rev: a"], []) ) + def test_a_bump_of_the_real_zizmor_rev_line_is_allowed(self): + # The line as Renovate's git-tags lookup finds it in this repository's config. + config = (SCRIPTS.parent / ".pre-commit-config.yaml").read_text().splitlines() + repo = config.index(" - repo: https://github.com/zizmorcore/zizmor-pre-commit") + rev = config[repo + 1] + bumped = f" rev: {'f' * 40} # frozen: v99.0.0" + self.assertIsNone(cbd.file_problem(".pre-commit-config.yaml", [rev], [bumped])) + def test_patch_parser_keeps_dashed_content(self): patch = "diff --git a/f b/f\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n--- x\n+++ y\n\\ No newline at end of file\n" self.assertEqual(cbd.parse_patch(patch), (["-- x"], ["++ y"])) From 308589e7f9da8ac4a4f5db8e6abe9efa7722ba53 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sat, 21 Nov 2026 18:53:41 +0000 Subject: [PATCH 2/2] ci: name the single credential check in the image step and its log The step no longer chooses between credentials, so its name says it checks the push token, and the log records that the probe passed. The Dockerfile says why the smoke workflow sets `permissions: {}`. Refs #145 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG --- .gitea/workflows/image.yml | 3 ++- images/ci/Dockerfile | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/image.yml b/.gitea/workflows/image.yml index a9175ad..3362465 100644 --- a/.gitea/workflows/image.yml +++ b/.gitea/workflows/image.yml @@ -135,7 +135,7 @@ jobs: # The credential: the org push token, checked by one write probe before the build. # Credentials reach commands only through the environment and curl's stdin # config, never the argument list or the log. Writes /kaniko/.docker/config.json. - - name: Prepare kaniko and select the registry credential + - name: Prepare kaniko and check the registry credential env: KANIKO_CA_LAYER_SHA256: 5ecab5922e6a5124fab96e8cb404b8cf93e8a96716af4fbbea2a9221b1216f86 KANIKO_EXECUTOR_LAYER_SHA256: 49c71aa9e7197af5dcf12811395f182f831c7e714ba94241908c955dd1649ca7 @@ -206,6 +206,7 @@ jobs: exit 1 fi write_config "$PAT_USER" "$PAT_TOKEN" + echo "Registry credential accepted: PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN" # Gitea cannot make a tag immutable, so look the version tag up first: when it # exists the build must not move it (the build step reads this marker). diff --git a/images/ci/Dockerfile b/images/ci/Dockerfile index 7ee7b0b..5a09e81 100644 --- a/images/ci/Dockerfile +++ b/images/ci/Dockerfile @@ -118,7 +118,8 @@ RUN ANSIBLE_COLLECTIONS_PATH=/opt/collections \ # environments fit: with them the layer was about 103 MB compressed and the push of # ci 0.2.16 got a 413. The hooks below run on a staged sample before and after the # deletion: a cache that is incomplete (a missing environment would be rebuilt, which -# needs the network) or no longer works fails the image build, not the jobs. +# needs the network) or no longer works fails the image build, not the jobs. The sample +# workflow sets `permissions: {}` because zizmor flags a workflow that leaves them default. # # The last lines print the compressed size of each directory a later COPY turns into a # layer, which is what the 100 MB (95 MiB) limit applies to. These gzip -6 figures run