Skip to content

Commit 6437bd2

Browse files
authored
fix: Change the valid time for temp API keys to 30 seconds. (supabase#43390)
This pull request makes a minor adjustment to the temporary API key validation logic. The key is now considered invalid if it has less than 30 seconds remaining before expiry, instead of the previous 20 seconds. This change helps avoid edge cases where a key might expire during use.
1 parent 4a9455c commit 6437bd2

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

apps/studio/data/api-keys/temp-api-keys-utils.test.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
23
import {
34
createTemporaryApiKey,
45
isTemporaryApiKeyValid,
@@ -86,7 +87,7 @@ describe('isTemporaryUploadKeyValid', () => {
8687
expect(result).toBe(false)
8788
})
8889

89-
it('should return true for a key with more than 20 seconds remaining', () => {
90+
it('should return true for a key with more than 30 seconds remaining', () => {
9091
const now = Date.now()
9192
vi.setSystemTime(now)
9293

@@ -100,21 +101,21 @@ describe('isTemporaryUploadKeyValid', () => {
100101
expect(result).toBe(true)
101102
})
102103

103-
it('should return false for a key with exactly 20 seconds remaining', () => {
104+
it('should return false for a key with exactly 30 seconds remaining', () => {
104105
const now = Date.now()
105106
vi.setSystemTime(now)
106107

107108
const key: TemporaryApiKey = {
108109
apiKey: 'test-key',
109-
expiryTimeMs: now + 20000, // Exactly 20 seconds
110+
expiryTimeMs: now + 30000, // Exactly 30 seconds
110111
}
111112

112113
const result = isTemporaryApiKeyValid(key)
113114

114115
expect(result).toBe(false)
115116
})
116117

117-
it('should return false for a key with less than 20 seconds remaining', () => {
118+
it('should return false for a key with less than 30 seconds remaining', () => {
118119
const now = Date.now()
119120
vi.setSystemTime(now)
120121

@@ -156,13 +157,13 @@ describe('isTemporaryUploadKeyValid', () => {
156157
expect(result).toBe(false)
157158
})
158159

159-
it('should return true for a key with exactly 21 seconds remaining', () => {
160+
it('should return true for a key with exactly 31 seconds remaining', () => {
160161
const now = Date.now()
161162
vi.setSystemTime(now)
162163

163164
const key: TemporaryApiKey = {
164165
apiKey: 'test-key',
165-
expiryTimeMs: now + 21000, // 21 seconds from now
166+
expiryTimeMs: now + 31000, // 31 seconds from now
166167
}
167168

168169
const result = isTemporaryApiKeyValid(key)
@@ -182,11 +183,11 @@ describe('isTemporaryUploadKeyValid', () => {
182183
// Initially valid
183184
expect(isTemporaryApiKeyValid(key)).toBe(true)
184185

185-
// Advance time by 99 seconds (should still be valid - 21 seconds remaining)
186-
vi.advanceTimersByTime(99000)
186+
// Advance time by 89 seconds (should still be valid - 31 seconds remaining)
187+
vi.advanceTimersByTime(89000)
187188
expect(isTemporaryApiKeyValid(key)).toBe(true)
188189

189-
// Advance time by 2 more seconds (should be invalid - 19 seconds remaining)
190+
// Advance time by 2 more seconds (should be invalid - 29 seconds remaining)
190191
vi.advanceTimersByTime(2000)
191192
expect(isTemporaryApiKeyValid(key)).toBe(false)
192193
})
@@ -238,7 +239,7 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',
238239
expect(isTemporaryApiKeyValid(key)).toBe(true)
239240
})
240241

241-
it('should create a key that becomes invalid after expiry time minus 20 seconds', () => {
242+
it('should create a key that becomes invalid after expiry time minus 30 seconds', () => {
242243
const now = Date.now()
243244
vi.setSystemTime(now)
244245

@@ -248,11 +249,11 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',
248249
// Initially valid
249250
expect(isTemporaryApiKeyValid(key)).toBe(true)
250251

251-
// Advance to 19 seconds before expiry (should still be valid - 21 seconds remaining)
252-
vi.advanceTimersByTime((expiryInSeconds - 21) * 1000)
252+
// Advance to 29 seconds before expiry (should still be valid - 31 seconds remaining)
253+
vi.advanceTimersByTime((expiryInSeconds - 31) * 1000)
253254
expect(isTemporaryApiKeyValid(key)).toBe(true)
254255

255-
// Advance to 20 seconds before expiry (should be invalid - 20 seconds remaining)
256+
// Advance to 20 seconds before expiry (should be invalid - 29 seconds remaining)
256257
vi.advanceTimersByTime(1000)
257258
expect(isTemporaryApiKeyValid(key)).toBe(false)
258259
})
@@ -261,38 +262,38 @@ describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid',
261262
const now = Date.now()
262263
vi.setSystemTime(now)
263264

264-
// Create a key that expires in 10 seconds (less than the 20 second buffer)
265+
// Create a key that expires in 10 seconds (less than the 30 second buffer)
265266
const key = createTemporaryApiKey('test-api-key', 10)
266267

267-
// Should be invalid immediately because it will expire in less than 20 seconds
268+
// Should be invalid immediately because it will expire in less than 30 seconds
268269
expect(isTemporaryApiKeyValid(key)).toBe(false)
269270
})
270271

271-
it('should handle expiry duration of exactly 20 seconds', () => {
272+
it('should handle expiry duration of exactly 30 seconds', () => {
272273
const now = Date.now()
273274
vi.setSystemTime(now)
274275

275-
// Create a key that expires in exactly 20 seconds
276-
const key = createTemporaryApiKey('test-api-key', 20)
276+
// Create a key that expires in exactly 30 seconds
277+
const key = createTemporaryApiKey('test-api-key', 30)
277278

278-
// Should be invalid because it has exactly 20 seconds remaining (not more than 20)
279+
// Should be invalid because it has exactly 30 seconds remaining (not more than 30)
279280
expect(isTemporaryApiKeyValid(key)).toBe(false)
280281
})
281282

282-
it('should handle expiry duration of 21 seconds', () => {
283+
it('should handle expiry duration of 31 seconds', () => {
283284
const now = Date.now()
284285
vi.setSystemTime(now)
285286

286-
// Create a key that expires in 21 seconds
287-
const key = createTemporaryApiKey('test-api-key', 21)
287+
// Create a key that expires in 31 seconds
288+
const key = createTemporaryApiKey('test-api-key', 31)
288289

289-
// Should be valid because it has 21 seconds remaining (more than 20)
290+
// Should be valid because it has 31 seconds remaining (more than 30)
290291
expect(isTemporaryApiKeyValid(key)).toBe(true)
291292

292293
// Advance by 1 second
293294
vi.advanceTimersByTime(1000)
294295

295-
// Should now be invalid because it has exactly 20 seconds remaining
296+
// Should now be invalid because it has exactly 30 seconds remaining
296297
expect(isTemporaryApiKeyValid(key)).toBe(false)
297298
})
298299
})

apps/studio/data/api-keys/temp-api-keys-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export function isTemporaryApiKeyValid(
2323

2424
const now = Date.now()
2525
const timeRemaining = key.expiryTimeMs - now
26-
return timeRemaining > 20_000 // More than 20 seconds remaining
26+
// Consider the key invalid if it has less than 30 seconds remaining to avoid edge cases where the key
27+
// expires during use.
28+
return timeRemaining > 30_000
2729
}
2830

2931
const checkOrRefreshTemporaryApiKey = async (

0 commit comments

Comments
 (0)