From d944ce595f51bf41365ac4684de3b8ba35552f41 Mon Sep 17 00:00:00 2001 From: ashika112 Date: Tue, 12 Mar 2024 16:03:44 -0700 Subject: [PATCH 1/4] add deprecation warning --- packages/core/src/singleton/Storage/types.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/core/src/singleton/Storage/types.ts b/packages/core/src/singleton/Storage/types.ts index 3245a008989..4a206399f19 100644 --- a/packages/core/src/singleton/Storage/types.ts +++ b/packages/core/src/singleton/Storage/types.ts @@ -20,6 +20,7 @@ export interface S3ProviderConfig { export type StorageConfig = AtLeastOne; +/** @deprecated This may be removed in the next major version. */ type StoragePrefixResolver = (params: { accessLevel: StorageAccessLevel; targetIdentityId?: string; @@ -27,7 +28,15 @@ type StoragePrefixResolver = (params: { export interface LibraryStorageOptions { S3: { + /** + * @deprecated This may be removed in the next major version. + * This will be used for storage API signature using key as input parameter. + * */ prefixResolver?: StoragePrefixResolver; + /** + * @deprecated This may be removed in the next major version. + * This will be used for storage API signature using key as input parameter. + * */ defaultAccessLevel?: StorageAccessLevel; isObjectLockEnabled?: boolean; }; From fc817f36bc5d778b105e94bf2b0bc95a7988012c Mon Sep 17 00:00:00 2001 From: ashika112 Date: Wed, 13 Mar 2024 10:54:06 -0700 Subject: [PATCH 2/4] add input validation on path --- .../utils/validateStorageOperationInput.test.ts | 17 +++++++++++++---- packages/storage/src/errors/types/validation.ts | 10 +++++++--- .../s3/utils/validateStorageOperationInput.ts | 7 ++++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/storage/__tests__/providers/s3/apis/utils/validateStorageOperationInput.test.ts b/packages/storage/__tests__/providers/s3/apis/utils/validateStorageOperationInput.test.ts index 83388bd3457..684fec06544 100644 --- a/packages/storage/__tests__/providers/s3/apis/utils/validateStorageOperationInput.test.ts +++ b/packages/storage/__tests__/providers/s3/apis/utils/validateStorageOperationInput.test.ts @@ -13,23 +13,23 @@ import { describe('validateStorageOperationInput', () => { it('should return inputType as STORAGE_INPUT_PATH and objectKey as testPath when input is path as string', () => { - const input = { path: 'testPath' }; + const input = { path: '/testPath' }; const result = validateStorageOperationInput(input); expect(result).toEqual({ inputType: STORAGE_INPUT_PATH, - objectKey: 'testPath', + objectKey: '/testPath', }); }); it('should return inputType as STORAGE_INPUT_PATH and objectKey as result of path function when input is path as function', () => { const input = { path: ({ identityId }: { identityId?: string }) => - `testPath/${identityId}`, + `/testPath/${identityId}`, }; const result = validateStorageOperationInput(input, '123'); expect(result).toEqual({ inputType: STORAGE_INPUT_PATH, - objectKey: 'testPath/123', + objectKey: '/testPath/123', }); }); @@ -42,6 +42,15 @@ describe('validateStorageOperationInput', () => { }); }); + it('should throw an error when input path does not start with a /', () => { + const input = { path: 'test' } as any; + expect(() => validateStorageOperationInput(input)).toThrow( + validationErrorMap[ + StorageValidationErrorCode.InvalidStoragePathInput + ].message, + ); + }); + it('should throw an error when input is invalid', () => { const input = { invalid: 'test' } as any; expect(() => validateStorageOperationInput(input)).toThrow( diff --git a/packages/storage/src/errors/types/validation.ts b/packages/storage/src/errors/types/validation.ts index d63e3d21b34..4c03e1f0c03 100644 --- a/packages/storage/src/errors/types/validation.ts +++ b/packages/storage/src/errors/types/validation.ts @@ -11,10 +11,11 @@ export enum StorageValidationErrorCode { NoDestinationKey = 'NoDestinationKey', NoBucket = 'NoBucket', NoRegion = 'NoRegion', - UrlExpirationMaxLimitExceed = 'UrlExpirationMaxLimitExceed', - ObjectIsTooLarge = 'ObjectIsTooLarge', - InvalidUploadSource = 'InvalidUploadSource', InvalidStorageOperationInput = 'InvalidStorageOperationInput', + InvalidStoragePathInput = 'InvalidStoragePathInput', + InvalidUploadSource = 'InvalidUploadSource', + ObjectIsTooLarge = 'ObjectIsTooLarge', + UrlExpirationMaxLimitExceed = 'UrlExpirationMaxLimitExceed', } export const validationErrorMap: AmplifyErrorMap = { @@ -53,4 +54,7 @@ export const validationErrorMap: AmplifyErrorMap = { [StorageValidationErrorCode.InvalidStorageOperationInput]: { message: 'Missing path or key parameter in Input', }, + [StorageValidationErrorCode.InvalidStoragePathInput]: { + message: 'Input `path` is missing a leading slash (/).', + }, }; diff --git a/packages/storage/src/providers/s3/utils/validateStorageOperationInput.ts b/packages/storage/src/providers/s3/utils/validateStorageOperationInput.ts index 038df38f931..b8f40cbe60a 100644 --- a/packages/storage/src/providers/s3/utils/validateStorageOperationInput.ts +++ b/packages/storage/src/providers/s3/utils/validateStorageOperationInput.ts @@ -29,10 +29,15 @@ export const validateStorageOperationInput = ( if (isInputWithPath(input)) { const { path } = input; + const objectKey = typeof path === 'string' ? path : path({ identityId }); + assertValidationError( + objectKey.startsWith('/'), + StorageValidationErrorCode.InvalidStoragePathInput, + ); return { inputType: STORAGE_INPUT_PATH, - objectKey: typeof path === 'string' ? path : path({ identityId }), + objectKey, }; } else { return { inputType: STORAGE_INPUT_KEY, objectKey: input.key }; From 9ada5860a7da11a0df50995f4283394c47db2371 Mon Sep 17 00:00:00 2001 From: ashika112 Date: Wed, 13 Mar 2024 11:40:46 -0700 Subject: [PATCH 3/4] misc changes --- packages/core/src/singleton/Storage/types.ts | 5 +++-- .../src/providers/s3/utils/resolveS3ConfigAndInput.ts | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/singleton/Storage/types.ts b/packages/core/src/singleton/Storage/types.ts index 4a206399f19..b21413a797a 100644 --- a/packages/core/src/singleton/Storage/types.ts +++ b/packages/core/src/singleton/Storage/types.ts @@ -3,6 +3,7 @@ import { AtLeastOne } from '../types'; +/** @deprecated This may be removed in the next major version. */ export type StorageAccessLevel = 'guest' | 'protected' | 'private'; export interface S3ProviderConfig { @@ -30,12 +31,12 @@ export interface LibraryStorageOptions { S3: { /** * @deprecated This may be removed in the next major version. - * This will be used for storage API signature using key as input parameter. + * This is currently used for Storage API signature using key as input parameter. * */ prefixResolver?: StoragePrefixResolver; /** * @deprecated This may be removed in the next major version. - * This will be used for storage API signature using key as input parameter. + * This is currently used for Storage API signature using key as input parameter. * */ defaultAccessLevel?: StorageAccessLevel; isObjectLockEnabled?: boolean; diff --git a/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts b/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts index 8eb28ef8681..701c046d52f 100644 --- a/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts +++ b/packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts @@ -5,7 +5,6 @@ import { AmplifyClassV6, StorageAccessLevel } from '@aws-amplify/core'; import { assertValidationError } from '../../../errors/utils/assertValidationError'; import { StorageValidationErrorCode } from '../../../errors/types/validation'; -import { StorageError } from '../../../errors/StorageError'; import { resolvePrefix as defaultPrefixResolver } from '../../../utils/resolvePrefix'; import { ResolvedS3Config } from '../types/options'; @@ -31,7 +30,7 @@ interface ResolvedS3ConfigAndInput { * @param {AmplifyClassV6} amplify The Amplify instance. * @param {S3ApiOptions} apiOptions The input options for S3 provider. * @returns {Promise} The resolved common input options for S3 API handlers. - * @throws A {@link StorageError} with `error.name` from {@link StorageValidationErrorCode} indicating invalid + * @throws A `StorageError` with `error.name` from `StorageValidationErrorCode` indicating invalid * configurations or Amplify library options. * * @internal From ce075bb6058ccfcbcf1969ad5aa82be264a0d541 Mon Sep 17 00:00:00 2001 From: ashika112 Date: Wed, 13 Mar 2024 11:50:13 -0700 Subject: [PATCH 4/4] update bundle size --- packages/aws-amplify/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index b4f4be3f1b7..5b037dd55f8 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -466,7 +466,7 @@ "name": "[Storage] downloadData (S3)", "path": "./dist/esm/storage/index.mjs", "import": "{ downloadData }", - "limit": "14.10 kB" + "limit": "14.15 kB" }, { "name": "[Storage] getProperties (S3)",