-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,14 @@ | ||
import { parseHost } from 'ufo'; | ||
|
||
export function getStorageMeta() { | ||
const { hostname: host, port } = parseHost(process.env.FIREBASE_STORAGE_EMULATOR_HOST); | ||
|
||
if (!host || !port) { | ||
throw new Error('Could not parse host and/or port from FIREBASE_STORAGE_EMULATOR_HOST'); | ||
} | ||
|
||
return { | ||
host, | ||
port: Number(port), | ||
}; | ||
} |
This file contains 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 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,58 @@ | ||
import { | ||
RulesTestEnvironment, | ||
assertFails, | ||
initializeTestEnvironment, | ||
} from '@firebase/rules-unit-testing'; | ||
import { deleteObject, getDownloadURL, ref, updateMetadata } from 'firebase/storage'; | ||
import { readFileSync } from 'node:fs'; | ||
import { afterAll, beforeAll, beforeEach, describe, test } from 'vitest'; | ||
import { getStorageMeta } from '../helpers/storage'; | ||
|
||
const PROJECT_ID = 'demo-test'; // Must match the project name used to start the emulators | ||
|
||
let testEnv: RulesTestEnvironment; | ||
|
||
beforeAll(async () => { | ||
const { host, port } = getStorageMeta(); | ||
testEnv = await initializeTestEnvironment({ | ||
projectId: PROJECT_ID, | ||
storage: { | ||
port, | ||
host, | ||
rules: readFileSync('storage.rules', 'utf8'), | ||
}, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testEnv.cleanup(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testEnv.clearStorage(); | ||
}); | ||
|
||
describe('Storage security rules', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
test('does not allow any reads, writes or deletes to an unused object by an unauthenticated user', async () => { | ||
const storage = testEnv.unauthenticatedContext().storage(); | ||
const objectRef = ref(storage, 'unused.jpg'); | ||
|
||
await assertFails(getDownloadURL(objectRef)); | ||
|
||
await assertFails(updateMetadata(objectRef, { cacheControl: 'public, max-age=300' })); | ||
|
||
await assertFails(deleteObject(objectRef)); | ||
}); | ||
|
||
test('does not allow any reads, writes or deletes to an unused object by an authenticated user', async () => { | ||
const storage = testEnv.authenticatedContext('alice').storage(); | ||
const objectRef = ref(storage, 'unused.jpg'); | ||
|
||
await assertFails(getDownloadURL(objectRef)); | ||
|
||
await assertFails(updateMetadata(objectRef, { cacheControl: 'public, max-age=300' })); | ||
|
||
await assertFails(deleteObject(objectRef)); | ||
}); | ||
}); |