-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
126 lines (106 loc) · 4.63 KB
/
firestore.rules
File metadata and controls
126 lines (106 loc) · 4.63 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// 0. Default Deny
match /{document=**} {
allow read, write: if false;
}
// --- Primitives & Helpers ---
function isSignedIn() {
return request.auth != null;
}
function isOwner(userId) {
return isSignedIn() && request.auth.uid == userId;
}
function isValidId(id) {
return id is string && id.size() <= 128 && id.matches('^[a-zA-Z0-9_\\-]+$');
}
function incoming() {
return request.resource.data;
}
function existing() {
return resource.data;
}
// --- Entity Validations ---
function isValidFileEntry(data) {
return data.keys().hasAll(['id', 'name', 'timestamp', 'userId']) &&
data.id is string && data.id.size() <= 128 &&
data.name is string && data.name.size() <= 256 &&
data.timestamp is number &&
data.userId == request.auth.uid &&
(data.get('content', '') is string && data.get('content', '').size() <= 1048576) &&
(data.get('category', 'OTHER') is string) &&
(data.get('indexStatus', 'IDLE') in ['IDLE', 'INDEXING', 'DONE', 'ERROR']);
}
function isValidCaseRecord(data) {
return data.keys().hasAll(['id', 'name', 'nr', 'userId']) &&
data.id is string && data.id.size() <= 128 &&
data.name is string && data.name.size() <= 256 &&
data.nr is string && data.nr.size() <= 64 &&
data.userId == request.auth.uid;
}
function isValidAuditTask(data) {
return data.keys().hasAll(['id', 'files', 'status', 'timestamp', 'userId']) &&
data.id is string && data.id.size() <= 128 &&
data.files is list && data.files.size() <= 100 &&
data.status in ['pending', 'processing', 'done', 'error'] &&
data.timestamp is number &&
data.userId == request.auth.uid;
}
function isValidVersionRecord(data) {
return data.keys().hasAll(['id', 'version', 'timestamp', 'userId']) &&
data.id is string && data.id.size() <= 128 &&
data.version is string && data.version.size() <= 64 &&
data.timestamp is number &&
data.userId == request.auth.uid;
}
function isValidUserSettings(data) {
return data.keys().hasAll(['userId']) &&
data.userId == request.auth.uid;
}
// --- Collection Routes ---
match /users/{userId} {
allow get: if isOwner(userId);
match /files/{fileId} {
allow get: if isOwner(userId) && isValidId(fileId);
allow list: if isOwner(userId);
allow create: if isOwner(userId) && isValidId(fileId) && isValidFileEntry(incoming());
allow update: if isOwner(userId) && isValidFileEntry(incoming()) && (
incoming().diff(existing()).affectedKeys().hasAny(['isArchived', 'category', 'insight', 'indexStatus', 'content', 'githubPath', 'driveId'])
);
allow delete: if isOwner(userId);
}
match /cases/{caseId} {
allow get: if isOwner(userId) && isValidId(caseId);
allow list: if isOwner(userId);
allow create: if isOwner(userId) && isValidId(caseId) && isValidCaseRecord(incoming());
allow update: if isOwner(userId) && isValidCaseRecord(incoming()) && (
incoming().diff(existing()).affectedKeys().hasAny(['name', 'nr', 'activeVersion'])
);
allow delete: if isOwner(userId);
}
match /queue/{taskId} {
allow get: if isOwner(userId) && isValidId(taskId);
allow list: if isOwner(userId);
allow create: if isOwner(userId) && isValidId(taskId) && isValidAuditTask(incoming());
allow update: if isOwner(userId) && isValidAuditTask(incoming()) && (
incoming().diff(existing()).affectedKeys().hasAny(['status', 'result', 'isNotified', 'error', 'timestamp'])
);
allow delete: if isOwner(userId);
}
match /history/{historyId} {
allow get: if isOwner(userId) && isValidId(historyId);
allow list: if isOwner(userId);
allow create: if isOwner(userId) && isValidId(historyId) && isValidVersionRecord(incoming());
allow update: if isOwner(userId) && isValidVersionRecord(incoming()) && (
incoming().diff(existing()).affectedKeys().hasAny(['text', 'selectedFiles', 'selectedPillars', 'timestamp', 'version'])
);
allow delete: if isOwner(userId);
}
match /settings/current {
allow get: if isOwner(userId);
allow write: if isOwner(userId) && isValidUserSettings(incoming());
}
}
}
}