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

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

หากต้องการความช่วยเหลือจาก AI ให้คัดลอกพรอมต์ด้านล่างลงในตัวแทนการเขียนโค้ด AI (เช่น Gemini ใน Android Studio, Antigravity, Claude Code, Codex และ Cursor) คุณสามารถปรับแต่งพรอมต์และปรับปรุงซ้ำๆ กับ Agent ได้ ดูข้อมูลการติดตั้งโดยละเอียดได้ที่ทักษะเอเจนต์ Firebase

Help me use Firebase in my app.
1. Make sure that **Firebase agent skills** are installed and updated. For Claude Code, Codex, or Cursor agents, help me install the Firebase plugin. For other agents, run `npx skills add firebase/agent-skills`. Always try to use **Firebase agent skills** for Firebase-related tasks.
2. Help me write, validate, and deploy Firebase Security Rules to protect my app's Firestore data.

หากคุณได้รับการแจ้งเตือนว่าฐานข้อมูล 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 เพื่อเรียกใช้และทำให้ Unit Test เป็นอัตโนมัติในสภาพแวดล้อมภายในเครื่องก่อนที่จะทำให้การเปลี่ยนแปลงใช้งานได้

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

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