-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
148 lines (130 loc) · 6.05 KB
/
Copy pathfirestore.rules
File metadata and controls
148 lines (130 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ---------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function isPendingUserOfCurrentAuth(pendingUserId) {
return isAuthenticated()
&& request.auth.token.email != null
&& pendingUserId == "pending_" + hashing.sha256(request.auth.token.email.lower()).toHexString().lower();
}
function isMemberOrPending(memberIds) {
return isAuthenticated() && (
request.auth.uid in memberIds ||
(
request.auth.token.email != null &&
("pending_" + hashing.sha256(request.auth.token.email.lower()).toHexString().lower() in memberIds)
)
);
}
// exists() guard handles the case where the group doc is being created
// in the same WriteBatch — get() would fail because the doc doesn't
// exist yet in the committed database state.
function isGroupActiveOrNew(groupId) {
return !exists(/databases/$(database)/documents/groups/$(groupId))
|| get(/databases/$(database)/documents/groups/$(groupId)).data.get("status", "ACTIVE") != "ARCHIVED"
|| get(/databases/$(database)/documents/groups/$(groupId)).data.get("deletionRequested", false) == true;
}
// ---------------------------------------------------------------
// Users
// ---------------------------------------------------------------
match /users/{userId} {
allow read: if isAuthenticated();
allow create, update: if isOwner(userId)
|| isPendingUserOfCurrentAuth(userId)
|| (isAuthenticated() && userId.matches('pending_.*') && request.resource.data.isPending == true);
allow delete: if isOwner(userId)
|| isPendingUserOfCurrentAuth(userId)
|| (isAuthenticated() && userId.matches('pending_.*'));
// Devices subcollection — only the owning user can manage their devices
match /devices/{deviceId} {
allow read, write: if isOwner(userId);
}
}
// ---------------------------------------------------------------
// Collection group queries
// ---------------------------------------------------------------
// The app queries collectionGroup("members") to find all groups
// the current user belongs to (where userId == auth.uid).
match /{path=**}/members/{memberId} {
allow read: if isAuthenticated();
}
// ---------------------------------------------------------------
// Groups
// ---------------------------------------------------------------
// DESIGN DECISION: Subcollection rules (members, expenses, contributions,
// cash_withdrawals, subunits) intentionally use only isAuthenticated()
// without group-level membership checks. Reasons:
// 1. Group membership is enforced at app level (navigation + repository).
// 2. Adding per-document membership reads would significantly increase
// Firestore read costs and introduce latency on every operation.
// 3. The security boundary is auth-level (only signed-in users), not
// group-level. This is an accepted trade-off for this app's model.
// Do NOT add group membership checks to subcollection rules without
// evaluating the cost/performance impact first.
match /groups/{groupId} {
// Any authenticated user can read groups they're a member of
// (membership enforced at app level; rule allows read for now)
allow read: if isAuthenticated();
allow create: if isAuthenticated();
allow update: if isMemberOrPending(resource.data.memberIds)
&& (resource.data.get("status", "ACTIVE") != "ARCHIVED" || request.resource.data.get("deletionRequested", false) == true);
allow delete: if isMemberOrPending(resource.data.memberIds);
// Members subcollection
match /members/{memberId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Expenses subcollection
match /expenses/{expenseId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Contributions subcollection
match /contributions/{contributionId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Cash withdrawals subcollection
match /cash_withdrawals/{withdrawalId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Subunits subcollection
match /subunits/{subunitId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Settlements subcollection
match /settlements/{settlementId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Nudges subcollection
match /nudges/{nudgeId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
// Cash transfers subcollection
match /cash_transfers/{transferId} {
allow read: if isAuthenticated();
allow create, update: if isAuthenticated() && isGroupActiveOrNew(groupId);
allow delete: if isAuthenticated();
}
}
}
}