-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
183 lines (166 loc) · 10.4 KB
/
firestore.rules
File metadata and controls
183 lines (166 loc) · 10.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ── Auth ─────────────────────────────────────────────────────────────────
// Require a live Google Sign-In session.
// Explicitly checking the provider prevents any accidentally re-enabled
// auth method (e.g. email/password) from silently granting access.
function isGoogleAuth() {
return request.auth != null
&& request.auth.token.firebase.sign_in_provider == 'google.com';
}
function isOwner(userId) {
return request.auth.uid == userId;
}
// ── Validators ───────────────────────────────────────────────────────────
// User profile: /users/{userId}
// Required: email, displayName, lastLogin
// Optional: photoURL, fcmToken, notificationsEnabled, lastTokenUpdate
function isValidUserData(data) {
return data.keys().hasOnly([
'email', 'displayName', 'photoURL',
'fcmToken', 'notificationsEnabled',
'lastLogin', 'lastTokenUpdate'
])
&& data.email is string && data.email.size() >= 3 && data.email.size() <= 254
&& data.displayName is string && data.displayName.size() >= 1 && data.displayName.size() <= 100
&& data.lastLogin is string && data.lastLogin.size() >= 1 && data.lastLogin.size() <= 30
&& (!('photoURL' in data) || (data.photoURL is string && data.photoURL.size() <= 2048))
&& (!('fcmToken' in data) || (data.fcmToken is string && data.fcmToken.size() <= 512))
&& (!('lastTokenUpdate' in data) || (data.lastTokenUpdate is string && data.lastTokenUpdate.size() <= 30))
&& (!('notificationsEnabled' in data) || data.notificationsEnabled is bool);
}
// App settings: /users/{userId}/settings/general
// Required: monthlyIncome, fixedCosts, monthlySavings, isOnboarded
// Optional: currency, bentoPreset, createdAt, updatedAt
function isValidSettingsData(data) {
return data.keys().hasOnly([
'monthlyIncome', 'fixedCosts', 'monthlySavings',
'currency', 'bentoPreset', 'isOnboarded', 'createdAt', 'updatedAt'
])
&& data.monthlyIncome is number && data.monthlyIncome >= 0 && data.monthlyIncome <= 100000000
&& data.fixedCosts is number && data.fixedCosts >= 0 && data.fixedCosts <= 100000000
&& data.monthlySavings is number && data.monthlySavings >= 0 && data.monthlySavings <= 100000000
&& data.isOnboarded is bool
&& (!('currency' in data) || (data.currency is string && data.currency.size() >= 1 && data.currency.size() <= 10))
&& (!('bentoPreset' in data) || data.bentoPreset in ['compact', 'balanced', 'airy'])
&& (!('createdAt' in data) || (data.createdAt is string && data.createdAt.size() >= 1 && data.createdAt.size() <= 30))
&& (!('updatedAt' in data) || (data.updatedAt is string && data.updatedAt.size() >= 1 && data.updatedAt.size() <= 30));
}
// Envelope: /users/{userId}/envelopes/{envelopeId}
// Required: name, budget, spent, icon, color, order
// Optional: tileSize, createdAt, isTemporary, activeMonths
//
// Temporary-envelope fields:
// isTemporary — bool flag marking the envelope as month-scoped.
// activeMonths — list of YYYY-MM strings (1–24 entries) that controls
// which months the envelope is surfaced in the UI.
//
// ⚠️ Firestore Security Rules do not support list iteration, so
// per-item YYYY-MM format validation cannot be enforced here.
// Client code and/or a Cloud Function trigger must reject items
// that do not match /^\d{4}-(0[1-9]|1[0-2])$/ before writing.
function isValidActiveMonths(months) {
// list capped at 24 months (two years of look-ahead/back).
// Empty list is valid: permanent envelopes always write [] and a
// temporary envelope with no months assigned is legitimately inactive.
return months is list
&& months.size() <= 24;
}
function isValidEnvelopeData(data) {
return data.keys().hasOnly([
'name', 'budget', 'spent', 'icon', 'color', 'order',
'tileSize', 'createdAt', 'isTemporary', 'activeMonths'
])
&& data.name is string && data.name.size() >= 1 && data.name.size() <= 50
&& data.icon is string && data.icon.size() >= 1 && data.icon.size() <= 100
&& data.color is string && data.color.size() >= 1 && data.color.size() <= 100
&& data.budget is number && data.budget > 0 && data.budget <= 1000000
&& data.spent is number && data.spent >= 0 && data.spent <= 1000000
&& data.order is number && data.order >= 0 && data.order <= 10000
&& (!('tileSize' in data) || data.tileSize == null || data.tileSize in ['small', 'wide'])
&& (!('createdAt' in data) || (data.createdAt is string && data.createdAt.size() >= 1 && data.createdAt.size() <= 30))
// ── Temporary-envelope optional fields ────────────────────────────────
&& (!('isTemporary' in data) || data.isTemporary is bool)
&& (!('activeMonths' in data) || isValidActiveMonths(data.activeMonths));
}
// Transaction: /users/{userId}/transactions/{transactionId}
// Required: amount, envelopeId, date, createdAt
// Optional: description, note (note retained for backward-compat with pre-rename iOS clients)
function isValidTransactionData(data) {
return data.keys().hasOnly([
'amount', 'envelopeId', 'date', 'createdAt', 'description', 'note'
])
&& data.amount is number && data.amount > 0 && data.amount <= 1000000
&& data.envelopeId is string && data.envelopeId.size() >= 1 && data.envelopeId.size() <= 128
&& data.date is string && data.date.size() >= 1 && data.date.size() <= 30
&& data.createdAt is string && data.createdAt.size() >= 1 && data.createdAt.size() <= 30
&& (!('description' in data) || (data.description is string && data.description.size() <= 255))
&& (!('note' in data) || (data.note is string && data.note.size() <= 255));
}
// Daily activity: /users/{userId}/dailyActivity/{date}
// Required: loggedIn, date
function isValidDailyActivityData(data, dateId) {
return data.keys().hasOnly(['loggedIn', 'date'])
&& data.loggedIn == true
&& data.date is string && data.date.size() == 10
&& data.date == dateId;
}
// ── User document ────────────────────────────────────────────────────────
match /users/{userId} {
allow read: if isGoogleAuth() && isOwner(userId);
allow create: if isGoogleAuth() && isOwner(userId)
&& isValidUserData(request.resource.data);
allow update: if isGoogleAuth() && isOwner(userId)
&& isValidUserData(request.resource.data);
// delete intentionally absent — account deletion must be performed
// server-side via the Admin SDK to atomically remove subcollections
// and the Firebase Auth entry.
// ── Settings subcollection ─────────────────────────────────────────────
// Only the single document 'general' is ever written.
match /settings/{document} {
allow read: if isGoogleAuth() && isOwner(userId);
allow create: if isGoogleAuth() && isOwner(userId)
&& isValidSettingsData(request.resource.data);
allow update: if isGoogleAuth() && isOwner(userId)
&& isValidSettingsData(request.resource.data);
// delete intentionally absent — a missing settings doc breaks the
// isOnboarded check and crashes app initialization.
}
// ── Envelopes subcollection ────────────────────────────────────────────
match /envelopes/{envelopeId} {
allow read: if isGoogleAuth() && isOwner(userId);
allow create: if isGoogleAuth() && isOwner(userId)
&& isValidEnvelopeData(request.resource.data);
allow update: if isGoogleAuth() && isOwner(userId)
&& isValidEnvelopeData(request.resource.data);
allow delete: if isGoogleAuth() && isOwner(userId);
}
// ── Transactions subcollection ─────────────────────────────────────────
match /transactions/{transactionId} {
allow read: if isGoogleAuth() && isOwner(userId);
allow create: if isGoogleAuth() && isOwner(userId)
&& isValidTransactionData(request.resource.data);
allow update: if isGoogleAuth() && isOwner(userId)
&& isValidTransactionData(request.resource.data)
// createdAt is immutable — prevent audit-trail backdating.
&& request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isGoogleAuth() && isOwner(userId);
}
// ── Daily activity subcollection ───────────────────────────────────────
match /dailyActivity/{date} {
allow read: if isGoogleAuth() && isOwner(userId);
allow create: if isGoogleAuth() && isOwner(userId)
&& isValidDailyActivityData(request.resource.data, date);
allow update: if isGoogleAuth() && isOwner(userId)
&& isValidDailyActivityData(request.resource.data, date)
// date is immutable — prevents history tampering.
&& request.resource.data.date == resource.data.date;
}
}
// Deny all unmatched paths by default
match /{document=**} {
allow read, write: if false;
}
}
}