為 Cloud Functions 啟用應用程式檢查強制執行

當您了解 App Check 將如何影響您的使用者並且準備好繼續操作時,您可以啟用 App Check 強制執行。

啟用強制執行

若要開始在可呼叫的 Cloud Functions 中強制執行 App Check 令牌要求,請修改您的函式以檢查有效的 App Check 令牌,如下所示。一旦啟用強制執行,所有未經驗證的請求都將被拒絕。

  1. 安裝雲端函數 SDK。

    Node.js(第一代)

    將專案的firebase-functions相依性更新至版本 4.0.0 或更高版本:

    npm install firebase-functions@">=4.0.0"
    

    Node.js(第二代)

    將專案的firebase-functions相依性更新至版本 4.0.0 或更高版本:

    npm install firebase-functions@">=4.0.0"
    

    Python(預覽)

    firebase-functions加入functions/requirements.txt

    firebase-functions >= 0.1.0
    

    然後,更新專案虛擬環境中的依賴項:

    ./venv/bin/pip install -r requirements.txt
    
  2. 為您的函數啟用 App Check 強制執行時間選項:

    Node.js(第一代)

    const functions = require("firebase-functions/v1");
    
    exports.yourV1CallableFunction = functions
      .runWith({
          enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens.
      })
      .https.onCall((data, context) => {
            // context.app contains data from App Check, including the app ID.
            // Your function logic follows.
            ...
      });
    

    Node.js(第二代)

    const { onCall } = require("firebase-functions/v2/https");
    
    exports.yourV2CallableFunction = onCall(
      {
        enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens.
      },
      (request) => {
        // request.app contains data from App Check, including the app ID.
        // Your function logic follows.
        ...
      }
    );
    

    Python(預覽)

    from firebase_functions import https_fn
    
    @https_fn.on_call(
        enforce_app_check=True  # Reject requests with missing or invalid App Check tokens.
    )
    def your_callable_function(req: https_fn.CallableRequest) -> https_fn.Response:
        # req.app contains data from App Check, including the app ID.
        # Your function logic follows.
        ...
    
  3. 重新部署您的功能:

    firebase deploy --only functions
    

部署這些變更後,您的可呼叫 Cloud Functions 將需要有效的 App Check 令牌。當您呼叫可呼叫函數時,Cloud Functions 用戶端 SDK 會自動附加 App Check 令牌。

重播保護(測試版)

為了保護可呼叫函數免受重播攻擊,您可以在驗證後使用 App Check 令牌。令牌一旦被消耗,就無法再使用。

請注意,使用重播保護會增加令牌驗證的網路往返,因此會增加雲端函數呼叫的延遲。因此,大多數應用程式通常僅在特別敏感的端點上啟用重播保護。

消耗代幣:

  1. Cloud 控制台中,向 Cloud Function 使用的服務帳號授予「Firebase App Check Token Verifier」角色。

    • 如果您明確初始化 Admin SDK 並指定了專案的 Admin SDK 服務帳戶憑證,則已授予所需的角色。
    • 如果您使用具有預設 Admin SDK 設定的第一代 Cloud Functions,請將角色授予App Engine 預設服務帳戶。請參閱變更服務帳戶權限
    • 如果您使用具有預設 Admin SDK 設定的第二代 Cloud Functions,請將角色授予預設計算服務帳戶
  2. 在函數定義中將consumeAppCheckToken設定為true

    Node.js(第一代)

    const functions = require("firebase-functions/v1");
    
    exports.yourV1CallableFunction = functions
      .runWith({
          enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens.
          consumeAppCheckToken: true  // Consume the token after verification.
      })
      .https.onCall((data, context) => {
          // context.app contains data from App Check, including the app ID.
          // Your function logic follows.
          ...
      });
    

    Node.js(第二代)

    const { onCall } = require("firebase-functions/v2/https");
    
    exports.yourV2CallableFunction = onCall(
      {
        enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens.
        consumeAppCheckToken: true  // Consume the token after verification.
      },
      (request) => {
        // request.app contains data from App Check, including the app ID.
        // Your function logic follows.
        ...
      }
    );
    
  3. 更新您的應用程式用戶端程式碼,以在呼叫該函數時取得可消耗的限制使用令牌:

    迅速

    let options = HTTPSCallableOptions(requireLimitedUseAppCheckTokens: true)
    let yourCallableFunction =
        Functions.functions().httpsCallable("yourCallableFunction", options: options)
    do {
        let result = try await yourCallableFunction.call()
    } catch {
        // ...
    }
    

    網路模組化API

    import { getFunctions, httpsCallable } from "firebase/functions";
    
    const yourCallableFunction = httpsCallable(
      getFunctions(),
      "yourCallableFunction",
      { limitedUseAppCheckTokens: true },
    );
    await yourCallableFunction();
    

    Kotlin+KTX

    val yourCallableFunction = Firebase.functions.getHttpsCallable("yourCallableFunction") {
        limitedUseAppCheckTokens = true
    }
    val result = yourCallableFunction.call().await()
    

    Java

    HttpsCallableReference yourCallableFunction = FirebaseFunctions.getInstance().getHttpsCallable(
            "yourCallableFunction",
            new HttpsCallableOptions.Builder()
                    .setLimitedUseAppCheckTokens(true)
                    .build()
    );
    Task<HttpsCallableResult> result = yourCallableFunction.call();