要在Firestore中添加/更新/删除记录,我可以在执行此操作的应用程序中运行代码,也可以调用处理该操作的云函数。对我的应用程序来说,用云功能的方式做这件事“安全”吗?这样会花费更长的时间,但我担心Firebase会受到某种程度的攻击。我曾经读到过,为了制作一个非常安全的应用程序,您应该假设某个恶意用户已经反向设计了您的应用程序。我是否应该假设使用云函数进行所有的修改都是最安全的方法,还是没有什么区别?
使用客户端代码的:
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]
}发布于 2022-09-09 02:50:32
我同意道格·史蒂文森的回答。这还取决于您调用应用程序中的操作的频率以及速度对您或您的用户的重要程度。
我有一个已经建立的颤振应用程序,它主要使用Flutterfire直接调用Firestore,但在某些情况下调用云功能。如果云函数最近没有被同一用户或其他用户调用,则在第一次调用云函数时会出现明显的延迟。
我建议在以下情况下使用直接Firestore访问:
Doug指出,无论是直接访问还是通过云函数进行访问,都存在需要管理的漏洞。
https://stackoverflow.com/questions/73656436
复制相似问题