Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-groups-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fixes an issue where a handshake would trigger more than intended in development.
2 changes: 1 addition & 1 deletion integration/tests/handshake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ test.describe('Client handshake with an organization activation avoids infinite
// Critical cookie: __clerk_redirect_count
headers.set(
'Cookie',
`${devBrowserCookie} __client_uat=${claims.iat}; __session=${token}; __clerk_redirect_count=1`,
`${devBrowserCookie} __client_uat=${claims.iat}; __session=${token}; __clerk_redirect_count=3`,
);

const res = await fetch(thisApp.serverUrl + '/organizations-by-id/org_a', {
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/tokens/handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export class HandshakeService {
const newUrl = new URL(this.authenticateContext.clerkUrl);
newUrl.searchParams.delete(constants.QueryParameters.Handshake);
newUrl.searchParams.delete(constants.QueryParameters.HandshakeHelp);
newUrl.searchParams.delete(constants.QueryParameters.DevBrowser);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Was there any reason we used to delete it from the clerk-js side only or just an omission?

headers.append(constants.Headers.Location, newUrl.toString());
headers.set(constants.Headers.CacheControl, 'no-store');
}
Expand Down Expand Up @@ -323,7 +324,7 @@ ${developmentError.getFullMessage()}`,

const newCounterValue = this.authenticateContext.handshakeRedirectLoopCounter + 1;
const cookieName = constants.Cookies.RedirectCount;
headers.append('Set-Cookie', `${cookieName}=${newCounterValue}; SameSite=Lax; HttpOnly; Max-Age=3`);
headers.append('Set-Cookie', `${cookieName}=${newCounterValue}; SameSite=Lax; HttpOnly; Max-Age=2`);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 was an arbitrary choice here, I'm reducing to 2 to mitigate the case where someone switches between orgs quickly when using org url sync

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/tokens/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export const authenticateRequest: AuthenticateRequest = (async (
if (!mustActivate) {
return null;
}
if (authenticateContext.handshakeRedirectLoopCounter > 0) {
if (authenticateContext.handshakeRedirectLoopCounter >= 3) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, reducing the case where someone might be switching quickly. this should help, in addition to the max-age change, reduce false-positives of redirect loop detection

// We have an organization that needs to be activated, but this isn't our first time redirecting.
// This is because we attempted to activate the organization previously, but the organization
// must not have been valid (either not found, or not valid for this user), and gave us back
Expand Down