This commit is contained in:
Mateusz Gruszczyński committed 2026-11-20 23:14:19 +01:00
1 parent d455028de3
commit 5b2eaaff61
36 files changed
+2544 -102

No files matched your search

+37 -27
View File
@@ -126,7 +126,18 @@ if let Some(exchange) = request.extensions().get::<Arc<Exchange>>() {
let mut cache_rule_context: Option<Arc<crate::cache::RuleContext>> = None;
let mut cache_rule_query: Option<Option<String>> = None;
let mut cache_rule_variant = String::new();
let mut cache_rule_pass = false;
let mut cache_rule_gate = crate::cache::RuleGate::None;
// Security and configuration gates are known before any rule runs; they always
// win, so a Cookie guard is only ever relaxed when none of them applies.
let gate_input = crate::cache::GateInput {
admin_control_plane,
basic_auth: frontend.is_some_and(|f| f.basic_auth.is_some()),
auth_provider: frontend.and_then(|f| f.auth_provider.as_ref()).map(|p| p.id()),
sticky_cookie: route.pool.as_ref().is_some_and(|pool| pool.cookie_affinity()),
cache_enabled: cache_settings.enabled,
route_disabled: route.cache == Some(false),
upgrade: handshake.is_some(),
};
let mut cache_rule_lookup = false;
// VCL request mutations (cookie/query edits) still affect the origin when
// cache storage is disabled for this route. Storage eligibility is evaluated
@@ -141,7 +152,21 @@ if !admin_control_plane {
headers: request.headers().clone(),
client_ip: Some(context.client_ip),
};
let outcome = rules.evaluate_request(&rule_request, true);
let mut outcome = rules.evaluate_request(&rule_request, true);
cache_rule_gate = crate::cache::RuleGate::from_outcome(&outcome);
// Central static-cookie decision: a `cookie_pass` guard (never an
// explicit `pass`) may yield to the static-file cookie policy. The
// unguarded outcome then drives the forwarded request and cache key.
if cache_rule_gate == crate::cache::RuleGate::CookieGuard
&& crate::cache::request_gate(gate_input, crate::cache::RuleGate::None).is_none()
{
if let Some(relaxed) = crate::cache::static_cookie_guard_relaxation(
&rules, &rule_request, &outcome, cache_settings,
) {
outcome = relaxed.outcome;
cache_rule_gate = crate::cache::RuleGate::CookieGuardRelaxed;
}
}
if !outcome.matched.is_empty() {
outcome.apply_headers(request.headers_mut());
outcome.rewrite_cookies(request.headers_mut());
@@ -160,7 +185,6 @@ if !admin_control_plane {
}
}
cache_rule_variant = outcome.hash_variant();
cache_rule_pass = outcome.pass.is_some();
cache_rule_lookup = outcome.lookup.is_some();
rule_request.headers = request.headers().clone();
rule_request.query = request.uri().query().map(str::to_owned);
@@ -174,29 +198,9 @@ if !admin_control_plane {
}
}
}
let policy_reason = if admin_control_plane {
Some("admin_control_plane")
} else if frontend.is_some_and(|f| f.basic_auth.is_some()) {
Some("basic_auth")
} else if let Some(provider) = frontend.and_then(|f| f.auth_provider.as_ref()) {
Some(provider.id())
} else if route
.pool
.as_ref()
.is_some_and(|pool| pool.cookie_affinity())
{
Some("sticky_cookie")
} else if !cache_settings.enabled {
Some("cache_disabled")
} else if route.cache == Some(false) {
Some("route_disabled")
} else if handshake.is_some() {
Some("request_upgrade")
} else if cache_rule_pass {
Some("cache_rule")
} else {
None
};
let policy_reason = crate::cache::request_gate(gate_input, cache_rule_gate);
let static_cookie_relaxed = policy_reason.is_none()
&& cache_rule_gate == crate::cache::RuleGate::CookieGuardRelaxed;
let cache_enabled = policy_reason.is_none();
let body_empty = if cache_enabled
&& handshake.is_none()
@@ -339,7 +343,12 @@ let range_requested = !matches!(range_selection, crate::cache::RangeSelection::N
let cache_route = route.id.as_str();
let mut cache_key_headers = None;
if effective_cacheable && ignore_static_cookie && request.headers().contains_key(::http::header::COOKIE) {
// The forwarded request still carries a Cookie that the cache key ignores:
// the backend response must prove that it is shareable before storage.
let shared_static_cookie_fill = effective_cacheable
&& ignore_static_cookie
&& request.headers().contains_key(::http::header::COOKIE);
if shared_static_cookie_fill {
let mut headers = request.headers().clone();
headers.remove(::http::header::COOKIE);
cache_key_headers = Some(headers);
@@ -390,5 +399,6 @@ let miss_recorded = false;
range_selection, if_range, range_requested, effective_reason, revalidation_reason,
cache_ticket, client_conditionals, cached_revalidation, freshness_revalidation_reason,
pending_revalidation, miss_recorded, cache_rule_context,
static_cookie_relaxed, shared_static_cookie_fill,
)
}