From a7ed7a58e9ef997ab54b37424dae53070aa6d548 Mon Sep 17 00:00:00 2001 From: luen Date: Tue, 3 Nov 2026 09:29:53 +1000 Subject: [PATCH 1/2] Clear Snyk container and code findings with Alpine hardening and safer URL unwrap. Co-authored-by: Cursor --- .dockerignore | 16 ++++++++++++++++ .github/dependabot.yml | 5 +++++ Dockerfile | 35 +++++++++++++++++++++++++---------- src/normalize.py | 11 +++++++++-- tests/test_normalize.py | 11 +++++++++++ 5 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d575b22 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.git +.github +.venv +__pycache__ +*.py[cod] +.pytest_cache +.ruff_cache +.mypy_cache +.env +.env.* +!.env.example +data +tests +*.md +.snyk +docker-compose*.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index e8949bd..01bece4 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -14,3 +14,8 @@ updates: directory: "/" schedule: interval: "monthly" + + - package-ecosystem: "docker" + directory: "/" + schedule: + interval: "monthly" diff --git a/Dockerfile b/Dockerfile index 8148033..900f0fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,35 @@ -FROM python:3.12-slim +# syntax=docker/dockerfile:1 + +FROM python:3.12.16-alpine3.24 AS build + +WORKDIR /src + +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +RUN --mount=type=bind,source=requirements.txt,target=requirements.txt \ + --mount=type=cache,target=/root/.cache/pip \ + pip install --no-cache-dir --no-compile -r requirements.txt + +FROM python:3.12.16-alpine3.24 AS runtime WORKDIR /app ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ - TZ=Australia/Brisbane - -RUN apt-get update \ - && apt-get install -y --no-install-recommends tzdata \ - && rm -rf /var/lib/apt/lists/* + TZ=Australia/Brisbane \ + PATH="/opt/venv/bin:$PATH" -COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +RUN --mount=type=cache,target=/etc/apk/cache,sharing=locked \ + apk add tzdata \ + && addgroup -S -g 1001 appgroup \ + && adduser -S -u 1001 -G appgroup appuser \ + && mkdir -p /app/data \ + && chown appuser:appgroup /app/data -COPY src ./src +COPY --from=build --chown=appuser:appgroup /opt/venv /opt/venv +COPY --chown=appuser:appgroup src ./src -RUN mkdir -p /app/data +USER appuser CMD ["python", "-m", "src", "--schedule"] diff --git a/src/normalize.py b/src/normalize.py index 2d53cb7..fe95429 100644 --- a/src/normalize.py +++ b/src/normalize.py @@ -13,12 +13,19 @@ } +def _is_google_host(host: str) -> bool: + # Require an exact google.com label (or subdomain). Plain endswith("google.com") + # would also match attacker hosts like "evilgoogle.com". + return host == "google.com" or host.endswith(".google.com") + + def unwrap_google_url(url: str) -> str: parsed = urlparse(url) host = (parsed.hostname or "").lower() - if host.endswith("google.com") and parsed.path == "/url": + if _is_google_host(host) and parsed.path == "/url": target = parse_qs(parsed.query).get("q", [""])[0] - if target.startswith("http"): + # Require a real http(s) scheme; startswith("http") accepts "httpevil://…". + if target.startswith(("http://", "https://")): return target return url diff --git a/tests/test_normalize.py b/tests/test_normalize.py index e74874f..db98b45 100644 --- a/tests/test_normalize.py +++ b/tests/test_normalize.py @@ -8,6 +8,17 @@ def test_unwraps_google_redirect(): assert unwrap_google_url(wrapped) == "https://www.abc.net.au/news/story" +def test_rejects_lookalike_google_host(): + # endswith("google.com") alone would treat this as Google. + spoofed = "https://evilgoogle.com/url?q=https://www.abc.net.au/news/story" + assert unwrap_google_url(spoofed) == spoofed + + +def test_rejects_non_http_google_target(): + wrapped = "https://www.google.com/url?q=httpevil://example.com/payload" + assert unwrap_google_url(wrapped) == wrapped + + def test_strips_utm(): url = "https://www.theguardian.com/environment/story?utm_source=rss&utm_medium=feed" assert canonicalize_url(url) == "https://www.theguardian.com/environment/story" From bb80a1539c07a97b69c29ba0b92d51760621a1c5 Mon Sep 17 00:00:00 2001 From: luen Date: Tue, 3 Nov 2026 09:34:24 +1000 Subject: [PATCH 2/2] Keep ./data writable for the non-root container user. The bind mount hides the in-image chown, so uid 1001 couldn't write seen.json on typical Linux hosts (and older root-owned data/ dirs), which would re-email the same items after every run. Use uid/gid 1000, make the compose user configurable via APP_UID/APP_GID, document the one-time chown for existing data/, and drop the pip cache mount that --no-cache-dir made a no-op. Co-Authored-By: Claude Opus 5.5 --- .env.example | 4 ++++ AGENTS.md | 1 + Dockerfile | 7 ++++--- README.md | 6 ++++++ docker-compose.yml | 2 ++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index a10cd8e..fd81e1e 100644 --- a/.env.example +++ b/.env.example @@ -22,6 +22,10 @@ GOOGLE_CX_ID= # ScraperAPI — redirect/fetch helper only; do not scrape full articles SCRAPER_API_KEY= +# Container user; must own ./data on the host (check with `id -u` / `id -g`) +APP_UID=1000 +APP_GID=1000 + # Schedule in Australia/Brisbane TZ=Australia/Brisbane DIGEST_HOUR=7 diff --git a/AGENTS.md b/AGENTS.md index 9417d23..7c9e008 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,6 +67,7 @@ Add or update tests for code you change. Fix failures before finishing. - Do **not** add full-page article scraping; ScraperAPI is only for exceptional fetch helpers (e.g. redirect resolution), never as a content source - Own sites (`rummerlab.com`, `jodierummer.com`, `physioshark.org`) stay excluded - Seen URLs live in `data/seen.json` — do not wipe production state casually +- The container runs non-root (`APP_UID`/`APP_GID` in compose, default 1000). The `./data` bind mount must be writable by that uid, or `seen.json` can't be saved and items are re-emailed ## README and docs diff --git a/Dockerfile b/Dockerfile index 900f0fa..ad5ffd5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,6 @@ RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" RUN --mount=type=bind,source=requirements.txt,target=requirements.txt \ - --mount=type=cache,target=/root/.cache/pip \ pip install --no-cache-dir --no-compile -r requirements.txt FROM python:3.12.16-alpine3.24 AS runtime @@ -20,10 +19,12 @@ ENV PYTHONDONTWRITEBYTECODE=1 \ TZ=Australia/Brisbane \ PATH="/opt/venv/bin:$PATH" +# uid/gid 1000 matches the usual first host user, so the ./data bind mount stays +# writable; compose can override it via APP_UID/APP_GID when the host differs. RUN --mount=type=cache,target=/etc/apk/cache,sharing=locked \ apk add tzdata \ - && addgroup -S -g 1001 appgroup \ - && adduser -S -u 1001 -G appgroup appuser \ + && addgroup -S -g 1000 appgroup \ + && adduser -S -u 1000 -G appgroup appuser \ && mkdir -p /app/data \ && chown appuser:appgroup /app/data diff --git a/README.md b/README.md index c11e527..1ee0ae7 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,12 @@ docker compose run --rm digest python -m src --dry-run Seen URLs are stored in `data/seen.json` so repeats are not emailed again. +The container runs as a non-root user (`APP_UID`/`APP_GID`, default `1000:1000`), and that user must be able to write `./data`. If your host user isn't 1000, set `APP_UID`/`APP_GID` in `.env` to the output of `id -u` / `id -g`. Images up to v0.0.2 ran as root, so an existing `data/` may be root-owned. Fix it once before upgrading: + +```bash +sudo chown -R "${APP_UID:-1000}:${APP_GID:-1000}" data +``` + ## Local tests ```bash diff --git a/docker-compose.yml b/docker-compose.yml index 5709eb4..115d94b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,8 @@ services: digest: build: . container_name: rummerlab-media-alerts + # Must own ./data on the host, or seen.json can't be saved and items repeat. + user: "${APP_UID:-1000}:${APP_GID:-1000}" env_file: - path: .env required: false