diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 3c3d8dc..ecd4453 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -11,6 +11,11 @@ on: paths: - "container/**" - ".gitea/workflows/build.yml" + # Weekly rebuild with no commit needed, so OS security fixes published since + # the last push reach the image (see APT_REFRESH in container/Dockerfile). + # Scheduled runs use the default branch, i.e. main. + schedule: + - cron: "17 4 * * 1" env: REGISTRY: repo.anhonesthost.net @@ -64,13 +69,23 @@ jobs: username: shadowdao password: ${{ secrets.GH_PAT }} + # Changes once a week, which invalidates the cached `apt-get upgrade` + # layer (container/Dockerfile) so later security fixes are picked up. + - name: Compute the apt refresh stamp + id: apt + run: echo "stamp=$(date -u +%G-W%V)" >> $GITHUB_OUTPUT + - name: Build and push container image uses: docker/build-push-action@v5 with: context: ./container file: ./container/Dockerfile platforms: linux/amd64,linux/arm64 - push: ${{ gitea.event_name == 'push' }} + # Publish from main pushes and the weekly scheduled rebuild (which + # runs on main); a pull_request run only validates. + push: ${{ gitea.event_name != 'pull_request' }} + build-args: | + APT_REFRESH=${{ steps.apt.outputs.stamp }} tags: | ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ gitea.sha }} diff --git a/app/src-tauri/src/docker/image.rs b/app/src-tauri/src/docker/image.rs index 7a664b3..7c0970a 100644 --- a/app/src-tauri/src/docker/image.rs +++ b/app/src-tauri/src/docker/image.rs @@ -339,3 +339,54 @@ mod tests { assert_eq!(read(&dest.join("config.d/work")), "Host work"); } } + +#[cfg(test)] +mod apt_refresh_tests { + use super::DOCKERFILE; + + const BUILD_WORKFLOW: &str = include_str!("../../../../.gitea/workflows/build.yml"); + + /// The `RUN` instruction (with its `\` continuations) that contains `needle`. + fn run_containing(needle: &str) -> String { + let mut run = String::new(); + let mut current = String::new(); + for line in DOCKERFILE.lines() { + if line.starts_with("RUN ") { + current.clear(); + } + current.push_str(line); + current.push('\n'); + if !line.trim_end().ends_with('\\') { + if current.contains(needle) && current.starts_with("RUN ") { + run = current.clone(); + } + current.clear(); + } + } + run + } + + /// CI builds with `cache-from: type=gha`, so the `apt-get upgrade` layer is + /// only re-run when its cache key changes. An ARG changes a RUN's key only + /// where it is *used*, so the upgrade's RUN must reference it, after an + /// `ARG APT_REFRESH` declared in its stage. + #[test] + fn the_apt_upgrade_layer_is_keyed_on_apt_refresh() { + let arg = DOCKERFILE.find("\nARG APT_REFRESH").expect("ARG APT_REFRESH declared"); + let run = run_containing("upgrade -y"); + assert!(!run.is_empty(), "no RUN with apt-get upgrade"); + assert!(DOCKERFILE.find(&run).unwrap() > arg, "ARG must come before the RUN"); + assert!(run.contains("${APT_REFRESH"), "RUN must use APT_REFRESH: {run}"); + } + + /// …and CI must actually pass a value that changes at least weekly, and + /// rebuild on a schedule so a week with no commits still gets the fixes. + #[test] + fn ci_passes_a_weekly_apt_refresh_and_rebuilds_on_a_schedule() { + assert!(BUILD_WORKFLOW.contains("APT_REFRESH=${{ steps.apt.outputs.stamp }}")); + assert!(BUILD_WORKFLOW.contains("date -u +%G-W%V")); + assert!(BUILD_WORKFLOW.contains("schedule:")); + // A scheduled run must publish, a PR run must not. + assert!(BUILD_WORKFLOW.contains("push: ${{ gitea.event_name != 'pull_request' }}")); + } +} diff --git a/container/Dockerfile b/container/Dockerfile index ba4b9c8..0a16ad5 100644 --- a/container/Dockerfile +++ b/container/Dockerfile @@ -35,13 +35,20 @@ ENV DEBIAN_FRONTEND=noninteractive # base tag stays out of this image until the next refresh. Packages the later # layers install come straight from the archive, so this one upgrade is enough. # -# Note CI builds with `cache-from: type=gha`, so this layer (and everything -# after it) only re-runs when the base digest or this layer's text changes. +# CI builds with `cache-from: type=gha`, so this layer (and everything after +# it) only re-runs when its cache key changes: the base digest, this layer's +# text, or `APT_REFRESH`. CI sets that to the ISO week (`2026-W45`) and also +# rebuilds weekly on a schedule, so a security fix published after the last +# commit is in the image within a week. An ARG only changes the key of a RUN +# that uses it, hence the `echo`. Local builds leave it empty. +# # Scanned on 2026-11-02, the published image of 2026-10-16 carried # libbrotli1 1.1.0-2build2 (CVE-2026-111497, HIGH) and zlib1g 1.3.dfsg-3.1ubuntu2.2 # (CVE-2026-85091), both with fixed versions in the archive, and pnpm's bundled # undici 6.29.0 (three HIGH CVEs, fixed in the pnpm 12.16.1 a rebuild installs). -RUN for i in 1 2 3 4 5; do \ +ARG APT_REFRESH= +RUN echo "apt refresh: ${APT_REFRESH:-unset}" \ + && for i in 1 2 3 4 5; do \ apt-get -o Acquire::Retries=3 update && break; \ echo "apt-get update failed (attempt $i), retrying in 10s..."; \ rm -rf /var/lib/apt/lists/*; \ @@ -288,7 +295,9 @@ RUN npx --yes playwright@latest --version \ && rm -rf /var/lib/apt/lists/* /root/.npm # ── Python 3 + pip + uv + ruff ────────────────────────────────────────────── -RUN for i in 1 2 3 4 5; do \ +ARG APT_REFRESH= +RUN echo "apt refresh: ${APT_REFRESH:-unset}" \ + && for i in 1 2 3 4 5; do \ apt-get -o Acquire::Retries=3 update && break; \ echo "apt-get update failed (attempt $i), retrying in 10s..."; \ rm -rf /var/lib/apt/lists/*; \