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 Emulator crash on iOS auth error for resumable uploads #4210

Merged
merged 6 commits into from Mar 1, 2022

Conversation

tohhsinpei
Copy link
Member

@tohhsinpei tohhsinpei commented Feb 22, 2022

Description

Issue(s)

Follow up to #4184. This PR fixes #3550, #3584, #3765, and #3890.

Previously, we fixed the issue where the iOS client receives a permissions error despite being authenticated to make a write. However, we still need to address the issue where the Storage Emulator crashes on any such permissions error.

Cause

When the upload request is finalised, the file is moved. When the iOS client subsequently receives a 403, it cancels the request. During the cancel, the emulator attempts to delete the file at its old temporary location, resulting in the following error:

Error: ENOENT: no such file or directory, unlink '/var/folders/jt/rrd3wxwj09bd2twr6cxdx1z80000gn/T/firebase/storage/blobs/d08a3410-0980-4fbe-b46c-eb4111ad46e9_b_my-new-app.appspot.com_o_aGtgkgT04cudlZXaPFC72R9tZMlv%2FINSPJ6MFM%2FjxRlG6mkqJAesWsyBpYb%2F1625387702%2Fimg%2F16253877290.jpeg'
    at Object.unlinkSync (node:fs:1708:3)
    at Persistence.deleteFile (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/files.js:478:18)
    at StorageLayer.cancelUpload (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/files.js:152:27)
    at handleUpload (/usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/apis/firebase.js:366:45)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/storage/apis/firebase.js:443:24

Scenarios Tested

Tested against example linked in issue and verified that crash does not occur after fix. Also tested with storage rules that always give an auth error, i.e. allow read, write: if false.

@tohhsinpei tohhsinpei changed the title Fix Storage Emulator crash on iOS auth for resumable uploads Fix Storage Emulator crash on iOS auth error for resumable uploads Feb 22, 2022
@tonyjhuang
Copy link
Contributor

Capturing some of what we synced on offline:

There seems to be a bigger bug in our handling of finalize upload requests. Namely, we call StorageLayer.finalizeUpload before we evaluate rules which marks the upload as successful and triggers any CREATE Cloud Functions. Additionally, using StorageLayer.deleteFile to clean up our persistence layer if auth fails has the unintended side effect of triggering any DELETE Cloud Functions.

My take is that we probably want to refactor the finalize upload handler in firebase.ts to only call StorageLayer.finalizeUpload after rules evaluation. Unfortunately, it seems that we use StorageLayer.getMetadata to pass some metadata to rules and in its current form, it requires the file be persisted to the internal memory store (StorageLayer._files), so some additional refactoring may be required.

scripts/storage-emulator-integration/tests.ts Outdated Show resolved Hide resolved
scripts/storage-emulator-integration/tests.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
scripts/storage-emulator-integration/tests.ts Outdated Show resolved Hide resolved
scripts/storage-emulator-integration/tests.ts Show resolved Hide resolved
scripts/storage-emulator-integration/tests.ts Outdated Show resolved Hide resolved
src/emulator/storage/apis/firebase.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
@tohhsinpei tohhsinpei force-pushed the hsinpei/storage-403-crash branch 2 times, most recently from 505338b to 9d446e6 Compare February 26, 2022 00:18
Copy link
Contributor

@tonyjhuang tonyjhuang left a comment

Choose a reason for hiding this comment

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

Overall lgtm, just a few small remaining comments

scripts/storage-emulator-integration/tests.ts Outdated Show resolved Hide resolved
})
.expect(200);

await supertest(STORAGE_EMULATOR_HOST)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add an assertion in this test case or another that ensures the querying for the upload after a cancel denotes that the upload was cancelled?

go/unified-upload-protocols#ended-uploads

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure we're doing this now actually 😅 But I would fix this in another PR.

src/test/emulators/storage/files.spec.ts Outdated Show resolved Hide resolved
src/test/emulators/storage/files.spec.ts Show resolved Hide resolved
src/emulator/storage/apis/firebase.ts Show resolved Hide resolved
src/emulator/storage/files.ts Outdated Show resolved Hide resolved
src/emulator/storage/files.ts Show resolved Hide resolved
Copy link
Contributor

@tonyjhuang tonyjhuang left a comment

Choose a reason for hiding this comment

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

Thanks for the fix

@tohhsinpei tohhsinpei merged commit 82c8da5 into master Mar 1, 2022
@tohhsinpei tohhsinpei deleted the hsinpei/storage-403-crash branch March 1, 2022 20:26
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.

Emulator Suite Crashes with Unexpected Error on Uploading Photo to Storage Emulator
3 participants