This commit is contained in:
Mateusz Gruszczyński committed 2026-11-20 19:51:27 +01:00
1 parent 15425fb803
commit 0fc9c0d239
21 files changed
+395 -36

No files matched your search

+49 -4
View File
@@ -1,6 +1,6 @@
# VCL cookbook: concrete use cases and HTTP traffic scenarios
**Target:** RustProxy **0.39.0** with the Docker Cache/Traffic and CDN cookie patch. This guide documents the *actual importer* (`src/cache/vcl.rs`), validated cache-rule actions (`src/cache/rules.rs`), and shared-cache admission documented in [Cache](CACHE.md). It is **not** a full Varnish implementation or a promise of exact Varnish semantics.
**Target:** RustProxy **0.39.1** with the Docker Cache/Traffic and CDN cookie patch. This guide documents the *actual importer* (`src/cache/vcl.rs`), validated cache-rule actions (`src/cache/rules.rs`), and shared-cache admission documented in [Cache](CACHE.md). It is **not** a full Varnish implementation or a promise of exact Varnish semantics.
Read together with [VCL compatibility](VCL.md) and the [supported/unsupported capability matrix](VCL_SUPPORT_MATRIX.md). The examples are **separate policies**, not one file that should be imported wholesale. Copy one recipe into the correct Vhost scope, then reconcile it with existing global/local rules; **never paste contradictory `return(pass)` policies together without reviewing rule order**.
@@ -31,6 +31,7 @@ Read together with [VCL compatibility](VCL.md) and the [supported/unsupported ca
| CMS articles or a public catalog | [Public catalog](#2-public-catalog-with-explicit-short-ttl) | Cache genuinely shared 200 GETs, set TTL/grace | Force storage of `private`, `Set-Cookie` or 404 |
| Public JSON endpoint for prices/stock | [Public JSON API](#3-public-get-json-api) | Short-lived shared cache | Cache credentialed/private API responses |
| Admin, login, checkout, user dashboard | [Private routes](#1-exclude-private-routes-and-unsafe-methods) | Bypass shared cache | Replace ACL, OIDC, Basic Auth, app permissions |
| Cookies inherited from a parent domain on a **fully public** Vhost | [Public Vhost inherited cookies](#12-public-vhost-inherited-cookies-opt-in) | Remove **all** Cookie for eligible GET/HEAD after operator review | Detect which cookies came from a parent domain; guarantee safe login/personalization routes |
| Cloudflare/Akamai technical cookies | [CDN cookie allowlist](#4-cdn-technical-cookies-without-dropping-sessions) | Strip an **entire** allowlisted Cookie header | Partially delete one Cookie from a mixed list |
| Public CSS, JS, fonts, images | [Static assets](#5-static-assets-and-vary-cookie) + native static settings | Strip request Cookie if proven safe; optional **conditional** `Vary: Cookie` treatment | Override any other arbitrary `Vary` dimension |
| Multilingual catalog | [Language variants](#6-language-specific-representations) | Preserve origin `Vary: Accept-Language` and separate variants | Automatically translate or change language |
@@ -40,7 +41,7 @@ Read together with [VCL compatibility](VCL.md) and the [supported/unsupported ca
| Remove tracking parameters from cache identity | [Query rewrite](#10-tracking-query-normalization-restricted) or graphical Strip query | Drop approved params after URL normalization | Import arbitrary `regsub` or preserve regex URL semantics exactly |
| Origin sets irrelevant `Set-Cookie` on public SVG | [Advanced public cookie cleanup](#11-advanced-removing-origin-set-cookie-high-risk) | Remove it before storage | Prove it was not needed for identity/session |
| CORS responses differentiated by `Origin` | Origin's `Vary: Origin` | Keep origins separate | Safely collapse all origins into one cached response |
| Cache error pages/redirects (`404`, `301`, `302`) | **Not supported** | No cache storage in 0.39.0 | Invent negative/redirect-cache TTL via VCL |
| Cache error pages/redirects (`404`, `301`, `302`) | **Not supported** | No cache storage in 0.39.1 | Invent negative/redirect-cache TTL via VCL |
| IP ranges, backend routing, purge, TLS security | Native ACL/Vhost/Cache API settings | Dedicated supported configuration | Import ACL/director/`return(purge)` as executable VCL |
## C. Copy-and-import VCL recipes
@@ -300,7 +301,7 @@ sub vcl_hash {
**Prerequisites:** Use native proxy/WebSocket/stream and backend retry configuration for those separate operations.
**Caveat:** This is not a Varnish `vcl_backend_error`, retry, pipe or stale-if-error implementation. `500` was never eligible for 0.39.0 shared storage anyway.
**Caveat:** This is not a Varnish `vcl_backend_error`, retry, pipe or stale-if-error implementation. `500` was never eligible for 0.39.1 shared storage anyway.
Source: [`examples/vcl/stream-and-errors-no-store.vcl`](../examples/vcl/stream-and-errors-no-store.vcl).
@@ -402,6 +403,42 @@ sub vcl_backend_response {
}
```
### 12. Public Vhost: inherited cookies (opt-in)
**Goal:** Keep cache working on a completely public site when a browser includes unrelated cookies set with `Domain=.example.test` by another host. This is generic for any hostname; **it cannot detect the cookie's original Domain**, which is not part of the HTTP `Cookie` request header.
**Expected traffic:** `GET /catalog`, `Cookie: parent_session=x; analytics=y` → Cookie stripped before cache lookup and before origin forwarding. The origin serves its anonymous public representation; an eligible repeat GET may HIT. `GET /account` or a request with `Authorization` keeps its cookies and follows normal bypass/auth policies. POST is unaffected.
**Prerequisites:** Use the per-Vhost cache rule editor, confirm that the *entire eligible area* is public for guests and logged-in users, and extend the excluded path regex to cover **all** private routes of the application. The graphical template is **disabled by default** until manually enabled. Avoid installing it globally or after existing terminal Cookie `pass` rules. The safest fix is often to remove the broad `Domain` attribute from `Set-Cookie` at the issuing application so browsers no longer send unrelated cookies to siblings.
**Caveat:** This deliberately removes **every** cookie on the allowed public pages, not just analytics or inherited cookies. It can break logins, preferences, locale choice, cart, stickiness or security challenge behavior, and may serve anonymous content to a logged-in user. Never apply it to personalized content. It does not rewrite origin `Vary: Cookie`, `Set-Cookie` or `Cache-Control` and does not bypass RustProxy response privacy checks. Use [`docs/CACHE.md`](CACHE.md#inherited-cookies-on-public-vhosts-opt-in-disabled-by-default) for configuration and risk details.
Source: [`examples/vcl/public-vhost-ignore-cookies.vcl`](../examples/vcl/public-vhost-ignore-cookies.vcl).
```vcl
// OPT-IN: use ONLY on an HTTP reverse-proxy Vhost with fully public,
// cookie-independent GET/HEAD content. Import into that Vhost, NOT globally.
// Useful when a parent domain sets Domain=.example.test cookies that a public
// sibling subdomain neither needs nor should forward to the backend.
//
// Cookie does not include the cookie's Domain attribute. It is IMPOSSIBLE
// to distinguish inherited cookies from host-only cookies in vcl_recv.
// This deliberately strips ALL cookies on permitted public paths; verify
// that users do not need login, preferences, locale, cart or affinity there.
// Customize the excluded path list to fit your application before enabling.
// Backend must return an eligible PUBLIC response (no private/no-store or
// Set-Cookie); this does not override response cache privacy or Vary.
sub vcl_recv {
if ((req.method == "GET" || req.method == "HEAD") &&
req.http.Cookie &&
!req.http.Authorization &&
req.url !~ "^/(admin|api|auth|login|logout|account|profile|user|settings|dashboard|cart|checkout|panel|konto|wp-admin|wp-login[.]php)(/|[?]|$)") {
unset req.http.Cookie;
return(hash);
}
}
```
## D. Real HTTP traffic: what is handled and what is not
The outcomes below describe **decision paths**, not a guaranteed X-Cache label: proxy route exclusions, access checks, configuration, origin responses, object sizes, validators, time, and storage limits still apply. The word **eligible** means the request may look up or populate cache if all other conditions succeed.
@@ -423,6 +460,9 @@ The outcomes below describe **decision paths**, not a guaranteed X-Cache label:
| `Cookie: __cf_bm=a; cf_clearance=b` | CDN cookie allowlist | Strip whole Cookie header | Only exact allowlisted cookie names pass. |
| `Cookie: __cf_bm=a; session=b` | CDN cookie allowlist | PASS | Even one non-allowlisted name prevents removal. |
| Two physical Cookie fields, one technical and one session | CDN cookie allowlist | PASS | Patched matcher examines combined cookie values. |
| `GET /catalog`, Cookie from parent domain | Public-Vhost opt-in | Strip all Cookie; eligible | Only on verified cookie-independent pages with reviewed exclusions. |
| `GET /account`, parent-domain Cookie | Public-Vhost opt-in | Ordinary bypass/auth rules | Protected-path guard leaves Cookie untouched. |
| `GET /catalog`, Cookie plus Authorization | Public-Vhost opt-in | Native auth/bypass | The template does not remove Cookie with Authorization. |
| `GET /assets/site.css`, arbitrary Cookie | Public static | Strip Cookie **if verified safe** | Separate native `Vary: Cookie` setting has additional conditions. |
| `GET /assets/user-avatar.png`, Cookie | Do **not** use static bypass | PASS recommended | Do not remove session personalization merely due to extension. |
| `GET /experiment`, `X-Experiment: A` | A/B variants | Eligible A partition | Origin must emit downstream `Vary: X-Experiment`. |
@@ -432,7 +472,7 @@ The outcomes below describe **decision paths**, not a guaranteed X-Cache label:
### D2. Origin response and `Vary`
| Backend sends | RustProxy 0.39.0 consequence | Safety check |
| Backend sends | RustProxy 0.39.1 consequence | Safety check |
|---|---|---|
| `200`, public/fresh, no Set-Cookie, valid Vary | Potential shared-store candidate | Response must really be identical for all users. |
| `200`, `Cache-Control: private` | Not storable without **dangerous explicit rewrite** | Never rewrite private content to public. |
@@ -507,6 +547,11 @@ curl -sS -o /dev/null -D - "$BASE/catalog/item/1"
curl -sS -o /dev/null -D - -H 'Cookie: session=test' "$BASE/catalog/item/1"
curl -sS -o /dev/null -D - -H 'Authorization: Bearer test' "$BASE/catalog/item/1"
# Optional public-Vhost template (after explicitly enabling and reviewing paths):
# browser-equivalent Cookie is removed for genuinely public pages.
curl -sS -o /dev/null -D - -H 'Cookie: parent_session=abc' "$BASE/catalog/item/1"
curl -sS -o /dev/null -D - -H 'Cookie: parent_session=abc' "$BASE/catalog/item/1"
# Technical-only cookies vs mixed cookies: inspect origin logs as well.
curl -sS -o /dev/null -D - -H 'Cookie: __cf_bm=test' "$BASE/catalog/item/1"
curl -sS -o /dev/null -D - -H 'Cookie: __cf_bm=test; session=test' "$BASE/catalog/item/1"