Fireship · @fireship

Firebase Security in 100 Seconds

Uploaded December 3, 2020 · Length 2:20

Firebase Security in 100 Seconds

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.

  1. 0:00

    firebase security when you build a

  2. 0:02

    firebase app you can perform user

  3. 0:03

    authentication

  4. 0:04

    and work with your database entirely

  5. 0:06

    from your front-end code

  6. 0:07

    but how is this possible if our database

  7. 0:10

    is exposed on the client wouldn't that

  8. 0:11

    mean that any hacker could then read or

  9. 0:13

    write private user data

  10. 0:14

    the answer is no because your

  11. 0:15

    infrastructure is protected by a

  12. 0:17

    mechanism known as firebase security

  13. 0:19

    rules which can be applied to real-time

  14. 0:21

    database

  15. 0:21

    cloud storage and firestore in a

  16. 0:23

    traditional web application you have a

  17. 0:25

    server

  18. 0:26

    on that server you implement security

  19. 0:27

    logic that uses either tokens or cookies

  20. 0:30

    to verify that a user making a request

  21. 0:32

    is authorized to do so firebase rules

  22. 0:34

    achieve the same thing but without the

  23. 0:36

    need to write maintain and deploy

  24. 0:38

    server-side code

  25. 0:39

    instead you create a policy to define

  26. 0:41

    who has access to what

  27. 0:43

    in your database using a very easy to

  28. 0:45

    learn language called common expression

  29. 0:47

    language

  30. 0:47

    whenever a user makes a request to read

  31. 0:49

    or write to your database that request

  32. 0:51

    is routed through this policy

  33. 0:52

    every operation is denied by default

  34. 0:55

    when a request comes in

  35. 0:56

    it will look for the first rule to allow

  36. 0:58

    it once allowed firebase can then

  37. 1:00

    securely read or write to your database

  38. 1:02

    you can write rules from the firebase

  39. 1:03

    console or your ide in the console

  40. 1:05

    you can time travel back to any previous

  41. 1:07

    rules you've used in the past or use the

  42. 1:09

    playground to send a mock request to

  43. 1:11

    your database

  44. 1:12

    and in production you can analyze

  45. 1:14

    exactly how your rules are being

  46. 1:15

    executed

  47. 1:16

    to enforce a rule we start by matching a

  48. 1:18

    path in the database

  49. 1:19

    which can be as broad as the entire

  50. 1:21

    database or as granular as a single

  51. 1:23

    document

  52. 1:24

    a single document can be matched with a

  53. 1:25

    collection followed by a document id

  54. 1:28

    if we want anybody to read and write to

  55. 1:29

    this document we can say allow read and

  56. 1:32

    write

  57. 1:32

    but that's not very secure let's make

  58. 1:34

    sure a user is logged into our app to

  59. 1:36

    view the document

  60. 1:37

    which we can do by writing conditional

  61. 1:39

    logic we allow the operation

  62. 1:41

    if the request auth object does not

  63. 1:43

    equal null when it comes to writes we

  64. 1:45

    can make sure

  65. 1:45

    only the user that owns the document can

  66. 1:47

    write to it by checking the request

  67. 1:49

    auth user id in most cases though you'll

  68. 1:51

    want your rules to apply to an entire

  69. 1:53

    collection

  70. 1:53

    if we wrap the document id in braces the

  71. 1:55

    rule now applies to any document in that

  72. 1:57

    collection and we can use the document

  73. 1:59

    id

  74. 1:59

    as a variable in our logic and now we