限制对 admin-sdk 的 Firestore 访问 [英] Restrict firestore access to admin-sdk

查看:18
本文介绍了限制对 admin-sdk 的 Firestore 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个基于 VueJs(用于 UI)+ NodeJs(用于后端,将在 Google Cloud Platform 中运行)+ Firestore(用于 auth + 数据库).

I am designing an application (let's call it a TodoList app) based on VueJs (for the UI) + NodeJs (for the backend, which will run in Google Cloud Platform) + Firestore (for the auth + database).

我浏览了 Google 的大量文档(有时是多余的!)以实现一些应该可行的东西,但我不确定它是否适用于生产.

I have wandered through the huge documentation of Google (sometimes redundant!) to achieve something that should work but I am not sure it's production-proof.

情况:

  • 由于 Firebase 基于密码的身份验证,用户登录了我的 VueJs 应用(并且用户的凭据,包括他的 accessToken,都存储在我的 Vuex 商店中).
  • Firebase Admin SDK 在我的后端运行.
  • 我的 VueJs 应用正在请求我的后端.
  • 后端验证客户端发送的accessToken-附带请求
  • 借助 Admin SDK,我的后端请求我的 Firestore 数据库

我在 Firestore 数据库上设置了一些安全规则:

I have set some security rules on my Firestore database:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/{document=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

这样我就不希望任何登录的用户访问其他用户的数据.

so that I don't want any logged users to access data from another user.

问题:

由于 Firebase Admin SDK 对我的 Firestore 数据库拥有完全权限,我如何确保不会出现任何安全问题.目前,我只是在验证请求中发送到我的后端的 accessToken,但是......有些事情让我觉得这有问题!

As the Firebase Admin SDK has full privilege on my Firestore database, how can I make sure there won't be any security issues. For now, I am just verifying the accessToken sent in the request to my backend, but ... something makes me feel wrong with this!

代码:

在客户端:

auth.onAuthStateChanged((user) => {
  if (user) {
    // Save the user credentials
  }
}

在服务器端:

// idToken comes from the client app (shown above)
// ...
admin.auth().verifyIdToken(idToken)
  .then(function(decodedToken) {
    var uid = decodedToken.uid;
    // Retrieve or add some data in /users/{userId}/{document=**}
  }).catch(function(error) {
    // Handle error
  });

如您所见,一旦我验证了 accessToken 并检索了 uid,我就可以对我的数据库执行任何操作.

As you can see, once I validate the accessToken and retrieve the uid, I can do anything on my database.

参考资料

感谢您的帮助!

推荐答案

管理 SDK 将始终拥有对数据库的完全访问权限.

由于客户端从不直接访问 Firebase,您应该完全禁用对所有文档的访问.您的 API 仍然可以访问

Since the client is never accessing Firebase directly, you should totally disable access to all documents. Your API will still have access

match /{document=**} {
  allow read, write: if false;
}

这篇关于限制对 admin-sdk 的 Firestore 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