แก้ไขกฎที่ไม่ปลอดภัย

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

หากคุณได้รับการแจ้งเตือนว่าฐานข้อมูล Cloud Firestore ไม่ได้รับการรักษาความปลอดภัยอย่างเหมาะสม คุณสามารถแก้ไขช่องโหว่ได้โดยการแก้ไขและทดสอบ Cloud Firestore Security Rules

หากต้องการดูกฎการรักษาความปลอดภัยที่มีอยู่ ให้ไปที่แท็บกฎ ในคอนโซลFirebase

ทำความเข้าใจ Cloud Firestore Security Rules

Cloud Firestore Security Rules ช่วยปกป้องข้อมูลของคุณจากผู้ใช้ที่ไม่ประสงค์ดี กฎเริ่มต้น สำหรับอินสแตนซ์ Cloud Firestore ใดๆ ที่สร้างขึ้นในคอนโซล Firebase จะปฏิเสธ การเข้าถึงของผู้ใช้ทั้งหมด หากต้องการพัฒนาแอปและเข้าถึงฐานข้อมูล คุณจะต้องแก้ไขกฎเหล่านั้นและอาจพิจารณาให้สิทธิ์เข้าถึงแบบครอบคลุมแก่ผู้ใช้ทั้งหมดในสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ อย่างไรก็ตาม ก่อนที่จะนำแอปไปใช้งานในสภาพแวดล้อมการใช้งานจริง โปรดใช้เวลาในการกำหนดค่ากฎอย่างเหมาะสมและรักษาความปลอดภัยข้อมูล

ขณะพัฒนาแอปและทดสอบการกำหนดค่าต่างๆ สำหรับ กฎ ให้ใช้โปรแกรมจำลอง Cloud Firestore เพื่อเรียกใช้แอปใน สภาพแวดล้อมในการพัฒนาซอฟต์แวร์ในเครื่อง

สถานการณ์ที่พบบ่อยเกี่ยวกับกฎที่ไม่ปลอดภัย

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

การเข้าถึงแบบเปิด

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

ไม่แนะนำ: สิทธิ์อ่านและเขียนสำหรับผู้ใช้ทุกคน
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
วิธีแก้ปัญหา: กฎที่จำกัดสิทธิ์อ่านและ เขียน

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

เฉพาะเจ้าของเนื้อหา

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /some_collection/{document} {
      // Allow reads and deletion if the current user owns the existing document
      allow read, delete: if request.auth.uid == resource.data.author_uid;
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;

    }
  }
}
  

การเข้าถึงแบบผสมระหว่างสาธารณะและส่วนตัว

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      // Allow public reads
      allow read: if true
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;
      // Allow deletion if the current user owns the existing document
      allow delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}
  

สิทธิ์เข้าถึงสำหรับผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์แล้ว

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

ไม่แนะนำ: ผู้ใช้ที่เข้าสู่ระบบทุกคนมีสิทธิ์อ่าน และเขียนฐานข้อมูลทั้งหมด
service cloud.firestore {
  match /databases/{database}/documents {
    match /some_collection/{document} {
      allow read, write: if request.auth != null;
    }
  }
}
วิธีแก้ปัญหา: จำกัดสิทธิ์เข้าถึงโดยใช้เงื่อนไขด้านความปลอดภัย

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

การเข้าถึงตามบทบาท

service cloud.firestore {
  match /databases/{database}/documents {
    // Assign roles to all users and refine access based on user roles
    match /some_collection/{document} {
     allow read: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"

     // Note: Checking for roles in your database using `get` (as in the code
     // above) or `exists` carry standard charges for read operations.
    }
  }
}

การเข้าถึงตามแอตทริบิวต์

// Give each user in your database a particular attribute
// and set it to true/false
// Then, use that attribute to grant access to subsets of data
// For example, an "admin" attribute set
// to "true" grants write access to data

service cloud.firestore {
  match /databases/{database}/documents {
    match /collection/{document} {
      allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
      allow read: true;
    }
  }
}
  

