Testowanie i debugowanie reguł bezpieczeństwa
Zyskaj pewność co do reguł Realtime Database, symulując żądania, korzystając z Rules Playground, pisząc automatyczne testy z emulatorem i analizując komunikaty o odmowie dostępu.
Testowanie i debugowanie reguł bezpieczeństwa to bezpłatna lekcja Firebase Auth & Realtime Database Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Firebase Auth & Realtime Database Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
Często zadawane pytania
Czy lekcja „Testowanie i debugowanie reguł bezpieczeństwa” jest bezpłatna?
Tak — pełny tekst „Testowanie i debugowanie reguł bezpieczeństwa” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Firebase Auth & Realtime Database Apps, przejdź na CoddyKit PRO. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Testowanie i debugowanie reguł bezpieczeństwa”?
Zyskaj pewność co do reguł Realtime Database, symulując żądania, korzystając z Rules Playground, pisząc automatyczne testy z emulatorem i analizując komunikaty o odmowie dostępu. Ćwiczysz Firebase Auth & Realtime Database Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Firebase Auth & Realtime Database Apps?
Nie wymagamy żadnego doświadczenia. Firebase Auth & Realtime Database Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Testowanie i debugowanie reguł bezpieczeństwa”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Firebase Auth & Realtime Database Apps?
Tak. Każda lekcja Firebase Auth & Realtime Database Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Zrozumienie składni reguł bezpieczeństwa
- Kontrola dostępu oparta na użytkownikach
- Walidacja danych za pomocą reguł
- Testowanie i debugowanie reguł bezpieczeństwa