# unbound-sec: security-hardened Unbound Docker image (crazymax/unbound replacement)
#
# NOTE: No `# syntax=docker/dockerfile:1` directive on purpose — it would force
# BuildKit to pull the docker/dockerfile:1 frontend image on every build, which
# fails on offline/WSL builds even when all base images are cached. The Dockerfile
# uses no advanced syntax features, so the built-in BuildKit frontend suffices.
#
# Security deltas vs. crazymax/unbound:
#   - Unbound 1.26.2 (security release; fixes CVE-2026-97206, CVE-2026-96405,
#     CVE-2026-109125, CVE-2026-50106, CVE-2026-35501)
#   - no dns-root-hints package -> no curl/nghttp2/zlib/c-ares/brotli in runtime
#     (fixes CVE-2026-58055, CVE-2026-85091; unbound has root servers built in)
#   - current Alpine 3.24 expat 2.9.0 (fixes CVE-2026-77214, CVE-2026-93990,
#     CVE-2026-102633)
#   - shadow package removed after user creation -> no SUID-root binaries
#   - hide-identity / hide-version enabled (no version leak via CHAOS queries)
#
# Improvements adopted from the 10/2026 comparison with other unbound images
# (mvance/unbound, klutchell/unbound, madnuttah/unbound — see README):
#   - source tarballs: SHA256 check + GPG signature (NLnet Labs release key G2)
#   - base images pinned by digest (reproducible builds)
#   - binary hardening: --enable-pie --enable-relro-now
#   - features: --enable-subnet (EDNS Client Subnet), TFO server + client

ARG UNBOUND_VERSION=1.26.2
ARG UNBOUND_SHA256=d8ee7aa6bbb3ff1be7d9e57637d0d0ec2f18273a8efff1a0afe2c665858b96f7
ARG LDNS_VERSION=1.9.2
ARG LDNS_SHA256=b524fa21994b6e834200ceb8c27f1b84bda5982fe35706f058196c079db94d5d
# NLnet Labs release key G2 — independently verified fingerprint
# (checked 2026-10-23 against nlnetlabs.nl/downloads/keys/releases-g2.asc).
# The imported key must carry this fingerprint; otherwise the build fails.
ARG GNUPG_FINGERPRINT=231018690C4D903EF419146AA144323DEAACDF45
ARG XX_VERSION=1.9.0
ARG XX_DIGEST=sha256:c64defb9ed5a91eacb37f96ccc3d4cd72521c4bd18d5442905b95e2226b0e707
ARG ALPINE_VERSION=3.24
ARG ALPINE_DIGEST=sha256:71e9b7bae160ba167fa17414401b94cbcdaf3c32feeab9298bd794a2a0614c35

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION}@${XX_DIGEST} AS xx

FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST} AS base
COPY --from=xx / /
RUN apk --update --no-cache add binutils clang curl file gnupg make pkgconf tar tree xz

FROM base AS base-build
ENV XX_CC_PREFER_LINKER=ld
ARG TARGETPLATFORM
RUN xx-apk --no-cache add gcc g++ expat-dev hiredis hiredis-dev libevent-dev libcap libpcap-dev openssl-dev perl \
 && xx-clang --setup-target-triple

FROM base AS unbound-src
WORKDIR /src/unbound
ARG UNBOUND_VERSION
ARG UNBOUND_SHA256
ARG GNUPG_FINGERPRINT
# CACHEBUST: test suite passes a timestamp so integrity verification (SHA256 +
# GPG) re-runs on every test run even when the build stages are cached.
# Default empty: plain builds (no --build-arg) must not trip `set -u`.
ARG CACHEBUST=""
# Retry flags for downloads: DNS on the build host can be flaky
# (observed 2026-10-23: WSL/Windows resolver intermittently fails on
# nlnetlabs.nl). --retry-all-errors covers DNS failures too (curl >= 7.71).
RUN set -eux \
 && echo "cachebust=$CACHEBUST" \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
       "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz" -o unbound.tar.gz \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
       "https://unbound.net/downloads/unbound-${UNBOUND_VERSION}.tar.gz.asc" -o unbound.tar.gz.asc \
 && echo "${UNBOUND_SHA256}  unbound.tar.gz" | sha256sum -c - \
 && export GNUPGHOME="$(mktemp -d)" \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
       "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
 && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
 && gpg --no-tty --list-keys --with-colons \
    | awk -F: '/^fpr:/{print $10}' \
    | grep -qx "$GNUPG_FINGERPRINT" \
 && gpg --batch --verify --status-fd 1 unbound.tar.gz.asc unbound.tar.gz > /tmp/gpg.status 2>/dev/null \
 && awk '/^\[GNUPG:\] VALIDSIG/ {print $3; exit}' /tmp/gpg.status \
    | grep -qx "$GNUPG_FINGERPRINT" \
 && echo "GPG-VALIDSIG unbound.tar.gz signer=$GNUPG_FINGERPRINT" \
 && tar xzf unbound.tar.gz --strip 1

