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

Firestore: Adjust rules for campaigns #961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ service cloud.firestore {
match /world-development-indicators/{data} {
allow read: if true;
}

match /campaigns/{document=**} {
allow create: if true;
}
Comment on lines +83 to +85
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add authentication and validation checks for campaign creation

The current rule allows anyone to create campaigns without authentication or data validation, which could lead to spam or abuse.

Consider this safer alternative:

    match /campaigns/{document=**} {
-     allow create: if true;
+     allow create: if request.auth != null 
+       && request.resource.data.keys().hasAll(['title', 'description'])
+       && request.resource.data.title is string
+       && request.resource.data.title.size() > 0;
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
match /campaigns/{document=**} {
allow create: if true;
}
match /campaigns/{document=**} {
allow create: if request.auth != null
&& request.resource.data.keys().hasAll(['title', 'description'])
&& request.resource.data.title is string
&& request.resource.data.title.size() > 0;
}

}

match /databases/{database}/documents {
Expand Down
15 changes: 14 additions & 1 deletion shared/tests/firestore.rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
RulesTestEnvironment,
} from '@firebase/rules-unit-testing';
import { describe, expect } from '@jest/globals';
import { DocumentData } from 'firebase-admin/firestore';
import firebase from 'firebase/compat/app';
import { collection, doc, getDoc, getDocs, query, setDoc } from 'firebase/firestore';
import { addDoc, collection, doc, getDoc, getDocs, query, setDoc } from 'firebase/firestore';
import * as fs from 'fs';
import * as path from 'path';
import { AdminUser } from '../src/types/admin-user';
import { CAMPAIGN_FIRESTORE_PATH } from '../src/types/campaign';
import { USER_FIRESTORE_PATH } from '../src/types/user';

let testEnvironment: RulesTestEnvironment;
Expand Down Expand Up @@ -113,6 +115,17 @@ describe('Test user access', () => {
});
});

describe('Test campaign rules', () => {
it('Permission to create campaigns, but not reading them', async () => {
const ref = await addDoc(collection(userAppAccess, CAMPAIGN_FIRESTORE_PATH), {
title: 'TestCampaign',
} as DocumentData);
expect(ref.id).toBeDefined();

await assertFails(getDoc(doc(userAppAccess, CAMPAIGN_FIRESTORE_PATH, ref.id)));
});
});
Comment on lines +118 to +127
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for campaign rules

The current test suite only covers basic create/read scenarios. Add tests for:

  1. Unauthenticated access
  2. Invalid campaign data
  3. Subcollection access
describe('Test campaign rules', () => {
  it('Unauthenticated users cannot create campaigns', async () => {
    const unauthStore = testEnvironment.unauthenticatedContext().firestore();
    await assertFails(addDoc(collection(unauthStore, CAMPAIGN_FIRESTORE_PATH), {
      title: 'TestCampaign',
    }));
  });

  it('Rejects invalid campaign data', async () => {
    await assertFails(addDoc(collection(userAppAccess, CAMPAIGN_FIRESTORE_PATH), {
      invalid_field: true,
    }));
  });
});


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