-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
198 lines (188 loc) · 11 KB
/
firestore.rules
File metadata and controls
198 lines (188 loc) · 11 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
/**
* @fileoverview Firestore Security Rules for Saveur Express.
*
* Core Philosophy: This ruleset enforces a strict owner-only access model for
* customer-related data and allows public read access for restaurant data.
* All writes are protected by authorization checks based on path-based ownership.
*
* Data Structure:
* - Restaurants are stored at the root in `/restaurants/{restaurantId}`.
* - Restaurant-specific data (dishes, orders, feedback, reservations) resides
* in subcollections under each restaurant document.
* - Customer data is stored under `/customers/{customerId}`.
*
* Key Security Decisions:
* - Listing of customer documents is forbidden.
* - Restaurant data is publicly readable.
* - Write access is restricted to owners/authorized users based on the specific
* collection.
*
* Denormalization for Authorization: No explicit denormalization is used in the
* initial ruleset, but it is recommended to denormalize roles into documents
* for collaborative access (e.g., restaurant staff managing tables and orders).
*
* Structural Segregation: Public restaurant information resides in the `restaurants`
* collection, while sensitive customer data is stored in a private subcollection
* under the `/customers/{customerId}` path. This separation ensures that `list`
* operations can be securely implemented based on the specific access requirements
* of each collection.
*/
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
/**
* @description Allows anyone to read restaurant information, but restricts
* writes to authorized users (e.g., restaurant owners or admins - to be defined).
* @path /restaurants/{restaurantId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Allows anyone to read dishes information for a restaurant, but restricts
* writes to authorized users (e.g., restaurant owners or admins).
* @path /restaurants/{restaurantId}/dishes/{dishId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId}/dishes/{dishId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Allows anyone to read table information, but restricts writes
* to authorized users (e.g., restaurant owners or admins).
* @path /tables/{tableId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /tables/{tableId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Allows anyone to read order information for a restaurant, but restricts
* writes to authorized users (e.g., restaurant owners or admins, or the customer who placed the order).
* @path /restaurants/{restaurantId}/orders/{orderId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId}/orders/{orderId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Allows anyone to read order item information for an order, but restricts
* writes to authorized users (e.g., restaurant owners or admins).
* @path /restaurants/{restaurantId}/orders/{orderId}/orderItems/{orderItemId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId}/orders/{orderId}/orderItems/{orderItemId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Restricts access to customer information to the customer themselves.
* @path /customers/{customerId}
* @allow get: if isOwner(customerId);
* @allow list: if false;
* @allow create: if request.auth.uid == customerId;
* @allow update: if isOwner(customerId);
* @allow delete: if isOwner(customerId);
* @principle Enforces document ownership for writes.
*/
match /customers/{customerId} {
allow get: if isOwner(customerId);
allow list: if false;
allow create: if request.auth.uid == customerId;
allow update: if isOwner(customerId);
allow delete: if isOwner(customerId);
}
/**
* @description Allows anyone to read feedback information for a restaurant, but restricts
* writes to authorized users (e.g., restaurant owners or admins, or the customer who provided the feedback).
* @path /restaurants/{restaurantId}/feedback/{feedbackId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId}/feedback/{feedbackId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Restricts access to loyalty points information to the customer themselves.
* @path /customers/{customerId}/loyaltyPoints/{loyaltyPointsId}
* @allow get: if isOwner(customerId);
* @allow list: if isOwner(customerId);
* @allow create: if request.auth.uid == customerId;
* @allow update: if isOwner(customerId);
* @allow delete: if isOwner(customerId);
* @principle Enforces document ownership for writes.
*/
match /customers/{customerId}/loyaltyPoints/{loyaltyPointsId} {
allow get: if isOwner(customerId);
allow list: if isOwner(customerId);
allow create: if request.auth.uid == customerId;
allow update: if isOwner(customerId);
allow delete: if isOwner(customerId);
}
/**
* @description Allows anyone to read reservation information for a restaurant, but restricts
* writes to authorized users (e.g., restaurant owners or admins, or the customer who made the reservation).
* @path /restaurants/{restaurantId}/reservations/{reservationId}
* @allow get, list: if true;
* @allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
* @principle Allows public read access, requires ownership for writes.
*/
match /restaurants/{restaurantId}/reservations/{reservationId} {
allow get, list: if true;
allow create: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow update: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
allow delete: if false; // TODO: Add owner validation once the schema is updated with an ownership field.
}
/**
* @description Checks if the requesting user is the owner of the document.
* @param {string} userId The user ID to check against the request's authentication UID.
* @return {bool} True if the user ID matches the request's authentication UID, false otherwise.
*/
function isOwner(userId) {
return request.auth.uid == userId;
}
}
}