diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a2674bc..abe5153 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -8,6 +8,18 @@ on: branches: [main] workflow_dispatch: +# Read-only by default for every job; the jobs that publish an image ask for +# `packages: write` themselves. A job that neither declares nor needs more gets no +# more, so a compromised step cannot push to the repository with the workflow token. +permissions: + contents: read + +# Every action below is pinned to a commit, with the release it was when pinned in the +# comment. A tag like `v5` can be moved to point at anything, including code the +# publisher did not write; a commit cannot. To bump one: pick the release, resolve it +# with `gh api repos///git/ref/tags/ --jq .object.sha` (for an +# annotated tag, `gh api repos///git/tags/ --jq .object.sha` gives +# the commit), and update the comment with it. env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} @@ -16,8 +28,8 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v5 - - uses: actions/setup-node@v5 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0 + - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 with: node-version: 22 cache: npm @@ -48,13 +60,13 @@ jobs: digest-amd64: ${{ steps.export.outputs.digest-amd64 }} digest-arm64: ${{ steps.export.outputs.digest-arm64 }} steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0 - - uses: docker/setup-buildx-action@v3 + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 - name: Log in to GHCR if: github.event_name != 'pull_request' - uses: docker/login-action@v3 + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -62,7 +74,7 @@ jobs: - name: Build and push by digest id: build - uses: docker/build-push-action@v6 + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 with: context: . platforms: ${{ matrix.platform }} @@ -85,7 +97,7 @@ jobs: mkdir -p /tmp/digests echo -n "${{ steps.build.outputs.digest }}" > "/tmp/digests/${{ matrix.suffix }}" - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 if: github.event_name != 'pull_request' with: name: digest-${{ matrix.suffix }} @@ -102,16 +114,16 @@ jobs: contents: read packages: write steps: - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: path: /tmp/digests pattern: digest-* merge-multiple: true - - uses: docker/setup-buildx-action@v3 + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 - name: Log in to GHCR - uses: docker/login-action@v3 + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -119,7 +131,7 @@ jobs: - name: Work out the tags id: meta - uses: docker/metadata-action@v5 + uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | @@ -174,7 +186,7 @@ jobs: contents: read packages: write steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0 # The CUDA toolchain and build cache need more room than a runner has free. - name: Free disk space @@ -182,11 +194,11 @@ jobs: sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL df -h / - - uses: docker/setup-buildx-action@v3 + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 - name: Log in to GHCR if: github.event_name != 'pull_request' - uses: docker/login-action@v3 + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -194,7 +206,7 @@ jobs: - name: Work out the tags id: meta - uses: docker/metadata-action@v5 + uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} flavor: | @@ -207,7 +219,7 @@ jobs: type=sha,format=short - name: Build and push - uses: docker/build-push-action@v6 + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 with: context: . file: Dockerfile.cuda diff --git a/CHANGELOG.md b/CHANGELOG.md index bc66762..37e01ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -204,6 +204,33 @@ changes what your listeners see, it says so. without bound; the advert tables' rule and restore columns are indexed; a few statements the scanner runs for every file are prepared once. +### Changed — a lighter boot, a smaller image, and nothing taken on trust + +- **A network share is no longer checked twice over.** When live file detection does not + work on a volume — normal for SMB and NFS — SelfPod polls the share instead, and it was + polling every minute while the scheduled rescan also went through every file at the + rescan interval: two full sweeps of the one mount where every look at a file is a round + trip. The poll now runs at the rescan interval, which is what the notice on the + dashboard had been saying all along, and the container log says so when polling starts. +- **The container starts quicker.** The image library is loaded the first time a cover or + an episode's artwork is handled, and the QR encoder the first time a show page is drawn, + rather than before the first page can be served — whole seconds on a small NAS. The + permissions on SelfPod's own code used to be rewritten file by file on every start, + 4,500 files; they are now checked and left alone when already right, and the image + ships them right for the default `PUID`/`PGID`. +- **The image is 20 MB smaller**, by leaving out the SQLite source and the seven other + platforms' binaries that the database library ships with and never reads. An image built + for one box can also leave out the speech model it will not use (`WHISPER_MODELS` in the + README's *Building your own image*); the published image still carries both. +- **`TRUST_PROXY`** says whose `X-Forwarded-*` headers to believe. The default is every + connection, as before; set it to your tunnel's or reverse proxy's address when anything + else can reach port 8080 directly, and a LAN client can no longer claim to have arrived + over HTTPS from somewhere else. A value with a typo in it trusts nobody and says so in + the log, rather than quietly going back to trusting everyone. +- **Everything the build fetches is pinned to exactly what was reviewed:** the base images + by digest, whisper.cpp by commit rather than a tag that could be moved, and every GitHub + Action by commit. The build workflow's token is read-only except where it publishes. + ## 1.9.1 — 2026-09-16 ### Fixed diff --git a/Dockerfile b/Dockerfile index 02236af..400b056 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,11 @@ # arm64, so this normally installs without compiling anything; python3/make/g++ # are kept as insurance against a missing prebuild. # --------------------------------------------------------------------------- -FROM node:22-alpine AS build +# Pinned by digest (the multi-arch index, so the same line serves amd64 and arm64) and +# not just by tag, so a rebuild gets the base image that was reviewed and not whatever +# the tag points at that day. To bump: `docker buildx imagetools inspect node:22-alpine +# --format '{{.Manifest.Digest}}'`, then paste the digest here and in the runtime stage. +FROM node:22-alpine@sha256:0a7108bf6c7bf5de370ffb1a3ed6be93d405b43ff159f681a8d18c0e2bc2e402 AS build # cmake and git are for whisper.cpp, below; the rest is insurance for native modules. RUN apk add --no-cache python3 make g++ cmake git @@ -20,6 +24,12 @@ COPY package.json package-lock.json ./ # platform binaries are optional deps, and dropping them breaks it at runtime. RUN npm ci --omit=dev +# Drops the 24 MB of better-sqlite3 that this platform never reads (the SQLite source +# and the seven other platforms' binaries) — before the check below, so the check also +# proves the module loads with only what is kept. The script says what it removed. +COPY docker/trim-node-modules.mjs ./docker/ +RUN node docker/trim-node-modules.mjs node_modules + # Fail the build here rather than at container start if a native module is unusable. RUN node -e "require('better-sqlite3'); require('sharp'); console.log('native modules OK')" @@ -28,7 +38,7 @@ COPY src ./src # --------------------------------------------------------------------------- # whisper.cpp — the speech recogniser behind spoken-advert detection (spec §19.6). # -# Built from a pinned tag rather than installed, because there is no musl build to +# Built from a pinned commit rather than installed, because there is no musl build to # install and because what goes into the image should be what was reviewed. MIT. # # Two builds on amd64, chosen at boot by src/lib/cpu-features.js: `-v3` uses AVX2 @@ -42,14 +52,25 @@ COPY src ./src # The model is fetched here, checked against a pinned digest, and never fetched # again: the running container makes no request this feature adds. # --------------------------------------------------------------------------- -ARG WHISPER_CPP_TAG=b4938 +# The commit tag b4938 pointed at when it was reviewed. A tag can be moved; a commit +# cannot. To bump: pick a release tag, then +# `gh api repos/ggml-org/whisper.cpp/git/ref/tags/ --jq .object.sha` (if that +# object is itself a tag, `gh api repos/ggml-org/whisper.cpp/git/tags/` gives the +# commit) and paste it here and in Dockerfile.cuda. +ARG WHISPER_CPP_COMMIT=371b5a7561823ab2bb32142d2751e35e7534727b # Two models. `base` is the default: 60 MB, quick, good enough for English. `small` is # 190 MB and about three times the work, and markedly better in French — set -# WHISPER_MODEL=small on the container to use it. Both are proved at build time. +# WHISPER_MODEL=small on the container to use it. Every model baked in is proved at +# build time; WHISPER_MODELS chooses which (both, unless an image is built for one +# box that only ever uses one of them — `--build-arg WHISPER_MODELS=base` saves 190 MB). +ARG WHISPER_MODELS=base,small ARG WHISPER_BASE_SHA256=422f1ae452ade6f30a004d7e5c6a43195e4433bc370bf23fac9cc591f01a8898 ARG WHISPER_SMALL_SHA256=ae85e4a935d7a567bd102fe55afc16bb595bdb618e11b2fc7591bc08120411bb -RUN git clone --depth 1 --branch "$WHISPER_CPP_TAG" https://github.com/ggml-org/whisper.cpp /tmp/whisper.cpp +# A shallow fetch of one commit; `git clone --branch` only takes names. +RUN git init -q /tmp/whisper.cpp \ + && git -C /tmp/whisper.cpp fetch -q --depth 1 https://github.com/ggml-org/whisper.cpp "$WHISPER_CPP_COMMIT" \ + && git -C /tmp/whisper.cpp checkout -q FETCH_HEAD ENV WHISPER_CMAKE="-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DGGML_STATIC=ON -DGGML_NATIVE=OFF -DGGML_OPENMP=OFF \ -DWHISPER_BUILD_EXAMPLES=ON -DWHISPER_BUILD_TESTS=OFF -DWHISPER_BUILD_SERVER=OFF -DWHISPER_SDL2=OFF -DWHISPER_CURL=OFF" @@ -71,15 +92,26 @@ RUN set -e; mkdir -p /app/whisper; \ esac; \ rm -rf /tmp/wb /tmp/wb2 /tmp/wb3 /tmp/whisper.cpp -RUN wget -qO /app/whisper/ggml-base-q5_1.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-q5_1.bin \ - && echo "$WHISPER_BASE_SHA256 /app/whisper/ggml-base-q5_1.bin" | sha256sum -c - \ - && wget -qO /app/whisper/ggml-small-q5_1.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-q5_1.bin \ - && echo "$WHISPER_SMALL_SHA256 /app/whisper/ggml-small-q5_1.bin" | sha256sum -c - +# Each model named in WHISPER_MODELS is fetched and checked against its pinned digest; +# a name this file has no digest for fails the build rather than fetching something +# unchecked, and an empty list fails it too. +RUN set -e; \ + for model in $(printf '%s' "$WHISPER_MODELS" | tr ',' ' '); do \ + case "$model" in \ + base) sum="$WHISPER_BASE_SHA256";; \ + small) sum="$WHISPER_SMALL_SHA256";; \ + *) echo "WHISPER_MODELS names \"$model\", which this Dockerfile has no digest for (it knows base and small)." >&2; exit 1;; \ + esac; \ + wget -qO "/app/whisper/ggml-$model-q5_1.bin" "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-$model-q5_1.bin" \ + && echo "$sum /app/whisper/ggml-$model-q5_1.bin" | sha256sum -c -; \ + done; \ + ls /app/whisper/ggml-*.bin >/dev/null 2>&1 || { echo "WHISPER_MODELS=\"$WHISPER_MODELS\" baked no model into the image." >&2; exit 1; } # --------------------------------------------------------------------------- # Runtime stage # --------------------------------------------------------------------------- -FROM node:22-alpine +# The same digest as the build stage; bump both together (see the top of the file). +FROM node:22-alpine@sha256:0a7108bf6c7bf5de370ffb1a3ed6be93d405b43ff159f681a8d18c0e2bc2e402 # su-exec drops privileges without leaving a process between PID 1 and node; # shadow provides usermod/groupmod for the PUID/PGID pattern; tzdata makes the @@ -95,7 +127,9 @@ RUN deluser --remove-home node \ WORKDIR /app -COPY --from=build /app/node_modules ./node_modules +# Owned by the default PUID/PGID from the start, so a container run with the defaults +# never has to chown the 4,500 files in here at boot (docker/entrypoint.sh). +COPY --from=build --chown=1000:1000 /app/node_modules ./node_modules COPY --from=build /app/package.json ./package.json COPY --from=build /app/whisper ./whisper COPY src ./src @@ -108,6 +142,13 @@ COPY scripts ./scripts # A broken build fails here, not on the NAS after the update has been applied. RUN node docker/whisper-smoke.mjs +# The model the container uses unless WHISPER_MODEL says otherwise. It has to be one of +# the models baked in: an image whose default model is missing would start, say the +# recogniser is unavailable, and publish every episode unheard. So the build refuses. +ARG WHISPER_DEFAULT_MODEL=base +RUN test -f "/app/whisper/ggml-${WHISPER_DEFAULT_MODEL}-q5_1.bin" \ + || { echo "WHISPER_DEFAULT_MODEL=${WHISPER_DEFAULT_MODEL} is not among the models baked in ($(ls /app/whisper | grep '^ggml-' | tr '\n' ' ')). Add it to WHISPER_MODELS, or set WHISPER_DEFAULT_MODEL to one of them." >&2; exit 1; } + RUN chmod +x docker/entrypoint.sh \ && mkdir -p /data \ && chown selfpod:selfpod /app @@ -117,7 +158,7 @@ ENV NODE_ENV=production \ DATA_DIR=/data \ PUID=1000 \ PGID=1000 \ - WHISPER_MODEL=base + WHISPER_MODEL=${WHISPER_DEFAULT_MODEL} EXPOSE 8080 VOLUME ["/data"] diff --git a/Dockerfile.cuda b/Dockerfile.cuda index f398389..1a5cee3 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -15,17 +15,23 @@ # mounts into the container at start. The build proves the CPU builds on real audio, # and that the CUDA build links against everything but that one library. # --------------------------------------------------------------------------- -ARG CUDA_VERSION=12.6.3 -ARG UBUNTU=ubuntu22.04 - -FROM nvidia/cuda:${CUDA_VERSION}-devel-${UBUNTU} AS build +# CUDA 12.6.3 on Ubuntu 22.04, pinned by digest so a rebuild gets the base images that +# were reviewed rather than whatever the tags point at that day. To bump, pick the new +# tags and run `docker buildx imagetools inspect nvidia/cuda: --format +# '{{.Manifest.Digest}}'` for the devel image here and the runtime image below. +FROM nvidia/cuda:12.6.3-devel-ubuntu22.04@sha256:d49bb8a4ff97fb5fe477947a3f02aa8c0a53eae77e11f00ec28618a0bcaa2ad1 AS build ARG NODE_VERSION=22.23.2 ARG NODE_SHA256=d60acfe00a2932254bb0ad20e01b0d74397a0875595de719654b214f4b03f307 # The RTX 3050 is Ampere, compute capability 8.6. Building for every architecture takes # several times as long and is several times the size; override for other cards. ARG CUDA_ARCHITECTURES=86 -ARG WHISPER_CPP_TAG=b4938 +# The commit tag b4938 pointed at when it was reviewed; the Dockerfile says how to bump +# it, and the two must stay the same commit. +ARG WHISPER_CPP_COMMIT=371b5a7561823ab2bb32142d2751e35e7534727b +# Which models to bake in (the Dockerfile explains). Both by default; the GPU makes +# `small` cheap, so an image for one GPU box could carry only that one. +ARG WHISPER_MODELS=base,small ARG WHISPER_BASE_SHA256=422f1ae452ade6f30a004d7e5c6a43195e4433bc370bf23fac9cc591f01a8898 ARG WHISPER_SMALL_SHA256=ae85e4a935d7a567bd102fe55afc16bb595bdb618e11b2fc7591bc08120411bb @@ -44,9 +50,15 @@ WORKDIR /app COPY package.json package-lock.json ./ # glibc prebuilds for better-sqlite3 and sharp; never --omit=optional (sharp's binaries are optional deps). RUN npm ci --omit=dev +# Drops what better-sqlite3 carries for other platforms (the Dockerfile explains); +# before the check, so the check proves the module loads with only what is kept. +COPY docker/trim-node-modules.mjs ./docker/ +RUN node docker/trim-node-modules.mjs node_modules RUN node -e "require('better-sqlite3'); require('sharp'); console.log('native modules OK')" -RUN git clone --depth 1 --branch "$WHISPER_CPP_TAG" https://github.com/ggml-org/whisper.cpp /tmp/whisper.cpp +RUN git init -q /tmp/whisper.cpp \ + && git -C /tmp/whisper.cpp fetch -q --depth 1 https://github.com/ggml-org/whisper.cpp "$WHISPER_CPP_COMMIT" \ + && git -C /tmp/whisper.cpp checkout -q FETCH_HEAD ENV WHISPER_CMAKE="-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DGGML_NATIVE=OFF -DGGML_OPENMP=OFF \ -DWHISPER_BUILD_EXAMPLES=ON -DWHISPER_BUILD_TESTS=OFF -DWHISPER_BUILD_SERVER=OFF -DWHISPER_SDL2=OFF -DWHISPER_CURL=OFF" @@ -66,15 +78,25 @@ RUN set -e; mkdir -p /app/whisper; \ && cp /tmp/wb2/bin/whisper-cli /app/whisper/whisper-cli-v2; \ rm -rf /tmp/wbc /tmp/wb3 /tmp/wb2 /tmp/whisper.cpp -RUN curl -fsSLo /app/whisper/ggml-base-q5_1.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base-q5_1.bin \ - && echo "$WHISPER_BASE_SHA256 /app/whisper/ggml-base-q5_1.bin" | sha256sum -c - \ - && curl -fsSLo /app/whisper/ggml-small-q5_1.bin https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small-q5_1.bin \ - && echo "$WHISPER_SMALL_SHA256 /app/whisper/ggml-small-q5_1.bin" | sha256sum -c - +# Each model named in WHISPER_MODELS is fetched and checked against its pinned digest; +# a name with no digest here fails the build, and so does an empty list. +RUN set -e; \ + for model in $(printf '%s' "$WHISPER_MODELS" | tr ',' ' '); do \ + case "$model" in \ + base) sum="$WHISPER_BASE_SHA256";; \ + small) sum="$WHISPER_SMALL_SHA256";; \ + *) echo "WHISPER_MODELS names \"$model\", which this Dockerfile has no digest for (it knows base and small)." >&2; exit 1;; \ + esac; \ + curl -fsSLo "/app/whisper/ggml-$model-q5_1.bin" "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-$model-q5_1.bin" \ + && echo "$sum /app/whisper/ggml-$model-q5_1.bin" | sha256sum -c -; \ + done; \ + ls /app/whisper/ggml-*.bin >/dev/null 2>&1 || { echo "WHISPER_MODELS=\"$WHISPER_MODELS\" baked no model into the image." >&2; exit 1; } # --------------------------------------------------------------------------- # Runtime stage: CUDA runtime libraries (cuBLAS, the CUDA runtime), no compilers. # --------------------------------------------------------------------------- -FROM nvidia/cuda:${CUDA_VERSION}-runtime-${UBUNTU} +# The runtime twin of the devel image above; bump both together. +FROM nvidia/cuda:12.6.3-runtime-ubuntu22.04@sha256:63a18dd805367dacfb077aeced8384ab2fb569598ec5f5f5220c3f90a5c23650 ENV DEBIAN_FRONTEND=noninteractive # util-linux: setpriv, which drops privileges as su-exec does on Alpine. @@ -92,7 +114,8 @@ COPY --from=build /opt/node /opt/node ENV PATH=/opt/node/bin:$PATH WORKDIR /app -COPY --from=build /app/node_modules ./node_modules +# Owned by the default PUID/PGID from the start, so a default run never chowns it at boot. +COPY --from=build --chown=1000:1000 /app/node_modules ./node_modules COPY --from=build /app/package.json ./package.json COPY --from=build /app/whisper ./whisper COPY src ./src @@ -107,17 +130,22 @@ RUN WHISPER_SMOKE_SKIP=whisper-cli-cuda node docker/whisper-smoke.mjs \ && if [ -n "$missing" ]; then echo "whisper-cli-cuda is missing libraries: $missing"; exit 1; fi \ && echo "whisper-cli-cuda links (libcuda.so.1 comes from the driver at run time)" +# `small` by default here: the GPU makes it cheap, and it hears French markedly better. +# It has to be one of the models baked in, or the build refuses (the Dockerfile explains). +ARG WHISPER_DEFAULT_MODEL=small +RUN test -f "/app/whisper/ggml-${WHISPER_DEFAULT_MODEL}-q5_1.bin" \ + || { echo "WHISPER_DEFAULT_MODEL=${WHISPER_DEFAULT_MODEL} is not among the models baked in ($(ls /app/whisper | grep '^ggml-' | tr '\n' ' ')). Add it to WHISPER_MODELS, or set WHISPER_DEFAULT_MODEL to one of them." >&2; exit 1; } + RUN chmod +x docker/entrypoint.sh \ && mkdir -p /data \ && chown selfpod:selfpod /app -# `small` by default here: the GPU makes it cheap, and it hears French markedly better. ENV NODE_ENV=production \ PORT=8080 \ DATA_DIR=/data \ PUID=1000 \ PGID=1000 \ - WHISPER_MODEL=small \ + WHISPER_MODEL=${WHISPER_DEFAULT_MODEL} \ NVIDIA_VISIBLE_DEVICES=all \ NVIDIA_DRIVER_CAPABILITIES=compute,utility diff --git a/README.md b/README.md index 4f9ad29..9e5ea2c 100644 --- a/README.md +++ b/README.md @@ -272,11 +272,12 @@ of it can be changed later in **Settings** without touching the container. | `SUBSCRIPTIONS_ENABLED` | off | Lets SelfPod follow remote feeds. Off by default: it is the only thing that makes SelfPod fetch from the internet. Seeds the setting on first run; after that the Settings page wins. | | `REMOTE_POLL_INTERVAL_SECONDS` | `3600` | How often to check a followed feed. Clamped to 15 minutes – 24 hours — this is someone else's server. | | `ALLOW_PRIVATE_FEED_HOSTS` | *(empty)* | Comma-separated **IP addresses** that may be followed despite being private, e.g. a feed on your own NAS. Exempts only the addresses listed, and only from the address and port rules. Env-only on purpose: it weakens a guarantee, so changing it should mean touching the container. | +| `TRUST_PROXY` | `true` | Whose `X-Forwarded-*` headers to believe: `true` (every connection — what SelfPod always did), `false` (none), or a comma-separated list of the addresses and ranges your proxy connects from, e.g. `172.18.0.0/16` or `loopback`. Narrow it when something other than your proxy can reach port 8080 directly — the default compose file publishes it on every interface — so a LAN client cannot claim to have arrived over HTTPS from somewhere else. Env-only, for the same reason as the row above. | | `ADMIN_USERNAME` | `admin` | First-run only. | | `ADMIN_PASSWORD` | — | First-run only. If unset, a random password is generated and printed once to the logs. | | `SESSION_SECRET` | generated | First-run only; afterwards it lives in the database. | | `LOG_LEVEL` | `info` | `debug`, `info`, `warn`, `error`. | -| `WHISPER_MODEL` | `base` | Which speech model hears the words: `base` or `small` (both ship in the image), or a path to a whisper.cpp model file. `small` is about twice the work and markedly better in French. | +| `WHISPER_MODEL` | `base` | Which speech model hears the words: `base` or `small` (both ship in the published image; see [Building your own image](#building-your-own-image) to ship one), or a path to a whisper.cpp model file. `small` is about twice the work and markedly better in French. | | `WHISPER_CLI` | *(the image's own)* | Path to a `whisper-cli` binary, for a build of your own. The image picks its AVX2 or SSE4.2 build for the CPU at boot. | | `WHISPER_THREADS` | half the logical cores, less one (at least 2) | Threads the recogniser may use. An eight-core, sixteen-thread i7 gets 7. | | `WHISPER_NICE` | `10` | How politely the recogniser yields the processor, 0 (normal) to 19. It only matters when something else wants the processor — on a NAS, usually a listener's download. | @@ -752,6 +753,24 @@ docker build -t selfpod:test . ./test/acceptance/run.sh # 32 end-to-end checks against a real container ``` +### Building your own image + +The published image carries both speech models, `base` (60 MB) and `small` (190 MB), +so one image serves everyone. An image built for one box that will only ever use one +of them can leave the other out: + +```bash +docker build --build-arg WHISPER_MODELS=small --build-arg WHISPER_DEFAULT_MODEL=small -t selfpod:small . +``` + +`WHISPER_MODELS` is the comma-separated list to bake in (default `base,small`), and +`WHISPER_DEFAULT_MODEL` is what the container uses unless `WHISPER_MODEL` says otherwise +(default `base`; `small` in `Dockerfile.cuda`). The default has to be one of the models +baked in, or the build refuses and says so — an image whose default model is missing +would start, report the recogniser unavailable, and publish every episode unheard. +Every model that is baked in is checked against a pinned digest and proved on a +one-second recording during the build. + The acceptance script checks the behaviours that matter most: an `.m4a` appearing with no restart, a filename containing an emoji and curly quotes surviving into a downloadable URL, `cover.png` being found, a rename keeping its GUID, an edited diff --git a/doc/selfpod-spec.md b/doc/selfpod-spec.md index 4d3e0a1..6d5e406 100644 --- a/doc/selfpod-spec.md +++ b/doc/selfpod-spec.md @@ -524,6 +524,7 @@ the prototype by hand: | `SESSION_SECRET` | No | random, generated on first run and persisted in the DB | Signs admin session cookies | | `LOG_LEVEL` | No | `info` | `debug`/`info`/`warn`/`error` | | `MAX_UPLOAD_SIZE_MB` | No | `1024` | Cap for UI-based file uploads (§11.4) | +| `TRUST_PROXY` | No | `true` | Whose `X-Forwarded-*` headers are believed: `true` (every hop), `false` (none), or a comma-separated list of the proxy's addresses and CIDR ranges (passed to Fastify's `trustProxy`). Narrow it to the tunnel's or reverse proxy's address when anything else can reach the port directly, so a LAN client cannot forge the protocol or source address. Env-only, never a setting in the UI. | Only one required volume: diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 793ce1a..37168db 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -77,8 +77,20 @@ else log "running as $APP_USER ($RUN_AS)" fi -# Only ever touches the app's own directory, never anything under /data. -chown -R "$RUN_AS" /app/node_modules 2>/dev/null || true +# Only ever touches the app's own directory, never anything under /data — and only +# when the owner is actually wrong. `chown -R` walks the 4,500 files in node_modules, +# which took seconds on a Celeron on every boot, for a tree that was already right +# after the first one. The image ships it owned by the default ids, and the directory +# and a file deep inside it are both checked, so a chown that was interrupted part-way +# is finished rather than skipped. +node_modules_owner() { + stat -c '%u:%g' /app/node_modules /app/node_modules/.package-lock.json 2>/dev/null | sort -u +} +if [ "$(node_modules_owner)" != "$RUN_AS" ]; then + log "changing the owner of /app/node_modules to $RUN_AS" + chown -R "$RUN_AS" /app/node_modules 2>/dev/null \ + || log "could not change the owner of /app/node_modules; continuing, but the app may be unable to read its own code." +fi # --------------------------------------------------------------------------- # Read + write self-test as the target user. A failure is reported in detail and diff --git a/docker/trim-node-modules.mjs b/docker/trim-node-modules.mjs new file mode 100644 index 0000000..64c3c19 --- /dev/null +++ b/docker/trim-node-modules.mjs @@ -0,0 +1,61 @@ +#!/usr/bin/env node +/** + * Drops what node_modules carries that the image never reads, at build time. + * + * better-sqlite3 ships a prebuilt binary for every platform it supports — eight of + * them, 16 MB — and loads exactly one, chosen by its own `getPrebuildPath()`. The + * image is built for one platform, so the other seven are dead weight; so is `deps`, + * the SQLite source it would only compile if no prebuilt matched. Together they are + * about 24 MB of an image that is mostly whisper models. + * + * Asked of the package rather than guessed from `process.platform`, so a musl build + * keeps the musl binary. Run in the build stage, *before* the native-module check, + * so that check proves the module still loads with only what is kept. A layout this + * script does not recognise fails the build with a sentence saying so, rather than + * trimming blind or quietly doing nothing. + */ +import { createRequire } from 'node:module'; +import { existsSync, readdirSync, rmSync, statSync } from 'node:fs'; +import { dirname, join, resolve } from 'node:path'; + +const root = resolve(process.argv[2] ?? 'node_modules'); +const pkg = join(root, 'better-sqlite3'); +const require = createRequire(import.meta.url); + +function sizeOf(path) { + const stats = statSync(path); + if (!stats.isDirectory()) return stats.size; + return readdirSync(path).reduce((total, name) => total + sizeOf(join(path, name)), 0); +} +const mb = (bytes) => `${(bytes / 1024 / 1024).toFixed(1)} MB`; + +let removed = 0; + +const deps = join(pkg, 'deps'); +if (existsSync(deps)) { + removed += sizeOf(deps); + rmSync(deps, { recursive: true, force: true }); +} + +// The absolute path sidesteps the package's `exports` map, which does not list lib/. +let getPrebuildPath; +try { + ({ getPrebuildPath } = require(join(pkg, 'lib', 'binding.js'))); +} catch (err) { + console.error(`better-sqlite3's loader is not where this script expects it (${err.message}); update docker/trim-node-modules.mjs for this version before trimming.`); + process.exit(1); +} +const keep = getPrebuildPath?.(); +if (!keep) { + console.error('better-sqlite3 reports no prebuilt binary for this platform, so there is nothing safe to trim; it will try to compile instead, which the image does not support.'); + process.exit(1); +} +const prebuilds = dirname(keep); +for (const name of readdirSync(prebuilds)) { + const file = join(prebuilds, name); + if (file === keep) continue; + removed += sizeOf(file); + rmSync(file, { force: true }); +} + +console.log(`trimmed node_modules: kept ${keep.slice(root.length + 1)}, removed ${mb(removed)}`); diff --git a/docker/whisper-smoke.mjs b/docker/whisper-smoke.mjs index e4d2f68..fb45479 100644 --- a/docker/whisper-smoke.mjs +++ b/docker/whisper-smoke.mjs @@ -12,7 +12,7 @@ * it in the shape SelfPod reads, not that it spelled the name. */ import { execFileSync } from 'node:child_process'; -import { existsSync, readFileSync, readdirSync, rmSync } from 'node:fs'; +import { readFileSync, readdirSync, rmSync, statSync } from 'node:fs'; import { join } from 'node:path'; const dir = process.env.WHISPER_DIR ?? '/app/whisper'; @@ -33,18 +33,21 @@ if (!binaries.length) { } /* - * Every binary is proved with the default model. The larger model is proved only with - * the fastest binary: on the SSE4.2-only build a quantised model runs through scalar - * code and takes minutes even on a one-second file, and no box that needs that build - * should be running the larger model — the README says so — so proving the pairing - * would only prove the build host's patience. + * Every binary is proved with the smallest model shipped (`base`, unless the image was + * built without it — WHISPER_MODELS in the Dockerfile). Any larger model is proved only + * with the fastest binary: on the SSE4.2-only build a quantised model runs through + * scalar code and takes minutes even on a one-second file, and no box that needs that + * build should be running the larger model — the README says so — so proving the + * pairing would only prove the build host's patience. */ const fastest = binaries.includes('whisper-cli-v3') ? 'whisper-cli-v3' : binaries[0]; +const smallest = [...models].sort((a, b) => statSync(join(dir, a)).size - statSync(join(dir, b)).size)[0]; const pairs = binaries.flatMap((binary) => models - .filter((model) => model.includes('base') || binary === fastest) + .filter((model) => model === smallest || binary === fastest) .map((model) => [binary, model]), ); +console.log(`models: ${models.join(', ')}; every binary is proved with ${smallest}`); let failed = 0; for (const [name, modelName] of pairs) { diff --git a/src/app.js b/src/app.js index 0793de6..a2a24ff 100644 --- a/src/app.js +++ b/src/app.js @@ -28,9 +28,10 @@ export async function buildApp(services) { loggerInstance: logger, // The app always runs behind the user's own reverse proxy or tunnel, so // X-Forwarded-Proto has to be honoured for Secure cookies and correct URLs. - // Note this makes `request.ip` client-influenced — see plugins/auth.js for why - // rate limiting deliberately does not use it. - trustProxy: true, + // Every hop is trusted by default, which makes `request.ip` client-influenced — + // see plugins/auth.js for why rate limiting deliberately does not use it — and + // TRUST_PROXY narrows it to the proxy's own address (config.js). + trustProxy: config.trustProxy, bodyLimit: 2 * 1024 * 1024, // form posts only; uploads stream via multipart // Episode filenames are route parameters, and Fastify rejects any parameter diff --git a/src/config.js b/src/config.js index 7151d40..b773aec 100644 --- a/src/config.js +++ b/src/config.js @@ -94,6 +94,63 @@ function readAllowedPrivateHosts(raw, { name }) { return { value: Object.freeze(allowed), warnings }; } +/** The range names proxy-addr (what Fastify's `trustProxy` is built on) understands. */ +const TRUST_PROXY_RANGE_NAMES = new Set(['loopback', 'linklocal', 'uniquelocal']); + +/** + * Parses TRUST_PROXY into what Fastify's `trustProxy` option takes: `true` (every hop + * is trusted — the default, and what SelfPod always did), `false` (no hop is), or a + * list of the addresses and CIDR ranges that proxies arrive from. + * + * Why it exists: with every hop trusted, whoever can open a connection to port 8080 + * directly — anyone on the LAN, given compose publishes it on every interface — can + * set X-Forwarded-Proto and X-Forwarded-For themselves, and SelfPod believes them. + * Nothing security-relevant keys off those headers today (plugins/auth.js explains + * why rate limiting never used `request.ip`), so the default stays open; narrowing it + * to the tunnel's or reverse proxy's address closes the door anyway. + * + * A list with an entry that is not an address drops that entry and says so. A list + * with nothing usable in it trusts no hop at all rather than falling back to every + * hop: the operator plainly meant to stop trusting everyone, and trusting nobody + * costs only the Secure flag on a tunnel's cookie, which browsers do not need. + */ +function readTrustProxy(raw, { name }) { + const value = String(raw ?? '').trim(); + if (value === '' || value.toLowerCase() === 'true') return { value: true, warnings: [] }; + if (value.toLowerCase() === 'false') return { value: false, warnings: [] }; + + const warnings = []; + const entries = []; + for (const entry of value.split(',')) { + const item = entry.trim(); + if (!item) continue; + if (TRUST_PROXY_RANGE_NAMES.has(item.toLowerCase())) { + entries.push(item.toLowerCase()); + continue; + } + const [address, prefix, ...rest] = item.split('/'); + const bare = address.startsWith('[') && address.endsWith(']') ? address.slice(1, -1) : address; + const family = isIP(bare); + const prefixOk = + prefix === undefined || (/^\d{1,3}$/.test(prefix) && Number(prefix) <= (family === 4 ? 32 : 128)); + if (family === 0 || rest.length > 0 || !prefixOk) { + warnings.push( + `${name} entry "${item}" is not an IP address or CIDR range and was ignored. List the addresses your proxy connects from, like 172.18.0.5 or 10.0.0.0/8, or use true or false.`, + ); + continue; + } + entries.push(prefix === undefined ? bare : `${bare}/${prefix}`); + } + + if (entries.length === 0) { + warnings.push( + `${name} ("${value}") names no usable address, so no proxy is trusted: forwarded headers are ignored and every request is treated as arriving directly. Fix the value, or set TRUST_PROXY=true to trust every hop as before.`, + ); + return { value: false, warnings }; + } + return { value: Object.freeze(entries), warnings }; +} + const LOG_LEVELS = new Set(['trace', 'debug', 'info', 'warn', 'error', 'fatal', 'silent']); export function loadConfig(env = process.env) { @@ -275,6 +332,12 @@ export function loadConfig(env = process.env) { warnings.push(...privateHosts.warnings); config.allowedPrivateFeedHosts = privateHosts.value; + // Env-only for the same reason: it decides whose word on "this came over HTTPS from + // that address" is believed, and that should not be changeable from a web page. + const trustProxy = readTrustProxy(env.TRUST_PROXY, { name: 'TRUST_PROXY' }); + warnings.push(...trustProxy.warnings); + config.trustProxy = trustProxy.value; + if (!isAbsolute(config.dataDir)) { throw new Error(`DATA_DIR must be an absolute path (got "${config.dataDir}").`); } diff --git a/src/index.js b/src/index.js index 679c83d..d3d0b31 100644 --- a/src/index.js +++ b/src/index.js @@ -243,7 +243,7 @@ async function startDegradedServer(cause) { logger.error({ err: cause }, `SelfPod cannot open its database. ${detail}`); - const app = Fastify({ loggerInstance: logger, trustProxy: true }); + const app = Fastify({ loggerInstance: logger, trustProxy: config.trustProxy }); app.get('/health', async (request, reply) => { reply.header('access-control-allow-origin', '*').header('cache-control', 'no-store'); diff --git a/src/lib/lazy-sharp.js b/src/lib/lazy-sharp.js new file mode 100644 index 0000000..fe24c06 --- /dev/null +++ b/src/lib/lazy-sharp.js @@ -0,0 +1,25 @@ +/** + * sharp, loaded the first time an image is actually handled rather than at boot. + * + * Importing it means loading libvips, which is tens of milliseconds on a laptop and + * whole seconds on the Celeron in a small NAS — paid before the first page could be + * served, for a library that is only needed when a cover is inspected or saved or an + * episode's artwork is extracted. Both services share this one loader, so the module + * is loaded once however many of them ask, and a boot with no images never loads it. + * + * The image itself proves at build time that sharp loads (the native-module check in + * the Dockerfile), so in the container this cannot fail. On a development machine + * where it does, the error keeps sharp's own explanation and names the module, so it + * reads as "the image library is broken" rather than "this image is invalid". + */ +let loading = null; + +export function loadSharp() { + loading ??= import('sharp') + .then((module) => module.default) + .catch((err) => { + loading = null; + throw new Error(`The image library (sharp) could not be loaded: ${err?.message ?? err}`, { cause: err }); + }); + return loading; +} diff --git a/src/services/covers.js b/src/services/covers.js index 53c5d36..5136216 100644 --- a/src/services/covers.js +++ b/src/services/covers.js @@ -4,8 +4,6 @@ import { readdir, rename, stat, unlink } from 'node:fs/promises'; import { join } from 'node:path'; import { pipeline } from 'node:stream/promises'; -import sharp from 'sharp'; - import { IMAGE_MAX_INPUT_PIXELS, ARTWORK_MAX_PX, @@ -15,6 +13,7 @@ import { imageMimeType, } from '../constants.js'; import { badRequest } from '../lib/errors.js'; +import { loadSharp } from '../lib/lazy-sharp.js'; import { describeImageError } from './episode-art.js'; /** Every decode refuses at the header past the pixel ceiling — see the constant. */ @@ -59,6 +58,7 @@ export function createCovers({ config, logger }) { */ async inspect(filePath) { try { + const sharp = await loadSharp(); const [meta, stats] = await Promise.all([sharp(filePath, SHARP_LIMITS).metadata(), stat(filePath)]); const width = meta.width ?? null; const height = meta.height ?? null; @@ -115,6 +115,8 @@ export function createCovers({ config, logger }) { async saveUpload(showDir, sourcePath) { const target = join(showDir, CANONICAL_COVER_FILENAME); const tmp = join(showDir, `.cover-upload-${randomUUID()}.tmp`); + // Outside the try: a broken image library must not be reported as a bad image. + const sharp = await loadSharp(); try { await sharp(sourcePath, SHARP_LIMITS) .rotate() // honour EXIF orientation before discarding metadata @@ -154,8 +156,9 @@ export function createCovers({ config, logger }) { throw badRequest('That cover image could not be read, so it cannot be resized.', 'invalid_image'); } + const sharp = await loadSharp(); try { - const background = await dominantEdgeColour(source); + const background = await dominantEdgeColour(sharp, source); await sharp(source, SHARP_LIMITS) .rotate() .resize(size, size, { fit: 'contain', background, withoutEnlargement: false }) @@ -254,7 +257,7 @@ export function createCovers({ config, logger }) { * Samples the image's own edges for a padding colour, so a 16:9 cover padded to * square blends instead of gaining black bars. */ -async function dominantEdgeColour(filePath) { +async function dominantEdgeColour(sharp, filePath) { try { const { dominant } = await sharp(filePath, SHARP_LIMITS).stats(); if (dominant) return { r: dominant.r, g: dominant.g, b: dominant.b, alpha: 1 }; diff --git a/src/services/episode-art.js b/src/services/episode-art.js index 0075e43..b9c9f15 100644 --- a/src/services/episode-art.js +++ b/src/services/episode-art.js @@ -3,9 +3,8 @@ import { rmSync, unlinkSync } from 'node:fs'; import { mkdir, rename, unlink, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; -import sharp from 'sharp'; - import { DIRECTORY_IMAGE_FORMATS, IMAGE_MAX_INPUT_PIXELS } from '../constants.js'; +import { loadSharp } from '../lib/lazy-sharp.js'; /** Every decode refuses at the header past the pixel ceiling — see the constant. */ const SHARP_LIMITS = Object.freeze({ limitInputPixels: IMAGE_MAX_INPUT_PIXELS }); @@ -71,6 +70,7 @@ export function createEpisodeArt({ config, covers, logger }) { */ async store({ showId, episodeId, buffer, sourceFormat = null }) { const data = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer); + const sharp = await loadSharp(); let probe; try { probe = await sharp(data, SHARP_LIMITS).metadata(); diff --git a/src/services/watcher.js b/src/services/watcher.js index 29675e3..21ccf92 100644 Binary files a/src/services/watcher.js and b/src/services/watcher.js differ diff --git a/src/web/lib/qr.js b/src/web/lib/qr.js index b1c5bb1..cea9ad7 100644 --- a/src/web/lib/qr.js +++ b/src/web/lib/qr.js @@ -1,4 +1,13 @@ -import QRCode from 'qrcode'; +/** + * The QR encoder, loaded the first time a show page needs it rather than at boot: it + * is only ever used to draw the subscribe codes, and nothing else on the boot path + * should pay for it. Loaded once, however many pages ask. + */ +let qrcodeLoading = null; +function loadQRCode() { + qrcodeLoading ??= import('qrcode').then((module) => module.default); + return qrcodeLoading; +} /** * Feed URL → inline SVG. @@ -32,6 +41,7 @@ export async function feedQrSvg(url, { size = 200 } = {}) { const cached = cache.get(key); if (cached) return cached; + const QRCode = await loadQRCode(); const svg = await QRCode.toString(url, { type: 'svg', errorCorrectionLevel: 'M', diff --git a/test/integration/http.test.js b/test/integration/http.test.js index 55afe18..dc7b45f 100644 --- a/test/integration/http.test.js +++ b/test/integration/http.test.js @@ -870,3 +870,41 @@ describe('compression of the admin UI and the API', () => { assert.ok(response.statusCode >= 400 && response.statusCode < 500, `expected a client error, got ${response.statusCode}`); }); }); + +describe('TRUST_PROXY reaches the real app', () => { + // The session cookie is marked Secure only when the request arrived over HTTPS, and + // behind a proxy that is X-Forwarded-Proto's word — which is worth exactly as much + // as the address it came from. The cookie is the observable: no route echoes the + // protocol, and the cookie is what a forged header would actually change. + async function cookieFromLogin(app, { remoteAddress, headers }) { + const login = await app.inject({ + method: 'POST', + url: '/api/login', + remoteAddress, + payload: { username: 'admin', password: ADMIN_PASSWORD }, + headers: { 'sec-fetch-site': 'same-origin', ...headers }, + }); + assert.equal(login.statusCode, 200, login.body); + const raw = login.headers['set-cookie']; + return Array.isArray(raw) ? raw[0] : raw; + } + const claimsHttps = { 'x-forwarded-proto': 'https' }; + + it('believes X-Forwarded-Proto from every address by default', async () => { + const cookie = await cookieFromLogin(server.app, { remoteAddress: '192.168.1.20', headers: claimsHttps }); + assert.match(cookie, /;\s*Secure/i, 'a LAN client claiming HTTPS gets a Secure cookie: that is the default, and the point of the setting'); + }); + + it('believes it only from the listed proxy once narrowed', async () => { + const narrowed = await createTestServer({ env: { TRUST_PROXY: '172.18.0.0/16' } }); + try { + const viaProxy = await cookieFromLogin(narrowed.app, { remoteAddress: '172.18.0.5', headers: claimsHttps }); + assert.match(viaProxy, /;\s*Secure/i, 'the proxy says HTTPS and is believed'); + + const viaLan = await cookieFromLogin(narrowed.app, { remoteAddress: '192.168.1.20', headers: claimsHttps }); + assert.doesNotMatch(viaLan, /;\s*Secure/i, 'a LAN client saying the same thing is not'); + } finally { + await narrowed.cleanup(); + } + }); +}); diff --git a/test/unit/config-trust-proxy.test.js b/test/unit/config-trust-proxy.test.js new file mode 100644 index 0000000..e348cc5 --- /dev/null +++ b/test/unit/config-trust-proxy.test.js @@ -0,0 +1,91 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import Fastify from 'fastify'; + +import { loadConfig } from '../../src/config.js'; + +/** + * TRUST_PROXY decides whose word is taken on where a request came from and whether it + * arrived over HTTPS. The parsing is proved here, and then the parsed value is handed + * to a real Fastify instance, because what matters is not the shape of the value but + * whether Fastify believes a forwarded header from a given address. + */ +const base = { DATA_DIR: '/tmp/selfpod-config-test' }; + +describe('TRUST_PROXY parsing', () => { + it('trusts every hop unless told otherwise, which is what SelfPod always did', () => { + assert.equal(loadConfig(base).trustProxy, true); + assert.equal(loadConfig({ ...base, TRUST_PROXY: '' }).trustProxy, true); + assert.equal(loadConfig({ ...base, TRUST_PROXY: ' TRUE ' }).trustProxy, true); + assert.equal(loadConfig(base).warnings.length, 0, 'the default must not nag'); + }); + + it('can be switched off', () => { + const config = loadConfig({ ...base, TRUST_PROXY: 'false' }); + assert.equal(config.trustProxy, false); + assert.equal(config.warnings.length, 0); + }); + + it('accepts addresses, CIDR ranges, bracketed IPv6 and the named ranges, as a list', () => { + const config = loadConfig({ + ...base, + TRUST_PROXY: '172.18.0.5, 10.0.0.0/8 ,[fd00::1], fe80::/10,Loopback', + }); + assert.deepEqual([...config.trustProxy], ['172.18.0.5', '10.0.0.0/8', 'fd00::1', 'fe80::/10', 'loopback']); + assert.equal(config.warnings.length, 0, 'valid entries must not warn'); + }); + + it('drops an entry that is not an address and says which one', () => { + const config = loadConfig({ ...base, TRUST_PROXY: '10.0.0.0/8, cloudflared, 192.168.1.300, 10.0.0.1/33' }); + assert.deepEqual([...config.trustProxy], ['10.0.0.0/8'], 'the good entry survives the bad ones'); + assert.equal(config.warnings.length, 3, 'each ignored entry gets its own sentence'); + assert.match(config.warnings[0], /TRUST_PROXY entry "cloudflared"/); + assert.match(config.warnings[1], /TRUST_PROXY entry "192\.168\.1\.300"/); + assert.match(config.warnings[2], /TRUST_PROXY entry "10\.0\.0\.1\/33"/); + }); + + it('trusts nobody, not everybody, when nothing in the list is usable', () => { + const config = loadConfig({ ...base, TRUST_PROXY: 'yes' }); + assert.equal( + config.trustProxy, + false, + 'the operator set out to stop trusting every hop; a typo must not quietly put that back', + ); + assert.equal(config.warnings.length, 2, 'the bad entry, then what was done about it'); + assert.match(config.warnings[1], /no proxy is trusted/); + assert.match(config.warnings[1], /TRUST_PROXY=true/); + }); +}); + +describe('what Fastify does with the parsed value', () => { + async function seenBy(trustProxy, { remoteAddress, headers }) { + const app = Fastify({ trustProxy }); + app.get('/', async (request) => ({ ip: request.ip, protocol: request.protocol })); + const response = await app.inject({ method: 'GET', url: '/', remoteAddress, headers }); + await app.close(); + return response.json(); + } + const forged = { 'x-forwarded-for': '203.0.113.9', 'x-forwarded-proto': 'https' }; + + it('believes a forwarded header only from a listed address', async () => { + const trustProxy = loadConfig({ ...base, TRUST_PROXY: '172.18.0.0/16' }).trustProxy; + + const fromProxy = await seenBy(trustProxy, { remoteAddress: '172.18.0.5', headers: forged }); + assert.deepEqual(fromProxy, { ip: '203.0.113.9', protocol: 'https' }, 'the proxy is believed'); + + const fromLan = await seenBy(trustProxy, { remoteAddress: '192.168.1.20', headers: forged }); + assert.deepEqual(fromLan, { ip: '192.168.1.20', protocol: 'http' }, 'a LAN client setting the same headers is not'); + }); + + it('believes everyone by default, and no one when switched off', async () => { + const everyone = await seenBy(loadConfig(base).trustProxy, { remoteAddress: '192.168.1.20', headers: forged }); + assert.equal(everyone.ip, '203.0.113.9'); + + const nobody = await seenBy(loadConfig({ ...base, TRUST_PROXY: 'false' }).trustProxy, { + remoteAddress: '172.18.0.5', + headers: forged, + }); + assert.deepEqual(nobody, { ip: '172.18.0.5', protocol: 'http' }); + }); +}); diff --git a/test/unit/watcher-polling.test.js b/test/unit/watcher-polling.test.js new file mode 100644 index 0000000..0c53244 --- /dev/null +++ b/test/unit/watcher-polling.test.js @@ -0,0 +1,95 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { createWatcher } from '../../src/services/watcher.js'; + +/** + * On a network share the watcher gives up on events and polls instead. Polling means + * chokidar stats every watched file each interval, and the scheduler's rescan already + * stats every file at the rescan interval — so the poll has to run at that same + * interval, or an SMB mount pays for two full sweeps where one was promised. + * + * `watch` is swapped for a stand-in that records what chokidar is asked for: the + * options are the behaviour here, and nothing short of a real SMB share and a stopwatch + * would observe the stat traffic itself. + */ +function watcherWith({ rescanIntervalSeconds = 300 } = {}) { + const calls = []; + const logs = []; + const health = new Map(); + const fakeWatcher = { + on() { + return fakeWatcher; + }, + async close() {}, + }; + const watcher = createWatcher({ + config: { showsDir: '/tmp/selfpod-watcher-test/shows' }, + settings: { rescanIntervalSeconds: () => rescanIntervalSeconds, watcherEnabled: () => true }, + events: null, + logger: { info: (...args) => logs.push(args), debug() {}, warn() {}, error() {} }, + scanner: { enqueueShow() {}, enqueueAll() {} }, + shows: { getBySlug: () => null }, + health: { set: (key, value) => health.set(key, value), clear: (key) => health.delete(key) }, + watch: (path, options) => { + calls.push({ path, options }); + return fakeWatcher; + }, + }); + return { watcher, calls, logs, health }; +} + +/** Two scheduled scans that found changes the watcher never saw is what turns polling on. */ +async function degradeToPolling(watcher) { + watcher.reportScheduledScan({ changed: true }); + watcher.reportScheduledScan({ changed: true }); + // start(true) is fired and not awaited; it needs a turn to close the old watcher. + await new Promise((resolve) => setTimeout(resolve, 20)); +} + +describe('the polling watcher runs at the rescan interval', () => { + it('asks chokidar for events, not polling, to begin with', async () => { + const { watcher, calls } = watcherWith(); + await watcher.start(); + assert.equal(calls.length, 1); + assert.equal(calls[0].options.usePolling, false); + assert.equal(calls[0].options.interval, undefined, 'no poll interval when there is no polling'); + assert.equal(watcher.status().pollIntervalSeconds, null); + await watcher.stop(); + }); + + it('polls every rescan interval once degraded, not every minute on top of it', async () => { + const { watcher, calls, logs, health } = watcherWith({ rescanIntervalSeconds: 300 }); + await watcher.start(); + await degradeToPolling(watcher); + + assert.equal(calls.length, 2, 'the watcher was started again, in polling mode'); + const { options } = calls[1]; + assert.equal(options.usePolling, true); + assert.equal(options.interval, 300_000, 'the poll interval is the rescan interval, not min(rescan, 60 s)'); + assert.equal(options.binaryInterval, 300_000, 'audio files are binary, so this is the interval that actually applies'); + + const status = watcher.status(); + assert.equal(status.mode, 'polling'); + assert.equal(status.pollIntervalSeconds, 300, 'the status says how often the share is polled'); + + const starts = logs.filter(([, message]) => /file watcher started/.test(message)); + assert.equal(starts.length, 2, 'once for events, once again for polling'); + assert.equal(starts[0][0].pollIntervalSeconds, null, 'the events-mode start names no interval'); + assert.equal(starts[1][0].pollIntervalSeconds, 300); + assert.match(starts[1][1], /polling every 5 minutes \(the rescan interval\)/, 'the log line says so in words'); + + assert.match(health.get('watcher').message, /every 5 minutes/, 'the notice and the poll now agree'); + await watcher.stop(); + }); + + it('follows the rescan interval the operator chose, however long', async () => { + const { watcher, calls } = watcherWith({ rescanIntervalSeconds: 3600 }); + await watcher.start(); + await degradeToPolling(watcher); + assert.equal(calls[1].options.interval, 3_600_000, 'an hour means an hour; nothing caps it at a minute'); + assert.equal(watcher.status().pollIntervalSeconds, 3600); + await watcher.stop(); + assert.equal(watcher.status().pollIntervalSeconds, null, 'cleared with the watcher'); + }); +});