-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
📝 WalkthroughWalkthroughThe changes introduce a new match rule in the Firestore security rules for the Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
firestore.rules (1)
83-85
: Consider adding separate rules for subcollectionsThe
{document=**}
pattern grants the same permissions to all subcollections. If campaigns will have subcollections in the future, they might need different access rules.
match /campaigns/{document=**} { | ||
allow create: if true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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; | |
} |
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))); | ||
}); | ||
}); |
There was a problem hiding this comment.
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:
- Unauthenticated access
- Invalid campaign data
- 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,
}));
});
});
Visit the preview URL for this PR (updated for commit 241b15c): https://si-admin-staging--pr961-ahee-campaign-fireba-opqyc8f1.web.app (expires Sun, 08 Dec 2024 11:08:19 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: b7b0969384059dce6ea8fad1ee1d1737e54e6676 |
To get started with creating our own campaigns, it feels easiest to me, that we directly submit the filled out form to firestore, instead of looping it through a dedicated API backend.
I'm not giving any edit, read permissions. We can change that later down the road if we ever want to support that creators can edit their campaign.
Summary by CodeRabbit
New Features
campaigns
collection.Tests