-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
200 lines (176 loc) · 9.18 KB
/
Copy pathfirestore.rules
File metadata and controls
200 lines (176 loc) · 9.18 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
// ===============================================================
// Assumed Data Model
// ===============================================================
//
// This security rules file assumes the following data structures:
//
// Collection: /collections/{collectionId}
// Document ID: auto-generated
// Fields:
// - ownerId: string (required, immutable) - The UID of the user who owns this collection
// - title: string (required, min: 1, max: 100) - The title of the collection
// - description: string (optional, max: 500) - A brief description of the collection
// - createdAt: number (required, immutable) - Timestamp of creation
//
// Article: /articles/{articleId}
// Document ID: auto-generated
// Fields:
// - ownerId: string (required, immutable) - The UID of the user who owns this article
// - title: string (required, min: 1, max: 200) - The title of the article
// - url: string (required, isValidUrl) - The original URL of the article
// - author: string (optional, max: 100) - The author of the article
// - publicationDate: string (optional, max: 100) - The publication date of the article
// - content: string (required, max: 1000000) - The extracted Markdown content
// - collectionId: string (required, immutable) - The ID of the parent collection
// - isBookmarked: bool (optional) - Whether the article is bookmarked
// - tags: list (optional, max: 20 items, each max: 50 chars) - List of tags
// - createdAt: number (required, immutable) - Timestamp of creation
//
// Highlight: /highlights/{highlightId}
// Document ID: auto-generated
// Fields:
// - ownerId: string (required, immutable) - The UID of the user who owns this highlight
// - articleId: string (required, immutable) - The ID of the article this highlight belongs to
// - text: string (required, min: 1, max: 5000) - The highlighted text
// - createdAt: number (required, immutable) - Timestamp of creation
//
// Note: /notes/{noteId}
// Document ID: auto-generated
// Fields:
// - ownerId: string (required, immutable) - The UID of the user who owns this note
// - type: string (required, enum: ['note', 'question', 'comment']) - The type of note
// - content: string (required, min: 1, max: 5000) - The text content of the note
// - articleId: string (required, immutable) - The ID of the article this note belongs to
// - createdAt: number (required, immutable) - Timestamp of creation
//
// ===============================================================
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ===============================================================
// Helper Functions
// ===============================================================
function isAuthenticated() {
return request.auth != null;
}
function isOwner(ownerId) {
return isAuthenticated() && request.auth.uid == ownerId;
}
function isAdmin() {
return isAuthenticated() &&
(get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin' ||
(request.auth.token.email == "christopherpowell@precisionga.com" && request.auth.token.email_verified == true) ||
(request.auth.token.email == "cpowell1687@gmail.com" && request.auth.token.email_verified == true));
}
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
function hasOnlyAllowedFields(fields) {
return request.resource.data.keys().hasOnly(fields);
}
function areImmutableFieldsUnchanged(fields) {
return !request.resource.data.diff(resource.data).affectedKeys().hasAny(fields);
}
function isValidUrl(url) {
return url is string && (url.matches("^https://.*") || url.matches("^http://.*"));
}
function isValidStringLength(field, minLen, maxLen) {
return request.resource.data[field] is string &&
request.resource.data[field].size() >= minLen &&
request.resource.data[field].size() <= maxLen;
}
function isValidOptionalString(field, minLen, maxLen) {
return !(field in request.resource.data) ||
request.resource.data[field] == null ||
(request.resource.data[field] is string &&
request.resource.data[field].size() >= minLen &&
request.resource.data[field].size() <= maxLen);
}
function isValidList(field, maxSize, maxItemSize) {
return !(field in request.resource.data) ||
(request.resource.data[field] is list &&
request.resource.data[field].size() <= maxSize &&
request.resource.data[field].all(item, item is string && item.size() <= maxItemSize));
}
// ===============================================================
// Domain Validators
// ===============================================================
function isValidCollection(data) {
return hasRequiredFields(['ownerId', 'title', 'createdAt']) &&
hasOnlyAllowedFields(['ownerId', 'title', 'description', 'createdAt']) &&
data.ownerId == request.auth.uid &&
data.title is string && data.title.size() >= 1 && data.title.size() <= 100 &&
(!( 'description' in data) || (data.description is string && data.description.size() <= 500)) &&
data.createdAt is number;
}
function isValidArticle(data) {
return hasRequiredFields(['ownerId', 'title', 'url', 'content', 'collectionId', 'createdAt']) &&
hasOnlyAllowedFields(['ownerId', 'title', 'url', 'content', 'collectionId', 'isBookmarked', 'tags', 'createdAt', 'author', 'publicationDate', 'status']) &&
data.ownerId == request.auth.uid &&
data.title is string && data.title.size() >= 1 && data.title.size() <= 200 &&
(data.url == "" || isValidUrl(data.url)) &&
isValidOptionalString('author', 0, 100) &&
isValidOptionalString('publicationDate', 0, 100) &&
data.content is string && data.content.size() <= 1000000 &&
data.collectionId is string &&
(!( 'isBookmarked' in data) || data.isBookmarked is bool) &&
(!( 'status' in data) || data.status in ['active', 'deleted']) &&
isValidList('tags', 20, 50) &&
data.createdAt is number;
}
function isValidHighlight(data) {
return hasRequiredFields(['ownerId', 'articleId', 'text', 'createdAt']) &&
hasOnlyAllowedFields(['ownerId', 'articleId', 'text', 'createdAt']) &&
data.ownerId == request.auth.uid &&
data.articleId is string &&
data.text is string && data.text.size() >= 1 && data.text.size() <= 5000 &&
data.createdAt is number;
}
function isValidNote(data) {
return hasRequiredFields(['ownerId', 'type', 'content', 'articleId', 'createdAt']) &&
hasOnlyAllowedFields(['ownerId', 'type', 'content', 'articleId', 'createdAt']) &&
data.ownerId == request.auth.uid &&
data.type in ['note', 'question', 'comment'] &&
data.content is string && data.content.size() >= 1 && data.content.size() <= 5000 &&
data.articleId is string &&
data.createdAt is number;
}
// ===============================================================
// Rules
// ===============================================================
match /collections/{collectionId} {
allow read: if isOwner(resource.data.ownerId) || isAdmin();
allow create: if isAuthenticated() && isValidCollection(request.resource.data);
allow update: if isOwner(resource.data.ownerId) && isValidCollection(request.resource.data) && areImmutableFieldsUnchanged(['ownerId', 'createdAt']);
allow delete: if isOwner(resource.data.ownerId) || isAdmin();
}
match /articles/{articleId} {
allow read: if isOwner(resource.data.ownerId) || isAdmin();
allow create: if isAuthenticated() && isValidArticle(request.resource.data);
allow update: if isOwner(resource.data.ownerId) && isValidArticle(request.resource.data) && areImmutableFieldsUnchanged(['ownerId', 'createdAt', 'collectionId']);
allow delete: if isOwner(resource.data.ownerId) || isAdmin();
}
match /notes/{noteId} {
allow read: if isOwner(resource.data.ownerId) || isAdmin();
allow create: if isAuthenticated() && isValidNote(request.resource.data);
allow update: if isOwner(resource.data.ownerId) && isValidNote(request.resource.data) && areImmutableFieldsUnchanged(['ownerId', 'createdAt', 'articleId']);
allow delete: if isOwner(resource.data.ownerId) || isAdmin();
}
match /highlights/{highlightId} {
allow read: if isOwner(resource.data.ownerId) || isAdmin();
allow create: if isAuthenticated() && isValidHighlight(request.resource.data);
allow update: if isOwner(resource.data.ownerId) && isValidHighlight(request.resource.data) && areImmutableFieldsUnchanged(['ownerId', 'createdAt', 'articleId']);
allow delete: if isOwner(resource.data.ownerId) || isAdmin();
}
match /users/{userId} {
allow read: if isOwner(userId) || isAdmin();
allow create: if isAuthenticated() && isOwner(userId) && request.resource.data.role == 'user';
allow update: if isOwner(userId) && request.resource.data.role == resource.data.role;
allow delete: if isAdmin();
}
// Default deny
match /{path=**} {
allow read, write: if false;
}
}
}