Skip to content

Commit e6ecbaa

Browse files
authored
fix(backend): Fix unhandled handshake error (clerk#2541)
* fix(backend): Fix unhandled handshake error * fix(backend): Handle development verification failure
1 parent 59f9a72 commit e6ecbaa

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎.changeset/few-rings-push.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+
Fix an error in the handshake flow where the request would throw an unhandled error when verification of the handshake payload fails.

‎integration/tests/handshake.test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,26 +827,27 @@ test.describe('Client handshake @generic', () => {
827827
}),
828828
redirect: 'manual',
829829
});
830-
expect(res.status).toBe(500);
830+
expect(res.status).toBe(200);
831831
});
832832

833-
test('Handshake result - prod - skew - clock ahead', async () => {
833+
test('Handshake result - prod - session token expired and handshake stale', async () => {
834834
const config = generateConfig({
835835
mode: 'live',
836836
});
837+
const { token: currentSessionToken, claims } = config.generateToken({ state: 'expired' });
837838
const { token } = config.generateToken({ state: 'expired' });
838839
const cookiesToSet = [`__session=${token};path=/`, 'foo=bar;path=/;domain=example.com'];
839840
const handshake = await config.generateHandshakeToken(cookiesToSet);
840841
const res = await fetch(app.serverUrl + '/', {
841842
headers: new Headers({
842843
'X-Publishable-Key': config.pk,
843844
'X-Secret-Key': config.sk,
844-
Cookie: `__clerk_handshake=${handshake}`,
845+
Cookie: `__clerk_handshake=${handshake};__session=${currentSessionToken};__client_uat=${claims.iat}`,
845846
'Sec-Fetch-Dest': 'document',
846847
}),
847848
redirect: 'manual',
848849
});
849-
expect(res.status).toBe(500);
850+
expect(res.status).toBe(307);
850851
});
851852

852853
test('Handshake result - prod - mismatched keys', async () => {
@@ -866,6 +867,6 @@ test.describe('Client handshake @generic', () => {
866867
}),
867868
redirect: 'manual',
868869
});
869-
expect(res.status).toBe(500);
870+
expect(res.status).toBe(200);
870871
});
871872
});

‎packages/backend/src/tokens/request.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export async function authenticateRequest(
124124

125125
if (
126126
instanceType === 'development' &&
127-
(error.reason === TokenVerificationErrorReason.TokenExpired ||
128-
error.reason === TokenVerificationErrorReason.TokenNotActiveYet)
127+
(error?.reason === TokenVerificationErrorReason.TokenExpired ||
128+
error?.reason === TokenVerificationErrorReason.TokenNotActiveYet)
129129
) {
130130
error.tokenCarrier = 'cookie';
131131
// This probably means we're dealing with clock skew
@@ -204,7 +204,21 @@ ${error.getFullMessage()}`,
204204
* If we have a handshakeToken, resolve the handshake and attempt to return a definitive signed in or signed out state.
205205
*/
206206
if (authenticateContext.handshakeToken) {
207-
return resolveHandshake();
207+
try {
208+
return await resolveHandshake();
209+
} catch (error) {
210+
// If for some reason the handshake token is invalid or stale, we ignore it and continue trying to authenticate the request.
211+
// Worst case, the handshake will trigger again and return a refreshed token.
212+
if (error instanceof TokenVerificationError && instanceType === 'development') {
213+
if (error.reason === TokenVerificationErrorReason.TokenInvalidSignature) {
214+
throw new Error(
215+
`Clerk: Handshake token verification failed due to an invalid signature. If you have switched Clerk keys locally, clear your cookies and try again.`,
216+
);
217+
}
218+
219+
throw new Error(`Clerk: Handshake token verification failed: ${error.getFullMessage()}.`);
220+
}
221+
}
208222
}
209223

210224
/**

0 commit comments

Comments
 (0)