Fireship · @fireship
Firebase Security in 100 Seconds
Uploaded December 3, 2020 · Length 2:20
Summary
firebase security rules protect database from unauthorized access without server-side code
- firebase security
- firebase rules
- no server code
- database security
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
firebase security when you build a
- 0:02
firebase app you can perform user
- 0:03
authentication
- 0:04
and work with your database entirely
- 0:06
from your front-end code
- 0:07
but how is this possible if our database
- 0:10
is exposed on the client wouldn't that
- 0:11
mean that any hacker could then read or
- 0:13
write private user data
- 0:14
the answer is no because your
- 0:15
infrastructure is protected by a
- 0:17
mechanism known as firebase security
- 0:19
rules which can be applied to real-time
- 0:21
database
- 0:21
cloud storage and firestore in a
- 0:23
traditional web application you have a
- 0:25
server
- 0:26
on that server you implement security
- 0:27
logic that uses either tokens or cookies
- 0:30
to verify that a user making a request
- 0:32
is authorized to do so firebase rules
- 0:34
achieve the same thing but without the
- 0:36
need to write maintain and deploy
- 0:38
server-side code
- 0:39
instead you create a policy to define
- 0:41
who has access to what
- 0:43
in your database using a very easy to
- 0:45
learn language called common expression
- 0:47
language
- 0:47
whenever a user makes a request to read
- 0:49
or write to your database that request
- 0:51
is routed through this policy
- 0:52
every operation is denied by default
- 0:55
when a request comes in
- 0:56
it will look for the first rule to allow
- 0:58
it once allowed firebase can then
- 1:00
securely read or write to your database
- 1:02
you can write rules from the firebase
- 1:03
console or your ide in the console
- 1:05
you can time travel back to any previous
- 1:07
rules you've used in the past or use the
- 1:09
playground to send a mock request to
- 1:11
your database
- 1:12
and in production you can analyze
- 1:14
exactly how your rules are being
- 1:15
executed
- 1:16
to enforce a rule we start by matching a
- 1:18
path in the database
- 1:19
which can be as broad as the entire
- 1:21
database or as granular as a single
- 1:23
document
- 1:24
a single document can be matched with a
- 1:25
collection followed by a document id
- 1:28
if we want anybody to read and write to
- 1:29
this document we can say allow read and
- 1:32
write
- 1:32
but that's not very secure let's make
- 1:34
sure a user is logged into our app to
- 1:36
view the document
- 1:37
which we can do by writing conditional
- 1:39
logic we allow the operation
- 1:41
if the request auth object does not
- 1:43
equal null when it comes to writes we
- 1:45
can make sure
- 1:45
only the user that owns the document can
- 1:47
write to it by checking the request
- 1:49
auth user id in most cases though you'll
- 1:51
want your rules to apply to an entire
- 1:53
collection
- 1:53
if we wrap the document id in braces the
- 1:55
rule now applies to any document in that
- 1:57
collection and we can use the document
- 1:59
id
- 1:59
as a variable in our logic and now we


