diff --git a/CHANGELOG.md b/CHANGELOG.md index f6386d4..45e0d7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Release history focused on **features, settings, default behavior, compatibility - **Static assets for logged-in users:** fixed public CSS/JS/images/fonts getting `BYPASS / cache_rule` as soon as a browser carried a session cookie. Root cause: a request-stage `pass` from a session/Cookie rule disabled the cache in `route_cache.rs` before the static-cookie options (`static_ignore_cookies`, `static_ignore_vary_cookie`) were consulted. Added a central decision (`cache::request_gate`, `static_cookie_guard_relaxation`, `shared_static_response_reason`) with fixed precedence: security, configuration, explicit VCL `pass`, VCL Cookie guard, default request policy, backend response. New request action `cookie_pass` (VCL `return(cookie_pass)`, RustProxy extension) marks Cookie/session guards; it is relaxed only for GET/HEAD static-extension requests without `Authorization` when no other rule would bypass the request with guards removed or without cookies. Explicit `pass` is never relaxed and existing configurations are not rewritten; the rules editor offers **Convert to Cookie guard** for Cookie-only `pass` rules. Session, framework and CDN remaining-cookie templates now insert `cookie_pass` (WordPress/WooCommerce split into a path `pass` and a Cookie guard). A static fill whose Cookie is ignored in the key is stored only if the original backend reply is 200, explicitly public (`public`, `s-maxage`, `max-age>0` or future `Expires`), not HTML/XHTML and has no `Set-Cookie`, `private`, `no-store`, `Vary: *` or `Vary: Authorization` — evaluated before response rules, so `strip_set_cookie`/`ignore_cache_control` cannot share a private reply; such fills are refreshed proactively without the client Cookie. New reason codes `cache_rule_cookie`, `cache_miss_static_cookie`, `response_static_not_public`, `response_static_dynamic`; header names and formats are unchanged. **Test a request** uses the same decision and reports `reason_class`, `static_cookie_relaxed` and `shared_static_check`. Downgrading to 0.39.1 or older requires converting `cookie_pass` rules back to `pass`. Unit and proxy integration tests cover the regression matrix (anonymous/session CSS, Drupal 6 JS with query, fonts, `Vary: Cookie` + `Accept-Encoding` variants, HTML, private routes, Authorization, private/Set-Cookie/HTML/unproven statics, explicit pass, GET/HEAD, options off). - **Configuration compatibility:** configurations without `cookie_pass` are unchanged and load in both directions. A configuration that uses `cookie_pass` is rejected by 0.39.1 and older. - **Edge-case smoke test:** added `tools/dev/cache_edge_smoke.py`, a dependency-free black-box test of the real binary. `local` starts a mock origin and a headless proxy and runs 67 cache/VCL edge cases with cross-user leak detection (split and non-UTF-8 Cookie fields, `%2E`/path-parameter paths, HEAD/Range/304, compression and `Vary` variants, request coalescing with concurrent users); `remote` runs read-only checks against a deployed vhost with diagnostic headers enabled. +- **Fewer full cache purges:** saving global cache *tuning* (memory/entry/object limits, HOT auto-trim, COLD demotion/GC/limits, request coalescing, stale-if-error triggers, proactive refresh) no longer invalidates HOT/COLD for every domain; COLD objects also survive a restart after such changes. Representation and privacy settings (cache on/off, compatibility mode, TTL/grace, static Cookie policy, rules, COLD on/path, transformations, headers) still purge as before. See [Cache](docs/CACHE.md#clear-or-invalidate-content). - **Maintenance:** `cargo fmt --check` and `cargo clippy` are clean again (`cache_plan` formatting, `is_multiple_of` in the VCL importer). ## 0.39.1 — HEAD cache-fill and public Vhost cookie template (source-only changes) diff --git a/docs/CACHE.md b/docs/CACHE.md index c016557..2fbf327 100644 --- a/docs/CACHE.md +++ b/docs/CACHE.md @@ -343,7 +343,16 @@ Server-Timing: proxy;dur=2.31, upstream;dur=1.94, proxy-cache;desc="MISS" Authenticated Cache page/API operations can purge selected entries, domains or larger scopes. The optional public HTTP `PURGE`/`BAN` interface is **disabled by default**. Enable it only by listing trusted client IPs/CIDRs under **Settings → Cache → HTTP BAN / PURGE IP allowlist**; operations are additionally subject to Vhost binding and normal access controls. `PURGE` addresses an exact URL; `BAN` applies the supported bounded matching rules. Never allow untrusted Internet clients to purge all tenants' objects. -Changing operational Vhost settings is intended to preserve compatible HOT/COLD objects; content-affecting changes invalidate affected scopes and COLD carries an additional fingerprint against stale resurrection. **This new behavior still needs release-level verification**, so validate cache after configuration edits and during upgrades. Earlier persistent cache entries may need refilling following format/compatibility transitions. +Which configuration saves invalidate stored objects: + +| Change | Effect on HOT/COLD | +|---|---| +| Global cache **tuning**: memory and object limits (`max_bytes`, `max_entries`, `max_object_bytes`, `transient_max_bytes`, `cold_queue_max_bytes`, `hot_eviction_max_objects`), HOT auto-trim, COLD demotion/GC/limits (`cold_after_seconds`, `cold_on_*`, `cold_max_*`, `cold_gc_*`), request coalescing (`collapse_*`), `stale_if_error_*`, proactive refresh, memory/COLD presets, diagnostic headers, `via_header`, bypass paths/query, HTTP invalidation CIDRs | **Nothing is purged** (since 0.39.2). New values apply to the next requests; a lowered memory/entry limit evicts least-recently-used objects immediately. | +| Global **representation or privacy** settings: cache on/off, `varnish_compatible`, TTL/grace, static Cookie options and extensions, global cache rules, `cold_enabled`/`cold_path`, transformations, global headers/forwarding, Web Services, PHP-FPM profiles | **All** HOT/COLD objects of every Vhost are invalidated. | +| Vhost content settings: hosts, routes, backends' URLs, Vhost cache settings and rules | Only that Vhost's objects are invalidated. | +| Vhost operational settings: TLS, ACL, rate limit, timeouts, load balancing, weights/health checks, authentication, diagnostic headers | Nothing is purged. | + +COLD entries carry the same fingerprint, so after a restart objects survive tuning-only changes and are discarded after representation/privacy changes. Validate the cache after configuration edits and during upgrades; earlier persistent cache entries may need refilling following format/compatibility transitions. ## Troubleshooting diff --git a/src/proxy/forward/static_cookie_policy_tests.rs b/src/proxy/forward/static_cookie_policy_tests.rs index b59a341..ff8aa23 100644 --- a/src/proxy/forward/static_cookie_policy_tests.rs +++ b/src/proxy/forward/static_cookie_policy_tests.rs @@ -109,6 +109,19 @@ mod static_cookie_policy_tests { self.request(Method::GET, path, headers).await } + async fn change(&self, edit: impl FnOnce(&mut Config)) { + let current = self.app.config.load_full(); + let mut config = current.envelope.config.clone(); + edit(&mut config); + self.app + .update(crate::config::Update { + expected_revision: current.envelope.revision, + config, + }) + .await + .unwrap(); + } + async fn stop(self) { self.app.shutdown.cancel(); self.app.tunnels.close(); @@ -667,4 +680,62 @@ mod static_cookie_policy_tests { ); fixture.stop().await; } + + #[tokio::test] + async fn cache_tuning_saves_keep_objects_but_policy_saves_purge() { + let origin = start_origin().await; + let fixture = Fixture::new(test_config(origin.addr, shipped_rules())).await; + assert_eq!(fixture.get("/assets/app.css", &[]).await.cache(), "MISS"); + assert_eq!( + fixture + .get("/assets/auth.css", &[("cookie", SESSION)]) + .await + .cache(), + "MISS" + ); + fixture + .change(|config| { + config.cache.max_entries = 2048; + config.cache.max_bytes *= 2; + config.cache.collapse_enabled = true; + config.cache.stale_if_error_statuses = vec![503]; + config.cache.proactive_refresh_enabled = true; + config.cache.hot_auto_trim_after_seconds = 600; + }) + .await; + assert_eq!(fixture.get("/assets/app.css", &[]).await.cache(), "HIT"); + assert_eq!( + fixture + .get("/assets/auth.css", &[("cookie", SESSION)]) + .await + .cache(), + "HIT" + ); + // A lowered entry limit evicts instead of purging. + fixture.change(|config| config.cache.max_entries = 1).await; + let kept = [ + fixture.get("/assets/app.css", &[]).await.cache().to_owned(), + fixture + .get("/assets/auth.css", &[]) + .await + .cache() + .to_owned(), + ]; + assert!(kept.iter().filter(|c| *c == "HIT").count() <= 1, "{kept:?}"); + fixture + .change(|config| config.cache.max_entries = 4096) + .await; + assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS"); + assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "HIT"); + // Representation/privacy changes still purge. + fixture + .change(|config| config.cache.default_ttl_seconds = 30) + .await; + assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS"); + fixture + .change(|config| config.cache.static_ignore_vary_cookie = false) + .await; + assert_eq!(fixture.get("/fonts/site.woff2", &[]).await.cache(), "MISS"); + fixture.stop().await; + } } diff --git a/src/runtime/tests.rs b/src/runtime/tests.rs index 22d68c9..e040643 100644 --- a/src/runtime/tests.rs +++ b/src/runtime/tests.rs @@ -132,8 +132,61 @@ mod cache_revision_tests { CacheInvalidation::None )); + // Operational cache tuning keeps HOT/COLD objects. + let mut tuning = base.clone(); + tuning.cache.max_entries += 1; + tuning.cache.max_bytes *= 2; + tuning.cache.max_object_bytes /= 2; + tuning.cache.transient_max_bytes *= 2; + tuning.cache.hot_eviction_max_objects += 1; + tuning.cache.hot_auto_trim_enabled = !base.cache.hot_auto_trim_enabled; + tuning.cache.hot_auto_trim_after_seconds += 10; + tuning.cache.hot_auto_trim_target_percent = 70; + tuning.cache.cold_max_bytes *= 2; + tuning.cache.cold_max_entries += 1; + tuning.cache.cold_gc_interval_seconds += 1; + tuning.cache.cold_gc_max_delete += 1; + tuning.cache.cold_after_seconds += 1; + tuning.cache.cold_on_idle = !base.cache.cold_on_idle; + tuning.cache.cold_max_hot_seconds += 1; + tuning.cache.cold_on_age = !base.cache.cold_on_age; + tuning.cache.cold_on_lru = !base.cache.cold_on_lru; + tuning.cache.cold_on_shutdown = !base.cache.cold_on_shutdown; + tuning.cache.collapse_enabled = !base.cache.collapse_enabled; + tuning.cache.collapse_wait_timeout_ms += 1; + tuning.cache.stale_if_error_timeout = !base.cache.stale_if_error_timeout; + tuning.cache.stale_if_error_statuses = vec![503]; + tuning.cache.proactive_refresh_enabled = !base.cache.proactive_refresh_enabled; + tuning.cache.proactive_refresh_percent = 20; + tuning.cache.proactive_refresh_max_inflight = 8; + assert_eq!(cache_revision(&tuning), identity); + assert!(matches!( + cache_invalidation(&base, &tuning), + CacheInvalidation::None + )); + + // Representation and privacy settings still purge everything. + for change in [ + (|c: &mut config::Config| c.cache.default_ttl_seconds += 60) as fn(&mut config::Config), + |c| c.cache.max_ttl_seconds += 60, + |c| c.cache.varnish_grace_seconds += 1, + |c| c.cache.varnish_compatible = !c.cache.varnish_compatible, + |c| c.cache.static_ignore_cookies = !c.cache.static_ignore_cookies, + |c| c.cache.static_ignore_vary_cookie = !c.cache.static_ignore_vary_cookie, + |c| c.cache.static_extensions.push("json".into()), + |c| c.cache.cold_enabled = !c.cache.cold_enabled, + |c| c.cache.rules.push(serde_json::from_value(serde_json::json!( + {"id": "x", "conditions": [{"field": "path", "operator": "prefix", "values": ["/a"]}], + "actions": [{"type": "pass"}]})).unwrap()), + ] { + let mut changed = base.clone(); + change(&mut changed); + assert_ne!(cache_revision(&changed), identity); + assert!(matches!(cache_invalidation(&base, &changed), CacheInvalidation::All(_))); + } + let mut cache = base.clone(); - cache.cache.max_entries += 1; + cache.cache.default_ttl_seconds += 1; let cache_identity = cache_revision(&cache); assert_ne!(cache_identity, identity); assert!(matches!( diff --git a/src/runtime/types.rs b/src/runtime/types.rs index f5435a7..e659b42 100644 --- a/src/runtime/types.rs +++ b/src/runtime/types.rs @@ -100,6 +100,45 @@ fn cache_revision(config: &config::Config) -> u64 { cache.expose_only_hits = false; cache.expose_detailed_status = false; cache.via_header = None; + // Operational tuning: memory/eviction limits, COLD demotion and GC, + // request coalescing, stale-if-error triggers and proactive refresh. + // They change how the cache is run, not what a stored object contains or + // who may receive it, so changing them must not purge HOT/COLD. New limits + // apply immediately (`Cache::configure` evicts above a lowered limit). + // Representation/privacy fields (enabled, varnish_compatible, TTL/grace, + // static Cookie policy, rules, cold_enabled/cold_path) stay in the digest. + let neutral = crate::cache::Settings::default(); + cache.max_bytes = neutral.max_bytes; + cache.max_object_bytes = neutral.max_object_bytes; + cache.max_entries = neutral.max_entries; + cache.transient_max_bytes = neutral.transient_max_bytes; + cache.cold_queue_max_bytes = neutral.cold_queue_max_bytes; + cache.hot_eviction_max_objects = neutral.hot_eviction_max_objects; + cache.hot_auto_trim_enabled = neutral.hot_auto_trim_enabled; + cache.hot_auto_trim_after_seconds = neutral.hot_auto_trim_after_seconds; + cache.hot_auto_trim_target_percent = neutral.hot_auto_trim_target_percent; + cache.cold_max_bytes = neutral.cold_max_bytes; + cache.cold_max_entries = neutral.cold_max_entries; + cache.cold_gc_interval_seconds = neutral.cold_gc_interval_seconds; + cache.cold_gc_max_delete = neutral.cold_gc_max_delete; + cache.cold_after_seconds = neutral.cold_after_seconds; + cache.cold_on_idle = neutral.cold_on_idle; + cache.cold_max_hot_seconds = neutral.cold_max_hot_seconds; + cache.cold_on_age = neutral.cold_on_age; + cache.cold_on_lru = neutral.cold_on_lru; + cache.cold_on_shutdown = neutral.cold_on_shutdown; + cache.collapse_enabled = neutral.collapse_enabled; + cache.collapse_wait_timeout_ms = neutral.collapse_wait_timeout_ms; + cache.collapse_timeout_fallback = neutral.collapse_timeout_fallback; + cache.stale_if_error_timeout = neutral.stale_if_error_timeout; + cache.stale_if_error_connection = neutral.stale_if_error_connection; + cache.stale_if_error_dns = neutral.stale_if_error_dns; + cache.stale_if_error_tls = neutral.stale_if_error_tls; + cache.stale_if_error_statuses = neutral.stale_if_error_statuses; + cache.proactive_refresh_enabled = neutral.proactive_refresh_enabled; + cache.proactive_refresh_before_expiry_seconds = neutral.proactive_refresh_before_expiry_seconds; + cache.proactive_refresh_percent = neutral.proactive_refresh_percent; + cache.proactive_refresh_max_inflight = neutral.proactive_refresh_max_inflight; let mut transform = config.transform.clone(); transform.preset = None; transform.memory_budget_bytes = 0;