Skip to content

handleSchnorrComplete 504s when the _interaction cookie is missing #412

Closed

Description

@melvincarvalho

Symptom

GET /idp/interaction/<uid>/schnorr-complete?accountId=<id> hangs and the gateway times out with 504. JSS log shows:

```
Schnorr login completed (accountId=b0b1707f-... uid=_T_qlTgrG3zS1...)
Promise errored, but reply.sent = true was set
err: SessionNotFound: invalid_request — interaction session id cookie not found
at #getInteraction (oidc-provider/lib/provider.js:405)
at Provider.interactionResult (provider.js:199)
at Provider.interactionFinished (provider.js:217)
at handleSchnorrComplete (src/idp/interactions.js:833)
```

The connection hangs because the handler called `reply.hijack()` before `provider.interactionFinished(...)`, so when `interactionFinished` throws, Fastify can't send the error and the socket sits open until the gateway times it out.

Reproduction

Anything that breaks `_interaction` cookie continuity between the OIDC `/auth` step and `/schnorr-complete`:

  • Start the OIDC flow in browser A, copy-paste the interaction URL into browser B (today's repro)
  • Third-party cookie blocking on a cross-site IdP/RP boundary
  • Incognito → main session
  • The same flow after a long idle (cookie expired but interaction record still findable)

Cause

`handleSchnorrComplete` (`src/idp/interactions.js:805`) does the right thing structurally — looks up the interaction by `uid`, validates the accountId — and then calls `provider.interactionFinished(request.raw, reply.raw, interaction.result, ...)`. `interactionFinished` internally re-reads the `_interaction` cookie via `#getInteraction` and explodes when it's missing, even though we already proved we have the right interaction.

Two compounding issues:

  1. Unnecessary cookie dependency. We have the interaction object in hand. The cookie check is redundant for this code path.
  2. Hijack-then-throw silently hangs. `reply.hijack()` is called BEFORE `interactionFinished`. If `interactionFinished` throws, Fastify's `Promise errored, but reply.sent = true was set` warning fires and nobody writes a response. Connection hangs → 504.

Fix sketches

A. Move the OIDC finalize into `handleSchnorrLogin`. The login handler already saves `interaction.result` and has the `_interaction` cookie in flight (it's still the same browser, same request). After saving, it can call `provider.interactionFinished(request.raw, reply.raw, result, {...})` and return its redirect target as `redirectUrl` in the JSON. Drop `schnorr-complete` entirely. This is the cleanest fix because it removes the cookie-dependency window.

B. Keep `schnorr-complete` but bypass `interactionFinished`. We have the saved `interaction.result` and can directly construct the OIDC continue URL (`interaction.returnTo`) ourselves. Redirect the browser there and oidc-provider's auth endpoint picks up the saved result without needing the cookie.

C. Defensive: don't `reply.hijack()` until AFTER `interactionFinished` resolves. At minimum this turns a 504 hang into a clean 4xx so users get a real error.

A or B fix the actual flow. C should land regardless.

Refs

Activity

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

    @melvincarvalho
    ContributorAuthor

    Same anti-pattern in 4 other handlers in src/idp/interactions.js

    While preparing #539 (Approach C for this issue, scoped to handleSchnorrComplete), I confirmed the same reply.hijack() → provider.interactionFinished(...) → outer-catch-can't-respond shape exists at four other sites. All four have identical risk: if interactionFinished throws (most commonly SessionNotFound from a missing _interaction cookie), the outer catch { reply.code(500).type('text/html').send(...) } is a no-op against a hijacked reply, and the connection hangs to a gateway 504 — exactly the symptom this issue describes.

    Handler Lines Trigger Cookie-missing scenarios
    handleLogin (browser branch) 165-166 POST /idp/interaction/<uid>/login from a browser form Same as schnorr: cross-browser paste, 3rd-party cookies blocked, idle past TTL
    handleConsent 286-289 POST /idp/interaction/<uid>/confirm Same
    handlePasskeyComplete 605-606 POST /idp/interaction/<uid>/passkey-complete after WebAuthn ceremony Same
    handlePasskeySkip 643-644 POST /idp/interaction/<uid>/passkey-skip Same

    All four have the same outer try { … reply.hijack(); return provider.interactionFinished(...) } catch (err) { return reply.code(500)…send(errorPage(...)) } shape — the outer catch is structurally unable to recover after hijack.

    #539 deliberately stays narrow (issue scope = schnorr only). If a broader sweep is wanted, the fix is mechanical: each of the four gets the same try { await interactionFinished(...) } catch (err) { if (reply.raw.headersSent) throw err; reply.raw.writeHead(...); reply.raw.end(errorPage(...)) } wrapper. A small helper — e.g. await finishInteractionOrRecover(reply, request, provider, request.raw, reply.raw, result, opts) — would let all five sites collapse onto one call site and eliminate the anti-pattern entirely.

    Happy to file that as a follow-up issue ("sweep hijack-then-throw across all 5 interaction handlers"), or fold it into a separate PR if the scope feels right here.

  3. melvincarvalho commented on Jun 8, 2026

    @melvincarvalho
    ContributorAuthor

    Cross-reference: #526 is already the broader sweep

    Followed up on my "happy to file that as a follow-up issue" line in the previous comment — turns out #526 already covers the broader sweep. It explicitly lists the same five interactionFinished call sites (lines ~166, 289, 606, 644, 823) and proposes a stronger fix than what #539 lands here:

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

    So the layering is:

    Scope Behaviour on missing/stale cookie
    This issue + #539 handleSchnorrComplete only 504 hang → 400 with "session expired, restart login" page
    #526 All 5 interaction handlers Stale cookie → expire + 303 redirect to retry (self-heals into a fresh login)

    Not duplicates — layered. #539 is the safety net (no more 504 anywhere); #526 is the UX win (no user-visible error at all). Once #539 merges and closes this issue, the remaining work lives in #526.

  4. added a commit that references this issue on Jun 8, 2026
    90bf636
  5. added a commit that references this issue on Jun 10, 2026
    5863768
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions