-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.rules
More file actions
372 lines (314 loc) · 13 KB
/
storage.rules
File metadata and controls
372 lines (314 loc) · 13 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
rules_version = '2';
// Firebase Storage Security Rules for Focus Flow Timer
// Enterprise-grade security with role-based access control
// Last Updated: 2025-08-29
// Version: 2.0.0
service firebase.storage {
match /b/{bucket}/o {
// =============================================
// HELPER FUNCTIONS FOR SECURITY VALIDATION
// =============================================
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function hasRole(role) {
return isAuthenticated() &&
request.auth.token != null &&
request.auth.token.get(role, false) == true;
}
function isAdmin() {
return hasRole('admin');
}
function isPremium() {
return hasRole('premium') || hasRole('enterprise') || hasRole('admin');
}
function isSystem() {
return hasRole('system');
}
// File size validation helpers (in bytes)
function isWithinSizeLimit(maxSizeBytes) {
return request.resource.size <= maxSizeBytes;
}
// Image file validation
function isValidImage() {
return request.resource.contentType.matches('image/(jpeg|jpg|png|gif|webp|bmp)');
}
// Document file validation
function isValidDocument() {
return request.resource.contentType.matches('application/(pdf|msword|vnd\\.openxmlformats-officedocument\\.wordprocessingml\\.document|vnd\\.oasis\\.opendocument\\.text)') ||
request.resource.contentType.matches('text/(plain|csv|markdown|rtf)');
}
// Audio file validation
function isValidAudio() {
return request.resource.contentType.matches('audio/(mpeg|mp4|wav|ogg|webm|m4a|aac|flac)');
}
// Archive file validation
function isValidArchive() {
return request.resource.contentType.matches('application/(zip|x-rar-compressed|x-7z-compressed|gzip|x-tar)');
}
// Video file validation (for screen recordings, tutorials)
function isValidVideo() {
return request.resource.contentType.matches('video/(mp4|webm|ogg|avi|mov|wmv|flv)');
}
// Data export file validation
function isValidExportFile() {
return request.resource.contentType.matches('application/(json|csv)') ||
request.resource.contentType.matches('text/(plain|csv)') ||
request.resource.contentType == 'application/pdf';
}
// File name validation (alphanumeric, hyphens, underscores, periods only)
function isValidFileName(fileName) {
return fileName.matches('[a-zA-Z0-9._-]+');
}
// =============================================
// USER PROFILE STORAGE
// =============================================
// User profile pictures
match /users/{userId}/profile/avatar.{extension} {
allow read: if isAuthenticated() &&
(isOwner(userId) || isPremium() || isAdmin());
allow write: if isOwner(userId) &&
isValidImage() &&
isWithinSizeLimit(5 * 1024 * 1024) && // 5MB limit
extension.matches('(jpg|jpeg|png|webp)');
allow delete: if isOwner(userId) || isAdmin();
}
// User profile backgrounds (premium feature)
match /users/{userId}/profile/background.{extension} {
allow read: if isAuthenticated() &&
(isOwner(userId) || isAdmin());
allow write: if isOwner(userId) &&
isPremium() &&
isValidImage() &&
isWithinSizeLimit(10 * 1024 * 1024) && // 10MB limit
extension.matches('(jpg|jpeg|png|webp)');
allow delete: if isOwner(userId) || isAdmin();
}
// User custom sounds (premium feature)
match /users/{userId}/profile/sounds/{soundId}.{extension} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isOwner(userId) &&
isPremium() &&
isValidAudio() &&
isWithinSizeLimit(50 * 1024 * 1024) && // 50MB limit
extension.matches('(mp3|m4a|wav|ogg)');
allow delete: if isOwner(userId) || isAdmin();
}
// =============================================
// TASK ATTACHMENTS
// =============================================
// Task document attachments
match /users/{userId}/tasks/{taskId}/attachments/{fileName} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isOwner(userId) &&
(isValidDocument() || isValidImage() || isValidArchive()) &&
isWithinSizeLimit(25 * 1024 * 1024) && // 25MB limit
isValidFileName(fileName);
allow delete: if isOwner(userId) || isAdmin();
}
// Task image attachments (screenshots, diagrams, etc.)
match /users/{userId}/tasks/{taskId}/images/{fileName} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isOwner(userId) &&
isValidImage() &&
isWithinSizeLimit(15 * 1024 * 1024) && // 15MB limit
isValidFileName(fileName);
allow delete: if isOwner(userId) || isAdmin();
}
// Task voice notes (premium feature)
match /users/{userId}/tasks/{taskId}/voice/{fileName} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isOwner(userId) &&
isPremium() &&
isValidAudio() &&
isWithinSizeLimit(100 * 1024 * 1024) && // 100MB limit
isValidFileName(fileName);
allow delete: if isOwner(userId) || isAdmin();
}
// =============================================
// DATA EXPORTS AND BACKUPS
// =============================================
// User data exports (generated by Cloud Functions)
match /users/{userId}/exports/{exportId}.{extension} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isSystem() || isAdmin(); // Only backend can create exports
allow delete: if isOwner(userId) || isAdmin();
}
// Analytics reports (premium feature)
match /users/{userId}/reports/{reportId}.{extension} {
allow read: if isOwner(userId) || isAdmin();
allow write: if (isSystem() || isAdmin()) &&
isPremium() &&
isValidExportFile();
allow delete: if isOwner(userId) || isAdmin();
}
// User backup files (enterprise feature)
match /users/{userId}/backups/{backupId}.{extension} {
allow read: if isOwner(userId) || isAdmin();
allow write: if (isSystem() || isAdmin()) &&
hasRole('enterprise') &&
extension.matches('(json|zip)');
allow delete: if isOwner(userId) || isAdmin();
}
// =============================================
// COLLABORATION AND TEAM FEATURES
// =============================================
// Workspace shared files (enterprise feature)
match /workspaces/{workspaceId}/shared/{fileName} {
allow read: if isAuthenticated() &&
hasRole('enterprise');
allow write: if isAuthenticated() &&
hasRole('enterprise') &&
(isValidDocument() || isValidImage()) &&
isWithinSizeLimit(50 * 1024 * 1024) && // 50MB limit
isValidFileName(fileName);
allow delete: if hasRole('enterprise') || isAdmin();
}
// Organization assets (logos, branding, etc.)
match /organizations/{orgId}/assets/{fileName} {
allow read: if isAuthenticated() && hasRole('enterprise');
allow write: if hasRole('enterprise') &&
isValidImage() &&
isWithinSizeLimit(10 * 1024 * 1024) && // 10MB limit
isValidFileName(fileName);
allow delete: if hasRole('enterprise') || isAdmin();
}
// Team collaboration files
match /teams/{teamId}/files/{fileName} {
allow read: if isAuthenticated() && hasRole('enterprise');
allow write: if hasRole('enterprise') &&
(isValidDocument() || isValidImage() || isValidArchive()) &&
isWithinSizeLimit(100 * 1024 * 1024) && // 100MB limit
isValidFileName(fileName);
allow delete: if hasRole('enterprise') || isAdmin();
}
// =============================================
// SHARED SYSTEM ASSETS
// =============================================
// Default focus sounds and ambient tracks
match /assets/sounds/focus/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin(); // Only admin can upload system sounds
allow delete: if isAdmin();
}
// Notification sounds
match /assets/sounds/notifications/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// Default app icons and UI assets
match /assets/images/ui/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// Achievement badges and reward images
match /assets/images/achievements/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// Category icons and app themes
match /assets/images/categories/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// Onboarding and tutorial content
match /assets/content/tutorials/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// =============================================
// AI AND ML MODELS
// =============================================
// Machine learning model files
match /models/ai/{modelId}/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin() || isSystem();
allow delete: if isAdmin();
}
// AI training data (anonymized user patterns)
match /models/training/{datasetId}/{fileName} {
allow read: if isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isAdmin();
}
// =============================================
// TEMPORARY AND CACHE FILES
// =============================================
// Temporary upload files (24 hour TTL)
match /temp/{userId}/{uploadId}/{fileName} {
allow read: if isOwner(userId) || isAdmin();
allow write: if isOwner(userId) &&
isWithinSizeLimit(500 * 1024 * 1024) && // 500MB limit for temp files
isValidFileName(fileName);
allow delete: if isOwner(userId) || isSystem() || isAdmin();
}
// Processing queue files (for async operations)
match /processing/{jobId}/{fileName} {
allow read: if isSystem() || isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isSystem() || isAdmin();
}
// =============================================
// ANALYTICS AND MONITORING
// =============================================
// Usage analytics files (system generated)
match /analytics/usage/{date}/{fileName} {
allow read: if isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isAdmin();
}
// Performance monitoring data
match /monitoring/performance/{fileName} {
allow read: if isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isAdmin();
}
// Security audit logs
match /security/logs/{date}/{fileName} {
allow read: if isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isAdmin();
}
// =============================================
// DEVELOPMENT AND TESTING
// =============================================
// Development assets (non-production only)
match /dev/{path=**} {
allow read, write, delete: if isAdmin();
}
// Test data and fixtures
match /test/{path=**} {
allow read, write, delete: if isAdmin();
}
// =============================================
// COMPLIANCE AND LEGAL
// =============================================
// GDPR data deletion confirmations
match /compliance/gdpr/{userId}/{fileName} {
allow read: if isAdmin();
allow write: if isSystem() || isAdmin();
allow delete: if isAdmin();
}
// Legal documents and terms of service
match /legal/documents/{fileName} {
allow read: if isAuthenticated();
allow write: if isAdmin();
allow delete: if isAdmin();
}
// =============================================
// DEFAULT DENY RULE
// =============================================
// Deny all other access by default
match /{allPaths=**} {
allow read, write, delete: if false;
}
}
}