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 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-31 10:22:12 -06:00 • committed by Roland
parent bde1209c63
commit ecdad6c3e0
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 71 additions and 8 deletions

View file

@ -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

12
scripts/add_caddy_apt_repo.sh Executable file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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.

View file

@ -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