Skip to content

Commit 5a9a212

Browse files
committed
chore(storage): explicitly validate for the upload data payload size
1 parent 07883e4 commit 5a9a212

File tree

7 files changed

+125
-75
lines changed

7 files changed

+125
-75
lines changed

packages/storage/__tests__/providers/s3/apis/internal/uploadData/index.test.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ describe('uploadData with key', () => {
5555
);
5656
});
5757

58-
it('should NOT throw if data size is unknown', async () => {
59-
uploadData({
60-
key: 'key',
61-
data: {} as any,
62-
});
63-
expect(mockCreateUploadTask).toHaveBeenCalled();
58+
it('should throw if data size is unknown', async () => {
59+
expect(() =>
60+
uploadData({
61+
key: 'key',
62+
data: {} as any,
63+
}),
64+
).toThrow(
65+
expect.objectContaining(
66+
validationErrorMap[StorageValidationErrorCode.InvalidUploadSource],
67+
),
68+
);
6469
});
6570
});
6671

@@ -166,12 +171,17 @@ describe('uploadData with path', () => {
166171
);
167172
});
168173

169-
it('should NOT throw if data size is unknown', async () => {
170-
uploadData({
171-
path: testPath,
172-
data: {} as any,
173-
});
174-
expect(mockCreateUploadTask).toHaveBeenCalled();
174+
it('should throw if data size is unknown', async () => {
175+
expect(() =>
176+
uploadData({
177+
path: testPath,
178+
data: {} as any,
179+
}),
180+
).toThrow(
181+
expect.objectContaining(
182+
validationErrorMap[StorageValidationErrorCode.InvalidUploadSource],
183+
),
184+
);
175185
});
176186
});
177187

packages/storage/__tests__/providers/s3/apis/internal/uploadData/multipartHandlers.test.ts

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ describe('getMultipartUploadHandlers with key', () => {
244244
data: twoPartsPayload,
245245
options: options as StorageOptions,
246246
},
247-
byteLength(twoPartsPayload),
247+
byteLength(twoPartsPayload)!,
248248
);
249249
const result = await multipartUploadJob();
250250
await expect(
@@ -293,7 +293,7 @@ describe('getMultipartUploadHandlers with key', () => {
293293
checksumAlgorithm: CHECKSUM_ALGORITHM_CRC32,
294294
},
295295
},
296-
byteLength(twoPartsPayload),
296+
byteLength(twoPartsPayload)!,
297297
);
298298
await multipartUploadJob();
299299

