Skip to content

Commit b73afbf

Browse files
committed
prevent handshake from triggering twice when a dev browser is present, and tighten redirect counter detection to reduce false positives
1 parent 453cf86 commit b73afbf

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

.changeset/grumpy-groups-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/backend': patch
3+
---
4+
5+
Fixes an issue where a handshake would trigger more than intended in development.

packages/backend/src/tokens/handshake.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export class HandshakeService {
222222
const newUrl = new URL(this.authenticateContext.clerkUrl);
223223
newUrl.searchParams.delete(constants.QueryParameters.Handshake);
224224
newUrl.searchParams.delete(constants.QueryParameters.HandshakeHelp);
225+
newUrl.searchParams.delete(constants.QueryParameters.DevBrowser);
225226
headers.append(constants.Headers.Location, newUrl.toString());
226227
headers.set(constants.Headers.CacheControl, 'no-store');
227228
}
@@ -323,7 +324,7 @@ ${developmentError.getFullMessage()}`,
323324

324325
const newCounterValue = this.authenticateContext.handshakeRedirectLoopCounter + 1;
325326
const cookieName = constants.Cookies.RedirectCount;
326-
headers.append('Set-Cookie', `${cookieName}=${newCounterValue}; SameSite=Lax; HttpOnly; Max-Age=3`);
327+
headers.append('Set-Cookie', `${cookieName}=${newCounterValue}; SameSite=Lax; HttpOnly; Max-Age=2`);
327328
return false;
328329
}
329330

packages/backend/src/tokens/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export const authenticateRequest: AuthenticateRequest = (async (
386386
if (!mustActivate) {
387387
return null;
388388
}
389-
if (authenticateContext.handshakeRedirectLoopCounter > 0) {
389+
if (authenticateContext.handshakeRedirectLoopCounter >= 3) {
390390
// We have an organization that needs to be activated, but this isn't our first time redirecting.
391391
// This is because we attempted to activate the organization previously, but the organization
392392
// must not have been valid (either not found, or not valid for this user), and gave us back

0 commit comments

Comments
 (0)