|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -import { generateHS512JWK, SignedToken, signJWT, verifyJWT } from '@internal/auth' |
| 3 | +import { |
| 4 | + generateHS512JWK, |
| 5 | + getMaxNumericJWTExpiration, |
| 6 | + SignedToken, |
| 7 | + signJWT, |
| 8 | + verifyJWT, |
| 9 | +} from '@internal/auth' |
4 | 10 | import { getPostgresConnection, getServiceKeyUser } from '@internal/database' |
5 | 11 | import { ErrorCode, StorageBackendError } from '@internal/errors' |
6 | 12 | import { randomUUID } from 'crypto' |
@@ -1830,6 +1836,38 @@ describe('testing generating signed URL', () => { |
1830 | 1836 | }) |
1831 | 1837 | expect(response.statusCode).toBe(400) |
1832 | 1838 | }) |
| 1839 | + |
| 1840 | + test('rejects oversized expiresIn values for signed URLs before jwt signing', async () => { |
| 1841 | + const response = await appInstance.inject({ |
| 1842 | + method: 'POST', |
| 1843 | + url: '/object/sign/bucket2/authenticated/cat.jpg', |
| 1844 | + headers: { |
| 1845 | + authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`, |
| 1846 | + }, |
| 1847 | + payload: { |
| 1848 | + expiresIn: 1e21, |
| 1849 | + }, |
| 1850 | + }) |
| 1851 | + |
| 1852 | + expect(response.statusCode).toBe(400) |
| 1853 | + expect(JSON.parse(response.body).message).toContain('expiresIn') |
| 1854 | + }) |
| 1855 | + |
| 1856 | + test('rejects expiresIn values above the current runtime maximum for signed URLs', async () => { |
| 1857 | + const response = await appInstance.inject({ |
| 1858 | + method: 'POST', |
| 1859 | + url: '/object/sign/bucket2/authenticated/cat.jpg', |
| 1860 | + headers: { |
| 1861 | + authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`, |
| 1862 | + }, |
| 1863 | + payload: { |
| 1864 | + expiresIn: getMaxNumericJWTExpiration() + 10, |
| 1865 | + }, |
| 1866 | + }) |
| 1867 | + |
| 1868 | + expect(response.statusCode).toBe(400) |
| 1869 | + expect(JSON.parse(response.body).message).toContain('expiresIn') |
| 1870 | + }) |
1833 | 1871 | }) |
1834 | 1872 |
|
1835 | 1873 | /** |
@@ -2034,7 +2072,7 @@ describe('testing uploading with generated signed upload URL', () => { |
2034 | 2072 | const urlToSign = `${BUCKET_ID}/${OBJECT_NAME}` |
2035 | 2073 | const owner = '317eadce-631a-4429-a0bb-f19a7a517b4a' |
2036 | 2074 |
|
2037 | | - const jwtToken = await signJWT({ owner, url: urlToSign }, jwtSecret, -1) |
| 2075 | + const jwtToken = await signJWT({ owner, url: urlToSign }, jwtSecret, '-1s') |
2038 | 2076 | const response = await appInstance.inject({ |
2039 | 2077 | method: 'PUT', |
2040 | 2078 | url: `/object/upload/sign/${urlToSign}?token=${jwtToken}`, |
@@ -2207,6 +2245,23 @@ describe('testing generating signed URLs', () => { |
2207 | 2245 | const result = JSON.parse(response.body) |
2208 | 2246 | expect(result[0].error).toBe('Either the object does not exist or you do not have access to it') |
2209 | 2247 | }) |
| 2248 | + |
| 2249 | + test('rejects oversized expiresIn values for batch signed URLs before jwt signing', async () => { |
| 2250 | + const response = await appInstance.inject({ |
| 2251 | + method: 'POST', |
| 2252 | + url: '/object/sign/bucket2', |
| 2253 | + headers: { |
| 2254 | + authorization: `Bearer ${process.env.AUTHENTICATED_KEY}`, |
| 2255 | + }, |
| 2256 | + payload: { |
| 2257 | + expiresIn: 1e21, |
| 2258 | + paths: ['authenticated/cat.jpg'], |
| 2259 | + }, |
| 2260 | + }) |
| 2261 | + |
| 2262 | + expect(response.statusCode).toBe(400) |
| 2263 | + expect(JSON.parse(response.body).message).toContain('expiresIn') |
| 2264 | + }) |
2210 | 2265 | }) |
2211 | 2266 |
|
2212 | 2267 | /** |
@@ -2300,7 +2355,7 @@ describe('testing retrieving signed URL', () => { |
2300 | 2355 |
|
2301 | 2356 | test('get object with an expired JWT', async () => { |
2302 | 2357 | const urlToSign = 'bucket2/public/sadcat-upload.png' |
2303 | | - const expiredJWT = await signJWT({ url: urlToSign }, jwtSecret, -1) |
| 2358 | + const expiredJWT = await signJWT({ url: urlToSign }, jwtSecret, '-1s') |
2304 | 2359 | const response = await appInstance.inject({ |
2305 | 2360 | method: 'GET', |
2306 | 2361 | url: `/object/sign/${urlToSign}?token=${expiredJWT}`, |
|
0 commit comments