Firestore security rules are not being respected


I’m creating a website with a whitelabel system that I need to limit the access to the firestore collections and documents based on a Role system.

The rules are:

  1. Users will receive a custom claim called “role” that is the ID of a
    document of the role collection
  2. This Role document can allow the user read, write, delete
    documents in a specific collection and also could limit this user
    to a single whitelabel if the field whitelabelId is set.
  3. If the user can read a collection, there is a whitebaleId set and
    the documents of the collection have a whitelabelId field set, this
    user can read ONLY the documentos associated to his whitelabel.
  4. If a user can read a collection and there is no whitelabelId set in
    his associated Role document, this user will be able to read all the documents
    in this collection
  5. If the user have a whitelabelId set in his Role but the collection
    that he is reading have no whitelabelId in their docs set, this user
    will be able to read all the documents

What is the issue?

I have a user that can read the videos collection and this user is associated to a specific whitelabel, so, this user should be reading ONLY the videos that belong to this whitelabel but this is not working as expected. The user is being able to read all docs.

Here it is the rules:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function isAuthenticated() {
return request.auth != null;
}
function isAdmin() {
return request.auth != null && request.auth.token.role == 'Admin';
}
function canRead(collection){
let roleDoc = get(/databases/$(database)/documents/roles/$(request.auth.token.role)).data;
// User can read the collection based on the Role Doc
let userCanReadCollection = roleDoc.collections[collection].read == true;
// Role Doc does not contain the whitelabelId field
let roleIsBasedOnWhitelabel = !("whitelabelId" in roleDoc);
// whitelabelId field is not in the retrieved doc
let docDoesNotContainWhitelabelIdField = !("whitelabelId" in resource.data);
// Retrieved doc whitelabelId is the same of the role whitelabelId
let docIsTheSameWhitelabelId = resource.data.whitelabelId == roleDoc.whitelabelId;
return isAdmin() || (isAuthenticated() && userCanReadCollection && (roleIsBasedOnWhitelabel || docDoesNotContainWhitelabelIdField || docIsTheSameWhitelabelId));
}
match /videos/{videoId}{
allow read: if canRead('videos');
allow create: if canCreate('videos');
allow update: if canUpdate('videos');
allow delete: if canDelete('videos');
}
</code>
<code>function isAuthenticated() { return request.auth != null; } function isAdmin() { return request.auth != null && request.auth.token.role == 'Admin'; } function canRead(collection){ let roleDoc = get(/databases/$(database)/documents/roles/$(request.auth.token.role)).data; // User can read the collection based on the Role Doc let userCanReadCollection = roleDoc.collections[collection].read == true; // Role Doc does not contain the whitelabelId field let roleIsBasedOnWhitelabel = !("whitelabelId" in roleDoc); // whitelabelId field is not in the retrieved doc let docDoesNotContainWhitelabelIdField = !("whitelabelId" in resource.data); // Retrieved doc whitelabelId is the same of the role whitelabelId let docIsTheSameWhitelabelId = resource.data.whitelabelId == roleDoc.whitelabelId; return isAdmin() || (isAuthenticated() && userCanReadCollection && (roleIsBasedOnWhitelabel || docDoesNotContainWhitelabelIdField || docIsTheSameWhitelabelId)); } match /videos/{videoId}{ allow read: if canRead('videos'); allow create: if canCreate('videos'); allow update: if canUpdate('videos'); allow delete: if canDelete('videos'); } </code>
function isAuthenticated() {
  return request.auth != null;
}
function isAdmin() {
  return request.auth != null && request.auth.token.role == 'Admin';
}

function canRead(collection){
  let roleDoc = get(/databases/$(database)/documents/roles/$(request.auth.token.role)).data;      
  // User can read the collection based on the Role Doc
  let userCanReadCollection = roleDoc.collections[collection].read == true;
  // Role Doc does not contain the whitelabelId field
  let roleIsBasedOnWhitelabel = !("whitelabelId" in roleDoc);
  // whitelabelId field is not in the retrieved doc
  let docDoesNotContainWhitelabelIdField = !("whitelabelId" in resource.data);
  // Retrieved doc whitelabelId is the same of the role whitelabelId
  let docIsTheSameWhitelabelId = resource.data.whitelabelId == roleDoc.whitelabelId;
  return isAdmin() || (isAuthenticated() && userCanReadCollection && (roleIsBasedOnWhitelabel || docDoesNotContainWhitelabelIdField || docIsTheSameWhitelabelId));
}

match /videos/{videoId}{
  allow read: if canRead('videos');
  allow create: if canCreate('videos');
  allow update: if canUpdate('videos');   
  allow delete: if canDelete('videos');
}

Here it is the Role document of this user:

Here it is an example of a video document:

So, what am I doing wrong to allow this user read all the documents?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật