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 rules resource evaluation #4214

Merged
merged 5 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
- Fixes arg order for `firebase emulators:start --only storage` (#4195).
- Fixes bug where environment variable for gen 2 functions weren't updated on deploy (#4209).
- Fixes an issue in the storage emulator where a file upload would trigger functions with a metadata update handler (#4213).
- Fixes Storage Emulator rules resource evaluation (#4214).
3 changes: 2 additions & 1 deletion scripts/storage-emulator-integration/storage.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
allow read, create, update: if request.auth != null;
allow delete: if request.auth != null && resource.contentType != 'text/plain';
}

match /public/{allPaths=**} {
Expand Down
56 changes: 36 additions & 20 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,30 +1187,46 @@ describe("Storage emulator", () => {
});
});

it("#delete()", async () => {
const downloadUrl = await page.evaluate((filename) => {
return firebase.storage().ref(filename).getDownloadURL();
}, filename);
describe("deleteFile", () => {
it("should delete file", async () => {
await page.evaluate((filename) => {
return firebase.storage().ref(filename).delete();
}, filename);

expect(downloadUrl).to.be.not.null;
const error = await page.evaluate((filename) => {
return new Promise((resolve) => {
firebase
.storage()
.ref(filename)
.getDownloadURL()
.catch((err) => {
resolve(err.message);
});
});
}, filename);

await page.evaluate((filename) => {
return firebase.storage().ref(filename).delete();
}, filename);
expect(error).to.contain("does not exist.");
});

const error = await page.evaluate((filename) => {
return new Promise((resolve) => {
firebase
.storage()
.ref(filename)
.getDownloadURL()
.catch((err) => {
resolve(err.message);
});
});
}, filename);
it("should not delete file when security rule on resource object disallows it", async () => {
await page.evaluate((filename) => {
firebase.storage().ref(filename).updateMetadata({ contentType: "text/plain" });
}, filename);

const error = await page.evaluate((filename) => {
return new Promise((resolve) => {
firebase
.storage()
.ref(filename)
.delete()
.catch((err) => {
resolve(err.message);
});
});
}, filename);

expect(error).to.contain("does not exist.");
expect(error).to.contain("does not have permission to access");
});
});
});

Expand Down
13 changes: 8 additions & 5 deletions src/emulator/storage/apis/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,16 +591,21 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {
firebaseStorageAPI.delete("/b/:bucketId/o/:objectId", async (req, res) => {
const decodedObjectId = decodeURIComponent(req.params.objectId);
const operationPath = ["b", req.params.bucketId, "o", decodedObjectId].join("/");
const md = storageLayer.getMetadata(req.params.bucketId, decodedObjectId);

const rulesFiles: { before?: RulesResourceMetadata } = {};

if (md) {
rulesFiles.before = md.asRulesResource();
tohhsinpei marked this conversation as resolved.
Show resolved Hide resolved
}

if (
!(await isPermitted({
ruleset: emulator.rules,
method: RulesetOperationMethod.DELETE,
path: operationPath,
authorization: req.header("authorization"),
file: {
// TODO load before metadata
},
file: rulesFiles,
}))
) {
return res.status(403).json({
Expand All @@ -611,8 +616,6 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {
});
}

const md = storageLayer.getMetadata(req.params.bucketId, decodedObjectId);

if (!md) {
res.sendStatus(404);
return;
Expand Down