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 <[email protected]>
This commit is contained in:
1 parent
c87aad0254
commit
e48a1eacf4
4 files changed
+8
-3
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
+4
-1
@@ -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
|
||||
|
||||
@@ -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/<slug>/p/`, `index.<build>.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.
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user