Repository navigation
Expand file tree
/
Copy pathstorage.rules
More file actions
74 lines (62 loc) · 2.98 KB
/
Copy pathstorage.rules
File metadata and controls
74 lines (62 loc) · 2.98 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
// Co-authored-by: Claude Code (Claude Opus 5.5)
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
// Same check as isVerified in firestore.rules: a verified EPFL address.
function isVerified() {
return request.auth != null
&& request.auth.token.email is string
&& request.auth.token.email.lower().matches('.*@epfl[.]ch$')
&& request.auth.token.email_verified == true;
}
function isOwner(uid) {
return isVerified() && request.auth.uid == uid;
}
// Profile photos are uploaded uncompressed, and full-size phone photos can reach 8 MB.
function isImageUpload() {
return request.resource.size < 10 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
}
match /users/{uid}/profile.jpg {
allow read: if isVerified();
allow create, update: if isOwner(uid) && isImageUpload();
allow delete: if isOwner(uid);
}
// At most 5 photos per listing, stored as images/0 to images/4.
match /listings/{uid}/{listingId}/images/{index} {
allow read: if isVerified();
allow create, update: if isOwner(uid) && index.matches('[0-4]') && isImageUpload();
allow delete: if isOwner(uid);
}
// A chat photo, at the imagePath its message names: conversations/{conversationId}/{messageId}.jpg.
match /conversations/{conversationId}/{fileName} {
// A missing conversation has no data, so the lookup errors and the rule fails closed.
function participants() {
return firestore.get(/databases/(default)/documents/conversations/$(conversationId)).data.participants;
}
// The message named by the file, conversations/{conversationId}/messages/{messageId}. A missing
// message has no data, so the lookup errors and the rule fails closed.
function message() {
return firestore.get(/databases/(default)/documents/conversations/$(conversationId)/messages/$(fileName.split('[.]')[0])).data;
}
allow get: if isVerified() && request.auth.uid in participants();
// The app writes the message first and uploads its photo once the server has it. Only the
// message's sender can upload, only at the path the message names: one photo per message.
// The sender is a participant, since firestore.rules only lets participants write messages.
// resource == null refuses an upload over an existing file. The app compresses every chat photo
// to JPEG, so the file's format matches its .jpg name.
allow create: if isVerified()
&& resource == null
&& fileName.matches('[^/.]+[.]jpg')
&& message().senderUid == request.auth.uid
&& message().imagePath == 'conversations/' + conversationId + '/' + fileName
&& isImageUpload()
&& request.resource.contentType == 'image/jpeg';
// A sent photo cannot be swapped or removed.
allow update, delete: if false;
}
match /{allPaths=**} {
allow read, write: if false;
}
}
}