Plan: SameSite=Strict Session Cookies
Status
| Step | Description | Status |
|---|---|---|
1 |
Add |
Done (2026-04-09) — GET /auth/landing handler in auth.rs |
2 |
Update |
Done (2026-04-09) — double-redirect pattern: callback → landing → destination |
3 |
Change |
Done (2026-04-09) — main.rs line 78: with_same_site(SameSite::Strict) |
4 |
Update |
Done (2026-04-09) — landing reads next query param and redirects |
5 |
Tests |
Done (2026-04-09) — auth.spec.ts tests login/callback/landing flow, session_test.rs tests cookie settings |
Epic: TBD
Issues: #257
Branch: fix/samesite-strict
Context
canopy-web is the worker portal BFF. It uses Keycloak OIDC for authentication with PKCE (S256). The current session cookie configuration in services/canopy-web/src/main.rs (line 78) uses SameSite::Lax:
let session_layer = SessionManagerLayer::new(session_store)
.with_secure(/* ... */)
.with_same_site(tower_sessions::cookie::SameSite::Lax)
.with_http_only(true)
.with_expiry(/* ... */);
SameSite=Lax is required today because the Keycloak OIDC callback (GET /auth/callback) is a cross-site redirect from Keycloak. When the browser follows the redirect from keycloak.example.com back to canopy-web.example.com/auth/callback, a SameSite=Strict cookie would not be sent on that initial request — meaning the session created during the callback would be invisible to the next page load.
However, SameSite=Strict provides stronger CSRF protection than SameSite=Lax. While canopy-web already has explicit CSRF middleware (services/canopy-web/src/csrf.rs) on POST/PUT/PATCH/DELETE methods, SameSite=Strict adds defense-in-depth by ensuring the session cookie is never sent on any cross-site navigation.
The coding conventions note: "canopy-web uses SameSite=Lax (required for Keycloak OIDC redirect); canopy-portal uses SameSite=Strict." This plan upgrades canopy-web to match canopy-portal’s stricter posture.
The Double-Redirect Pattern
The standard solution for OIDC + SameSite=Strict is a double-redirect:
-
Keycloak redirects to
/auth/callback?code=…&state=…(cross-site — cookie NOT sent by browser) -
/auth/callbackexchanges the code for tokens, stores session data, and responds with a same-site redirect to/auth/landing -
Browser follows the redirect to
/auth/landing(same-site — cookie IS sent) -
/auth/landingreads the session, extractsreturn_to, and redirects to the final destination
The key insight: step 2’s redirect response sets the session cookie (via Set-Cookie header). The browser stores it. In step 3, the browser makes a same-site request to /auth/landing, so the SameSite=Strict cookie is now sent.
Current Auth Flow
The current callback handler in services/canopy-web/src/auth.rs (pub async fn callback(…)) does:
-
Verify OAuth state parameter (CSRF protection)
-
Retrieve PKCE verifier from session
-
Exchange authorization code for tokens via internal Keycloak URL
-
Validate JWT signature via JWKS
-
Extract worker role from
realm_access.rolesviaWorkerRole::from_keycloak_roles() -
Store
SessionData(worker_id, worker_name, email, role, access_token) viastore_session() -
Read
return_toURL from session -
Clean up OIDC flow state (PKCE verifier, state, return_to)
-
Validate
return_towithis_safe_redirect()(open-redirect protection) -
Redirect to
return_toor/
Scope
In scope:
-
New
/auth/landingendpoint inservices/canopy-web/src/auth.rs -
Modified callback to redirect to
/auth/landingafter session creation -
Session cookie upgrade from
SameSite::LaxtoSameSite::Strict -
Preservation of
return_toURL through the double-redirect -
Unit tests for the landing handler
-
E2E test verifying login flow still works
Out of scope:
-
canopy-portal (already uses
SameSite::Strictwith no OIDC flow) -
Keycloak configuration changes (none required)
-
Modifying the PKCE or OAuth state flow (unchanged)
Design
Modified Callback Flow
The callback handler is split into two phases:
Phase 1 (/auth/callback): Everything up to and including store_session() stays the same. After storing the session, instead of reading return_to and redirecting to the final destination, it redirects to /auth/landing:
// After store_session() succeeds:
// DO NOT clean up return_to yet -- landing handler needs it.
session.remove::<String>(SESSION_PKCE_VERIFIER_KEY).await.ok();
session.remove::<String>(SESSION_STATE_KEY).await.ok();
// return_to stays in session for /auth/landing to consume.
Redirect::to("/auth/landing").into_response()
Phase 2 (/auth/landing): A new handler that:
-
Reads
return_tofrom the session (now accessible because this is a same-site request) -
Removes
return_tofrom the session -
Validates with
is_safe_redirect() -
Redirects to the final destination (or
/as fallback)
/// GET /auth/landing -- second hop of the double-redirect pattern.
///
/// After Keycloak OIDC callback stores the session, the browser is
/// redirected here (same-site) so the SameSite=Strict cookie is sent.
/// This handler reads the stored return_to URL and redirects to it.
pub async fn landing(session: Session) -> impl IntoResponse {
let return_to: Option<String> = session.get(SESSION_RETURN_TO_KEY).await.unwrap_or(None);
session.remove::<String>(SESSION_RETURN_TO_KEY).await.ok();
let redirect_to = return_to
.filter(|u| is_safe_redirect(u))
.unwrap_or_else(|| "/".into());
Redirect::to(&redirect_to)
}
Steps
Step 1: Add /auth/landing Handler
Files: services/canopy-web/src/auth.rs
Add pub async fn landing(session: Session) → impl IntoResponse handler. The handler:
-
Reads
SESSION_RETURN_TO_KEYfrom session -
Removes it from session (one-time use)
-
Validates with
is_safe_redirect() -
Redirects to the URL or falls back to
/
If no session exists (e.g., direct navigation to /auth/landing), redirect to /login.
Step 2: Modify Callback to Redirect to Landing
Files: services/canopy-web/src/auth.rs
In pub async fn callback(…):
-
Remove the
return_toreading andSESSION_RETURN_TO_KEYcleanup at the end -
Remove the
is_safe_redirectcheck (moved to landing) -
Remove the final redirect-to-return_to logic
-
Replace with:
Redirect::to("/auth/landing").into_response() -
Keep the cleanup of
SESSION_PKCE_VERIFIER_KEYandSESSION_STATE_KEY
Step 3: Register Landing Route
Files: services/canopy-web/src/main.rs
Add .route("/auth/landing", get(auth::landing)) to the router, in the unauthenticated auth routes section (after /auth/callback, before /logout).
Step 4: Upgrade to SameSite=Strict
Files: services/canopy-web/src/main.rs
Change SameSite::Lax to SameSite::Strict on the SessionManagerLayer.
Step 5: Tests
Files: services/canopy-web/src/auth.rs (#[cfg(test)] module)
Unit tests:
-
landing_redirects_to_stored_return_to— verify that when session hasreturn_to=/cases, landing redirects there -
landing_falls_back_to_root— when noreturn_toin session, redirects to/ -
landing_rejects_unsafe_redirects— storedreturn_to=//evil.comresults in redirect to/ -
landing_clears_return_to_from_session— after landing,return_tokey is removed from session
E2E test (Playwright):
-
worker_login_flow_with_strict_cookies— full login flow: navigate to protected page, redirect to login, authenticate with Keycloak, verify arrival at protected page
Files Touched
| File | Change |
|---|---|
|
Add |
|
Change |
|
E2E test for double-redirect login flow |
Verification
-
cargo nextest run --workspace --lib— unit tests pass -
cargo xtask dev reload— canopy-web restarts with new config -
cargo nextest run --workspace— integration tests pass -
Manual test: open browser, navigate to canopy-web, verify login flow works with the double redirect (check browser DevTools Network tab for the redirect chain: Keycloak → /auth/callback → /auth/landing → /)
-
Verify
Set-Cookieheader hasSameSite=Strict(browser DevTools Application tab) -
Verify that direct navigation to
/auth/landingwithout a session redirects to/login -
cargo xtask e2e— E2E tests pass
Documentation Updates
-
.claude/docs/coding-conventions.md— update "BFF sessions" note: canopy-web now uses SameSite=Strict (remove the parenthetical about Lax being required for OIDC) -
.claude/docs/services.md— update canopy-web session config description -
.claude/docs/security.md— document double-redirect pattern under "Session Management" -
CHANGELOG.adoc— entry under== Unreleased