การเข้าถึงแบบผสมระหว่างสาธารณะและส่วนตัว

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      allow read: if true
      allow write: if request.auth.uid == request.resource.data.author_uid
    }
  }
}
  

สิทธิ์เข้าถึงสำหรับอีเมลที่ยังไม่ได้ยืนยัน

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

ไม่แนะนำ: ผู้ใช้ทุกคนสามารถลงชื่อเข้าใช้ด้วยอีเมลใดก็ได้
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow access based on email domain
    match /some_collection/{document} {
     allow read: if request.auth != null
                 && request.auth.email.endsWith('@example.com')
    }
  }
}
วิธีแก้ปัญหา: จำกัดสิทธิ์เข้าถึงเฉพาะอีเมลที่ยืนยันแล้ว

ยืนยันอีเมล

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow access based on email domain
    match /some_collection/{document} {
     allow read: if request.auth != null
                 && request.auth.email_verified
                 && request.auth.email.endsWith('@example.com')
    }
  }
}

การเข้าถึงแบบปิด

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

// Deny read/write access to all users under any conditions
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

Firebase Admin SDK และ Cloud Functions จะยังคงเข้าถึงฐานข้อมูลของคุณได้ ใช้กฎเหล่านี้เมื่อคุณต้องการใช้ Cloud Firestore เป็นแบ็กเอนด์แบบเซิร์ฟเวอร์เท่านั้น ร่วมกับ Firebase Admin SDK แม้ว่าวิธีนี้จะปลอดภัย แต่คุณควรทดสอบว่าไคลเอ็นต์ของแอปสามารถดึงข้อมูลได้อย่างถูกต้อง

ดูข้อมูลเพิ่มเติมเกี่ยวกับ Cloud Firestore Security Rules และวิธีการทำงานของกฎเหล่านี้ได้ที่ เริ่มต้นใช้งาน Cloud Firestore Security Rules

ตรวจสอบ Cloud Firestore Security Rules

หากต้องการตรวจสอบลักษณะการทำงานของแอปและยืนยันการกำหนดค่าCloud Firestore Security Rules ให้ใช้โปรแกรมจำลองCloud Firestore ใช้โปรแกรมจำลองCloud Firestore เพื่อเรียกใช้และทำให้การทดสอบหน่วยเป็นแบบอัตโนมัติในสภาพแวดล้อมในเครื่องก่อนที่จะนำ การเปลี่ยนแปลงไปใช้งาน

หากต้องการทดสอบCloud Firestore Security Rulesที่อัปเดตแล้วอย่างรวดเร็วในคอนโซล Firebase ให้ใช้ เครื่องมือ Rules Playground

  1. หากต้องการเปิด Rules Playground ให้คลิก Rules Playground จาก แท็บกฎ
  2. ในการตั้งค่า Rules Playground ให้เลือกตัวเลือกสำหรับการทดสอบ ซึ่งรวมถึง
    • การทดสอบการอ่านหรือการเขียน
    • ตำแหน่ง ที่เฉพาะเจาะจงในฐานข้อมูลเป็นเส้นทาง
    • ประเภทการตรวจสอบสิทธิ์ ได้แก่ ผู้ใช้ที่ไม่ผ่านการตรวจสอบสิทธิ์ ผู้ใช้ที่ไม่ระบุตัวตนที่ผ่านการตรวจสอบสิทธิ์ หรือรหัสผู้ใช้ที่เฉพาะเจาะจง
    • ข้อมูลที่เฉพาะเจาะจงของเอกสารที่กฎอ้างอิงถึงโดยเฉพาะ (เช่น หากกฎกำหนดให้ต้องมีฟิลด์ที่เฉพาะเจาะจงก่อนที่จะอนุญาตให้เขียน)
  3. คลิกเรียกใช้ และดูผลลัพธ์ในแบนเนอร์เหนือหน้าต่างกฎ