การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย
สร้างความมั่นใจให้กฎของ Realtime Database ด้วยการจำลองคำขอ ใช้ Rules Playground เขียนการทดสอบอัตโนมัติด้วยตัวจำลอง และอ่านข้อความปฏิเสธการเข้าถึง
การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Test Your Rules
Security Rules are the only thing standing between your data and the open internet. A single mistake can expose private data or block legitimate users.
Testing rules is as important as testing code, and Firebase gives you several tools to do it.
The Rules Playground
The Firebase console includes a Rules Playground where you simulate a single read or write without touching real data.
- Pick read or write
- Set a path and auth state
- See instantly whether it is allowed or denied
Simulating Auth State
In the simulator you can run as an unauthenticated user or supply a fake auth.uid and custom claims. This is how you verify that user-based access control behaves correctly.
Reading a Denial
When a request is denied, the simulator highlights the exact rule that evaluated to false. Use this to pinpoint why a legitimate request is being blocked.
The Local Emulator
For repeatable, automated testing, use the Firebase Local Emulator Suite. It runs the Realtime Database and its rules entirely on your machine, with no cloud costs.
firebase emulators:start --only databaseWriting a Rules Test
The @firebase/rules-unit-testing library lets you assert that operations succeed or fail. This is the gold standard for rule confidence.
import { assertSucceeds, assertFails } from '@firebase/rules-unit-testing';
await assertSucceeds(authedDb.ref('users/alice').set({ name: 'Alice' }));
await assertFails(authedDb.ref('users/bob').set({ name: 'hax' }));Test Both Directions
Good rule tests check both outcomes:
- Authorized users can do allowed actions (no false denials)
- Unauthorized users cannot do forbidden actions (no security holes)
Testing only the happy path hides the dangerous gaps.
Testing Validation Rules
Beyond access, test your .validate rules: confirm that malformed data is rejected and well-formed data is accepted.
await assertFails(db.ref('age').set('not-a-number'));
await assertSucceeds(db.ref('age').set(30));Common Rule Bugs
Watch for these frequent mistakes:
- Rules cascade: a true
.readhigher up overrides children - Forgetting that read and write rules are independent
- Assuming
authis non-null without checking
Debugging with newData
Inside write rules, newData represents what the write would produce and data is the current value. Logging your reasoning about these in test cases clears up many confusing denials.
{
"posts": {
"$id": {
".write": "!data.exists() || data.child('owner').val() === auth.uid"
}
}
}CI Integration
Run your emulator-based rule tests in continuous integration so a risky rule change is caught before it reaches production. This turns security into a regression-tested guarantee.
Quick Check
Test your understanding of rules testing.
Recap
You can now validate rules with confidence.
- Use the Rules Playground for quick manual checks
- Use the Local Emulator for repeatable runs
- Write tests with
assertSucceeds/assertFails - Cover both access and validation, both directions
- Run rule tests in CI
คำถามที่พบบ่อย
บทเรียน “การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย”
สร้างความมั่นใจให้กฎของ Realtime Database ด้วยการจำลองคำขอ ใช้ Rules Playground เขียนการทดสอบอัตโนมัติด้วยตัวจำลอง และอ่านข้อความปฏิเสธการเข้าถึง คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจไวยากรณ์ของกฎความปลอดภัย
- การควบคุมการเข้าถึงตามผู้ใช้
- การตรวจสอบความถูกต้องของข้อมูลด้วยกฎ
- การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย