กฎความปลอดภัยและการตรวจสอบสิทธิ์ Firebase

Firebase Security Rules ให้การควบคุมการเข้าถึงและการตรวจสอบความถูกต้องของข้อมูลในรูปแบบที่รองรับความซับซ้อนหลายระดับ หากต้องการสร้างระบบการเข้าถึงตามผู้ใช้และตามบทบาทที่รักษาข้อมูลของผู้ใช้ให้ปลอดภัย ให้ใช้ Firebase Authentication กับ Firebase Security Rules

ระบุผู้ใช้

Authentication จะระบุผู้ใช้ที่ขอสิทธิ์เข้าถึงข้อมูลของคุณและให้ข้อมูลดังกล่าวเป็นตัวแปรที่คุณสามารถใช้ประโยชน์ในกฎได้ ตัวแปร auth มีข้อมูลต่อไปนี้

  • uid: รหัสผู้ใช้ที่ไม่ซ้ำกันซึ่งกำหนดให้กับผู้ใช้ที่ส่งคำขอ
  • token: แผนที่ของค่าที่รวบรวมโดย Authentication

ตัวแปร auth.token มีค่าต่อไปนี้

ช่อง คำอธิบาย
email อีเมลที่เชื่อมโยงกับบัญชี (หากมี)
email_verified true หากผู้ใช้ยืนยันแล้วว่ามีสิทธิ์เข้าถึงอีเมล email ผู้ให้บริการบางรายจะยืนยันอีเมลที่ตนเป็นเจ้าของโดยอัตโนมัติ
phone_number หมายเลขโทรศัพท์ที่เชื่อมโยงกับบัญชี (หากมี)
name ชื่อที่แสดงของผู้ใช้ หากตั้งค่าไว้
sub UID ของ Firebase ของผู้ใช้ ซึ่งไม่ซ้ำกันภายในโปรเจ็กต์
firebase.identities พจนานุกรมของข้อมูลประจำตัวทั้งหมดที่เชื่อมโยงกับบัญชีของผู้ใช้รายนี้ คีย์ของพจนานุกรมอาจเป็น email, phone, google.com, facebook.com, github.com, twitter.com อย่างใดอย่างหนึ่งต่อไปนี้ ค่าของพจนานุกรมคืออาร์เรย์ของตัวระบุที่ไม่ซ้ำกันสำหรับผู้ให้บริการข้อมูลประจำตัวแต่ละรายที่เชื่อมโยงกับบัญชี เช่น auth.token.firebase.identities["google.com"][0] มีรหัสผู้ใช้ Google แรกที่เชื่อมโยงกับบัญชี
firebase.sign_in_provider ผู้ให้บริการลงชื่อเข้าใช้ที่ใช้เพื่อรับโทเค็นนี้ อาจเป็นสตริงใดสตริงหนึ่งต่อไปนี้ custom, password, phone, anonymous, google.com, facebook.com, github.com, twitter.com
firebase.tenant tenantId ที่เชื่อมโยงกับบัญชี (หากมี) เช่น tenant2-m6tyz

หากต้องการเพิ่มแอตทริบิวต์การตรวจสอบสิทธิ์ที่กำหนดเอง ตัวแปร auth.token จะมีการอ้างสิทธิ์ที่กำหนดเองที่คุณระบุด้วย

เมื่อผู้ใช้ที่ขอสิทธิ์เข้าถึงไม่ได้ลงชื่อเข้าใช้ ตัวแปร auth จะเป็น null คุณสามารถใช้ประโยชน์จากสิ่งนี้ในกฎได้ เช่น หากต้องการจำกัดสิทธิ์การอ่านสำหรับผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว auth != null อย่างไรก็ตาม โดยทั่วไปเราขอแนะนำให้จำกัดสิทธิ์การเขียนเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแปร auth ได้ที่เอกสารอ้างอิงสำหรับ Cloud Firestore, Realtime Database และ Cloud Storage

ใช้ประโยชน์จากข้อมูลผู้ใช้ในกฎ

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

ในกฎ ให้กำหนดวิธีที่ข้อมูลในตัวแปร auth ซึ่งเป็นข้อมูลผู้ใช้ของผู้ขอ ตรงกับข้อมูลผู้ใช้ที่เชื่อมโยงกับข้อมูลที่ขอ

เช่น แอปของคุณอาจต้องการตรวจสอบว่าผู้ใช้สามารถอ่านและเขียนข้อมูลของตนเองได้เท่านั้น ในสถานการณ์นี้ คุณจะต้องจับคู่ระหว่างตัวแปร auth.uid กับรหัสผู้ใช้ในข้อมูลที่ขอ ดังนี้

Cloud Firestore

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

Realtime Database

{
  "rules": {
    "users": {
      "$userId": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($userId)
        ".write": "$userId === auth.uid"
      }
    }
  }
}

Cloud Storage

service firebase.storage {
  // Only a user can upload their file, but anyone can view it
  match /users/{userId}/{fileName} {
    allow read;
    allow write: if request.auth != null && request.auth.uid == userId;
  }
}

กำหนดข้อมูลผู้ใช้ที่กำหนดเอง

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

ตัวอย่างเช่น สมมติว่าคุณต้องการสร้างบทบาท "ผู้ดูแลระบบ" ที่ให้สิทธิ์เขียนในบางเส้นทาง คุณจะกำหนดแอตทริบิวต์ดังกล่าวให้กับผู้ใช้ แล้วใช้ประโยชน์จากแอตทริบิวต์นั้นในกฎที่ให้สิทธิ์เข้าถึงในเส้นทาง

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

Cloud Firestore

service cloud.firestore {
  match /databases/{database}/documents/some_collection: {
    // Remember that, in Cloud Firestore, reads embedded in your rules are billed operations
    write: if request.auth != null && get(/databases/(database)/documents/users/$(request.auth.uid)).data.admin == true;
    read: if request.auth != null;
  }
}

คุณสามารถเข้าถึงการอ้างสิทธิ์ที่กำหนดเองใน Security Rules หลังจากสร้างการอ้างสิทธิ์ที่กำหนดเองใน Authentication จากนั้นคุณจะอ้างอิงการอ้างสิทธิ์ที่กำหนดเองเหล่านั้นได้โดยใช้ตัวแปร auth.token

Cloud Firestore

service cloud.firestore {
  match /databases/{database}/documents {
    // For attribute-based access control, check for an admin claim
    allow write: if request.auth.token.admin == true;
    allow read: true;

    // Alterntatively, for role-based access, assign specific roles to users
    match /some_collection/{document} {
     allow read: if request.auth.token.reader == "true";
     allow write: if request.auth.token.writer == "true";
   }
  }
}

Realtime Database

{
  "rules": {
    "some_path": {
      "$sub_path": {
      // Create a custom claim for the admin role
      ".write": "auth.uid !== null && auth.token.writer === true",
      ".read": "auth.uid !== null"
      }
    }
  }
}

Cloud Storage

service firebase.storage {
  // Create a custom claim for the admin role
  match /files/{fileName} {
    allow read: if request.auth.uid != null;
    allow write: if request.auth.token.admin == true;
  }
}

หากต้องการดูตัวอย่างเพิ่มเติมเกี่ยวกับSecurity Rulesพื้นฐานที่ใช้ประโยชน์จากAuthentication โปรดดูกฎความปลอดภัยพื้นฐาน