@@ -346,10 +346,13 @@ describe('getMultipartUploadHandlers with key', () => {
346346

347347
it('should throw if unsupported payload type is provided', async () => {
348348
mockMultipartUploadSuccess();
349-
const { multipartUploadJob } = getMultipartUploadHandlers({
350-
key: defaultKey,
351-
data: 1 as any,
352-
});
349+
const { multipartUploadJob } = getMultipartUploadHandlers(
350+
{
351+
key: defaultKey,
352+
data: 1 as any,
353+
},
354+
1,
355+
);
353356
await expect(multipartUploadJob()).rejects.toThrow(
354357
expect.objectContaining(
355358
validationErrorMap[StorageValidationErrorCode.InvalidUploadSource],
@@ -427,10 +430,13 @@ describe('getMultipartUploadHandlers with key', () => {
427430
mockCreateMultipartUpload.mockReset();
428431
mockCreateMultipartUpload.mockRejectedValueOnce(new Error('error'));
429432

430-
const { multipartUploadJob } = getMultipartUploadHandlers({
431-
key: defaultKey,
432-
data: new ArrayBuffer(8 * MB),
433-
});
433+
const { multipartUploadJob } = getMultipartUploadHandlers(
434+
{
435+
key: defaultKey,
436+
data: new ArrayBuffer(8 * MB),
437+
},
438+
8 * MB,
439+
);
434440
await expect(multipartUploadJob()).rejects.toThrow('error');
435441
});
436442

@@ -440,10 +446,13 @@ describe('getMultipartUploadHandlers with key', () => {
440446
mockCompleteMultipartUpload.mockReset();
441447
mockCompleteMultipartUpload.mockRejectedValueOnce(new Error('error'));
442448

443-
const { multipartUploadJob } = getMultipartUploadHandlers({
444-
key: defaultKey,
445-
data: new ArrayBuffer(8 * MB),
446-
});
449+
const { multipartUploadJob } = getMultipartUploadHandlers(
450+
{
451+
key: defaultKey,
452+
data: new ArrayBuffer(8 * MB),
453+
},
454+
8 * MB,
455+
);
447456
await expect(multipartUploadJob()).rejects.toThrow('error');
448457
});
449458

@@ -458,10 +467,13 @@ describe('getMultipartUploadHandlers with key', () => {
458467
});
459468
mockUploadPart.mockRejectedValueOnce(new Error('error'));
460469

461-
const { multipartUploadJob } = getMultipartUploadHandlers({
462-
key: defaultKey,
463-
data: new ArrayBuffer(8 * MB),
464-
});
470+
const { multipartUploadJob } = getMultipartUploadHandlers(
471+
{
472+
key: defaultKey,
473+
data: new ArrayBuffer(8 * MB),
474+
},
475+
8 * MB,
476+
);
465477
await expect(multipartUploadJob()).rejects.toThrow('error');
466478
expect(mockUploadPart).toHaveBeenCalledTimes(2);
467479
expect(mockCompleteMultipartUpload).not.toHaveBeenCalled();
@@ -481,7 +493,7 @@ describe('getMultipartUploadHandlers with key', () => {
481493
bucket: { bucketName: mockBucket, region: mockRegion },
482494
},
483495
},
484-
byteLength(mockData),
496+
byteLength(mockData)!,
485497
);
486498
await multipartUploadJob();
487499
await expect(
@@ -510,7 +522,7 @@ describe('getMultipartUploadHandlers with key', () => {
510522
bucket: 'default-bucket',
511523
},
512524
},
513-
byteLength(mockData),
525+
byteLength(mockData)!,
514526
);
515527
await multipartUploadJob();
516528
await expect(
@@ -800,10 +812,13 @@ describe('getMultipartUploadHandlers with key', () => {
800812

801813
describe('cancel()', () => {
802814
it('should abort in-flight uploadPart requests and throw if upload is canceled', async () => {
803-
const { multipartUploadJob, onCancel } = getMultipartUploadHandlers({
804-
key: defaultKey,
805-
data: new ArrayBuffer(8 * MB),
806-
});
815+
const { multipartUploadJob, onCancel } = getMultipartUploadHandlers(
816+
{
817+
key: defaultKey,
818+
data: new ArrayBuffer(8 * MB),
819+
},
820+
8 * MB,
821+
);
807822
let partCount = 0;
808823
mockMultipartUploadCancellation(() => {
809824
partCount++;
@@ -1007,7 +1022,7 @@ describe('getMultipartUploadHandlers with path', () => {
10071022
path: inputPath,
10081023
data: twoPartsPayload,
10091024
},
1010-
byteLength(twoPartsPayload),
1025+
byteLength(twoPartsPayload)!,
10111026
);
10121027
const result = await multipartUploadJob();
10131028
await expect(
@@ -1056,7 +1071,7 @@ describe('getMultipartUploadHandlers with path', () => {
10561071
checksumAlgorithm: CHECKSUM_ALGORITHM_CRC32,
10571072
},
10581073
},
1059-
byteLength(twoPartsPayload),
1074+
byteLength(twoPartsPayload)!,
10601075
);
10611076
await multipartUploadJob();
10621077

@@ -1109,10 +1124,13 @@ describe('getMultipartUploadHandlers with path', () => {
11091124

11101125
it('should throw if unsupported payload type is provided', async () => {
11111126
mockMultipartUploadSuccess();
1112-
const { multipartUploadJob } = getMultipartUploadHandlers({
1113-
path: testPath,
1114-
data: 1 as any,
1115-
});
1127+
const { multipartUploadJob } = getMultipartUploadHandlers(
1128+
{
1129+
path: testPath,
1130+
data: 1 as any,
1131+
},
1132+
1,
1133+
);
11161134
await expect(multipartUploadJob()).rejects.toThrow(
11171135
expect.objectContaining(
11181136
validationErrorMap[StorageValidationErrorCode.InvalidUploadSource],
@@ -1190,10 +1208,13 @@ describe('getMultipartUploadHandlers with path', () => {
11901208
mockCreateMultipartUpload.mockReset();
11911209
mockCreateMultipartUpload.mockRejectedValueOnce(new Error('error'));
11921210

1193-
const { multipartUploadJob } = getMultipartUploadHandlers({
1194-
path: testPath,
1195-
data: new ArrayBuffer(8 * MB),
1196-
});
1211+
const { multipartUploadJob } = getMultipartUploadHandlers(
1212+
{
1213+
path: testPath,
1214+
data: new ArrayBuffer(8 * MB),
1215+
},
1216+
8 * MB,
1217+
);
11971218
await expect(multipartUploadJob()).rejects.toThrow('error');
11981219
});
11991220

@@ -1203,10 +1224,13 @@ describe('getMultipartUploadHandlers with path', () => {
12031224
mockCompleteMultipartUpload.mockReset();
12041225
mockCompleteMultipartUpload.mockRejectedValueOnce(new Error('error'));
12051226

1206-
const { multipartUploadJob } = getMultipartUploadHandlers({
1207-
path: testPath,
1208-
data: new ArrayBuffer(8 * MB),
1209-
});
1227+
const { multipartUploadJob } = getMultipartUploadHandlers(
1228+
{
1229+
path: testPath,
1230+
data: new ArrayBuffer(8 * MB),
1231+
},
1232+
8 * MB,
1233+
);
12101234
await expect(multipartUploadJob()).rejects.toThrow('error');
12111235
});
12121236

@@ -1221,10 +1245,13 @@ describe('getMultipartUploadHandlers with path', () => {
12211245
});
12221246
mockUploadPart.mockRejectedValueOnce(new Error('error'));
12231247

1224-
const { multipartUploadJob } = getMultipartUploadHandlers({
1225-
path: testPath,
1226-
data: new ArrayBuffer(8 * MB),
1227-
});
1248+
const { multipartUploadJob } = getMultipartUploadHandlers(
1249+
{
1250+
path: testPath,
1251+
data: new ArrayBuffer(8 * MB),
1252+
},
1253+
8 * MB,
1254+
);
12281255
await expect(multipartUploadJob()).rejects.toThrow('error');
12291256
expect(mockUploadPart).toHaveBeenCalledTimes(2);
12301257
expect(mockCompleteMultipartUpload).not.toHaveBeenCalled();
@@ -1273,7 +1300,7 @@ describe('getMultipartUploadHandlers with path', () => {
12731300
bucket: { bucketName: mockBucket, region: mockRegion },
12741301
},
12751302
},
1276-
byteLength(mockData),
1303+
byteLength(mockData)!,
12771304
);
12781305
await multipartUploadJob();
12791306
await expect(
@@ -1304,7 +1331,7 @@ describe('getMultipartUploadHandlers with path', () => {
13041331
bucket: 'default-bucket',
13051332
},
13061333
},
1307-
byteLength(mockData),
1334+
byteLength(mockData)!,
13081335
);
13091336
await multipartUploadJob();
13101337
await expect(
@@ -1596,10 +1623,13 @@ describe('getMultipartUploadHandlers with path', () => {
15961623

15971624
describe('cancel()', () => {
15981625
it('should abort in-flight uploadPart requests and throw if upload is canceled', async () => {
1599-
const { multipartUploadJob, onCancel } = getMultipartUploadHandlers({
1600-
path: testPath,
1601-
data: new ArrayBuffer(8 * MB),
1602-
});
1626+
const { multipartUploadJob, onCancel } = getMultipartUploadHandlers(
1627+
{
1628+
path: testPath,
1629+
data: new ArrayBuffer(8 * MB),
1630+
},
1631+
8 * MB,
1632+
);
16031633
let partCount = 0;
16041634
mockMultipartUploadCancellation(() => {
16051635
partCount++;

0 commit comments

Comments
 (0)