Skip to content

Commit

Permalink
fix(nextjs,backend): Support handshake in iframes (#3555)
Browse files Browse the repository at this point in the history
This change makes requests that have the `Sec-Fetch-Dest` value set to `iframe` to be eligible for handshake.

Co-authored-by: Bryce Kalow <[email protected]>
  • Loading branch information
anagstef and BRKalow committed Jul 11, 2024
1 parent 5291515 commit 5642b26
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/poor-knives-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/backend': patch
'@clerk/nextjs': patch
---

Fixes a bug where Clerk's Handshake mechanism would not run when an application is rendered in an iframe.
3 changes: 2 additions & 1 deletion packages/backend/src/tokens/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function isRequestEligibleForHandshake(authenticateContext: { secFetchDest?: str
const { accept, secFetchDest } = authenticateContext;

// NOTE: we could also check sec-fetch-mode === navigate here, but according to the spec, sec-fetch-dest: document should indicate that the request is the data of a user navigation.
if (secFetchDest === 'document') {
// Also, we check for 'iframe' because it's the value set when a doc request is made by an iframe.
if (secFetchDest === 'document' || secFetchDest === 'iframe') {
return true;
}

Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/server/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const isServerActionRequest = (req: Request) => {
const isPageRequest = (req: Request): boolean => {
return (
req.headers.get(constants.Headers.SecFetchDest) === 'document' ||
req.headers.get(constants.Headers.SecFetchDest) === 'iframe' ||
req.headers.get(constants.Headers.Accept)?.includes('text/html') ||
isAppRouterInternalNavigation(req) ||
isPagesRouterInternalNavigation(req)
Expand Down

0 comments on commit 5642b26

Please sign in to comment.