-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfirestore.TEMPLATE.rules
315 lines (257 loc) · 13.1 KB
/
firestore.TEMPLATE.rules
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//When you update this file, also run 'npm run test:security' to verify all tests pass
rules_version = '2'
service cloud.firestore {
match /databases/{database}/documents {
function userMatchesDomain() {
//inject here:domain
return loggedIn() && !isAnonymous() && request.auth.token.email.lower().split('@')[1] == domain;
}
function userTypeAllComposedPermissions() {
//inject here:all
return rules;
}
function userTypeAnonymousComposedPermissions() {
//inject here:anonymous
//Should return if the user is AT LEAST anonymous
return rules;
}
function userTypeSignedInComposedPermissions() {
//inject here:signed_in
return rules;
}
function userTypeSignedInDomainComposedPermissions() {
//inject here:signed_in_domain
return rules;
}
function userTypePermissions() {
//Returns the highest userType permission (they're all composed)
return userMatchesDomain() ? userTypeSignedInDomainComposedPermissions() : loggedIn() && !isAnonymous() ? userTypeSignedInComposedPermissions() : loggedIn() ? userTypeAnonymousComposedPermissions() : userTypeAllComposedPermissions();
}
function userPermissions() {
return (request.auth != null && request.auth.uid != null && exists(/databases/$(database)/documents/permissions/$(request.auth.uid))) ? get(/databases/$(database)/documents/permissions/$(request.auth.uid)).data : {};
}
function getPermissions(keys) {
//we can convert to a set because we disallow userTypePermissions to mark
//negative keys, and also do the same (in client side checks, at least,
//for permissions on user object)
let mergedSet = userPermissions().keys().toSet().union(userTypePermissions().keys().toSet());
return mergedSet.hasAny(keys);
}
function getPermission(key) {
return getPermissions([key])
}
function userIsAdmin() {
//don't do the whole transform of getPermission, because admin is so sensitive that it should only be
//able to be set per user, and getPermission is really expensive to run twice
return userPermissions().get('admin', false);
}
function userMayViewApp() {
return userIsAdmin() || getPermission('viewApp');
}
function userMayViewUnpublished() {
return loggedIn() && userIsAdmin() || getPermissions(['edit', 'editCard', 'viewUnpublished']) || request.auth.uid in resource.data.permissions.editCard || request.auth.uid == resource.data.author;
}
function userMayEditPermissions() {
//For now we don'b bother having an editPermissions permission, but factor this out so it's easier to add later if necessary
return userIsAdmin();
}
function userMayEditCard(cardID) {
return userIsAdmin() || getPermissions(['edit', 'editCard']) || userIsAuthorOrEditorForCard(cardID);
}
function userIsAuthorOrEditorForCard(cardID) {
//This rule might be called in the context of a card, or in the context of a card update, where it needs to be fetched
//TODO: use request.resource.data if it's in the former context
let card = get(/databases/$(database)/documents/cards/$(cardID)).data;
let uid = request.auth.uid;
return uid == card.author || uid in card.permissions.editCard;
}
function userMayEditSection(sectionID) {
return userIsAdmin() || getPermissions(['edit', 'editSection']);
}
function userMayEditTag(tagID) {
return userIsAdmin() || getPermissions(['edit', 'editTag']);
}
function userMayCreateCard() {
return userIsAdmin() || getPermissions(['edit','createCard']);
}
function userMayComment() {
return userIsAdmin() || getPermission('comment');
}
function userMayStar() {
return userIsAdmin() || getPermission('star');
}
function userMayMarkRead() {
return userIsAdmin() || getPermission('markRead');
}
function userMayModifyReadingList() {
return userIsAdmin() || getPermission('modifyReadingList');
}
function editDoesNotAffectAdminKey() {
return !request.resource.data.diff(resource == null || resource.data == null ? {} : resource.data).affectedKeys().hasAny(['admin']);
}
function editOnlyUpdatesTimestamp(key) {
let requestData = request.resource.data;
return requestData.diff(resource.data).affectedKeys().hasOnly([key]) && requestData[key] == request.time;
}
function editOnlyIncrementsAndUpdatesTimestamp(incrementKey, timestampKey) {
let requestData = request.resource.data;
let resourceData = resource.data;
return requestData.diff(resourceData).affectedKeys().hasOnly([incrementKey, timestampKey]) && requestData[timestampKey] == request.time && resourceData[incrementKey] + 1 == requestData[incrementKey]
}
function editOnlyAddsMessage() {
let requestData = request.resource.data;
let requestMessages = requestData.messages;
let resourceMessages = resource.data.messages;
return requestData.diff(resource.data).affectedKeys().hasOnly(['messages', 'updated']) && requestMessages.size() == resourceMessages.size() + 1 && requestMessages[0:resourceMessages.size()] == resourceMessages;
}
function editOnlyIncrements(key) {
let requestData = request.resource.data;
let resourceData = resource.data;
return requestData.diff(resourceData).affectedKeys().hasOnly([key]) && resourceData[key] + 1 == requestData[key];
}
function editOnlyIncrementsOrDecrements(key, otherKey) {
let requestData = request.resource.data;
let resourceData = resource.data;
let resourceDataKey = resourceData[key];
let requestDataKey = requestData[key];
let resourceDataOtherKey = resourceData[otherKey];
let requestDataOtherKey = requestData[otherKey];
return requestData.diff(resourceData).affectedKeys().hasOnly([key, otherKey]) && (resourceDataKey + 1 == requestDataKey || resourceDataKey - 1 == requestDataKey) && (resourceDataOtherKey + 1 == requestDataOtherKey || resourceDataOtherKey - 1 == requestDataOtherKey);
}
function editOnlyIncrementsAndDecrements(incKey, decKey) {
let requestData = request.resource.data;
let resourceData = resource.data;
return requestData.diff(resourceData).affectedKeys().hasOnly([incKey, decKey]) && resourceData[decKey] - 1 == requestData[decKey] && resourceData[incKey] + 1 == requestData[incKey];
}
function cardEditInboundReferences() {
let affectedKeys = request.resource.data.diff(resource.data).affectedKeys();
let allKeys = ['references_inbound', 'references_info_inbound'];
//TODO: for each modifiedCardID, verify userMayEditCard. There is no way
//to access keys by index in the set, or to loop currently which will mean
//it would always be hacky.
//let modifiedCardIDs = request.resource.data.get('references_inbound', {}).diff(resource.data.get('references_inbound', {})).changedKeys();
return (resource.data.published || userMayViewUnpublished()) && affectedKeys.hasOnly(allKeys) && affectedKeys.hasAny(allKeys);
}
function cardEditMinor() {
let affectedKeys = request.resource.data.diff(resource.data).affectedKeys();
//we can bail only if there are any keys not in this set, or if they aren't any keys in this set, without doing the more expensive calculations.
let allKeys = ['star_count', 'star_count_manual', 'thread_count', 'thread_resolved_count', 'thread_count', 'updated_message'];
return affectedKeys.hasOnly(allKeys) && affectedKeys.hasAny(allKeys) && cardEditLegalMessages(affectedKeys) || cardEditLegalStars(affectedKeys);
}
function cardEditLegalMessages(affectedKeys) {
//TODO: pass in the affectedKeys into the children to save another calculation
return userMayComment() && (editOnlyIncrements('thread_count') || editOnlyIncrementsAndDecrements('thread_resolved_count', 'thread_count') || editOnlyUpdatesTimestamp('updated_message') || editOnlyIncrementsAndUpdatesTimestamp('thread_count', 'updated_message'));
}
function cardEditLegalStars(affectedKeys) {
//TODO: pass in the affectedKeys into the children to save another calculation
return userMayStar() && editOnlyIncrementsOrDecrements('star_count', 'star_count_manual');
}
function loggedIn() {
return request.auth != null && request.auth.uid != null;
}
function isAnonymous() {
return request.auth != null && request.auth.token != null && request.auth.token.firebase != null && request.auth.token.firebase.sign_in_provider == 'anonymous';
}
function createIsOwner() {
return loggedIn() && request.auth.uid == request.resource.data.owner;
}
function createIsAuthor() {
return loggedIn() && request.auth.uid == request.resource.data.author;
}
function updateIsOwner() {
return loggedIn() && request.auth.uid == resource.data.owner;
}
function updateIsAuthor() {
return loggedIn() && request.auth.uid == resource.data.author;
}
match /permissions/{user} {
allow read: if request.auth.uid == user || userMayEditPermissions();
allow write: if userMayEditPermissions() && editDoesNotAffectAdminKey();
allow delete: if userMayEditPermissions()
}
match /sections/{section} {
allow read: if userMayViewApp();
allow write: if userMayEditSection(section);
match /updates/{update} {
allow read, write: if userMayEditSection(section);
}
}
match /tags/{tag} {
allow read: if userMayViewApp();
allow write: if userMayEditTag(tag);
match /updates/{update} {
allow read, write: if userMayEditTag(tag);
}
}
match /authors/{author} {
allow read: if userMayViewApp();
//Allow authors to write their own author entry, or any admin to create a
//stub placeholder to be filled in later. The user must be an admin
//because a) they need to know the uid, which implies access to the
//firebase auth console anyway, and secondarily, anyone who can edit any
//card can note editors, and there's no good way to detect that, so this
//would be left open to anyeone, which would mean anyone could compell a
//user to divulge their displayName and photoURL to the public when they
//next visited.
allow create: if request.auth.uid == author || (userIsAdmin() && request.resource.data.size() == 0)
allow write: if request.auth.uid == author || (userIsAdmin() && request.resource.data.diff(resource.data).affectedKeys().size() == 0);
}
match /users/{user} {
allow read: if request.auth.uid == user;
allow write: if request.auth.uid == user;
}
match /cards/{card} {
allow read: if userMayViewApp() && (resource.data.published == true || userMayViewUnpublished());
allow create: if userMayCreateCard() && createIsAuthor();
allow update: if cardEditMinor() || cardEditInboundReferences() || userMayEditCard(card);
//This logic mirrors the logic in selectors/getReasonUserMayNotDeleteCard
allow delete: if userMayEditCard(card) && resource.data.get('section', '') == '' && resource.data.get('tags', []).size() == 0 && resource.data.get('references_inbound', {}).size() == 0;
match /updates/{update} {
allow read, write: if userMayEditCard(card);
}
}
match /embeddings/{card} {
allow read, write: if false;
}
match /maintenance_tasks/{task} {
allow read: if userIsAdmin();
allow write: if userIsAdmin();
}
match /messages/{message} {
allow read: if userMayViewApp();
allow create: if createIsAuthor() && userMayComment();
allow update: if (updateIsAuthor() || userIsAdmin()) && userMayComment();
}
match /threads/{thread} {
allow read: if userMayViewApp();
allow create: if createIsAuthor() && userMayComment()
allow update: if userMayComment() && (updateIsAuthor() || (!isAnonymous() && editOnlyAddsMessage()) || userIsAdmin());
}
match /stars/{star} {
allow create: if userMayStar() && createIsOwner();
allow update: if userMayStar() && updateIsOwner();
allow delete: if userMayStar() && updateIsOwner();
allow read: if updateIsOwner();
}
match /reads/{read} {
allow create: if userMayMarkRead() && createIsOwner();
allow update: if userMayMarkRead() && updateIsOwner();
allow delete: if userMayMarkRead() && updateIsOwner();
allow read: if updateIsOwner();
}
match /tweets/{tweet} {
allow read: if userMayViewApp();
}
match /reading_lists/{list_id} {
allow create: if userMayModifyReadingList() && createIsOwner();
allow update: if userMayModifyReadingList() && updateIsOwner();
allow delete: if userMayModifyReadingList() && updateIsOwner();
allow read: if updateIsOwner();
match /updates/{update} {
//Anyone can read or write the updates sub-collection as long as they're logged in as
//the ID of the reading_list, which is the same as the owner of the reading-list.
allow read, write: if list_id == request.auth.uid;
}
}
}
}