Um mit der Durchsetzung der App-Check-Token-Anforderungen in Ihren aufrufbaren Cloud-Funktionen zu beginnen, ändern Sie Ihre Funktionen so, dass sie nach gültigen App-Check-Token suchen.
Bevor Sie beginnen
Aktivieren Sie App Check in Ihren Apple- , Android- und Web- Clients.
App Check-Unterstützung zu einer Funktion hinzufügen
Aktualisieren Sie die Abhängigkeit der
firebase-functions
Ihres Projekts auf Version 3.14.0 oder höher:npm install firebase-functions@">=3.14.0"
Und aktualisieren Sie die
firebase-admin
Abhängigkeit Ihres Projekts auf Version 9.8.0 oder höher:npm install firebase-admin@">=9.8.0"
Fügen Sie Ihrer Funktion eine Überprüfung auf
context.app
. Ihre Funktion sollte fehlschlagen, wenncontext.app
nicht definiert ist.exports.yourCallableFunction = functions.https.onCall((data, context) => { // context.app will be undefined if the request doesn't include an // App Check token. (If the request includes an invalid App Check // token, the request will be rejected with HTTP error 401.) if (context.app == undefined) { throw new functions.https.HttpsError( 'failed-precondition', 'The function must be called from an App Check verified app.') } // Your function logic follows. });
(Optional) Wenn Sie ungültige App Check-Token mit Ihrer eigenen Logik handhaben möchten (wenn Sie beispielsweise ungültige Anforderungen vorübergehend protokollieren und nicht ablehnen möchten, bevor Sie die vollständige Erzwingung aktivieren), setzen
allowInvalidAppCheckToken
auftrue
:exports.yourCallableFunction = functions. .runWith({ allowInvalidAppCheckToken: true // Opt-out: Requests with invalid App // Check tokens continue to your code. }) .https.onCall((data, context) => { // Now, requests with an invalid App Check token are not rejected. // // context.app will be undefined if the request: // 1) Does not include an App Check token // 2) Includes an invalid App Check token if (context.app == undefined) { // You can inspect the raw request header to check whether an App // Check token was provided in the request. If you're not ready to // fully enable App Check yet, you could log these conditions instead // of throwing errors. const rawToken = context.rawRequest.header['X-Firebase-AppCheck']; if (rawToken == undefined) { throw new functions.https.HttpsError( 'failed-precondition', 'The function must be called from an App Check verified app.' ); } else { throw new functions.https.HttpsError( 'unauthenticated', 'Provided App Check token failed to validate.' ); } } // Your function logic follows. });
Um den vollständigen App Check-Schutz zu aktivieren, setzen
allowInvalidAppCheckToken
auffalse
.Stellen Sie Ihre Funktionen neu bereit:
firebase deploy --only functions
Sobald diese Änderungen bereitgestellt sind, benötigen Ihre aufrufbaren Cloud-Funktionen gültige App Check-Token. Die Cloud Functions-Client-SDKs hängen automatisch ein App Check-Token an, wenn Sie eine aufrufbare Funktion aufrufen.