![](/img/trans.png)
[英]Accessing user data in firestore using Cloud Functions, but how do I securely send the UID/IDToken?
[英]Is it safer to do Firestore operations using Cloud Functions?
要在 Firestore 中添加/更新/删除记录,我可以在我的应用程序中运行代码来执行此操作,或者我可以调用云 function 来处理该操作。 我的应用程序以云 function 方式执行它是否“更安全”? 这种方式需要更长的时间,但我担心 Firebase 在某种程度上容易受到攻击。 我曾经读过,要制作一个真正安全的应用程序,您应该假设某些恶意用户已经对您的应用程序进行了反向工程。 我应该假设对 Firestore 的所有更改使用 Cloud Functions 是最安全的路线还是没有区别?
使用来自客户端的代码:
CollectionReference users = FirebaseFirestore.instance.collection('users');
Future<void> addUser() {
// Call the user's CollectionReference to add a new user
return users
.add({
'full_name': fullName, // John Doe
'company': company, // Stokes and Sons
'age': age // 42
})
.then((value) => print("User Added"))
.catchError((error) => print("Failed to add user: $error"));
}
使用云函数:
exports.createUser = functions.https.onCall(async (data, context) => {
const uid = context?.auth?.uid
if (!uid) {
return {result: 0, message: 'You are not authorized.'}
}
const username = data.user
const email = data.email
var result: number
var message: string
//Check to see if that username already exists
const doesUserExist = await TS_doesUserExist(username)
const createUser = await TS_createUser(uid, username, email)
if (doesUserExist[0] == 0) {
if (createUser[0] == 1) {
result = 1
message = ''
} else {
result = 0
message = createUser[1]
}
} else {
result = 0
message = doesUserExist[1]
}
return {result: result, message: message}
})
async function TS_createUser(uid: string, username: string, email: string): Promise<[number, string]> {
var result: number = 0
var message: string = ''
//Create SECURE_userinfo doc
const userInfoDoc = await admin.firestore()
.collection(usersCollPath)
.doc(uid) // use uid as document ID
.collection(secureUserInfoCollPath)
.doc()
.set({
createDate: admin.firestore.FieldValue.serverTimestamp(), //signupdate
email: email,
})
//Create stat doc
const statDoc = await admin.firestore()
.collection(usersCollPath)
.doc(uid)
.collection(statCollPath)
.doc()
.set({
stat: 1,
})
//Create username doc
const usernameDoc = await admin.firestore()
.collection(usersCollPath)
.doc(uid)
.collection(usernameCollPath)
.doc()
.set({
username: username,
})
if (userInfoDoc) {
if (statDoc) {
if (usernameDoc) {
result = 1
message = ''
} else {
result = 0
message = 'Could not create username doc'
}
} else {
result = 0
message = 'Could not create status doc'
}
} else {
result = 0
message = 'Could not create user info doc'
}
return [result, message]
}
这取决于您所说的“安全”是什么意思。 将您的数据库逻辑放在 function 后面只会使攻击者使用 function 进行滥用而不是数据库接口。 它们都是完全可用的攻击媒介。
直接数据库访问(使用安全规则)与云功能之间的主要区别在于,您对在 function 中检查哪些输入具有更强的表达能力,因为您拥有 JavaScript 的全部功能。 这为您提供了比安全规则更多的选择,但在任何情况下都不一定“更安全”。
我同意 Doug Stevenson 的回答。 它还取决于您在应用程序中调用操作的频率以及速度对您或您的用户有多重要。
我有一个已建立的 Flutter 应用程序,它主要使用 Flutterfire 直接调用 Firestore,但在某些情况下调用云 function。 首次调用云 function 如果最近没有被同一用户或其他用户调用,则会出现明显的延迟。
如果出现以下情况,我建议使用直接 Firestore 访问:
正如 Doug 指出的,直接访问和通过云访问 function 仍然存在需要管理的漏洞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.