Uh oh!
There was an error while loading. Please reload this page.
feat(backend): Limit handshake to document requests - #2352
Conversation
🦋 Changeset detectedLatest commit: df9aba8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| }); | ||
| test('cookieToken: returns undefined when crossOriginReferrer in development and is satellite [6n]', async assert => { | ||
| test('cookieToken: returns signedIn when satellite but valid token and clientUat', async assert => { |
There was a problem hiding this comment.
This test name was inaccurate 🧐
| mockRequestWithCookies( | ||
| { | ||
| ...defaultHeaders, | ||
| 'sec-fetch-dest': 'empty', |
There was a problem hiding this comment.
Now that we're defaulting to sec-fetch-dest: document, we need to explicitly set it to empty for this test case
| function isRequestEligibleForHandshake(authenticateContext: { secFetchDest?: string; accept?: string }) { | ||
| 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 result of a user navigation. |
There was a problem hiding this comment.
Also, you can't overwrite sec-fetch-mode, so testing became difficult.
| function handleMaybeHandshakeStatus( | ||
| context: typeof authenticateContext, | ||
| reason: AuthErrorReason, | ||
| message: string, | ||
| headers?: Headers, | ||
| ) { | ||
| if (isRequestEligibleForHandshake(context)) { | ||
| // Right now the only usage of passing in different headers is for multi-domain sync, which redirects somewhere else. | ||
| // In the future if we want to decorate the handshake redirect with additional headers per call we need to tweak this logic. | ||
| return handshake(context, reason, message, headers ?? buildRedirectToHandshake()); | ||
| } | ||
| return signedOut(context, reason, message, new Headers()); | ||
| } |
There was a problem hiding this comment.
This felt like a reasonable abstraction, easy to tweak the handshake heuristic in one place going forward.
Description
Limit the handshake flow to document requests. We identify this by checking for the presence of the
Sec-Fetch-Destheader with the valuedocument. For browsers that don't support this header, we check that theAcceptheader value starts withtext/html, which is a reasonable enough indication that it's a document request.Checklist
npm testruns as expected.npm run buildruns as expected.Type of change
Packages affected
@clerk/backend@clerk/chrome-extension@clerk/clerk-js@clerk/clerk-expo@clerk/fastifygatsby-plugin-clerk@clerk/localizations@clerk/nextjs@clerk/clerk-react@clerk/remix@clerk/clerk-sdk-node@clerk/shared@clerk/themes@clerk/typesbuild/tooling/chore