Fireship · @fireship

CORS in 100 Seconds

Uploaded March 29, 2021 · Length 2:31

CORS in 100 Seconds

Summary

how to enable cors on a server to allow cross-origin requests

  • cors
  • cross-origin resource sharing
  • express.js
  • same origin policy
  • access control allow origin header

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    cross-origin resource sharing or cores

  2. 0:03

    is a mechanism that allows a website on

  3. 0:05

    one url to request data from a different

  4. 0:07

    url

  5. 0:07

    and it's frustrated both front-end and

  6. 0:09

    back-end developers since the dawn of

  7. 0:11

    time you may have tried to use an image

  8. 0:13

    on your website that lives on a

  9. 0:14

    different url

  10. 0:15

    only to end up with a broken image or

  11. 0:17

    your website might try to fetch data

  12. 0:19

    from your api

  13. 0:20

    then fail with a coors error in the

  14. 0:21

    console that happens

  15. 0:23

    because the browser implements the same

  16. 0:25

    origin policy as part of its security

  17. 0:27

    model which allows a website to freely

  18. 0:29

    request

  19. 0:30

    images and data from its own url but

  20. 0:32

    blocks anything from an external url

  21. 0:34

    unless certain conditions are met when

  22. 0:36

    the browser makes a request

  23. 0:37

    it adds an origin header to the request

  24. 0:39

    message if that request goes to a server

  25. 0:41

    on the same origin

  26. 0:42

    then it's allowed by the browser with no

  27. 0:44

    questions asked however

  28. 0:45

    if that request goes to a different url

  29. 0:47

    then it's known as a cross origin

  30. 0:49

    request

  31. 0:50

    when sending the response the server

  32. 0:52

    will add the access control allow origin

  33. 0:54

    header

  34. 0:55

    its value needs to match the origin

  35. 0:56

    header on the request

  36. 0:58

    or it can be a wild card which allows

  37. 0:59

    any url to make the request

  38. 1:01

    if it's a mismatch though the browser

  39. 1:03

    will prevent the response data from

  40. 1:04

    being shared with the client

  41. 1:05

    this results in an error in the browser

  42. 1:07

    but for security reasons there's very

  43. 1:09

    limited information about what actually

  44. 1:11

    went wrong

  45. 1:12

    the solution to a corsair lies on the

  46. 1:14

    server if you don't control the server

  47. 1:15

    you're out of luck

  48. 1:16

    if you do control it you likely just

  49. 1:18

    need to configure it to respond with a

  50. 1:19

    proper header in express.js for example

  51. 1:22

    that can be achieved with a single line

  52. 1:23

    of middleware code which tells the

  53. 1:25

    server to include the course headers on

  54. 1:27

    every response

  55. 1:28

    now certain http requests like put or

  56. 1:31

    any request with a non-standard header

  57. 1:32

    will need to be preflighted

  58. 1:34

    and just like at the airport it's a

  59. 1:36

    sanity check that ensures the passenger

  60. 1:38

    or request

  61. 1:38

    is safe to fly on the airplane or server

  62. 1:41

    the browser automatically knows when to

  63. 1:42

    preflight and will first make a request

  64. 1:44

    using the options http verb

  65. 1:46

    the server will then respond saying yes

  66. 1:48

    i allow this origin

  67. 1:49

    to make requests with the following

  68. 1:51

    methods at which point the main request

  69. 1:53

    can happen

  70. 1:54

    without fear of disaster that may sound

  71. 1:56

    inefficient but the server can respond

  72. 1:57

    with a max age header allowing the

  73. 1:59

    browser to cache a preflight for a