Skip to content

Commit 9036427

Browse files
authored
fix(backend): Prevent excessive handshakes (#6635)
1 parent 5628b0d commit 9036427

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
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.

integration/tests/handshake.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ test.describe('Client handshake with an organization activation avoids infinite
14371437
// Critical cookie: __clerk_redirect_count
14381438
headers.set(
14391439
'Cookie',
1440-
`${devBrowserCookie} __client_uat=${claims.iat}; __session=${token}; __clerk_redirect_count=1`,
1440+
`${devBrowserCookie} __client_uat=${claims.iat}; __session=${token}; __clerk_redirect_count=3`,
14411441
);
14421442

14431443
const res = await fetch(thisApp.serverUrl + '/organizations-by-id/org_a', {

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)