Plan: SameSite=Strict Session Cookies

Status

Step Description Status

1

Add /auth/landing intermediate redirect endpoint

Done (2026-04-09) — GET /auth/landing handler in auth.rs

2

Update /auth/callback to redirect to /auth/landing instead of final destination

Done (2026-04-09) — double-redirect pattern: callback → landing → destination

3

Change SessionManagerLayer from SameSite::Lax to SameSite::Strict

Done (2026-04-09) — main.rs line 78: with_same_site(SameSite::Strict)

4

Update /auth/landing to read session and redirect to stored return_to or /

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:

  1. Keycloak redirects to /auth/callback?code=…​&state=…​ (cross-site — cookie NOT sent by browser)

  2. /auth/callback exchanges the code for tokens, stores session data, and responds with a same-site redirect to /auth/landing

  3. Browser follows the redirect to /auth/landing (same-site — cookie IS sent)

  4. /auth/landing reads the session, extracts return_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:

  1. Verify OAuth state parameter (CSRF protection)

  2. Retrieve PKCE verifier from session

  3. Exchange authorization code for tokens via internal Keycloak URL

  4. Validate JWT signature via JWKS

  5. Extract worker role from realm_access.roles via WorkerRole::from_keycloak_roles()

  6. Store SessionData (worker_id, worker_name, email, role, access_token) via store_session()

  7. Read return_to URL from session

  8. Clean up OIDC flow state (PKCE verifier, state, return_to)

  9. Validate return_to with is_safe_redirect() (open-redirect protection)

  10. Redirect to return_to or /

Scope

In scope:

  • New /auth/landing endpoint in services/canopy-web/src/auth.rs

  • Modified callback to redirect to /auth/landing after session creation

  • Session cookie upgrade from SameSite::Lax to SameSite::Strict

  • Preservation of return_to URL 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::Strict with 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:

  1. Reads return_to from the session (now accessible because this is a same-site request)

  2. Removes return_to from the session

  3. Validates with is_safe_redirect()

  4. 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)
}

In services/canopy-web/src/main.rs, change line 78:

// Before:
.with_same_site(tower_sessions::cookie::SameSite::Lax)

// After:
.with_same_site(tower_sessions::cookie::SameSite::Strict)

Route Registration

In services/canopy-web/src/main.rs, add the landing route alongside the other auth routes (unauthenticated):

.route("/login", get(auth::login))
.route("/auth/callback", get(auth::callback))
.route("/auth/landing", get(auth::landing))
.route("/logout", get(auth::logout))

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_KEY from 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_to reading and SESSION_RETURN_TO_KEY cleanup at the end

  • Remove the is_safe_redirect check (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_KEY and SESSION_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 has return_to=/cases, landing redirects there

  • landing_falls_back_to_root — when no return_to in session, redirects to /

  • landing_rejects_unsafe_redirects — stored return_to=//evil.com results in redirect to /

  • landing_clears_return_to_from_session — after landing, return_to key 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

services/canopy-web/src/auth.rs

Add pub async fn landing() handler; modify callback() to redirect to /auth/landing instead of final destination; move return_to cleanup to landing()

services/canopy-web/src/main.rs

Change SameSite::Lax to SameSite::Strict; add .route("/auth/landing", get(auth::landing))

tests/e2e/worker-login.spec.ts (or equivalent)

E2E test for double-redirect login flow

Verification

  1. cargo nextest run --workspace --lib — unit tests pass

  2. cargo xtask dev reload — canopy-web restarts with new config

  3. cargo nextest run --workspace — integration tests pass

  4. 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 → /)

  5. Verify Set-Cookie header has SameSite=Strict (browser DevTools Application tab)

  6. Verify that direct navigation to /auth/landing without a session redirects to /login

  7. 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

Edit this page · default