【问题标题】:Is Anonymous Authentication Enough?匿名身份验证是否足够?
【发布时间】:2020-03-09 23:20:13
【问题描述】:

我正在开发一个不需要登录的应用,因为没有任何特定于用户的数据。我最初的计划是让我的整个数据库只读。但是,在进行一些研究后,我发现这些安全规则会使我的数据库非常容易受到攻击。我的新计划是为每个打开我的应用程序的新用户实施匿名身份验证,然后在他们退出我的应用程序后删除该用户。如果用户经过身份验证,安全规则将只是允许读取。这足以防止有人滥用对我的数据库的查询吗?

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-authentication firebase-security


    【解决方案1】:

    一般不会。

    单独使用匿名身份验证会增加访问数据库的障碍,并且会像您的数据库完全打开一样保护它免受简单的读取查询,但您应该将其与限制可执行查询的安全规则结合起来。

    假设我们从这些准系统规则开始:

    // Allow read access on all documents to any user signed in to the application,
    // and write access to only administrators
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read: if request.auth.uid != null;
          allow write: if request.auth.token.isAdmin === true;
        }
      }
    }
    

    要收紧您的规则,您应该首先删除通配符条目并将其替换为固定的文档路径。

    // Allow read access on all documents at /posts/{postId} to any user signed in to the application,
    // and write access to only administrators
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /posts/{postId} {
          allow read: if request.auth.uid != null;
          allow write: if request.auth.token.isAdmin === true;
        }
      }
    }
    

    甚至

    // Allow read access on all documents at /posts/{postId} to any user signed in to the application,
    // and write access to only administrators
    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /posts/{postId} {
          allow read: if request.auth.uid != null;
          allow write: if request.auth.token.isAdmin === true;
    
          // allow same permissions on subcollections of /posts/{postId}
          match /{document=**} {
            allow read: if request.auth.uid != null;
            allow write: if request.auth.token.isAdmin === true;
          }
        }
      }
    }
    

    接下来,您应该考虑添加规则,以限制使用 granular security rule list 对数据库执行的查询的大小,如 Firebase 文档的 Securely query data 中所述。

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /posts/{postid} {
    
          // Deny any query not limited to 10 or fewer documents
          allow list: if request.auth != null
                      && request.query.limit <= 10;
    
          // Anyone can retrieve an individual post
          allow get: if request.auth != null;
    
          // Only an admin can write to posts
          allow write: if request.auth.token.isAdmin === true;
        }
      }
    }
    

    根据数据更新的频率,您还可以考虑将数据包存储在 Firebase 存储中,或者您甚至可以从 Firebase 托管中提供数据,通过 CDN 而不是您的应用程序来提供这些数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-02
      • 2018-11-19
      • 2013-03-02
      • 2020-09-19
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多