Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
π Fix API keys with underscores in prefix being rejected (#2605)
Luke Vella800ae93ced5c
diff --git a/apps/web/src/features/api-keys/utils.test.ts b/apps/web/src/features/api-keys/utils.test.tsindex b2e5823e2..132d42561 100644--- a/apps/web/src/features/api-keys/utils.test.ts+++ b/apps/web/src/features/api-keys/utils.test.ts@@ -95,8 +95,13 @@ describe("isLegacyApiKeyHash", () => {describe("extractApiKeyPrefix", () => {it("should extract prefix from valid API key", () => {- const apiKey = "sk_abc123_def456";- expect(extractApiKeyPrefix(apiKey)).toBe("abc123");+ const apiKey = "sk_abc12345_def456ghi789jkl012mno345pqr678";+ expect(extractApiKeyPrefix(apiKey)).toBe("abc12345");+ });++ it("should extract prefix containing underscores", () => {+ const apiKey = "sk_a_b-c_d5_def456ghi789jkl012mno345pqr678";+ expect(extractApiKeyPrefix(apiKey)).toBe("a_b-c_d5");});it("should handle malformed keys with fallback", () => {@@ -104,9 +109,11 @@ describe("extractApiKeyPrefix", () => {expect(extractApiKeyPrefix(malformed)).toBe(malformed.slice(0, 12));});- it("should handle keys with extra underscores", () => {- const apiKey = "sk_prefix_secret_extra";- expect(extractApiKeyPrefix(apiKey)).toBe("prefix");+ it("should extract the stored prefix from generated keys", async () => {+ for (let i = 0; i < 50; i++) {+ const result = await createApiKey();+ expect(extractApiKeyPrefix(result.apiKey)).toBe(result.prefix);+ }});});diff --git a/apps/web/src/features/api-keys/utils.ts b/apps/web/src/features/api-keys/utils.tsindex 13365cb67..02f910c47 100644--- a/apps/web/src/features/api-keys/utils.ts+++ b/apps/web/src/features/api-keys/utils.ts@@ -29,6 +29,10 @@ export const hashApiKey = (rawKey: string): string => {return `sha256$${salt}$${hash}`;};+const API_KEY_SCHEME = "sk_";+// randomToken(6) β 8 base64url characters+const API_KEY_PREFIX_LENGTH = 8;+/*** Create an API key with the format: sk_{prefix}_{secret}* @returns Object containing the full API key and components for storage@@ -40,7 +44,7 @@ export const createApiKey = async (): Promise<{}> => {const prefix = randomToken(6);const secret = randomToken(24);- const apiKey = `sk_${prefix}_${secret}`;+ const apiKey = `${API_KEY_SCHEME}${prefix}_${secret}`;return {apiKey,@@ -50,14 +54,19 @@ export const createApiKey = async (): Promise<{};/**- * Extract the prefix from a raw API key+ * Extract the prefix from a raw API key.+ *+ * The prefix and secret are base64url, whose alphabet includes `_`, so the+ * key cannot be split on underscores β the prefix is extracted by position.* @param rawKey Raw API key in format sk_{prefix}_{secret}* @returns The prefix portion*/export const extractApiKeyPrefix = (rawKey: string): string => {- const parts = rawKey.split("_").filter(Boolean);- if (parts.length >= 3 && parts[1]) {- return parts[1];+ if (rawKey.startsWith(API_KEY_SCHEME)) {+ return rawKey.slice(+ API_KEY_SCHEME.length,+ API_KEY_SCHEME.length + API_KEY_PREFIX_LENGTH,+ );}// Fallback for malformed keysreturn rawKey.slice(0, 12);