diff --git a/.changeset/clever-crabs-smell.md b/.changeset/clever-crabs-smell.md new file mode 100644 index 00000000000..02cb4f3948b --- /dev/null +++ b/.changeset/clever-crabs-smell.md @@ -0,0 +1,5 @@ +--- +'@clerk/shared': patch +--- + +Catching ATOB errors in isPublishableKey diff --git a/packages/shared/src/keys.ts b/packages/shared/src/keys.ts index f8f21489c07..9377b8b7cdb 100644 --- a/packages/shared/src/keys.ts +++ b/packages/shared/src/keys.ts @@ -69,14 +69,22 @@ export function parsePublishableKey( }; } -export function isPublishableKey(key: string) { - key = key || ''; - - const hasValidPrefix = key.startsWith(PUBLISHABLE_KEY_LIVE_PREFIX) || key.startsWith(PUBLISHABLE_KEY_TEST_PREFIX); - - const hasValidFrontendApiPostfix = isomorphicAtob(key.split('_')[2] || '').endsWith('$'); - - return hasValidPrefix && hasValidFrontendApiPostfix; +/** + * Checks if the provided key is a valid publishable key. + * + * @param key - The key to be checked. Defaults to an empty string if not provided. + * @returns `true` if 'key' is a valid publishable key, `false` otherwise. + */ +export function isPublishableKey(key: string = '') { + try { + const hasValidPrefix = key.startsWith(PUBLISHABLE_KEY_LIVE_PREFIX) || key.startsWith(PUBLISHABLE_KEY_TEST_PREFIX); + + const hasValidFrontendApiPostfix = isomorphicAtob(key.split('_')[2] || '').endsWith('$'); + + return hasValidPrefix && hasValidFrontendApiPostfix; + } catch { + return false; + } } export function createDevOrStagingUrlCache() {