FROM base AS ldns-src
WORKDIR /src/ldns
ARG LDNS_VERSION
ARG LDNS_SHA256
ARG GNUPG_FINGERPRINT
ARG CACHEBUST=""
RUN set -eux \
 && echo "cachebust=$CACHEBUST" \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 120 \
       "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz" -o ldns.tar.gz \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
       "https://nlnetlabs.nl/downloads/ldns/ldns-${LDNS_VERSION}.tar.gz.asc" -o ldns.tar.gz.asc \
 && echo "${LDNS_SHA256}  ldns.tar.gz" | sha256sum -c - \
 && export GNUPGHOME="$(mktemp -d)" \
 && curl -sSL --retry 3 --retry-delay 2 --retry-all-errors --max-time 60 \
       "https://nlnetlabs.nl/downloads/keys/releases-g2.asc" -o "$GNUPGHOME/releases-g2.asc" \
 && gpg --no-tty --import "$GNUPGHOME/releases-g2.asc" \
 && gpg --no-tty --list-keys --with-colons \
    | awk -F: '/^fpr:/{print $10}' \
    | grep -qx "$GNUPG_FINGERPRINT" \
 && gpg --batch --verify --status-fd 1 ldns.tar.gz.asc ldns.tar.gz > /tmp/gpg.status 2>/dev/null \
 && awk '/^\[GNUPG:\] VALIDSIG/ {print $3; exit}' /tmp/gpg.status \
    | grep -qx "$GNUPG_FINGERPRINT" \
 && echo "GPG-VALIDSIG ldns.tar.gz signer=$GNUPG_FINGERPRINT" \
 && tar xzf ldns.tar.gz --strip 1

FROM base-build AS unbound-build
WORKDIR /src/unbound
RUN --mount=type=bind,from=unbound-src,source=/src/unbound,target=.,rw <<EOT
  set -ex
  CC=xx-clang CXX=xx-clang++ ./configure \
    --host=$(xx-clang --print-target-triple) \
    --prefix=/usr \
    --sysconfdir=/etc \
    --mandir=/usr/share/man \
    --localstatedir=/var \
    --with-chroot-dir="" \
    --with-pidfile=/var/run/unbound/unbound.pid \
    --with-run-dir=/var/run/unbound \
    --with-username="" \
    --disable-flto \
    --disable-rpath \
    --disable-shared \
    --enable-cachedb \
    --enable-event-api \
    --enable-subnet \
    --enable-tfo-server \
    --enable-tfo-client \
    --enable-pie \
    --enable-relro-now \
    --with-pthreads \
    --with-libhiredis=$(xx-info sysroot)usr \
    --with-libexpat=$(xx-info sysroot)usr \
    --with-libevent=$(xx-info sysroot)usr \
    --with-ssl=$(xx-info sysroot)usr
  make DESTDIR=/out install
  make DESTDIR=/out unbound-event-install
  install -Dm755 contrib/update-anchor.sh /out/usr/share/unbound/update-anchor.sh
  xx-verify /out/usr/sbin/unbound
  xx-verify /out/usr/sbin/unbound-anchor
  xx-verify /out/usr/sbin/unbound-checkconf
  xx-verify /out/usr/sbin/unbound-control
  xx-verify /out/usr/sbin/unbound-host
  # build-log evidence for the test suite (asserts "pie executable")
  file /out/usr/sbin/unbound
EOT

FROM base-build AS ldns-build
WORKDIR /src/ldns
RUN --mount=type=bind,from=ldns-src,source=/src/ldns,target=.,rw <<EOT
  set -ex
  CC=xx-clang CXX=xx-clang++ CPPFLAGS=-I/src/ldns/ldns ./configure \
    --host=$(xx-clang --print-target-triple) \
    --prefix=/usr \
    --sysconfdir=/etc \
    --mandir=/usr/share/man \
    --infodir=/usr/share/info \
    --localstatedir=/var \
    --disable-gost \
    --disable-rpath \
    --disable-shared \
    --with-drill \
    --with-ssl=$(xx-info sysroot)usr
  make DESTDIR=/out install
  xx-verify /out/usr/bin/drill
  file /out/usr/bin/drill
EOT

FROM alpine:${ALPINE_VERSION}@${ALPINE_DIGEST}
COPY --from=unbound-build /out /
COPY --from=ldns-build /out /

# Runtime deps only. No dns-root-hints (would pull curl+nghttp2+zlib; unbound
# has root servers built in), no shadow (only needed at build for adduser).
RUN apk --update --no-cache add \
      ca-certificates \
      dnssec-root \
      expat \
      hiredis \
      libevent \
      libpcap \
      openssl \
    && mkdir -p /run/unbound /var/unbound \
    && unbound -V \
    && ldns-config --version

COPY entrypoint.sh /entrypoint.sh
COPY config/unbound.conf /etc/unbound/unbound.conf

RUN chmod 755 /entrypoint.sh \
 && mkdir -p /config \
 && addgroup -g 1500 unbound \
 && adduser -D -H -u 1500 -G unbound -s /bin/sh unbound \
 && chown -R unbound:unbound /etc/unbound /run/unbound /var/unbound \
 && apk del --no-cache shadow \
 && rm -rf /tmp/* /var/cache/apk/*

USER unbound

EXPOSE 5053/tcp
EXPOSE 5053/udp
VOLUME ["/config", "/var/unbound"]

HEALTHCHECK --interval=30s --timeout=10s \
  CMD drill -p 5053 unbound.net @127.0.0.1

ENTRYPOINT ["sh", "/entrypoint.sh"]
CMD [""]
