Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Prefix resolver & Path input validation #13113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/singleton/Storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,14 +21,23 @@ export interface S3ProviderConfig {

export type StorageConfig = AtLeastOne<S3ProviderConfig>;

/** @deprecated This may be removed in the next major version. */
type StoragePrefixResolver = (params: {
accessLevel: StorageAccessLevel;
targetIdentityId?: string;
}) => Promise<string>;

export interface LibraryStorageOptions {
S3: {
/**
* @deprecated This may be removed in the next major version.
* 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 is currently used for Storage API signature using key as input parameter.
* */
defaultAccessLevel?: StorageAccessLevel;
isObjectLockEnabled?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});

Expand All @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions packages/storage/src/errors/types/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageValidationErrorCode> = {
Expand Down Expand Up @@ -53,4 +54,7 @@ export const validationErrorMap: AmplifyErrorMap<StorageValidationErrorCode> = {
[StorageValidationErrorCode.InvalidStorageOperationInput]: {
message: 'Missing path or key parameter in Input',
},
[StorageValidationErrorCode.InvalidStoragePathInput]: {
message: 'Input `path` is missing a leading slash (/).',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -31,7 +30,7 @@ interface ResolvedS3ConfigAndInput {
* @param {AmplifyClassV6} amplify The Amplify instance.
* @param {S3ApiOptions} apiOptions The input options for S3 provider.
* @returns {Promise<ResolvedS3ConfigAndInput>} 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Loading