From e48a1eacf498afcfcfcd774476128ae3bfbc0b95 Mon Sep 17 00:00:00 2001 From: Lucio Chen Date: Wed, 4 Nov 2026 22:49:45 -0800 Subject: [PATCH] Keep hotlink 403s out of CDN and browser caches The tile hotlink refusal is sent with Cache-Control: no-store from LuPhoto, Caddy and nginx instead of inheriting the one-year immutable header. cloudflare.md explains that edge-cached tiles bypass the origin Referer check, recommends a WAF rule for /t/, and notes that tile URLs depend only on the photo, the settings and the site key. Co-Authored-By: Claude Opus 5.5 --- internal/server/server.go | 1 + packaging/Caddyfile | 5 ++++- packaging/cloudflare.md | 4 ++-- packaging/nginx.conf | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 1d9e3f9..196c8ce 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -85,6 +85,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { settings := h.settings.Load() clean := path.Clean("/" + r.URL.Path) if strings.HasPrefix(clean, "/t/") && hotlinked(r, settings.HotlinkAllow) { + header.Set("Cache-Control", "no-store") http.Error(w, "forbidden", http.StatusForbidden) return } diff --git a/packaging/Caddyfile b/packaging/Caddyfile index 7b59db6..d39605f 100644 --- a/packaging/Caddyfile +++ b/packaging/Caddyfile @@ -6,7 +6,10 @@ photos.luciochen.com { header Referer * not header_regexp Referer ^https?://photos\.luciochen\.com(:[0-9]+)?(/|$) } - respond @hotlink 403 + handle @hotlink { + header Cache-Control "no-store" + respond 403 + } @hidden path */.* *.gz error @hidden 404 diff --git a/packaging/cloudflare.md b/packaging/cloudflare.md index da10769..04cd27b 100644 --- a/packaging/cloudflare.md +++ b/packaging/cloudflare.md @@ -19,7 +19,7 @@ client_ip_header = "CF-Connecting-IP" Caching → Cache Rules, in this order: 1. Key endpoint: when the URI path equals `/k`, *Bypass cache*. It is a per-visitor `POST` answered with `Cache-Control: no-store`; it must never be cached or served from the edge. -2. Tiles, OG images and assets: when the URI path starts with `/t/`, `/og/` or `/assets/`, set *Eligible for cache*, Edge TTL 1 month (or "use cache-control header"), Browser TTL "respect origin". Their names are content-hashed, so they never change. +2. Tiles, OG images and assets: when the URI path starts with `/t/`, `/og/` or `/assets/`, set *Eligible for cache*, Edge TTL 1 month (or "use cache-control header"), Browser TTL "respect origin". Their names are content-hashed, so they never change. A tile URL depends only on the photo's bytes, the processing settings and the site key in `/var/lib/luphoto/state/site.key`, so it stays the same across builds and restarts; back up that key, because a new one gives every tile a new URL and empties the edge cache. 3. Everything else (HTML, `/a/*` including the per-photo pool files under `/a//p/`, `index..json`, `robots.txt`): Edge TTL 1 minute or "respect origin" (`max-age=60`). Manifests and pool files are named after the build, so a new build needs no purge. Decoy tiles under `/t/` never change their bytes either, so rule 2 holds for them. -Keep "Cache by device type" and query-string variations off. Hotlink protection is done by LuPhoto, Caddy or nginx on `/t/*`, and `/k` checks `Origin`; Cloudflare's own Hotlink Protection can stay off, or be enabled for the same hostname. Bot Fight Mode and similar challenges must not be applied to `/k`, because they would answer the browser's `fetch` with an HTML page. +Keep "Cache by device type" and query-string variations off. Once a tile is in the edge cache, Cloudflare serves it without asking the origin, so the `Referer` check in LuPhoto, Caddy or nginx only sees cache misses (its 403 is sent with `Cache-Control: no-store`, so a refused request is never cached for real visitors). To block hotlinking of cached tiles too, add a WAF custom rule: when the URI path starts with `/t/`, the `Referer` header is not empty and it does not start with `https://photos.luciochen.com/`, *Block*. Cloudflare's Scrape Shield Hotlink Protection works as well, but it only covers image extensions it knows. `/k` checks `Origin` itself. Bot Fight Mode and similar challenges must not be applied to `/k`, because they would answer the browser's `fetch` with an HTML page. diff --git a/packaging/nginx.conf b/packaging/nginx.conf index 6be331d..7d1e6d2 100644 --- a/packaging/nginx.conf +++ b/packaging/nginx.conf @@ -54,6 +54,7 @@ server { location /t/ { if ($luphoto_hotlink) { + add_header Cache-Control "no-store" always; return 403; } add_header Cache-Control "public, max-age=31536000, immutable" always;