Skip to content

Commit 2411b15

Browse files
committed
Basic token validation.
1 parent f2196c5 commit 2411b15

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tools/diagnostics-app/src/library/powersync/TokenConnector.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class TokenConnector implements PowerSyncBackendConnector {
2323

2424
async signIn(credentials: Credentials) {
2525
validateSecureContext(credentials.endpoint);
26+
checkJWT(credentials.token);
2627
try {
2728
localStorage.setItem('powersync_credentials', JSON.stringify(credentials));
2829
await connect();
@@ -56,3 +57,21 @@ function validateSecureContext(url: string) {
5657
Run either the PowerSync endpoint on http://localhost, or the diagnostics app on http://localhost.`);
5758
}
5859
}
60+
61+
function checkJWT(token: string) {
62+
// Split the token into parts by "."
63+
const parts = token.split('.');
64+
65+
// Check that it has exactly three parts (header, payload, signature)
66+
if (parts.length !== 3) {
67+
throw new Error(`Token must be a JWT: Expected 3 parts, got ${parts.length}`);
68+
}
69+
70+
// Check that each part is base64 or base64url encoded
71+
const base64UrlRegex = /^[A-Za-z0-9-_]+$/;
72+
73+
const isBase64 = parts.every((part) => base64UrlRegex.test(part));
74+
if (!isBase64) {
75+
throw new Error(`Token must be a JWT: Not all parts are base64 encoded`);
76+
}
77+
}

0 commit comments

Comments
 (0)