feat(storage): align local presigned URL expiry with env vars and float hours (#543)

This commit is contained in:
Yiling-J
2026-11-23 19:35:40 +08:00
committed by GitHub
parent 6c71de4160
commit 5f6f3799fb
10 changed files with 410 additions and 81 deletions
+243 -4
View File
@@ -12,10 +12,13 @@ import {
} from '@aws-sdk/client-s3'
import {
buildContentDisposition,
getPresignedUrlExpirySeconds,
LocalStorageService,
S3StorageService,
signLocalGetUrl,
signLocalUrl,
verifyLocalGetUrlSignature,
verifyLocalUrlSignature,
} from './s3'
const s3ClientConstructorSpy = vi.fn()
@@ -82,7 +85,7 @@ describe('S3Service implementations', () => {
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(GetObjectCommand),
expect.objectContaining({ expiresIn: 18000 }),
expect.objectContaining({ expiresIn: 43200 }),
)
})
@@ -116,14 +119,14 @@ describe('S3Service implementations', () => {
const url1 = await s3.presign('bucket', 'key', 'GET')
expect(url1).toBe('http://presigned-url-1')
// Default GET lifetime is 5h; before 2.5h the cached URL should still be reused
const almostHalfMs = 2.5 * 3600 * 1000 - 1_000
// Default GET lifetime is 12h; before 6h the cached URL should still be reused
const almostHalfMs = 6 * 3600 * 1000 - 1_000
wallNow += almostHalfMs
perfNow += almostHalfMs
expect(await s3.presign('bucket', 'key', 'GET')).toBe('http://presigned-url-1')
expect(getSignedUrlSpy).toHaveBeenCalledTimes(1)
// Past 2.5h (half of the 5h lifetime), the cache must expire so callers never get nearly expired URLs
// Past 6h (half of the 12h lifetime), the cache must expire so callers never get nearly expired URLs
wallNow += 2_000
perfNow += 2_000
expect(await s3.presign('bucket', 'key', 'GET')).toBe('http://presigned-url-2')
@@ -301,6 +304,43 @@ describe('S3Service implementations', () => {
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
it('should support float values for PRESIGNED_URL_EXPIRES_IN and PRESIGNED_UPLOAD_URL_EXPIRES_IN', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '0.1'
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN = '1.5'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'GET')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(GetObjectCommand),
expect.objectContaining({ expiresIn: 360 }),
)
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 5400 }),
)
getSignedUrlSpy.mockClear()
await s3.presignMultipart('bucket', 'key', {
method: 'PUT',
key: 'key',
fileId: 'file-1',
})
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 5400 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
})
it('should not set contentDisposition for normal GET presign', async () => {
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
await s3.presign('bucket', 'key', 'GET')
@@ -920,6 +960,102 @@ describe('S3Service implementations', () => {
await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow()
})
it('should default local GET and PUT presign expiry to 12 hours', async () => {
const fixedNow = 1_700_000_000_000
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(fixedNow)
try {
const expectedExpires = String(Math.floor(fixedNow / 1000) + 12 * 3600)
const getUrl = new URL(await localS3.presign('my-bucket', 'dir/default.txt', 'GET'))
expect(getUrl.searchParams.get('Expires')).toBe(expectedExpires)
const putUrl = new URL(await localS3.presign('my-bucket', 'dir/default.txt', 'PUT'))
expect(putUrl.pathname).toBe('/api/upload/local')
expect(putUrl.searchParams.get('Expires')).toBe(expectedExpires)
expect(
verifyLocalUrlSignature(
'my-bucket',
'dir/default.txt',
putUrl.searchParams.get('Signature'),
putUrl.searchParams.get('Expires'),
),
).toBe(true)
} finally {
dateNowSpy.mockRestore()
}
})
it('should respect PRESIGNED_URL_EXPIRES_IN (including floats) for local GET and PUT fallback', async () => {
const fixedNow = 1_700_000_000_000
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(fixedNow)
process.env.PRESIGNED_URL_EXPIRES_IN = '0.1'
try {
const expectedExpires = String(Math.floor(fixedNow / 1000) + 360)
const getUrl = new URL(await localS3.presign('my-bucket', 'dir/float.txt', 'GET'))
expect(getUrl.searchParams.get('Expires')).toBe(expectedExpires)
const putUrl = new URL(await localS3.presign('my-bucket', 'dir/float.txt', 'PUT'))
expect(putUrl.searchParams.get('Expires')).toBe(expectedExpires)
} finally {
dateNowSpy.mockRestore()
delete process.env.PRESIGNED_URL_EXPIRES_IN
}
})
it('should respect PRESIGNED_UPLOAD_URL_EXPIRES_IN (including floats) for local PUT and presignMultipart', async () => {
const fixedNow = 1_700_000_000_000
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(fixedNow)
process.env.PRESIGNED_URL_EXPIRES_IN = '2'
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN = '1.5'
try {
const expectedGetExpires = String(Math.floor(fixedNow / 1000) + 2 * 3600)
const expectedPutExpires = String(Math.floor(fixedNow / 1000) + 5400)
const getUrl = new URL(await localS3.presign('my-bucket', 'dir/upload-env.txt', 'GET'))
expect(getUrl.searchParams.get('Expires')).toBe(expectedGetExpires)
const putUrl = new URL(await localS3.presign('my-bucket', 'dir/upload-env.txt', 'PUT'))
expect(putUrl.searchParams.get('Expires')).toBe(expectedPutExpires)
const { url: multipartUrlStr } = await localS3.presignMultipart(
'my-bucket',
'dir/upload-env.txt',
{
method: 'PUT',
key: 'dir/upload-env.txt',
fileId: 'file-1',
},
)
const multipartUrl = new URL(multipartUrlStr)
expect(multipartUrl.searchParams.get('Expires')).toBe(expectedPutExpires)
} finally {
dateNowSpy.mockRestore()
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
}
})
it('should respect explicit expiresInSeconds override for local GET and PUT', async () => {
const fixedNow = 1_700_000_000_000
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(fixedNow)
try {
const expectedExpires = String(Math.floor(fixedNow / 1000) + 600)
const getUrl = new URL(
await localS3.presign('my-bucket', 'dir/custom.txt', 'GET', false, undefined, false, 600),
)
expect(getUrl.searchParams.get('Expires')).toBe(expectedExpires)
const putUrl = new URL(
await localS3.presign('my-bucket', 'dir/custom.txt', 'PUT', false, undefined, false, 600),
)
expect(putUrl.searchParams.get('Expires')).toBe(expectedExpires)
} finally {
dateNowSpy.mockRestore()
}
})
it('should resolveInput to local file path if file exists on disk', async () => {
await localS3.putObject('my-bucket', 'video.mp4', 'dummy content', 13)
const input = await localS3.resolveInput('my-bucket', 'video.mp4')
@@ -1079,4 +1215,107 @@ describe('S3Service implementations', () => {
).toBe(false)
})
})
describe('getPresignedUrlExpirySeconds', () => {
it('supports float hours and falls back to 12 hours for invalid values', () => {
expect(getPresignedUrlExpirySeconds('GET')).toBe(12 * 3600)
expect(getPresignedUrlExpirySeconds('PUT')).toBe(12 * 3600)
process.env.PRESIGNED_URL_EXPIRES_IN = '0.1'
expect(getPresignedUrlExpirySeconds('GET')).toBe(360)
expect(getPresignedUrlExpirySeconds('PUT')).toBe(360)
process.env.PRESIGNED_URL_EXPIRES_IN = '0.0001'
expect(getPresignedUrlExpirySeconds('GET')).toBe(1)
for (const invalid of ['0', '-1', 'abc', 'Infinity', 'NaN', ' ']) {
process.env.PRESIGNED_URL_EXPIRES_IN = invalid
expect(getPresignedUrlExpirySeconds('GET')).toBe(12 * 3600)
expect(getPresignedUrlExpirySeconds('PUT')).toBe(12 * 3600)
}
expect(getPresignedUrlExpirySeconds('GET', 120)).toBe(120)
expect(getPresignedUrlExpirySeconds('PUT', -10)).toBe(-10)
})
})
describe('signLocalUrl and verifyLocalUrlSignature', () => {
it('signs and verifies valid local PUT url', () => {
const signed = signLocalUrl('my-bucket', 'files/123/file.mp4')
const parsed = new URL(`http://localhost:3000${signed}`)
const signature = parsed.searchParams.get('Signature')
const expires = parsed.searchParams.get('Expires')
expect(signature).toBeTruthy()
expect(Number(expires)).toBeGreaterThan(Math.floor(Date.now() / 1000))
expect(verifyLocalUrlSignature('my-bucket', 'files/123/file.mp4', signature, expires)).toBe(
true,
)
})
it('rejects expired local PUT url signature', () => {
const signed = signLocalUrl('my-bucket', 'files/123/file.mp4', -10)
const parsed = new URL(`http://localhost:3000${signed}`)
expect(
verifyLocalUrlSignature(
'my-bucket',
'files/123/file.mp4',
parsed.searchParams.get('Signature'),
parsed.searchParams.get('Expires'),
),
).toBe(false)
})
it('rejects tampered bucket, key, or malformed params', () => {
const signed = signLocalUrl('my-bucket', 'files/123/file.mp4')
const parsed = new URL(`http://localhost:3000${signed}`)
const signature = parsed.searchParams.get('Signature')
const expires = parsed.searchParams.get('Expires')
expect(
verifyLocalUrlSignature('other-bucket', 'files/123/file.mp4', signature, expires),
).toBe(false)
expect(verifyLocalUrlSignature('my-bucket', 'files/999/other.mp4', signature, expires)).toBe(
false,
)
expect(
verifyLocalUrlSignature(
'my-bucket',
'files/123/file.mp4',
signature,
String(Number(expires) + 60),
),
).toBe(false)
expect(verifyLocalUrlSignature('my-bucket', 'files/123/file.mp4', undefined, expires)).toBe(
false,
)
expect(verifyLocalUrlSignature('my-bucket', 'files/123/file.mp4', signature, undefined)).toBe(
false,
)
expect(
verifyLocalUrlSignature('my-bucket', 'files/123/file.mp4', signature, `${expires}abc`),
).toBe(false)
})
it('rejects reusing a GET signature for PUT and vice versa', () => {
const { signature: getSig, expires: getExp } = signLocalGetUrl(
'my-bucket',
'files/123/file.mp4',
)
expect(
verifyLocalUrlSignature('my-bucket', 'files/123/file.mp4', getSig, String(getExp)),
).toBe(false)
const signedPut = signLocalUrl('my-bucket', 'files/123/file.mp4')
const parsedPut = new URL(`http://localhost:3000${signedPut}`)
expect(
verifyLocalGetUrlSignature(
'my-bucket',
'files/123/file.mp4',
parsedPut.searchParams.get('Signature'),
parsedPut.searchParams.get('Expires'),
),
).toBe(false)
})
})
})
+60 -33
View File
@@ -26,34 +26,73 @@ import { ulid } from 'ulid'
import { LruTtlCache } from '../cache/lru-ttl-cache'
import { detectSupportedMimeType } from '../utils/mime'
export function signLocalUrl(bucket: string, key: string): string {
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
const hmac = crypto.createHmac('sha256', secret)
hmac.update(`${bucket}/${key}`)
const signature = hmac.digest('hex')
return `/api/upload/local?bucket=${encodeURIComponent(bucket)}&key=${encodeURIComponent(key)}&Signature=${signature}`
export const DEFAULT_PRESIGNED_URL_EXPIRY_HOURS = 12
export function getPresignedUrlExpirySeconds(method: string, expiresInSeconds?: number): number {
if (expiresInSeconds !== undefined) {
return expiresInSeconds
}
const raw =
method === 'PUT'
? process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN?.trim() ||
process.env.PRESIGNED_URL_EXPIRES_IN?.trim()
: process.env.PRESIGNED_URL_EXPIRES_IN?.trim()
if (raw) {
const hours = Number(raw)
if (Number.isFinite(hours) && hours > 0) {
return Math.max(1, Math.round(hours * 3600))
}
}
return DEFAULT_PRESIGNED_URL_EXPIRY_HOURS * 3600
}
export function verifyLocalUrlSignature(bucket: string, key: string, signature: string): boolean {
export function signLocalUrl(bucket: string, key: string, expiresInSeconds?: number): string {
const lifetimeSeconds = getPresignedUrlExpirySeconds('PUT', expiresInSeconds)
const expires = Math.floor(Date.now() / 1000) + lifetimeSeconds
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
const hmac = crypto.createHmac('sha256', secret)
hmac.update(`${bucket}/${key}`)
hmac.update(`PUT:${bucket}/${key}:${expires}`)
const signature = hmac.digest('hex')
return `/api/upload/local?bucket=${encodeURIComponent(bucket)}&key=${encodeURIComponent(key)}&Signature=${signature}&Expires=${expires}`
}
export function verifyLocalUrlSignature(
bucket: string,
key: string,
signature: string | undefined | null,
expiresStr: string | undefined | null,
): boolean {
if (!signature || !expiresStr) return false
if (!/^-?\d+$/.test(expiresStr)) return false
const expires = parseInt(expiresStr, 10)
if (isNaN(expires)) return false
const now = Math.floor(Date.now() / 1000)
if (now > expires) return false
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
const hmac = crypto.createHmac('sha256', secret)
hmac.update(`PUT:${bucket}/${key}:${expires}`)
const expected = hmac.digest('hex')
try {
return crypto.timingSafeEqual(Buffer.from(signature, 'hex'), Buffer.from(expected, 'hex'))
const sigBuf = Buffer.from(signature, 'hex')
const expBuf = Buffer.from(expected, 'hex')
if (sigBuf.length !== expBuf.length) return false
return crypto.timingSafeEqual(sigBuf, expBuf)
} catch {
return false
}
}
export const DEFAULT_LOCAL_GET_EXPIRY_SECONDS = 12 * 3600
export function signLocalGetUrl(
bucket: string,
key: string,
expiresInSeconds: number = DEFAULT_LOCAL_GET_EXPIRY_SECONDS,
expiresInSeconds?: number,
): { signature: string; expires: number } {
const expires = Math.floor(Date.now() / 1000) + expiresInSeconds
const lifetimeSeconds = getPresignedUrlExpirySeconds('GET', expiresInSeconds)
const expires = Math.floor(Date.now() / 1000) + lifetimeSeconds
const secret = process.env.BETTER_AUTH_SECRET || 'shumai-local-storage-secret'
const hmac = crypto.createHmac('sha256', secret)
hmac.update(`GET:${bucket}/${key}:${expires}`)
@@ -68,6 +107,7 @@ export function verifyLocalGetUrlSignature(
expiresStr: string | undefined | null,
): boolean {
if (!signature || !expiresStr) return false
if (!/^-?\d+$/.test(expiresStr)) return false
const expires = parseInt(expiresStr, 10)
if (isNaN(expires)) return false
@@ -392,7 +432,7 @@ export interface S3Service {
download?: boolean,
filename?: string,
internal?: boolean,
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET, or DEFAULT_LOCAL_GET_EXPIRY_SECONDS (12h) for local. */
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h, fallback PRESIGNED_URL_EXPIRES_IN) for PUT, or PRESIGNED_URL_EXPIRES_IN (12h) for GET and other methods. */
expiresInSeconds?: number,
) => Promise<string>
presignMultipart: (
@@ -664,21 +704,12 @@ export class S3StorageService implements S3Service {
internal?: boolean,
expiresInSeconds?: number,
): Promise<string> {
const defaultExpireHours =
method === 'PUT'
? parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN ||
process.env.PRESIGNED_URL_EXPIRES_IN ||
'12',
10,
)
: parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const lifetimeSeconds = expiresInSeconds ?? defaultExpireHours * 3600
const lifetimeSeconds = getPresignedUrlExpirySeconds(method, expiresInSeconds)
// Only reuse a cached URL while it has at least half of its lifetime left, so callers
// never receive a nearly expired link.
const cacheTtlMs = (lifetimeSeconds * 1000) / 2
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&internal=${internal || false}&expires=${expiresInSeconds ?? 'default'}`
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&internal=${internal || false}&expires=${lifetimeSeconds}`
if (method === 'GET' && !download) {
const cached = this.presignCache.get(cacheKey)
if (cached) {
@@ -724,11 +755,7 @@ export class S3StorageService implements S3Service {
key: string,
request: S3SignRequest,
): Promise<{ url: string }> {
const expireHours = parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN || process.env.PRESIGNED_URL_EXPIRES_IN || '12',
10,
)
const expiresInSeconds = expireHours * 3600
const expiresInSeconds = getPresignedUrlExpirySeconds('PUT')
let url: string
@@ -1094,12 +1121,12 @@ export class LocalStorageService implements S3Service {
throw new Error(`Invalid method: ${method}`)
}
if (method === 'PUT') {
return `${this.endpoint}${signLocalUrl(bucket, key)}`
return `${this.endpoint}${signLocalUrl(bucket, key, expiresInSeconds)}`
}
const lifetimeSeconds = expiresInSeconds ?? DEFAULT_LOCAL_GET_EXPIRY_SECONDS
const lifetimeSeconds = getPresignedUrlExpirySeconds('GET', expiresInSeconds)
const cacheTtlMs = (lifetimeSeconds * 1000) / 2
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&expires=${expiresInSeconds ?? 'default'}`
const cacheKey = `${bucket}/${key}?download=${download || false}&filename=${filename || ''}&expires=${lifetimeSeconds}`
if (method === 'GET' && !download) {
const cached = this.presignCache.get(cacheKey)