เปิดใช้งานการบังคับใช้ App Check สำหรับ Cloud Functions

เมื่อคุณ เข้าใจว่าการตรวจสอบแอปจะส่งผลต่อผู้ใช้ของคุณอย่างไร และคุณพร้อมที่จะดำเนินการต่อ คุณสามารถเปิดใช้การบังคับใช้การตรวจสอบแอปได้

เปิดใช้งานการบังคับใช้

หากต้องการเริ่มบังคับใช้ข้อกำหนดโทเค็น App Check ใน Cloud Functions ที่เรียกได้ ให้แก้ไขฟังก์ชันเพื่อตรวจสอบโทเค็น App Check ที่ถูกต้องตามที่แสดงด้านล่าง เมื่อคุณเปิดใช้งานการบังคับใช้ คำขอที่ไม่ได้รับการยืนยันทั้งหมดจะถูกปฏิเสธ

  1. ติดตั้ง Cloud Functions SDK

    Node.js (รุ่นที่ 1)

    อัปเดตการพึ่งพา firebase-functions ของโครงการเป็นเวอร์ชัน 4.0.0 หรือใหม่กว่า:

    npm install firebase-functions@">=4.0.0"
    

    Node.js (รุ่นที่ 2)

    อัปเดตการพึ่งพา firebase-functions ของโครงการเป็นเวอร์ชัน 4.0.0 หรือใหม่กว่า:

    npm install firebase-functions@">=4.0.0"
    

    ไพธอน (ดูตัวอย่าง)

    เพิ่ม firebase-functions ให้กับ functions/requirements.txt :

    firebase-functions >= 0.1.0
    

    จากนั้น อัปเดตการอ้างอิงในสภาพแวดล้อมเสมือนของโครงการของคุณ:

    ./venv/bin/pip install -r requirements.txt
    
  2. เปิดใช้ตัวเลือกรันไทม์การบังคับใช้ App Check สำหรับฟังก์ชันของคุณ:

    Node.js (รุ่นที่ 1)

    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)

    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.
        ...
      }
    );
    

    ไพธอน (ดูตัวอย่าง)

    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
    

เมื่อปรับใช้การเปลี่ยนแปลงเหล่านี้แล้ว ฟังก์ชันคลาวด์ที่เรียกได้ของคุณจะต้องใช้โทเค็น App Check ที่ถูกต้อง SDK ของไคลเอ็นต์ Cloud Functions จะแนบโทเค็นการตรวจสอบแอปโดยอัตโนมัติเมื่อคุณเรียกใช้ฟังก์ชันที่เรียกได้

การป้องกันการเล่นซ้ำ (เบต้า)

เพื่อป้องกันฟังก์ชันที่เรียกได้จากการโจมตีซ้ำ คุณสามารถใช้โทเค็น App Check หลังจากยืนยันแล้ว เมื่อใช้โทเค็นแล้ว จะไม่สามารถนำมาใช้ได้อีก

โปรดทราบว่าการใช้การป้องกันการเล่นซ้ำจะเพิ่มการวนรอบเครือข่ายไปยังการตรวจสอบโทเค็น และเพิ่มเวลาแฝงให้กับการเรียกใช้ฟังก์ชันคลาวด์ ด้วยเหตุผลนี้ แอปส่วนใหญ่มักจะเปิดใช้การป้องกันการเล่นซ้ำบนเอ็นด์พอยต์ที่มีความละเอียดอ่อนเป็นพิเศษเท่านั้น

ในการใช้โทเค็น:

  1. ใน คอนโซลระบบคลาวด์ ให้มอบบทบาท "ตัวตรวจสอบโทเค็นการตรวจสอบแอป Firebase" ให้กับบัญชีบริการที่ใช้โดย Cloud Function

    • หากคุณกำลังเริ่มต้น Admin SDK อย่างชัดแจ้ง และคุณระบุข้อมูลประจำตัวของบัญชีบริการ Admin SDK ของโครงการ แสดงว่าได้รับบทบาทที่จำเป็นแล้ว
    • หากคุณกำลังใช้ Cloud Functions รุ่นที่ 1 กับการกำหนดค่า Admin SDK เริ่มต้น ให้มอบบทบาทให้กับ บัญชีบริการเริ่มต้นของ App Engine โปรดดูที่ การเปลี่ยนสิทธิ์ของบัญชีบริการ
    • หากคุณกำลังใช้ Cloud Functions รุ่นที่ 2 ที่มีการกำหนดค่า Admin SDK เริ่มต้น ให้มอบบทบาทให้กับ บัญชีบริการประมวลผลเริ่มต้น
  2. ตั้งค่า consumeAppCheckToken ให้ true ในนิยามฟังก์ชันของคุณ:

    Node.js (รุ่นที่ 1)

    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)

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