Aktivieren Sie die App Check-Erzwingung für Cloud Functions

Wenn Sie verstehen, wie sich App Check auf Ihre Benutzer auswirkt, und Sie bereit sind, fortzufahren, können Sie die Durchsetzung von App Check aktivieren.

Durchsetzung ermöglichen

Um mit der Durchsetzung der App-Check-Token-Anforderungen in Ihren aufrufbaren Cloud-Funktionen zu beginnen, ändern Sie Ihre Funktionen, um nach gültigen App-Check-Tokens zu suchen, wie unten gezeigt. Sobald Sie die Durchsetzung aktivieren, werden alle nicht bestätigten Anfragen abgelehnt.

  1. Installieren Sie das Cloud Functions SDK.

    Node.js (1. Generation)

    Aktualisieren Sie firebase-functions Abhängigkeit Ihres Projekts auf Version 4.0.0 oder höher:

    npm install firebase-functions@">=4.0.0"
    

    Node.js (2. Generation)

    Aktualisieren Sie firebase-functions Abhängigkeit Ihres Projekts auf Version 4.0.0 oder höher:

    npm install firebase-functions@">=4.0.0"
    

    Python (Vorschau)

    Fügen Sie firebase-functions zu functions/requirements.txt hinzu:

    firebase-functions >= 0.1.0
    

    Aktualisieren Sie dann die Abhängigkeiten in der virtuellen Umgebung Ihres Projekts:

    ./venv/bin/pip install -r requirements.txt
    
  2. Aktivieren Sie die Laufzeitoption „App Check Enforcement“ für Ihre Funktion:

    Node.js (1. Generation)

    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 (2. Generation)

    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 (Vorschau)

    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. Stellen Sie Ihre Funktionen erneut bereit:

    firebase deploy --only functions
    

Sobald diese Änderungen bereitgestellt sind, benötigen Ihre aufrufbaren Cloud-Funktionen gültige App Check-Tokens. Die Cloud Functions-Client-SDKs hängen automatisch ein App Check-Token an, wenn Sie eine aufrufbare Funktion aufrufen.

Wiedergabeschutz (Beta)

Um eine aufrufbare Funktion vor Replay-Angriffen zu schützen, können Sie das App Check-Token nach der Überprüfung nutzen. Sobald der Token verbraucht ist, kann er nicht erneut verwendet werden.

Beachten Sie, dass die Verwendung des Wiedergabeschutzes einen Netzwerk-Roundtrip zur Token-Überprüfung hinzufügt und daher die Latenz für den Cloud-Funktionsaufruf erhöht. Aus diesem Grund aktivieren die meisten Apps den Wiedergabeschutz normalerweise nur auf besonders sensiblen Endpunkten.

So verbrauchen Sie Token:

  1. Weisen Sie in der Cloud-Konsole dem Dienstkonto, das von der Cloud-Funktion verwendet wird, die Rolle „Firebase App Check Token Verifier“ zu.

    • Wenn Sie das Admin SDK explizit initialisieren und die Anmeldeinformationen für das Admin SDK-Dienstkonto Ihres Projekts angegeben haben, ist die erforderliche Rolle bereits gewährt.
    • Wenn Sie Cloud Functions der 1. Generation mit der standardmäßigen Admin SDK-Konfiguration verwenden, weisen Sie die Rolle dem App Engine-Standarddienstkonto zu. Siehe Dienstkontoberechtigungen ändern .
    • Wenn Sie Cloud Functions der 2. Generation mit der Standard-Admin-SDK-Konfiguration verwenden, weisen Sie die Rolle dem Standard-Rechendienstkonto zu.
  2. Setzen Sie consumeAppCheckToken in Ihrer Funktionsdefinition auf true :

    Node.js (1. Generation)

    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 (2. Generation)

    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. Aktualisieren Sie Ihren App-Client-Code, um Verbrauchstoken mit begrenzter Nutzung zu erhalten, wenn Sie die Funktion aufrufen:

    Schnell

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

    Modulare Web-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();