匿名身份验证够吗?

时间:2020-03-09 23:20:13

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

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

1 个答案:

答案 0 :(得分:2)

通常,不是

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

假设我们从以下准系统规则开始:

// 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;
      }
    }
  }
}

接下来,您应该考虑按照Firebase文档的granular security rule list中的说明,添加规则,限制使用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 Storage上,或者甚至可以从Firebase Hosting提供数据,而CDN可以代替应用程序为它们提供数据。