-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfirestore.rules
More file actions
249 lines (211 loc) · 8.53 KB
/
firestore.rules
File metadata and controls
249 lines (211 loc) · 8.53 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isAdmin() {
return request.auth != null && request.auth.token.admin == true;
}
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userID} {
function isOwner() {
return request.auth != null && request.auth.uid == userID;
}
allow read: if isOwner();
allow create: if isOwner()
&& (
!('creationDate' in request.resource.data)
|| request.resource.data.creationDate is timestamp
);
allow update: if isOwner()
&& !request.resource.data.diff(resource.data).affectedKeys().hasAny([
'creationDate'
]);
allow delete: if false;
// Legal Agreements (Write-Once / Monotonic)
match /legal/agreements {
allow read: if request.auth.uid == userID;
function isValidLegalAgreement() {
return request.resource.data.keys().hasOnly([
'acceptedPrivacyPolicy',
'acceptedDataPolicy',
'acceptedTrackingPolicy',
'acceptedMarketingPolicy',
'acceptedDiagnosticsPolicy',
'acceptedTos'
]) &&
(request.resource.data.get('acceptedPrivacyPolicy', true) == true) &&
(request.resource.data.get('acceptedDataPolicy', true) == true) &&
(request.resource.data.get('acceptedTrackingPolicy', false) is bool) &&
(request.resource.data.get('acceptedMarketingPolicy', false) is bool) &&
(request.resource.data.get('acceptedDiagnosticsPolicy', true) == true) &&
(request.resource.data.get('acceptedTos', true) == true);
}
allow create: if request.auth.uid == userID && isValidLegalAgreement();
allow update: if request.auth.uid == userID
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly([
'acceptedPrivacyPolicy',
'acceptedDataPolicy',
'acceptedTrackingPolicy',
'acceptedMarketingPolicy',
'acceptedDiagnosticsPolicy',
'acceptedTos'
])
&& (request.resource.data.get('acceptedPrivacyPolicy', true) == true)
&& (request.resource.data.get('acceptedDataPolicy', true) == true)
&& (request.resource.data.get('acceptedTrackingPolicy', false) is bool)
&& (request.resource.data.get('acceptedMarketingPolicy', false) is bool)
&& (request.resource.data.get('acceptedDiagnosticsPolicy', true) == true)
&& (request.resource.data.get('acceptedTos', true) == true);
}
// Config Settings (Private / Owner Read-Write)
match /config/settings {
allow read, write: if request.auth.uid == userID;
}
// AI Insights Latest Snapshot (Private / Owner Read/Delete, fixed latest doc only)
match /aiInsightsRequests/{requestID} {
allow read: if request.auth.uid == userID && requestID == 'latest';
allow delete: if request.auth.uid == userID && requestID == 'latest';
allow create, update: if false;
}
// AI Insights Usage (Private / Server-Owned only)
match /aiInsightsUsage/{periodDocId} {
allow read: if false;
allow write: if false;
}
// System Status (Admin Only)
match /system/status {
allow read: if request.auth.uid == userID;
allow write: if false; // Only Cloud Functions (Admin SDK)
}
match /events/{eventID} {
// RESTRICTED ACCESS: Must be owner AND have a valid role (Free, Basic, or PRO)
// TEMPORARILY DISABLED FOR TESTING - Re-enable when ready to enforce paywall
// allow read, write: if request.auth.uid == userID && isSubscribed();
allow read: if request.auth.uid == userID;
allow create: if false;
allow update: if request.auth.uid == userID
&& !request.resource.data.diff(resource.data).affectedKeys().hasAny(['originalFile', 'originalFiles', 'isMerge', 'mergeType']);
allow delete: if request.auth.uid == userID;
// Restoring metaData inside events
match /metaData/processing {
allow read: if request.auth.uid == userID;
// Processing metadata is server-owned (Cloud Functions/Admin SDK only).
allow write: if false;
}
match /metaData/{document=**} {
allow read: if request.auth.uid == userID;
}
}
match /activities/{activityID} {
// New Flat Activities Collection
function isOwnEventRef(data) {
return data.keys().hasAll(['eventID']) && (
data.eventID is string &&
exists(/databases/$(database)/documents/users/$(userID)/events/$(data.eventID))
);
}
allow read: if request.auth.uid == userID;
allow create: if false;
allow delete: if false;
allow update: if request.auth.uid == userID
&& !request.resource.data.diff(resource.data).affectedKeys().hasAny(['eventID', 'userID', 'eventStartDate', 'sourceActivityKey'])
&& isOwnEventRef(request.resource.data);
}
match /meta/{document=**} {
allow read: if request.auth.uid == userID;
}
match /derivedMetrics/{document=**} {
allow read: if request.auth.uid == userID;
allow write: if false;
}
match /sleepSessions/{sleepSessionID} {
allow read: if request.auth.uid == userID;
allow write: if false;
}
match /sleepSyncState/{providerID} {
allow read: if request.auth.uid == userID;
allow write: if false;
}
}
// AI Insights repaired prompt backlog (global / server-owned only)
match /aiInsightsPromptRepairs/{docId} {
allow read: if false;
allow write: if false;
}
match /customers/{userID} {
allow read: if request.auth.uid == userID;
match /checkout_sessions/{id} {
allow read, write: if request.auth.uid == userID;
}
match /subscriptions/{id} {
allow read: if request.auth.uid == userID;
}
match /payments/{id} {
allow read: if request.auth.uid == userID;
}
}
match /products/{id} {
allow read: if true;
match /prices/{id} {
allow read: if true;
}
match /tax_rates/{id} {
allow read: if true;
}
}
match /suuntoAppAccessTokens/{userID} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
match /tokens/{document=**} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
}
}
match /COROSAPIAccessTokens/{userID} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
match /tokens/{document=**} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
}
}
match /garminAPITokens/{userID} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
match /tokens/{document=**} {
allow read, write: if request.auth.uid == userID && request.auth.token.firebase.sign_in_provider != 'anonymous';
}
}
match /garminAPIActivityQueue/{userID} {
allow read: if isAdmin();
allow write: if false;
}
match /COROSAPIWorkoutQueue/{userID} {
allow read: if isAdmin();
allow write: if false;
}
match /suuntoAppWorkoutQueue/{userID} {
allow read: if isAdmin();
allow write: if false;
}
match /activitySyncQueue/{queueItemID} {
allow read: if isAdmin();
allow write: if false;
}
match /sleepSyncQueue/{queueItemID} {
allow read: if isAdmin();
allow write: if false;
}
match /failed_jobs/{document=**} {
allow read: if isAdmin();
allow write: if false;
}
match /changelogs/{docId} {
allow read: if resource.data.published == true || isAdmin();
allow write: if isAdmin();
}
}
}
service firebase.storage {
match /b/{bucket}/o {
match /assets/{allPaths=**} {
allow read;
}
}
}