Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
163 lines (148 loc) · 6.48 KB
/
Copy pathfirestore.rules
File metadata and controls
163 lines (148 loc) · 6.48 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Annotations (GB-03). A note's full record belongs to its author and sits
// under their account. A public note also has a public copy, under the same
// id, holding only what a reader sees: the text, target, dates and, if the
// author chose to sign, the name on their account. The copy never holds the
// account ID, so a signed-out reader cannot obtain it. It holds an author
// key, SHA-256 of a prefix, the account ID and the note's id, which these
// rules recompute from the signed-in account: only the author can write or
// withdraw the copy, and no reader can link two of an author's notes.
match /users/{userId}/annotations/{annotationId} {
allow read, delete: if signedIn() && userId == request.auth.uid;
allow create, update: if signedIn()
&& userId == request.auth.uid
&& validOwnAnnotation();
}
match /publicAnnotations/{annotationId} {
allow read: if true;
allow create, update: if signedIn()
&& validPublicCopy(annotationId);
// Deleting a copy that does not exist is allowed, and does nothing: the
// app withdraws a copy whenever it saves or deletes a private note.
allow delete: if signedIn()
&& (resource == null || resource.data.get('authorKey', '') == authorKey(annotationId));
}
function authorKey(annotationId) {
return hashing.sha256('sstim-annotation-author:' + request.auth.uid + ':' + annotationId)
.toHexString().lower();
}
function validPublicCopy(annotationId) {
let path = /databases/$(database)/documents/users/$(request.auth.uid)/annotations/$(annotationId);
let data = request.resource.data;
let name = data.get('authorName', '');
return data.keys().hasOnly([
'targetIri', 'annotationType', 'annotationText', 'authorName', 'authorKey', 'createdAt', 'updatedAt'
])
&& data.keys().hasAll(['targetIri', 'annotationType', 'annotationText', 'authorName', 'authorKey'])
&& data.authorKey == authorKey(annotationId)
// An existing copy is changed only by the author who wrote it.
&& (resource == null || resource.data.get('authorKey', '') == authorKey(annotationId))
&& existsAfter(path)
&& getAfter(path).data.visibility == 'public'
&& data.targetIri == getAfter(path).data.targetIri
&& data.annotationType == getAfter(path).data.annotationType
&& data.annotationText == getAfter(path).data.annotationText
&& name is string
// Unsigned, or signed by its author's choice with the name the account
// carries, which may not be the email address's local part.
&& (name == ''
|| (getAfter(path).data.showName == true
&& name == request.auth.token.get('name', '')
&& name.lower() != request.auth.token.get('email', '').split('@')[0].lower()));
}
// Legacy annotations, from before GB-03, which hold the account ID and a
// display name. Only their owner can read or delete them, which is all the
// app needs to move them into the shape above on the owner's next visit,
// and no new ones can be written.
match /rdfAnnotations/{annotationId} {
allow read, delete: if signedIn()
&& resource.data.userId == request.auth.uid;
allow create, update: if false;
}
match /users/{userId} {
allow read: if signedIn() && userId == request.auth.uid;
allow write: if signedIn()
&& userId == request.auth.uid
&& validUserPayload();
}
match /patchStudioPatches/{patchId} {
allow read: if signedIn()
&& resource.data.userId == request.auth.uid;
allow create: if signedIn()
&& validPatchPayload();
allow update: if signedIn()
&& resource.data.userId == request.auth.uid
&& validPatchPayload();
allow delete: if signedIn()
&& resource.data.userId == request.auth.uid;
}
// Server-admin-only SSTIM ecosystem audit ledger. Client SDK access is
// denied even to authenticated users; publication uses Google Cloud IAM.
match /sstimEcosystemAudit/{auditId} {
allow read, write: if false;
}
}
function signedIn() {
return request.auth != null;
}
function validUserPayload() {
return request.resource.data.keys().hasOnly([
'displayName',
'email',
'bio',
'affiliation',
'updatedAt'
])
&& request.resource.data.get('displayName', '') is string
&& request.resource.data.get('displayName', '').size() <= 200
&& request.resource.data.get('bio', '') is string
&& request.resource.data.get('bio', '').size() <= 4000
&& request.resource.data.get('affiliation', '') is string
&& request.resource.data.get('affiliation', '').size() <= 200
&& request.resource.data.get('email', '') is string
&& request.resource.data.get('email', '').size() <= 200;
}
function validOwnAnnotation() {
let data = request.resource.data;
return data.keys().hasOnly([
'targetIri', 'annotationType', 'annotationText', 'visibility', 'showName', 'createdAt', 'updatedAt'
])
&& data.keys().hasAll(['targetIri', 'annotationType', 'annotationText', 'visibility', 'showName'])
&& data.targetIri is string
&& data.targetIri.size() <= 2000
&& data.annotationType is string
&& data.annotationType.size() <= 40
&& data.annotationText is string
&& data.annotationText.size() > 0
&& data.annotationText.size() <= 4000
&& data.visibility in ['public', 'private']
&& data.showName is bool;
}
function validPatchPayload() {
return request.resource.data.keys().hasOnly([
'userId',
'patchName',
'model',
'patch',
'createdAt',
'updatedAt'
])
&& request.resource.data.userId == request.auth.uid
&& request.resource.data.patchName is string
&& request.resource.data.patchName.size() > 0
&& request.resource.data.patchName.size() <= 200
&& request.resource.data.model in [
'patch-studio-model-1',
'patch-studio-model-2',
'patch-studio-model-3'
]
&& request.resource.data.patch is map
&& request.resource.data.patch.model == request.resource.data.model
&& request.resource.data.patch.patchName is string
&& request.resource.data.patch.patchName.size() > 0
&& request.resource.data.patch.patchName.size() <= 200
&& request.resource.data.patch.patchName == request.resource.data.patchName;
}
}