The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Fix: updated expiry to be 60 seconds (#40659)
Ali Waseem88c5e7cbd46c
diff --git a/apps/studio/data/api-keys/temp-api-keys-utils.test.ts b/apps/studio/data/api-keys/temp-api-keys-utils.test.tsindex 79807f4ef1a..3339d0755d4 100644--- a/apps/studio/data/api-keys/temp-api-keys-utils.test.ts+++ b/apps/studio/data/api-keys/temp-api-keys-utils.test.ts@@ -32,7 +32,7 @@ describe('createTemporaryUploadKey', () => {const result = createTemporaryApiKey(apiKey, expiryInSeconds)- expect(result.expiryTime).toBe(now + 3600 * 1000)+ expect(result.expiryTimeMs).toBe(now + 3600 * 1000)})it('should handle short expiry durations', () => {@@ -44,7 +44,7 @@ describe('createTemporaryUploadKey', () => {const result = createTemporaryApiKey(apiKey, expiryInSeconds)- expect(result.expiryTime).toBe(now + 60 * 1000)+ expect(result.expiryTimeMs).toBe(now + 60 * 1000)})it('should create keys with different expiry times when called at different times', () => {@@ -59,9 +59,9 @@ describe('createTemporaryUploadKey', () => {vi.setSystemTime(now2)const result2 = createTemporaryApiKey(apiKey, expiryInSeconds)- expect(result1.expiryTime).toBe(now1 + expiryInSeconds * 1000)- expect(result2.expiryTime).toBe(now2 + expiryInSeconds * 1000)- expect(result1.expiryTime).not.toBe(result2.expiryTime)+ expect(result1.expiryTimeMs).toBe(now1 + expiryInSeconds * 1000)+ expect(result2.expiryTimeMs).toBe(now2 + expiryInSeconds * 1000)+ expect(result1.expiryTimeMs).not.toBe(result2.expiryTimeMs)})})@@ -86,13 +86,13 @@ describe('isTemporaryUploadKeyValid', () => {expect(result).toBe(false)})- it('should return true for a key with more than 60 seconds remaining', () => {+ it('should return true for a key with more than 20 seconds remaining', () => {const now = Date.now()vi.setSystemTime(now)const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now + 120000, // 2 minutes from now+ expiryTimeMs: now + 120000, // 2 minutes from now}const result = isTemporaryApiKeyValid(key)@@ -100,13 +100,13 @@ describe('isTemporaryUploadKeyValid', () => {expect(result).toBe(true)})- it('should return false for a key with exactly 60 seconds remaining', () => {+ it('should return false for a key with exactly 20 seconds remaining', () => {const now = Date.now()vi.setSystemTime(now)const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now + 60000, // Exactly 60 seconds+ expiryTimeMs: now + 20000, // Exactly 20 seconds}const result = isTemporaryApiKeyValid(key)@@ -114,13 +114,13 @@ describe('isTemporaryUploadKeyValid', () => {expect(result).toBe(false)})- it('should return false for a key with less than 60 seconds remaining', () => {+ it('should return false for a key with less than 20 seconds remaining', () => {const now = Date.now()vi.setSystemTime(now)const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now + 30000, // 30 seconds from now+ expiryTimeMs: now + 10000, // 10 seconds from now}const result = isTemporaryApiKeyValid(key)@@ -134,7 +134,7 @@ describe('isTemporaryUploadKeyValid', () => {const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now - 1000, // 1 second ago+ expiryTimeMs: now - 1000, // 1 second ago}const result = isTemporaryApiKeyValid(key)@@ -148,7 +148,7 @@ describe('isTemporaryUploadKeyValid', () => {const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now - 3600000, // 1 hour ago+ expiryTimeMs: now - 3600000, // 1 hour ago}const result = isTemporaryApiKeyValid(key)@@ -156,13 +156,13 @@ describe('isTemporaryUploadKeyValid', () => {expect(result).toBe(false)})- it('should return true for a key with exactly 61 seconds remaining', () => {+ it('should return true for a key with exactly 21 seconds remaining', () => {const now = Date.now()vi.setSystemTime(now)const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now + 61000, // 61 seconds from now+ expiryTimeMs: now + 21000, // 21 seconds from now}const result = isTemporaryApiKeyValid(key)@@ -176,17 +176,17 @@ describe('isTemporaryUploadKeyValid', () => {const key: TemporaryApiKey = {apiKey: 'test-key',- expiryTime: now + 120000, // 2 minutes from now+ expiryTimeMs: now + 120000, // 2 minutes from now}// Initially validexpect(isTemporaryApiKeyValid(key)).toBe(true)- // Advance time by 59 seconds (should still be valid - 61 seconds remaining)- vi.advanceTimersByTime(59000)+ // Advance time by 99 seconds (should still be valid - 21 seconds remaining)+ vi.advanceTimersByTime(99000)expect(isTemporaryApiKeyValid(key)).toBe(true)- // Advance time by 2 more seconds (should be invalid - 59 seconds remaining)+ // Advance time by 2 more seconds (should be invalid - 19 seconds remaining)vi.advanceTimersByTime(2000)expect(isTemporaryApiKeyValid(key)).toBe(false)})@@ -196,7 +196,7 @@ describe('isTemporaryUploadKeyValid', () => {vi.setSystemTime(now)const key = {- expiryTime: now + 120000,+ expiryTimeMs: now + 120000,} as TemporaryApiKeyconst result = isTemporaryApiKeyValid(key)@@ -238,7 +238,7 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',expect(isTemporaryApiKeyValid(key)).toBe(true)})- it('should create a key that becomes invalid after expiry time minus 60 seconds', () => {+ it('should create a key that becomes invalid after expiry time minus 20 seconds', () => {const now = Date.now()vi.setSystemTime(now)@@ -248,11 +248,11 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',// Initially validexpect(isTemporaryApiKeyValid(key)).toBe(true)- // Advance to 59 seconds before expiry (should still be valid - 61 seconds remaining)- vi.advanceTimersByTime((expiryInSeconds - 61) * 1000)+ // Advance to 19 seconds before expiry (should still be valid - 21 seconds remaining)+ vi.advanceTimersByTime((expiryInSeconds - 21) * 1000)expect(isTemporaryApiKeyValid(key)).toBe(true)- // Advance to 60 seconds before expiry (should be invalid - 60 seconds remaining)+ // Advance to 20 seconds before expiry (should be invalid - 20 seconds remaining)vi.advanceTimersByTime(1000)expect(isTemporaryApiKeyValid(key)).toBe(false)})@@ -261,38 +261,38 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',const now = Date.now()vi.setSystemTime(now)- // Create a key that expires in 30 seconds (less than the 60 second buffer)- const key = createTemporaryApiKey('test-api-key', 30)+ // Create a key that expires in 10 seconds (less than the 20 second buffer)+ const key = createTemporaryApiKey('test-api-key', 10)- // Should be invalid immediately because it will expire in less than 60 seconds+ // Should be invalid immediately because it will expire in less than 20 secondsexpect(isTemporaryApiKeyValid(key)).toBe(false)})- it('should handle expiry duration of exactly 60 seconds', () => {+ it('should handle expiry duration of exactly 20 seconds', () => {const now = Date.now()vi.setSystemTime(now)- // Create a key that expires in exactly 60 seconds- const key = createTemporaryApiKey('test-api-key', 60)+ // Create a key that expires in exactly 20 seconds+ const key = createTemporaryApiKey('test-api-key', 20)- // Should be invalid because it has exactly 60 seconds remaining (not more than 60)+ // Should be invalid because it has exactly 20 seconds remaining (not more than 20)expect(isTemporaryApiKeyValid(key)).toBe(false)})- it('should handle expiry duration of 61 seconds', () => {+ it('should handle expiry duration of 21 seconds', () => {const now = Date.now()vi.setSystemTime(now)- // Create a key that expires in 61 seconds- const key = createTemporaryApiKey('test-api-key', 61)+ // Create a key that expires in 21 seconds+ const key = createTemporaryApiKey('test-api-key', 21)- // Should be valid because it has 61 seconds remaining (more than 60)+ // Should be valid because it has 21 seconds remaining (more than 20)expect(isTemporaryApiKeyValid(key)).toBe(true)// Advance by 1 secondvi.advanceTimersByTime(1000)- // Should now be invalid because it has exactly 60 seconds remaining+ // Should now be invalid because it has exactly 20 seconds remainingexpect(isTemporaryApiKeyValid(key)).toBe(false)})})diff --git a/apps/studio/data/api-keys/temp-api-keys-utils.ts b/apps/studio/data/api-keys/temp-api-keys-utils.tsindex 9cb0752b098..9b87938ab24 100644--- a/apps/studio/data/api-keys/temp-api-keys-utils.ts+++ b/apps/studio/data/api-keys/temp-api-keys-utils.ts@@ -6,13 +6,13 @@ const projectApiKeys = new Map<ProjectRef, Promise<TemporaryApiKey>>()export interface TemporaryApiKey {apiKey: string- expiryTime: number+ expiryTimeMs: number}export function createTemporaryApiKey(apiKey: string, expiryInSeconds: number): TemporaryApiKey {return {apiKey,- expiryTime: Date.now() + expiryInSeconds * 1000,+ expiryTimeMs: Date.now() + expiryInSeconds * 1000,}}@@ -22,8 +22,8 @@ export function isTemporaryApiKeyValid(if (!key) return falseconst now = Date.now()- const timeRemaining = key.expiryTime - now- return timeRemaining > 60000 // More than 60 seconds remaining+ const timeRemaining = key.expiryTimeMs - now+ return timeRemaining > 20_000 // More than 20 seconds remaining}const checkOrRefreshTemporaryApiKey = async (@@ -31,11 +31,12 @@ const checkOrRefreshTemporaryApiKey = async (existingKey: Promise<TemporaryApiKey> | undefined): Promise<TemporaryApiKey> => {const resolvedKey = await existingKey+if (isTemporaryApiKeyValid(resolvedKey)) {return resolvedKey}- const expiryInSeconds = 600+ const expiryInSeconds = 60const fetchedKey = getTemporaryAPIKey({projectRef,expiry: expiryInSeconds,@@ -47,9 +48,7 @@ const checkOrRefreshTemporaryApiKey = async (// This function should never be marked as async, it should always return a promise.export function getOrRefreshTemporaryApiKey(projectRef: ProjectRef): Promise<TemporaryApiKey> {const existingKey = projectApiKeys.get(projectRef)- if (!existingKey) {- const data = checkOrRefreshTemporaryApiKey(projectRef, undefined)- projectApiKeys.set(projectRef, data)- }- return projectApiKeys.get(projectRef)!+ const data = checkOrRefreshTemporaryApiKey(projectRef, existingKey)+ projectApiKeys.set(projectRef, data)+ return data}diff --git a/apps/studio/state/storage-explorer.tsx b/apps/studio/state/storage-explorer.tsxindex e3d2fce57d1..2bdf9066433 100644--- a/apps/studio/state/storage-explorer.tsx+++ b/apps/studio/state/storage-explorer.tsx@@ -76,6 +76,29 @@ if (typeof window !== 'undefined') {abortController = new AbortController()}+async function createSupabaseClient(projectRef: string, clientEndpoint: string) {+ try {+ const { apiKey } = await getOrRefreshTemporaryApiKey(projectRef)++ return createClient(clientEndpoint, apiKey, {+ auth: {+ persistSession: false,+ autoRefreshToken: false,+ detectSessionInUrl: false,+ storage: {+ getItem: (key) => {+ return null+ },+ setItem: (key, value) => {},+ removeItem: (key) => {},+ },+ },+ })+ } catch (error) {+ throw error+ }+}+function createStorageExplorerState({projectRef,connectionString,@@ -98,29 +121,6 @@ function createStorageExplorerState({resumableUploadUrl,uploadProgresses: [] as UploadProgress[],- supabaseClient: async () => {- try {- const { apiKey } = await getOrRefreshTemporaryApiKey(projectRef)-- return createClient(clientEndpoint, apiKey, {- auth: {- persistSession: false,- autoRefreshToken: false,- detectSessionInUrl: false,- storage: {- getItem: (key) => {- return null- },- setItem: (key, value) => {},- removeItem: (key) => {},- },- },- })- } catch (error) {- throw error- }- },-// abortController,abortApiCalls: () => {if (abortController) {@@ -313,7 +313,7 @@ function createStorageExplorerState({columnIndex: numberonError?: () => void}) => {- if (!state.supabaseClient) return console.error('Supabase Client is missing')+ const supabaseClient = await createSupabaseClient(state.projectRef, clientEndpoint)const autofix = falseconst formattedName = state.sanitizeNameForDuplicateInColumn({@@ -347,7 +347,8 @@ function createStorageExplorerState({const formattedPathToEmptyPlaceholderFile =pathToFolder.length > 0 ? `${pathToFolder}/${emptyPlaceholderFile}` : emptyPlaceholderFile- await (await state.supabaseClient()).storage+ const client = await createSupabaseClient(state.projectRef, clientEndpoint)+ await client.storage.from(state.selectedBucket.name).upload(formattedPathToEmptyPlaceholderFile,@@ -597,7 +598,8 @@ function createStorageExplorerState({if (data.length === 0) {const prefixToPlaceholder = `${parentFolderPrefix}/${EMPTY_FOLDER_PLACEHOLDER_FILE_NAME}`- await (await state.supabaseClient!())?.storage+ const client = await createSupabaseClient(state.projectRef, clientEndpoint)+ await client.storage.from(state.selectedBucket.name).upload(prefixToPlaceholder, new File([], EMPTY_FOLDER_PLACEHOLDER_FILE_NAME))}@@ -818,9 +820,6 @@ function createStorageExplorerState({{ id: toastId, closeButton: false, position: 'top-right' })- // Get authenticated Supabase client for Storage API access- const client = state.supabaseClient ? await state.supabaseClient() : undefined-const promises = files.map((file) => {const fileMimeType = (file.metadata?.mimetype as string) ?? nullreturn () => {@@ -833,6 +832,9 @@ function createStorageExplorerState({| boolean>(async (resolve) => {try {+ // Get authenticated Supabase client for Storage API access+ const client = await createSupabaseClient(state.projectRef, clientEndpoint)+if (!client) {throw new Error('Supabase client not available')}@@ -1517,7 +1519,7 @@ function createStorageExplorerState({const toastId = showToast ? toast.loading(`Retrieving ${fileName}...`) : undefinedtry {- const client = state.supabaseClient ? await state.supabaseClient() : undefined+ const client = await createSupabaseClient(state.projectRef, clientEndpoint)if (!client) {throw new Error('Supabase client not available')}@@ -1564,7 +1566,7 @@ function createStorageExplorerState({if (!file.path) return falsetry {- const client = state.supabaseClient ? await state.supabaseClient() : undefined+ const client = await createSupabaseClient(state.projectRef, clientEndpoint)if (!client) {throw new Error('Supabase client not available')}