Skip to content

IdP errors with SessionNotFound on stale session cookies instead of expiring + restarting the interaction #526

Open

Description

@melvincarvalho

Symptom

After re-provisioning a pod (or otherwise rotating cookie-signing keys), a browser that still holds an old _session / _interaction cookie for the IdP origin fails sign-in at the consent / "Allow access" step. provider.interactionFinished(...) throws and the request 400s:

SessionNotFound: invalid_request — "session not found"
  at #getInteraction (oidc-provider/lib/provider.js)
  at Provider.interactionResult / interactionFinished
"Promise errored, but reply.sent = true was set"

Login + the interaction pages all succeed; only the final confirm (interactionFinished) dies. The user is stuck until they manually clear site data for the IdP origin. Incognito works (no stale cookie), which pinpoints it.

Repro

  1. Sign in to a pod, leave the browser cookies in place.
  2. Re-provision / reinstall so the persisted cookie keys change (or the stored session/interaction is gone).
  3. Sign in again from the same (non-incognito) browser → reaches consent → "Allow access" → SessionNotFound.

(Surfaced repeatedly during the Android app dev cycle — js-pod/android — across reinstalls; a fresh user wouldn't hit it, but a reinstall does.)

Why it matters

A stale cookie shouldn't be a dead end. The user has no in-app way to recover; they have to know to clear site data, which a normal user won't.

Existing precedent (the fix is small)

JSS already handles the analogous deleted-account case and already has the helpers:

  • src/idp/provider.js:268,407,420 — guard that, when a session/grant is missing due to stale cookies, calls expireSessionCookiesKoa(ctx) and redirects back to retry.
  • src/idp/cookies.js — expireSessionCookies(reply, request) and expireSessionCookiesKoa(ctx).
  • src/idp/interactions.js:221-224 — already wraps one interactionFinished in try/catch with a fallback ("interactionFinished failed, using fallback").

Proposed fix

In the interaction handlers that call provider.interactionFinished(...) (src/idp/interactions.js — the /confirm path via src/idp/index.js:408, plus the other call sites at lines ~166, 289, 606, 644, 823), catch SessionNotFound (err.name === 'SessionNotFound') and, instead of erroring, expire the session/interaction cookies and 303 back to the authorization URL to restart the interaction cleanly — mirroring the existing deleted-account guard. Net effect: a stale cookie self-heals into a fresh login instead of a 400.

Activity

  1. added
    bug
    on May 24, 2026
  2. melvincarvalho commented on Jun 8, 2026

    @melvincarvalho
    ContributorAuthor

    Partial overlap with #412 / PR #539

    PR #539 (closes #412) lands a partial of the fix proposed here — scoped to handleSchnorrComplete only, and using the simpler Approach C (return a 4xx error page) rather than the auto-healing 303-redirect-and-restart shape this issue proposes.

    What #539 does What #526 still needs
    handleSchnorrComplete (line 823) ✅ wrap in try/catch, write 400 error page directly to reply.raw on SessionNotFound Upgrade the error page → expireSessionCookies + 303 to the authorization URL
    handleLogin browser branch (165) ❌ untouched Same
    handleConsent (289) ❌ untouched — and this is the path the issue describes ("Allow access" step) Same
    handlePasskeyComplete (606) ❌ untouched Same
    handlePasskeySkip (644) ❌ untouched Same

    So once #539 merges:

    • The 504 hang on schnorr-complete is gone (turns into a 400 with a "restart login" page).
    • The other four sites still hang on missing-cookie.
    • None of them yet self-heal via the cookie-expire + redirect pattern this issue calls out.

    This issue stays load-bearing for the remaining work. The mechanical shape is the same across all five — could be a single helper, e.g. await finishOrExpireAndRetry(reply, request, provider, returnTo, ...), with the four sites consolidating onto it.

    (Almost filed a separate "sweep" issue when working on #412; the dupe search caught this one — leaving the cross-reference here so the relationship is documented.)

  3. melvincarvalho commented on Jun 8, 2026

    @melvincarvalho
    ContributorAuthor

    Update: foundation landed across all 5 sites in #540

    PR #540 (squash-merged as 9051737) extracted the defensive recovery pattern into a single helper finishInteractionDefensively and replaced all five reply.hijack(); return provider.interactionFinished(...) sites in src/idp/interactions.js with calls to it.

    So the previous status comment is now stale — every interaction handler is now hang-proof on SessionNotFound:

    Handler Status
    handleSchnorrComplete ✅ helper-backed (since #539, refactored in #540)
    handleLogin (browser branch) ✅ helper-backed
    handleConsent (the "Allow access" path this issue originally describes) ✅ helper-backed
    handlePasskeyComplete ✅ helper-backed
    handlePasskeySkip ✅ helper-backed

    What this issue still owns

    The UX upgrade from "show a 400 error page" → "self-heal via expireSessionCookies + 303 to the authorization URL". The helper is now the single place to add that logic — once this issue gets the work, finishInteractionDefensively grows an extra branch (or the SessionNotFound branch swaps errorPage for an expireSessionCookies(reply, request); reply.raw.writeHead(303, { Location: <authUrl> })), and all five handlers inherit it for free.

    Sketch for whoever picks this up

    // inside finishInteractionDefensively's catch, replace the 400 branch:
    if (isSessionMissing) {
      // mirror the deleted-account guard at src/idp/provider.js:268,407,420
      expireSessionCookies(reply, request);
      // The authorization URL is reachable via interaction.returnTo —
      // but the helper doesn't currently receive the interaction object.
      // Either pass it as a new arg, or accept a `retryUrl` arg from each caller.
      reply.raw.writeHead(303, { Location: retryUrl });
      reply.raw.end();
      return;
    }

    Adding help wanted + priority: low to signal this is a well-scoped contributor task rather than an urgent fix — after #540 the user sees a clear error page instead of a hang, so the remaining work is UX polish.

  4. added
    help wanted
    priority: low
    on Jun 8, 2026
  5. added a commit that references this issue on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wantedpriority: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions