簡體   English   中英

使用 Cloud Functions 執行 Firestore 操作是否更安全?

[英]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 訪問:

  1. 速度對您的用戶體驗很重要;
  2. 您可以實施有效的安全規則來防范風險

正如 Doug 指出的,直接訪問和通過雲訪問 function 仍然存在需要管理的漏洞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM