了解 2023 年 Google I/O 大会上介绍的 Firebase 亮点。了解详情

Aktivieren Sie die Erzwingung der App-Prüfung 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.

Um Token zu konsumieren, 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.
    ...
  }
);

Wenn Sie diese Funktion für eine bestimmte Cloud-Funktion aktivieren, müssen Sie auch Ihren App-Client-Code aktualisieren, um verbrauchbare Token mit eingeschränkter 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();