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

fix(storage): Fix MD5 calculation #13458

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

cshfang
Copy link
Contributor

@cshfang cshfang commented Jun 2, 2024

Description of changes

When isObjectLockEnabled is set to true in configuration, the Amplify library calculates and appends ContentMD5 automatically. However, as a customer discovered, this MD5 calculation was not always being performed correctly and the upload task results in a BadDigest error from the service.

Upon investigation, it appears that the category went under a refactor which saw the MD5 calculations utilities be renamed and relocated. However, during this refactor, the implementations intended for web and native were accidentally flipped:

  • MD5Utils -> utils/md5.native
  • MD5Utils.native -> utils/md5

After undoing this switch, the library behaved as expected on Web but the native implementations continued to fail. Upon further deep dive, it was discovered that the readAsArrayBuffer gap which necessitated the diverged native path in the first place is only experienced in React Native version < 0.71.

This PR:

  • Unflips the web implementation for MD5 calculation
  • Wraps the attempt to use readAsArrayBuffer in a try/catch and only invokes the readAsDataURL strategy as a fallback if the prior function fails
  • Adds unit tests to prevent future regressions

Issue #, if available

#13434

Description of how you validated changes

Validated that MD5 calculation was not working in sample apps prior to changes but now work with the proposed fix for:

  • React web app
  • React native 0.74 app (current)
  • React native 0.71 app (latest version to not support readAsArrayBuffer in its FileReader)

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@cshfang cshfang requested a review from a team as a code owner June 2, 2024 22:42
@cshfang cshfang mentioned this pull request Jun 2, 2024
4 tasks
Copy link
Contributor

@erinleigh90 erinleigh90 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@jimblanc jimblanc left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good

// The FileReader in React Native 0.71 did not support `readAsArrayBuffer`. This native implementation accomodates this
// by attempting to use `readAsArrayBuffer` and changing the file reading strategy if it throws an error.
// TODO: This file should be removable when we drop support for React Native 0.71
describe('calculateContentMd5 (native)', () => {
Copy link
Contributor

@jimblanc jimblanc Jun 3, 2024

Choose a reason for hiding this comment

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

Thank you for the thorough testing on this PR ❤️

Copy link
Contributor

Choose a reason for hiding this comment

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

+++

@cshfang cshfang merged commit 30b5b85 into aws-amplify:main Jun 3, 2024
31 checks passed
@cshfang cshfang deleted the fix/md5-calculation branch June 3, 2024 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants