From ecdad6c3e070fcaa3492a52962b08815cf5e62f5 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Sat, 31 Oct 2026 10:22:12 -0600 Subject: [PATCH] Caddy's apt repository is trusted through the key committed with WROLPi (#911) The install scripts, the nginx-to-Caddy upgrade, and the Pi image downloaded Caddy's apt signing key and source list over TLS and trusted them as-is, so a forged certificate could serve a key that signs other caddy packages. add_caddy_apt_repo.sh installs the key the ISO already uses (fingerprint 65760C51EDEA2017CEA2CA15155B6D79CA56EA34) as an armored keyring and names it in the source's signed-by. Co-authored-by: Claude Opus 5.5 --- pi-gen/stage2/04-wrolpi/03-run-chroot.sh | 3 +- scripts/add_caddy_apt_repo.sh | 12 ++++++ scripts/install_debian.sh | 3 +- scripts/install_raspberrypios.sh | 3 +- scripts/upgrade.sh | 3 +- wrolpi/test/test_caddy_apt_repo.py | 55 ++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100755 scripts/add_caddy_apt_repo.sh create mode 100644 wrolpi/test/test_caddy_apt_repo.py diff --git a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh index 6ee07ddf..ca2c9c2b 100755 --- a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh +++ b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh @@ -85,8 +85,7 @@ git -C /opt/wrolpi reset --hard HEAD chown -R wrolpi:wrolpi /opt/wrolpi # Install Caddy from official apt repo. -curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg -curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list +/opt/wrolpi/scripts/add_caddy_apt_repo.sh apt-get update apt-get install -y caddy diff --git a/scripts/add_caddy_apt_repo.sh b/scripts/add_caddy_apt_repo.sh new file mode 100755 index 00000000..51e662aa --- /dev/null +++ b/scripts/add_caddy_apt_repo.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# Adds Caddy's apt repository, trusting the key committed with WROLPi instead of one downloaded over TLS (a forged +# certificate could serve a key that signs other caddy packages). The ISO uses the same key through live-build: +# debian-live-config/config/archives/caddy-stable.key, fingerprint 65760C51EDEA2017CEA2CA15155B6D79CA56EA34. +set -e + +PROJECT_DIR=$(dirname "$(dirname "$(realpath "$0")")") + +# apt reads an ASCII-armored key only from a .asc file. +install -m 644 "${PROJECT_DIR}/debian-live-config/config/archives/caddy-stable.key" /usr/share/keyrings/caddy-stable-archive-keyring.asc +echo 'deb [signed-by=/usr/share/keyrings/caddy-stable-archive-keyring.asc] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main' \ + >/etc/apt/sources.list.d/caddy-stable.list diff --git a/scripts/install_debian.sh b/scripts/install_debian.sh index 87bd7a58..e921b090 100755 --- a/scripts/install_debian.sh +++ b/scripts/install_debian.sh @@ -63,8 +63,7 @@ grep wrolpi: /etc/passwd || useradd -md /home/wrolpi wrolpi -s "$(command -v bas # Install Caddy from official apt repo if not already installed. if ! command -v caddy &>/dev/null; then - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list + /opt/wrolpi/scripts/add_caddy_apt_repo.sh apt-get update apt-get install -y caddy fi diff --git a/scripts/install_raspberrypios.sh b/scripts/install_raspberrypios.sh index 6cdc972d..479d7938 100755 --- a/scripts/install_raspberrypios.sh +++ b/scripts/install_raspberrypios.sh @@ -63,8 +63,7 @@ grep wrolpi: /etc/passwd || useradd -md /home/wrolpi wrolpi -s "$(command -v bas # Install Caddy from official apt repo if not already installed. if ! command -v caddy &>/dev/null; then - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list + /opt/wrolpi/scripts/add_caddy_apt_repo.sh apt-get update apt-get install -y caddy fi diff --git a/scripts/upgrade.sh b/scripts/upgrade.sh index 7888abb9..e7e238a2 100755 --- a/scripts/upgrade.sh +++ b/scripts/upgrade.sh @@ -292,8 +292,7 @@ if ! command -v caddy &>/dev/null; then systemctl stop nginx 2>/dev/null || : systemctl disable nginx 2>/dev/null || : # Install Caddy from official apt repo. - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list + /opt/wrolpi/scripts/add_caddy_apt_repo.sh apt-get update # Non-interactive: a conffile prompt (e.g. repair.sh already wrote /etc/caddy/Caddyfile) # would otherwise hang or abort under the tty-less wrolpi-upgrade.service. diff --git a/wrolpi/test/test_caddy_apt_repo.py b/wrolpi/test/test_caddy_apt_repo.py new file mode 100644 index 00000000..2736ec6e --- /dev/null +++ b/wrolpi/test/test_caddy_apt_repo.py @@ -0,0 +1,55 @@ +""" +Tests that Caddy's apt repository is trusted through the key committed with WROLPi, never a key downloaded over TLS. +""" +import re +import subprocess + +import pytest + +from wrolpi.vars import PROJECT_DIR + +CADDY_KEY = PROJECT_DIR / 'debian-live-config/config/archives/caddy-stable.key' +CADDY_FINGERPRINT = '65760C51EDEA2017CEA2CA15155B6D79CA56EA34' +HELPER = PROJECT_DIR / 'scripts/add_caddy_apt_repo.sh' +CADDY_INSTALLERS = [ + PROJECT_DIR / 'scripts/install_debian.sh', + PROJECT_DIR / 'scripts/install_raspberrypios.sh', + PROJECT_DIR / 'scripts/upgrade.sh', + PROJECT_DIR / 'pi-gen/stage2/04-wrolpi/03-run-chroot.sh', +] + + +def test_committed_caddy_key_fingerprint(): + output = subprocess.run(['gpg', '--batch', '--show-keys', '--with-colons', str(CADDY_KEY)], + capture_output=True, text=True, check=True).stdout + fingerprints = [line.split(':')[9] for line in output.splitlines() if line.startswith('fpr:')] + assert fingerprints[0] == CADDY_FINGERPRINT + + +def test_no_caddy_key_is_downloaded(): + """A key fetched over TLS could be swapped for one that signs other caddy packages.""" + for directory in ('scripts', 'pi-gen', 'debian-live-config', 'docker'): + for path in (PROJECT_DIR / directory).rglob('*'): + if path.is_file(): + text = path.read_text(errors='ignore') + assert 'caddy/stable/gpg.key' not in text, path + assert 'caddy/stable/debian.deb.txt' not in text, path + + +@pytest.mark.parametrize('path', CADDY_INSTALLERS, ids=lambda i: i.name) +def test_caddy_installers_use_the_committed_key(path): + lines = path.read_text().splitlines() + helper = next(i for i, line in enumerate(lines) if 'scripts/add_caddy_apt_repo.sh' in line) + # `apt-get install ... caddy`, possibly continued onto the next line. + install = next(i for i, line in enumerate(lines) if line.rstrip().endswith(' caddy') + and ('apt-get install' in line or 'apt-get install' in lines[i - 1])) + assert helper < install + + +def test_caddy_repo_is_signed_by_the_installed_key(): + """apt only trusts the key named by signed-by for this repository, and the helper installs that file.""" + text = HELPER.read_text() + keyring = re.search(r'install -m 644 \S+caddy-stable\.key"? (\S+)$', text, re.MULTILINE).group(1) + assert keyring.endswith('.asc'), 'apt reads an armored key only from a .asc file' + assert f'deb [signed-by={keyring}] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main' \ + in text