Skip to content

Commit 9bf39cd

Browse files
bnjmnt4ninian
authored andcommitted
fix: prevent creation of objects with no name
1 parent f83930c commit 9bf39cd

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/test/object.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ describe('testing POST object via multipart upload', () => {
285285
)
286286
})
287287

288+
test('return 400 when uploading to object with no file name', async () => {
289+
const form = new FormData()
290+
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
291+
const headers = Object.assign({}, form.getHeaders(), {
292+
authorization: `Bearer ${anonKey}`,
293+
})
294+
295+
const response = await app().inject({
296+
method: 'POST',
297+
url: '/object/bucket4/',
298+
headers,
299+
payload: form,
300+
})
301+
expect(response.statusCode).toBe(400)
302+
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
303+
})
304+
288305
test('should not add row to database if upload fails', async () => {
289306
// Mock S3 upload failure.
290307
jest.spyOn(S3Backend.prototype, 'uploadObject').mockRejectedValue(
@@ -496,6 +513,27 @@ describe('testing POST object via binary upload', () => {
496513
)
497514
})
498515

516+
test('return 400 when uploading to object with no file name', async () => {
517+
const path = './src/test/assets/sadcat.jpg'
518+
const { size } = fs.statSync(path)
519+
520+
const headers = {
521+
authorization: `Bearer ${anonKey}`,
522+
'Content-Length': size,
523+
'Content-Type': 'image/jpeg',
524+
'x-upsert': 'true',
525+
}
526+
527+
const response = await app().inject({
528+
method: 'POST',
529+
url: '/object/bucket4/',
530+
headers,
531+
payload: fs.createReadStream(path),
532+
})
533+
expect(response.statusCode).toBe(400)
534+
expect(S3Backend.prototype.uploadObject).not.toHaveBeenCalled()
535+
})
536+
499537
test('should not add row to database if upload fails', async () => {
500538
// Mock S3 upload failure.
501539
jest.spyOn(S3Backend.prototype, 'uploadObject').mockRejectedValue(

src/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ export function normalizeContentType(contentType: string | undefined): string |
9090
export function isValidKey(key: string): boolean {
9191
// only allow s3 safe characters and characters which require special handling for now
9292
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
93-
return /^(\w|\/|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(key)
93+
return key.length > 0 && /^(\w|\/|!|-|\.|\*|'|\(|\)| |&|\$|@|=|;|:|\+|,|\?)*$/.test(key)
9494
}

0 commit comments

Comments
 (0)