Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
137 lines (121 loc) · 7.55 KB
/
Copy pathfirestore.rules
File metadata and controls
137 lines (121 loc) · 7.55 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
/**
* @file Firestore Security Rules for ContentFlow AI.
*
* Core Philosophy:
* This ruleset enforces a collaborative workspace model with strict user-ownership for personal data.
* Workspaces are the top-level entities, and access to workspaces and their subcollections (channels, messages, contentIdeas)
* is controlled by a membership list. User data is private and only accessible to the user themselves.
*
* Data Structure:
* /users/{userId}: Stores user profiles. Only the user can read/write their own profile.
* /workspaces/{workspaceId}: Stores workspace data, including a list of member IDs.
* /workspaces/{workspaceId}/channels/{channelId}: Stores channel data within a workspace, including a list of member IDs.
* /workspaces/{workspaceId}/channels/{channelId}/messages/{messageId}: Stores messages within a channel.
* /workspaces/{workspaceId}/channels/{channelId}/contentIdeas/{contentIdeaId}: Stores content ideas generated from messages.
*
* Key Security Decisions:
* - User listing is disallowed to protect user privacy.
* - All write operations to workspaces, channels, messages, and contentIdeas require the user to be a member of the workspace.
* - Data required for authorization (member lists) is denormalized onto the documents being secured.
*/
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
/**
* @description Allows a user to read and write their own user profile.
* @path /users/{userId}
* @allow (get, create, update, delete) if request.auth.uid == userId
* @deny (get, create, update, delete) if request.auth.uid != userId
* @principle Enforces document ownership for writes.
*/
match /users/{userId} {
function isOwner(userId) {
return request.auth.uid == userId;
}
allow get: if isOwner(userId);
allow list: if false; // User listing is not allowed for privacy.
allow create: if isOwner(userId) && request.resource.data.id == userId;
allow update: if isOwner(userId) && resource.data.id == request.resource.data.id;
allow delete: if isOwner(userId) && resource != null;
}
/**
* @description Allows members of a workspace to read and write workspace data.
* @path /workspaces/{workspaceId}
* @allow (get, list) if request.auth.uid in resource.data.memberIds
* @allow (create) if request.auth.uid == request.resource.data.ownerId && request.auth.uid in request.resource.data.memberIds
* @allow (update, delete) if request.auth.uid in resource.data.memberIds && resource != null
* @deny (create, update, delete) if request.auth.uid not in resource.data.memberIds
* @principle Enforces shared access for collaborators using member list.
*/
match /workspaces/{workspaceId} {
function isWorkspaceMember() {
return request.auth.uid in resource.data.memberIds;
}
allow get: if isWorkspaceMember();
allow list: if request.auth != null; // Allows listing of workspaces.
allow create: if request.auth != null && request.auth.uid == request.resource.data.ownerId && request.auth.uid in request.resource.data.memberIds;
allow update: if isWorkspaceMember() && resource != null;
allow delete: if isWorkspaceMember() && resource != null;
}
/**
* @description Allows members of a channel to read and write channel data.
* @path /workspaces/{workspaceId}/channels/{channelId}
* @allow (get, list) if request.auth.uid in resource.data.memberIds
* @allow (create) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)).data.memberIds && request.resource.data.workspaceId == workspaceId
* @allow (update, delete) if request.auth.uid in resource.data.memberIds && resource != null
* @deny (create, update, delete) if request.auth.uid not in resource.data.memberIds
* @principle Enforces shared access for collaborators using member list.
*/
match /workspaces/{workspaceId}/channels/{channelId} {
function isWorkspaceMember() {
return request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)).data.memberIds;
}
function isChannelMember() {
return request.auth.uid in resource.data.memberIds;
}
allow get: if isChannelMember();
allow list: if isWorkspaceMember();
allow create: if isWorkspaceMember() && request.resource.data.workspaceId == workspaceId;
allow update: if isChannelMember() && resource != null;
allow delete: if isChannelMember() && resource != null;
}
/**
* @description Allows members of a channel to read and write messages.
* @path /workspaces/{workspaceId}/channels/{channelId}/messages/{messageId}
* @allow (get, list) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @allow (create) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @allow (update, delete) if request.auth.uid == resource.data.userId && request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds && resource != null
* @deny (create, update, delete) if request.auth.uid not in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @principle Enforces shared access for collaborators using member list.
*/
match /workspaces/{workspaceId}/channels/{channelId}/messages/{messageId} {
function isChannelMember() {
return request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds;
}
allow get: if isChannelMember();
allow list: if isChannelMember();
allow create: if isChannelMember() && request.resource.data.channelId == channelId && request.resource.data.userId == request.auth.uid;
allow update: if isChannelMember() && request.auth.uid == resource.data.userId && resource != null;
allow delete: if isChannelMember() && request.auth.uid == resource.data.userId && resource != null;
}
/**
* @description Allows members of a channel to read and write content ideas.
* @path /workspaces/{workspaceId}/channels/{channelId}/contentIdeas/{contentIdeaId}
* @allow (get, list) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @allow (create) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @allow (update, delete) if request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds && resource != null
* @deny (create, update, delete) if request.auth.uid not in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds
* @principle Enforces shared access for collaborators using member list.
*/
match /workspaces/{workspaceId}/channels/{channelId}/contentIdeas/{contentIdeaId} {
function isChannelMember() {
return request.auth.uid in get(/databases/$(database)/documents/workspaces/$(workspaceId)/channels/$(channelId)).data.memberIds;
}
allow get: if isChannelMember();
allow list: if isChannelMember();
allow create: if isChannelMember() && request.resource.data.channelId == channelId;
allow update: if isChannelMember() && resource != null;
allow delete: if isChannelMember() && resource != null;
}
}
}