我可以重叠Firestore安全规则以防止到处使用或操作员吗?

发布于 2025-02-07 04:04:48 字数 1527 浏览 2 评论 0原文

我有很多收藏品来定义安全规则,它们都有逻辑运算符以说明管理权的权利。这就是这样一个示例:

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        function isAdmin() {
            return request.auth != null && get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.admin == true;
        }

        function isSignedIn() {
            return request.auth != null;
        }

        function isOwnDocument() {
            return request.auth.uid == request.resource.id
        }

        match /userSettings/{doc} {
            allow read: if isOwnDocument() || isAdmin();
            allow write: if isOwnDocument() || isAdmin();
        }
    }
}

我可以提前声明管理权并省略操作员吗?

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        function isAdmin() {
            return request.auth != null && get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.admin == true;
        }

        function isSignedIn() {
            return request.auth != null;
        }

        function isOwnDocument() {
            return request.auth.uid == request.resource.id
        }

        match /{document=**} {
            allow read: if isAdmin();
            allow write: if isAdmin();
        }

        match /userSettings/{doc} {
            allow read: if isOwnDocument();
            allow write: if isOwnDocument();
        }
    }
}

这是一个好习惯吗?

I have a bunch of collections to define security rules for and they all have logical OR operators in them to account for admin rights. Here is one such example:

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        function isAdmin() {
            return request.auth != null && get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.admin == true;
        }

        function isSignedIn() {
            return request.auth != null;
        }

        function isOwnDocument() {
            return request.auth.uid == request.resource.id
        }

        match /userSettings/{doc} {
            allow read: if isOwnDocument() || isAdmin();
            allow write: if isOwnDocument() || isAdmin();
        }
    }
}

Could I, instead, declare admin rights up front and omit the OR operator?

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        function isAdmin() {
            return request.auth != null && get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.admin == true;
        }

        function isSignedIn() {
            return request.auth != null;
        }

        function isOwnDocument() {
            return request.auth.uid == request.resource.id
        }

        match /{document=**} {
            allow read: if isAdmin();
            allow write: if isAdmin();
        }

        match /userSettings/{doc} {
            allow read: if isOwnDocument();
            allow write: if isOwnDocument();
        }
    }
}

Would this be a good practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末骤雨初歇 2025-02-14 04:04:48

我可以提前声明管理员权利并省略或操作员吗?

是的!

我刚刚检查了Firebase控制台中的规则操场。

尝试规则游乐场,转到规则云firestore的选项卡,然后单击“规则”游乐场。您将看到它左下是规则编辑器。在那里,您可以构建数据和身份验证的状态,然后从那里模拟请求。

这是一个好习惯吗?

是的,当然。这将使您的Firestore规则更加清洁。因此,您无需在每场比赛中放置||(OR)。

说明

来自firebase docs:

文档可以匹配多个匹配语句。如果多个允许表达式匹配请求,则允许访问如果任何条件为true

因此当isadmin()()为TRUE时(并且由于所有文档的预先声明),Firestore将会允许访问文件。

从问题评论...

您可以通过自定义索赔设置管理员来详细说明您的意思吗?

自定义主张是Firebase中的一个功能,您可以将不同的角色分配给不同的角色用户。您可以使用自定义索赔来设置管理员特权,教师许可或应用程序所需的任何角色。

自定义索赔为有效的JSON(最好是带有布尔值的键值对)。

可以在firebase的不同部分访问用户上的自定义索赔:

  1. 在客户端,idtoken
  2. 在管理员SDK中,通过auth.token.token
  3. 在安全规则中(用于Firestore,实时数据库和存储)。

因此,在Firebase的这两个部分中,您可以在身份验证的用户上检查自定义索赔,以使他们可以访问资源是否访问。

请注意,自定义索赔并不意味着用作数据存储,以将用户对象从firebase身份验证中扩展。他们主要是为了安全规则。 Firebase将拒绝超过1000个字节的自定义索赔。

另外,您只能在管理员SDK中管理项目的自定义索赔。

了解自定义索赔在这里

Could I, instead, declare admin rights up front and omit the OR operator?

Yes!

I just checked the Rules Playground in the Firebase Console.

enter image description here

To try the Rules Playground, go to the Rules tab of Cloud Firestore and click on Rules Playground. You will see it bottom left to the Rules Editor. There, you can build the data and an authenticated state and from there simulate a request.

Would this be a good practice?

Yes, certainly. It will make your Firestore Rules cleaner. So you won't need to be putting || (or) in every match.

Explanation

From the Firebase Docs:

It's possible for a document to match more than one match statement. In the case where multiple allow expressions match a request, the access is allowed if any of the conditions is true

So when isAdmin() is true (and because it is declared upfront for all documents), Firestore will allow access for the documents.

From the question comments ...

can you elaborate on what you mean by setting admin through custom claims?

Custom Claims is a feature in Firebase where you can assign different roles to users. You can use custom claims to set admin privileges, teacher permissions, or any role your app needs.

Custom Claims take valid JSON (preferably key-value pairs with boolean values).

Custom Claims on a user can be accessed in different parts of Firebase:

  1. In the clients, through idToken,
  2. In the admin SDK, through auth.token, and
  3. In the security rules (for Firestore, Realtime Database, and Storage).

So, in either of these parts of Firebase, you can check the custom claims on an authenticated user to give them access to resources or not.

Note that custom claims are not meant to be used as a store of data to extend the User object from Firebase Authentication. They are primarily there for security rules. Firebase will reject custom claims that are more than 1000 bytes.

Also, you can only manage custom claims on your projects in the admin SDK.

Learn about custom claims here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文