Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
196 lines (177 loc) · 10.1 KB
/
Copy pathfirestore.rules
File metadata and controls
196 lines (177 loc) · 10.1 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ===============================================================
// CampusNotify (CNMS) - Cloud Firestore Security Rules
// Multi-Device Real-Time Sync & Live Campus Lounge Messaging
// ===============================================================
// Enables seamless, instant, bidirectional real-time synchronization
// across all user devices (smartphones, laptops, and web browsers)
// using Firestore's WebSocket snapshot listeners (onSnapshot).
//
// Data Models & Schemas:
//
// 1. Collection: cnms_community_messages (WhatsApp-like Live Chat)
// - id: string (e.g. 'lounge_1730...')
// - senderId: string (e.g. 'user_patil_01')
// - senderName: string (e.g. 'Dr. Rajesh Patil')
// - senderUsername: string (optional)
// - senderRealName: string (optional)
// - senderRealEmail: string (optional)
// - avatarUrl: string (optional)
// - avatar: string (e.g. 'RP')
// - senderRole: string (e.g. 'FACULTY', 'ADMIN', 'STUDENT')
// - senderBio: string (optional)
// - text: string (max 30,000 chars)
// - ciphertext: string | null (AES-256-GCM E2EE payload)
// - iv: string | null
// - isE2ee: bool (optional)
// - file: map | null (attachment metadata)
// - gifUrl: string | null (optional)
// - reactions: map (optional emoji reactions)
// - timestamp: string (ISO 8601 timestamp)
//
// 2. Collection: cnms_notices (Official Circulars & Proof Receipts)
// - id: string (e.g. 'CNMS-1730...')
// - refNo: string (e.g. 'CNMS/2026/EXAM/01')
// - title: string (max 500 chars)
// - department: string (e.g. 'Examination Cell')
// - priority: string ('HIGH', 'MEDIUM', 'LOW', 'URGENT')
// - audience: string (e.g. 'ALL', 'FACULTY', 'STUDENTS')
// - description: string (max 65,536 chars)
// - attachment: string | map | null
// - signatory: string
// - issuedAt: string (ISO 8601 timestamp)
// - acknowledgedCount: int | float
// - likes: list
// - reactions: map
// - comments: list
//
// 3. Collection: cnms_users (Campus User Profiles & Directory)
// - id: string (e.g. 'user_admin_001')
// - name: string
// - username: string
// - email: string
// - role: string ('ADMIN', 'FACULTY', 'STUDENT', 'VIEWER')
// - canPost: bool
// - status: string ('APPROVED', 'PENDING', 'REJECTED')
// - isSuperAdmin: bool
// - bio: string
// - avatar: string
// - avatarUrl: string (optional)
// - createdAt: int | float | timestamp | string
// - privacyConsentGiven: bool
// - privacyConsentDate: string
// ===============================================================
// ===============================================================
// Domain Validators
// ===============================================================
// Community Lounge Message Validator
function hasRequiredMessageFields(data) {
return data.id is string && data.id.size() > 0 && data.id.size() <= 128
&& data.senderId is string && data.senderId.size() > 0 && data.senderId.size() <= 128
&& data.senderName is string && data.senderName.size() > 0 && data.senderName.size() <= 128;
}
function isValidCommunityMessage(data) {
return (!('id' in data) || (data.id is string && data.id.size() <= 128))
&& (!('senderId' in data) || (data.senderId is string && data.senderId.size() <= 128))
&& (!('senderName' in data) || (data.senderName is string && data.senderName.size() <= 128))
&& (!('text' in data) || (data.text is string && data.text.size() <= 30000))
&& (!('timestamp' in data) || (data.timestamp is string && data.timestamp.size() <= 100))
&& (!('ciphertext' in data) || data.ciphertext == null || (data.ciphertext is string && data.ciphertext.size() <= 65536))
&& (!('iv' in data) || data.iv == null || (data.iv is string && data.iv.size() <= 100))
&& (!('isE2ee' in data) || data.isE2ee is bool)
&& (!('gifUrl' in data) || data.gifUrl == null || (data.gifUrl is string && data.gifUrl.size() <= 2048))
&& (!('file' in data) || data.file == null || data.file is map)
&& (!('reactions' in data) || data.reactions == null || data.reactions is map)
&& (!('senderUsername' in data) || (data.senderUsername is string && data.senderUsername.size() <= 100))
&& (!('senderRealName' in data) || (data.senderRealName is string && data.senderRealName.size() <= 128))
&& (!('senderRealEmail' in data) || (data.senderRealEmail is string && data.senderRealEmail.size() <= 200))
&& (!('senderRole' in data) || (data.senderRole is string && data.senderRole.size() <= 50))
&& (!('senderBio' in data) || (data.senderBio is string && data.senderBio.size() <= 2000))
&& (!('avatar' in data) || (data.avatar is string && data.avatar.size() <= 100))
&& (!('avatarUrl' in data) || (data.avatarUrl is string && data.avatarUrl.size() <= 2048));
}
// Official Notices Validator
function isValidNotice(data) {
return (!('id' in data) || (data.id is string && data.id.size() <= 128))
&& (!('title' in data) || (data.title is string && data.title.size() <= 500))
&& (!('description' in data) || (data.description is string && data.description.size() <= 65536))
&& (!('refNo' in data) || (data.refNo is string && data.refNo.size() <= 128))
&& (!('department' in data) || (data.department is string && data.department.size() <= 200))
&& (!('priority' in data) || (data.priority is string && data.priority.size() <= 50))
&& (!('audience' in data) || (data.audience is string && data.audience.size() <= 100))
&& (!('signatory' in data) || (data.signatory is string && data.signatory.size() <= 200))
&& (!('issuedAt' in data) || (data.issuedAt is string && data.issuedAt.size() <= 100))
&& (!('acknowledgedCount' in data) || data.acknowledgedCount is int || data.acknowledgedCount is float)
&& (!('likes' in data) || data.likes is list)
&& (!('reactions' in data) || data.reactions is map)
&& (!('comments' in data) || data.comments is list)
&& (!('attachment' in data) || data.attachment == null || data.attachment is string || data.attachment is map);
}
// User Profile Validator
function isValidUser(data) {
return (!('id' in data) || (data.id is string && data.id.size() <= 128))
&& (!('name' in data) || (data.name is string && data.name.size() <= 200))
&& (!('username' in data) || (data.username is string && data.username.size() <= 100))
&& (!('email' in data) || (data.email is string && data.email.size() <= 200))
&& (!('bio' in data) || (data.bio is string && data.bio.size() <= 2000))
&& (!('role' in data) || (data.role is string && data.role.size() <= 50))
&& (!('status' in data) || (data.status is string && data.status.size() <= 50))
&& (!('canPost' in data) || data.canPost is bool)
&& (!('isSuperAdmin' in data) || data.isSuperAdmin is bool)
&& (!('avatar' in data) || (data.avatar is string && data.avatar.size() <= 100))
&& (!('avatarUrl' in data) || (data.avatarUrl is string && data.avatarUrl.size() <= 2048))
&& (!('createdAt' in data) || data.createdAt is int || data.createdAt is float || data.createdAt is timestamp || data.createdAt is string)
&& (!('privacyConsentGiven' in data) || data.privacyConsentGiven is bool)
&& (!('privacyConsentDate' in data) || (data.privacyConsentDate is string && data.privacyConsentDate.size() <= 100))
&& (!('password' in data)); // Invariant: Plain-text passwords must never be stored in cloud database
}
// ---------------------------------------------------------------
// 1. Campus Community Lounge Messages (WhatsApp-Style Live Chat)
// ---------------------------------------------------------------
match /cnms_community_messages/{messageId} {
// Real-time snapshot listeners active across all devices (phones, laptops, browsers)
allow read: if true;
// Broadcast new message from any verified campus device
allow create: if hasRequiredMessageFields(request.resource.data)
&& isValidCommunityMessage(request.resource.data);
// Support multi-device reactions, edits, and merge syncs
allow update: if isValidCommunityMessage(request.resource.data);
// Author or admin message deletion
allow delete: if true;
}
// ---------------------------------------------------------------
// 2. Official Circulars & Notices Collection
// ---------------------------------------------------------------
match /cnms_notices/{noticeId} {
// Instant public/campus-wide circular broadcasting
allow read: if true;
// Notice creation by campus administration & faculty publishers
allow create: if isValidNotice(request.resource.data);
// Interactions: Emoji reactions, likes, read receipts, and inquiries
allow update: if isValidNotice(request.resource.data);
// Removal of official notices
allow delete: if true;
}
// ---------------------------------------------------------------
// 3. Campus User Profiles Collection
// ---------------------------------------------------------------
match /cnms_users/{userId} {
// Campus directory synchronization across devices
allow read: if true;
// User registration and cloud profile creation
allow create: if isValidUser(request.resource.data);
// Profile updates, status clearances, and cross-device sync
allow update: if isValidUser(request.resource.data);
// Account removal
allow delete: if true;
}
// ---------------------------------------------------------------
// Default Root Deny (Defense in Depth)
// ---------------------------------------------------------------
match /{document=**} {
allow read, write: if false;
}
}
}