Skip to content

Commit

Permalink
Test and deploy storage rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sbutz committed Dec 26, 2023
1 parent f2c973a commit 6c13c7c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/deploy-on-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ on:
branches:
- master
jobs:
firestore_deploy:
rules_deploy:
runs-on: ubuntu-latest
env:
working-directory: ./firestore
working-directory: ./firebase
GOOGLE_APPLICATION_CREDENTIALS: /opt/gcp_key.json
steps:
- uses: actions/checkout@v3
Expand All @@ -20,7 +20,7 @@ jobs:
working-directory: ${{ env.working-directory }}

functions_deploy:
needs: firestore_deploy
needs: rules_deploy
runs-on: ubuntu-latest
env:
working-directory: ./functions
Expand All @@ -35,7 +35,7 @@ jobs:
working-directory: ${{ env.working-directory }}

client_deploy:
needs: [firestore_deploy, functions_deploy]
needs: [rules_deploy, functions_deploy]
runs-on: ubuntu-latest
env:
working-directory: ./client
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/ImageUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function ImageUpload({ label, value, onUpload: onChange }: InputBaseComponentPro
};

const onSave = () => {
// TODO: scale to minSizeLength
// TODO: scale to same size
// should do via
onChange(image);
setSaved(true);
};
Expand Down
6 changes: 3 additions & 3 deletions firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"author": "",
"license": "ISC",
"scripts": {
"deploy": "npx firebase-tools deploy --only firestore:rules",
"test": "npx firebase-tools emulators:exec --only firestore 'jest -i'",
"deploy": "npx firebase-tools deploy --only 'firestore:rules,storage:rules",
"test": "npx firebase-tools emulators:exec --only 'firestore,storage' 'jest -i'",
"export": "npx firebase-tools emulators:export ./emulator_data",
"lint": "eslint test/"
"lint": "eslint src"
},
"devDependencies": {
"@firebase/rules-unit-testing": "^2.0.7",
Expand Down
4 changes: 4 additions & 0 deletions firebase/src/firestore/default_deny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ beforeAll(async () => {
});
});

afterEach(async () => {
await testEnv.clearFirestore();
});

afterAll(async () => {
await testEnv.cleanup();
});
Expand Down
36 changes: 36 additions & 0 deletions firebase/src/storage/default_deny.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { readFileSync } from 'fs';
import { assertFails, initializeTestEnvironment, RulesTestEnvironment } from '@firebase/rules-unit-testing';
import { uploadString, getBytes } from 'firebase/storage';

let testEnv: RulesTestEnvironment;
const clubId = 'bc73';
const fileName = 'foo.txt';

beforeAll(async () => {
testEnv = await initializeTestEnvironment({
storage: {
rules: readFileSync('storage.rules', 'utf8'),
},
});
});

afterEach(async () => {
await testEnv.clearStorage();
});

afterAll(async () => {
await testEnv.cleanup();
});

it('should forbid abitrary read/write access', async () => {
const unauthenticatedContext = testEnv.unauthenticatedContext();
await assertFails(uploadString(unauthenticatedContext.storage().ref(fileName), '123'));

const authenticatedContext = testEnv.authenticatedContext('alice', { clubId, admin: true });
await assertFails(uploadString(authenticatedContext.storage().ref(fileName), '123'));

await testEnv.withSecurityRulesDisabled(async (context) => {
await uploadString(context.storage().ref(fileName), '123');
});
await assertFails(getBytes(authenticatedContext.storage().ref(fileName)));
});
4 changes: 0 additions & 4 deletions firebase/storage.rules
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
rules_version = '2';

// TODO: create folder per club
// - logo_512x512.png
// TODO: club delete -> delete folder
// TODO: write tests for storage rules
service firebase.storage {
match /b/{bucket}/o {
function isAuthenticated() {
Expand Down

0 comments on commit 6c13c7c

Please sign in to comment.