Firestore security rules: get() use in hasAny() list method

狂风中的少年 提交于 2020-05-16 22:39:09

问题


I was wondering if this security rule would be possible:

function productForUser() {
        return resource.data.products.hasAny(get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.products);
    }

When I try to test it in the testing plaground on the Firebase website, it is sucessful. However, when I try to run it with Javascript, with this query, I get the read denied, with "missing or insufficient permissions":

query.where("products", "array-contains", productId);

I can confirm that the user has the array of products, containg the specific product that is being looked up in the query.

Thanks in advance.


回答1:


Your rule works in the console simulator because the simulator only supports "get" type requests for a single document. It doesn't work for queries because security rules are not filters. The rule will not be evaluated for each and every document in the collection, as that would not scale well at all for very large collections. To specify conditions for queries, you will need to provide exact values to check from the client - you will not be able to use a get() to find other values.

If you want to test queries before publishing your rules, you should not be using the simulator, and instead use the local emulator to test code that actually performs a query.



来源:https://stackoverflow.com/questions/60891502/firestore-security-rules-get-use-in-hasany-list-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!