Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(storage): omit unserializable properties when hash uploadDataOptions #14151

Merged
merged 6 commits into from
Jan 23, 2025
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 @@ -515,7 +515,7 @@
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "22.87 kB"
"limit": "22.95 kB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ const bucket = 'bucket';
const region = 'region';
const defaultKey = 'key';
const defaultContentType = 'application/octet-stream';
const defaultCacheKey =
'Jz3O2w==_8388608_application/octet-stream_bucket_public_key';
const emptyOptionHash = 'o6a/Qw=='; // crc32 for '{}'
const defaultCacheKey = `${emptyOptionHash}_8388608_application/octet-stream_bucket_public_key`;
const testPath = 'testPath/object';
const testPathCacheKey = `Jz3O2w==_8388608_${defaultContentType}_${bucket}_custom_${testPath}`;
const testPathCacheKey = `${emptyOptionHash}_8388608_${defaultContentType}_${bucket}_custom_${testPath}`;

const mockCreateMultipartUpload = jest.mocked(createMultipartUpload);
const mockUploadPart = jest.mocked(uploadPart);
Expand Down Expand Up @@ -545,14 +545,14 @@ describe('getMultipartUploadHandlers with key', () => {
describe('cache validation', () => {
it.each([
{
name: 'wrong part count',
name: 'mismatched part count between cached upload and actual upload',
parts: [{ PartNumber: 1 }, { PartNumber: 2 }, { PartNumber: 3 }],
},
{
name: 'wrong part numbers',
name: 'mismatched part numbers between cached upload and actual upload',
parts: [{ PartNumber: 1 }, { PartNumber: 1 }],
},
])('should throw with $name', async ({ parts }) => {
])('should throw integrity error with $name', async ({ parts }) => {
mockMultipartUploadSuccess();

const mockDefaultStorage = defaultStorage as jest.Mocked<
Expand Down Expand Up @@ -619,6 +619,42 @@ describe('getMultipartUploadHandlers with key', () => {
expect(mockListParts).not.toHaveBeenCalled();
});

it('should omit unserializable option properties when calculating the hash key', async () => {
mockMultipartUploadSuccess();
const serializableOptions = {
useAccelerateEndpoint: true,
bucket: { bucketName: 'bucket', region: 'us-west-2' },
expectedBucketOwner: '123',
contentDisposition: 'attachment',
contentEncoding: 'deflate',
contentType: 'text/html',
customEndpoint: 'abc',
metadata: {},
preventOverwrite: true,
checksumAlgorithm: 'crc-32' as const,
};
const size = 8 * MB;
const { multipartUploadJob } = getMultipartUploadHandlers(
{
key: defaultKey,
data: new ArrayBuffer(size),
options: {
...serializableOptions,
// The following options will be omitted
locationCredentialsProvider: jest.fn(),
onProgress: jest.fn(),
resumableUploadsCache: mockDefaultStorage,
},
},
size,
);
await multipartUploadJob();
expect(mockCalculateContentCRC32).toHaveBeenNthCalledWith(
1,
JSON.stringify(serializableOptions),
);
});

it('should send createMultipartUpload request if the upload task is not cached', async () => {
mockMultipartUploadSuccess();
const size = 8 * MB;
Expand Down Expand Up @@ -693,7 +729,7 @@ describe('getMultipartUploadHandlers with key', () => {
expect(Object.keys(cacheValue)).toEqual([
expect.stringMatching(
// \d{13} is the file lastModified property of a file
/someName_\d{13}_Jz3O2w==_8388608_application\/octet-stream_bucket_public_key/,
new RegExp(`someName_\\d{13}_${defaultCacheKey}`),
),
]);
});
Expand Down Expand Up @@ -1451,6 +1487,42 @@ describe('getMultipartUploadHandlers with path', () => {
expect(mockListParts).not.toHaveBeenCalled();
});

it('should omit unserializable option properties when calculating the hash key', async () => {
mockMultipartUploadSuccess();
const serializableOptions = {
useAccelerateEndpoint: true,
bucket: { bucketName: 'bucket', region: 'us-west-2' },
expectedBucketOwner: '123',
contentDisposition: 'attachment',
contentEncoding: 'deflate',
contentType: 'text/html',
customEndpoint: 'abc',
metadata: {},
preventOverwrite: true,
checksumAlgorithm: 'crc-32' as const,
};
const size = 8 * MB;
const { multipartUploadJob } = getMultipartUploadHandlers(
{
path: testPath,
data: new ArrayBuffer(size),
options: {
...serializableOptions,
// The following options will be omitted
locationCredentialsProvider: jest.fn(),
onProgress: jest.fn(),
resumableUploadsCache: mockDefaultStorage,
},
},
size,
);
await multipartUploadJob();
expect(mockCalculateContentCRC32).toHaveBeenNthCalledWith(
1,
JSON.stringify(serializableOptions),
);
});

it('should send createMultipartUpload request if the upload task is cached but outdated', async () => {
mockDefaultStorage.getItem.mockResolvedValue(
JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { UPLOADS_STORAGE_KEY } from '../../../../utils/constants';
import { ResolvedS3Config } from '../../../../types/options';
import { Part, listParts } from '../../../../utils/client/s3data';
import { logger } from '../../../../../../utils';
// TODO: Remove this interface when we move to public advanced APIs.
import { UploadDataInput as UploadDataWithPathInputWithAdvancedOptions } from '../../../../../../internals/types/inputs';

const ONE_HOUR = 1000 * 60 * 60;

Expand Down Expand Up @@ -96,6 +98,28 @@ const listCachedUploadTasks = async (
}
};

/**
* Serialize the uploadData API options to string so it can be hashed.
*/
export const serializeUploadOptions = (
options: UploadDataWithPathInputWithAdvancedOptions['options'] & {
resumableUploadsCache?: KeyValueStorageInterface;
} = {},
): string => {
const unserializableOptionProperties: string[] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets leave a comment maybe to update this as needed

'onProgress',
'resumableUploadsCache', // Internally injected implementation not set by customers
'locationCredentialsProvider', // Internally injected implementation not set by customers
] satisfies (keyof typeof options)[];
const serializableOptions = Object.fromEntries(
Object.entries(options).filter(
([key]) => !unserializableOptionProperties.includes(key),
),
);

return JSON.stringify(serializableOptions);
};

interface UploadsCacheKeyOptions {
size: number;
contentType?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import { StorageOperationOptionsInput } from '../../../../../../types/inputs';
import { IntegrityError } from '../../../../../../errors/IntegrityError';

import { uploadPartExecutor } from './uploadPartExecutor';
import { getUploadsCacheKey, removeCachedUpload } from './uploadCache';
import {
getUploadsCacheKey,
removeCachedUpload,
serializeUploadOptions,
} from './uploadCache';
import { getConcurrentUploadsProgressTracker } from './progressTracker';
import { loadOrCreateMultipartUpload } from './initialUpload';
import { getDataChunker } from './getDataChunker';
Expand Down Expand Up @@ -151,9 +155,9 @@ export const getMultipartUploadHandlers = (
resolvedAccessLevel = resolveAccessLevel(accessLevel);
}

const optionsHash = (
await calculateContentCRC32(JSON.stringify(uploadDataOptions))
).checksum;
const { checksum: optionsHash } = await calculateContentCRC32(
serializeUploadOptions(uploadDataOptions),
);

if (!inProgressUpload) {
const { uploadId, cachedParts, finalCrc32 } =
Expand Down
Loading