ใช้คู่มือนี้เพื่อทำความเข้าใจช่องโหว่ที่พบบ่อยใน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
- หากต้องการเปิดพื้นที่ทดลองกฎ ให้คลิกพื้นที่ทดลองกฎจากแท็บกฎ
- ในการตั้งค่าสนามเด็กเล่นของกฎ ให้เลือกตัวเลือกสำหรับการทดสอบ ซึ่งรวมถึงตัวเลือกต่อไปนี้
- การทดสอบการอ่านหรือเขียน
- ตำแหน่งที่เฉพาะเจาะจงในฐานข้อมูลของคุณเป็นเส้นทาง
- ประเภทการตรวจสอบสิทธิ์ - ผู้ใช้ที่ไม่ได้รับการตรวจสอบสิทธิ์ ผู้ใช้ที่ไม่ระบุตัวตนที่ได้รับการตรวจสอบสิทธิ์ หรือ รหัสผู้ใช้ที่เฉพาะเจาะจง
- ข้อมูลเฉพาะเอกสารที่กฎของคุณอ้างอิงโดยเฉพาะ (เช่น หากกฎกำหนดให้ต้องมีฟิลด์ที่เฉพาะเจาะจงก่อนจึงจะอนุญาตให้เขียนได้)
- คลิกเรียกใช้ แล้วดูผลลัพธ์ในแบนเนอร์เหนือหน้าต่างกฎ