use std::cell::RefCell;
use std::future::Future;
#[derive(Default)]
struct PageStaging {
cache_control: Option<String>,
csrf: String,
csp_nonce: String,
status: Option<u16>,
}
tokio::task_local! {
static PAGE_STAGING: RefCell<PageStaging>;
}
thread_local! {
static FALLBACK_STAGING: RefCell<PageStaging> = RefCell::new(PageStaging::default());
}
pub async fn scope_page_staging<F: Future>(fut: F) -> F::Output {
PAGE_STAGING
.scope(RefCell::new(PageStaging::default()), fut)
.await
}
fn with_staging<R>(f: impl FnOnce(&mut PageStaging) -> R) -> R {
let mut f = Some(f);
match PAGE_STAGING.try_with(|cell| (f.take().expect("staging fn"))(&mut cell.borrow_mut())) {
Ok(out) => out,
Err(_) => {
FALLBACK_STAGING.with(|cell| (f.take().expect("staging fn"))(&mut cell.borrow_mut()))
}
}
}
pub fn clear_request_staging() {
with_staging(|s| *s = PageStaging::default());
}
pub fn stage_response_status(status: u16) {
with_staging(|s| s.status = Some(status));
}
pub fn take_response_status() -> Option<u16> {
with_staging(|s| s.status.take())
}
pub fn stage_response_cache_control(value: impl Into<String>) {
let value = value.into();
with_staging(|s| s.cache_control = Some(value));
}
pub fn sanitize_cache_for_session(
cache: Option<String>,
sets_session_cookie: bool,
) -> Option<String> {
if !sets_session_cookie {
return cache;
}
if cache.as_deref().is_some_and(|c| {
let lower = c.to_ascii_lowercase();
lower.contains("public") || lower.contains("max-age")
}) {
tracing::warn!(
"overriding Cache-Control to private, no-store — page sets CSRF cookie"
);
}
Some("private, no-store".to_string())
}
pub fn take_response_cache_control() -> Option<String> {
with_staging(|s| s.cache_control.take())
}
pub fn stage_page_csrf(token: impl Into<String>) {
let token = token.into();
with_staging(|s| s.csrf = token);
}
pub fn page_csrf() -> String {
with_staging(|s| s.csrf.clone())
}
pub fn stage_page_csp_nonce(nonce: impl Into<String>) {
let nonce = nonce.into();
with_staging(|s| s.csp_nonce = nonce);
}
pub fn page_csp_nonce() -> String {
with_staging(|s| s.csp_nonce.clone())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanitize_cache_for_session_overrides_public() {
let out = sanitize_cache_for_session(Some("public, max-age=3600".into()), true);
assert_eq!(out.as_deref(), Some("private, no-store"));
}
#[test]
fn sanitize_cache_for_session_keeps_when_no_cookie() {
let cache = "public, max-age=60".to_string();
let out = sanitize_cache_for_session(Some(cache.clone()), false);
assert_eq!(out, Some(cache));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn page_staging_isolated_per_scoped_task() {
let mut handles = Vec::new();
for i in 0..32u32 {
handles.push(tokio::spawn(async move {
scope_page_staging(async {
let token = format!("csrf-{i:04}");
let nonce = format!("nonce-{i:04}");
stage_page_csrf(token.clone());
stage_page_csp_nonce(nonce.clone());
tokio::task::yield_now().await;
assert_eq!(page_csrf(), token);
assert_eq!(page_csp_nonce(), nonce);
})
.await
}));
}
for h in handles {
h.await.unwrap();
}
}
}