feat(backend): Limit handshake to document requests#2352
Merged
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 |
brkalow
commented
Dec 15, 2023
| }); | ||
|
|
||
| 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 => { |
Member
Author
There was a problem hiding this comment.
This test name was inaccurate 🧐
| mockRequestWithCookies( | ||
| { | ||
| ...defaultHeaders, | ||
| 'sec-fetch-dest': 'empty', |
Member
Author
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. |
Member
Author
There was a problem hiding this comment.
Also, you can't overwrite sec-fetch-mode, so testing became difficult.
Comment on lines
+181
to
+193
| 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()); | ||
| } |
Member
Author
There was a problem hiding this comment.
This felt like a reasonable abstraction, easy to tweak the handshake heuristic in one place going forward.
SokratisVidros
approved these changes
Dec 15, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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