Repository navigation
fix(delete-user-data): enforce path boundary on Cloud Storage deletion #3369
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f0723c4
fix(delete-user-data): enforce path boundary on Cloud Storage deletion
avolkovi 1d72c29
fix(delete-user-data): address review feedback and formatting
avolkovi 5809cef
fix(delete-user-data): address review comments on logging, bucket loo…
avolkovi 0eb585c
chore(delete-user-data): bump version to 0.1.32 and update CHANGELOG
avolkovi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
261 changes: 261 additions & 0 deletions
261
delete-user-data/functions/__tests__/clearStorageData.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as admin from "firebase-admin"; | ||
| import firebaseFunctionsTest from "firebase-functions-test"; | ||
|
|
||
| interface MockConfig { | ||
| location: string; | ||
| firestorePaths: string | undefined; | ||
| firestoreDeleteMode: string; | ||
| rtdbPaths: string | undefined; | ||
| storagePaths: string; | ||
| enableSearch: boolean; | ||
| storageBucketDefault: string; | ||
| selectedDatabaseInstance: string | undefined; | ||
| selectedDatabaseLocation: string | undefined; | ||
| searchFields: string; | ||
| searchFunction: string | undefined; | ||
| discoveryTopic: string; | ||
| deletionTopic: string; | ||
| searchDepth: number; | ||
| databaseId: string; | ||
| } | ||
|
|
||
| const mockConfig: MockConfig = { | ||
| location: "us-central1", | ||
| firestorePaths: undefined, | ||
| firestoreDeleteMode: "shallow", | ||
| rtdbPaths: undefined, | ||
| storagePaths: "", | ||
| enableSearch: false, | ||
| storageBucketDefault: "default-bucket", | ||
| selectedDatabaseInstance: undefined, | ||
| selectedDatabaseLocation: undefined, | ||
| searchFields: "uid", | ||
| searchFunction: undefined, | ||
| discoveryTopic: "ext-test-discovery", | ||
| deletionTopic: "ext-test-deletion", | ||
| searchDepth: 3, | ||
| databaseId: "(default)", | ||
| }; | ||
|
|
||
| jest.mock("../src/config", () => ({ | ||
| __esModule: true, | ||
| default: mockConfig, | ||
| })); | ||
|
|
||
| import { clearData } from "../src"; | ||
| import * as logs from "../src/logs"; | ||
|
|
||
| class FakeBucket { | ||
| public readonly objects: Set<string>; | ||
| private readonly errorToThrow?: Error & { code?: number }; | ||
|
|
||
| constructor( | ||
| initialObjects: string[], | ||
| errorToThrow?: Error & { code?: number } | ||
| ) { | ||
| this.objects = new Set(initialObjects); | ||
| this.errorToThrow = errorToThrow; | ||
| } | ||
|
|
||
| file(name: string) { | ||
| return { | ||
| delete: async (options?: { ignoreNotFound?: boolean }) => { | ||
| if (this.errorToThrow) { | ||
| throw this.errorToThrow; | ||
| } | ||
| if (this.objects.has(name)) { | ||
| this.objects.delete(name); | ||
| return; | ||
| } | ||
| if (!options?.ignoreNotFound) { | ||
| const err: Error & { code?: number } = new Error( | ||
| `No such object: ${name}` | ||
| ); | ||
| err.code = 404; | ||
| throw err; | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async deleteFiles(query: { prefix?: string } = {}) { | ||
| if (this.errorToThrow) { | ||
| throw this.errorToThrow; | ||
| } | ||
| const prefix = query.prefix ?? ""; | ||
| for (const key of Array.from(this.objects)) { | ||
| if (key.startsWith(prefix)) { | ||
| this.objects.delete(key); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const testEnv = firebaseFunctionsTest(); | ||
| const wrappedClearData = testEnv.wrap(clearData); | ||
|
|
||
| describe("clearStorageData boundary enforcement", () => { | ||
| let buckets: Record<string, FakeBucket>; | ||
| let storageSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| mockConfig.storagePaths = ""; | ||
| buckets = { | ||
| "default-bucket": new FakeBucket([ | ||
| "media/1/photo.jpg", | ||
| "media/1/nested/doc.pdf", | ||
| "media/12/photo.jpg", | ||
| "media/100/photo.jpg", | ||
| "media/1.png", | ||
| "1-pic.png", | ||
| "1-pic.png.bak", | ||
| "12-pic.png", | ||
| "secrets/1.json", | ||
| "secrets/12.json", | ||
| ]), | ||
| "other-bucket": new FakeBucket([ | ||
| "1-logs.txt", | ||
| "12-logs.txt", | ||
| "uploads/1/file.txt", | ||
| "uploads/12/file.txt", | ||
| ]), | ||
| }; | ||
|
|
||
| storageSpy = jest.spyOn(admin, "storage").mockReturnValue({ | ||
| bucket: (name: string) => buckets[name], | ||
| } as unknown as admin.storage.Storage); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| storageSpy.mockRestore(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| testEnv.cleanup(); | ||
| }); | ||
|
|
||
| test("directory path without trailing slash does not delete other users sharing UID prefix", async () => { | ||
| mockConfig.storagePaths = "{DEFAULT}/media/{UID}"; | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "1" })); | ||
|
|
||
| expect(buckets["default-bucket"].objects.has("media/1/photo.jpg")).toBe( | ||
| false | ||
| ); | ||
| expect( | ||
| buckets["default-bucket"].objects.has("media/1/nested/doc.pdf") | ||
| ).toBe(false); | ||
| expect(buckets["default-bucket"].objects.has("media/12/photo.jpg")).toBe( | ||
| true | ||
| ); | ||
| expect(buckets["default-bucket"].objects.has("media/100/photo.jpg")).toBe( | ||
| true | ||
| ); | ||
| expect(buckets["default-bucket"].objects.has("media/1.png")).toBe(true); | ||
| }); | ||
|
|
||
| test("directory path with trailing slash deletes only the user directory", async () => { | ||
| mockConfig.storagePaths = "{DEFAULT}/media/{UID}/"; | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "1" })); | ||
|
|
||
| expect(buckets["default-bucket"].objects.has("media/1/photo.jpg")).toBe( | ||
| false | ||
| ); | ||
| expect( | ||
| buckets["default-bucket"].objects.has("media/1/nested/doc.pdf") | ||
| ).toBe(false); | ||
| expect(buckets["default-bucket"].objects.has("media/12/photo.jpg")).toBe( | ||
| true | ||
| ); | ||
| expect(buckets["default-bucket"].objects.has("media/1.png")).toBe(true); | ||
| }); | ||
|
|
||
| test("flat file paths still delete exact user files without deleting sibling prefixes", async () => { | ||
| mockConfig.storagePaths = | ||
| "{DEFAULT}/{UID}-pic.png,{DEFAULT}/secrets/{UID}.json,other-bucket/{UID}-logs.txt"; | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "1" })); | ||
|
|
||
| expect(buckets["default-bucket"].objects.has("1-pic.png")).toBe(false); | ||
| expect(buckets["default-bucket"].objects.has("1-pic.png.bak")).toBe(true); | ||
| expect(buckets["default-bucket"].objects.has("12-pic.png")).toBe(true); | ||
| expect(buckets["default-bucket"].objects.has("secrets/1.json")).toBe(false); | ||
| expect(buckets["default-bucket"].objects.has("secrets/12.json")).toBe(true); | ||
| expect(buckets["other-bucket"].objects.has("1-logs.txt")).toBe(false); | ||
| expect(buckets["other-bucket"].objects.has("12-logs.txt")).toBe(true); | ||
| }); | ||
|
|
||
| test("empty bucket, empty prefix, or trailing comma does not throw or wipe the bucket", async () => { | ||
| mockConfig.storagePaths = | ||
| "/media/{UID},{DEFAULT}/,other-bucket/uploads/{UID},"; | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "1" })); | ||
|
|
||
| expect(buckets["default-bucket"].objects.size).toBe(10); | ||
| expect(buckets["other-bucket"].objects.has("uploads/1/file.txt")).toBe( | ||
| false | ||
| ); | ||
| expect(buckets["other-bucket"].objects.has("uploads/12/file.txt")).toBe( | ||
| true | ||
| ); | ||
| }); | ||
|
|
||
| test("empty uid warns and does not delete any objects", async () => { | ||
| mockConfig.storagePaths = "{DEFAULT}/media/{UID}"; | ||
| const warnSpy = jest.spyOn(logs, "warnMissingUid"); | ||
| const completeSpy = jest.spyOn(logs, "complete"); | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "" })); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(completeSpy).not.toHaveBeenCalled(); | ||
| expect(buckets["default-bucket"].objects.size).toBe(10); | ||
|
|
||
| warnSpy.mockRestore(); | ||
| completeSpy.mockRestore(); | ||
| }); | ||
|
|
||
| test("logs 404 and non-404 storage errors without aborting other paths", async () => { | ||
| const notFoundErr: Error & { code?: number } = new Error("Bucket missing"); | ||
| notFoundErr.code = 404; | ||
| const permErr: Error & { code?: number } = new Error("Permission denied"); | ||
| permErr.code = 403; | ||
|
|
||
| buckets["missing-bucket"] = new FakeBucket([], notFoundErr); | ||
| buckets["forbidden-bucket"] = new FakeBucket([], permErr); | ||
|
|
||
| const log404Spy = jest.spyOn(logs, "storagePath404"); | ||
| const logErrSpy = jest.spyOn(logs, "storagePathError"); | ||
|
|
||
| mockConfig.storagePaths = | ||
| "missing-bucket/media/{UID},forbidden-bucket/media/{UID},other-bucket/uploads/{UID}"; | ||
|
|
||
| await wrappedClearData(testEnv.auth.makeUserRecord({ uid: "1" })); | ||
|
|
||
| expect(log404Spy).toHaveBeenCalledWith("media/1"); | ||
| expect(logErrSpy).toHaveBeenCalledWith("media/1", permErr); | ||
| expect(buckets["other-bucket"].objects.has("uploads/1/file.txt")).toBe( | ||
| false | ||
| ); | ||
|
|
||
| log404Spy.mockRestore(); | ||
| logErrSpy.mockRestore(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.