Why do browsers allow CSRF?

aaa90210

I am pretty new to web security, and as I read more about the different attack vectors, my mind boggles that they are allowed in the first place. It's like the web was designed with a broken security model and to be vulnerable.

I am also amazed at the amount of vague and imprecise information. For example, at first the Single Origin Policy sounds pretty good, then I read that it only applies to XHR, and oh and by the way, doesn't actually prevent XHR cross-origin POST, which is the classic CSRF attack. Glad I kept reading.

There is also an Origin header that the server can use to make sure the request is coming from the right place -- but oops, it is set inconsistently across browsers, and if it is NOT set, you can't be quite sure if it was because of a same-origin request, or a request type that just didn't get it for certain browsers (maybe an IMG tag?). Keep reading.

So the right way seems to be set a CSRF token in the session cookie, and also add that token to forms/links, and then compare them server side on a submission. In theory (and lets exclude all XSS attacks for the purpose of this question), a CSRF attempt from another tab may make a POST request to a form that includes the cookie, but without a form input element set to the matching token (because it can't read the token from the cookie), so the server will reject the request. Works but kludgy, and make sure you never ever forget to check!

Holding that thought in mind for a second, here is my question -- why does the browser send the session cookie in a request that originates from a page that is not the origin of the cookie?

I mean, browsers will refuse to send cookies to different domains for good reason, but are quite happy to send them from different origins? Would stuff break if they didn't? Would it be a robust defence against CSRF, only requiring servers to do what they are doing anyway -- checking for a valid session cookie?

Edit: maybe this is an attempt to improve the situation? https://tools.ietf.org/html/draft-west-origin-cookies-01

SilverlightFox

I am pretty new to web security, and as I read more about the different attack vectors, my mind boggles that they are allowed in the first place. It's like the web was designed with a broken security model and to be vulnerable.

All true. It was never designed to be secure in the first place. The web was originally designed as a static document management and sharing system which allowed direct links to resources on different machines.

The dynamic web you see today is a kludge. We can fix it with CSRF tokens, HTTP headers and the like, but if you make a dynamic website without doing any of these things then chances are it's vulnerable (and keeps people like me in a job).

Check out its history in the Wikipedia article.

I am also amazed at the amount of vague and imprecise information. For example, at first the Single Origin Policy sounds pretty good, then I read that it only applies to XHR, and oh and by the way, doesn't actually prevent XHR cross-origin POST, which is the classic CSRF attack. Glad I kept reading.

Also mainly true. The Same Origin Policy also applies to windows and frames too (e.g. example.com cannot alter the content of example.org by JavaScript if example.com includes an IFrame to example.org). Yes, cross-domain XHR's can be made, but without CORS being enabled the responses cannot be read. This does protect CSRF tokens from being stolen, but as you say if you're not using CSRF protection then this presents a CSRF vulnerability.

Other defences such as adding a custom header can be used to mitigate CSRF, as custom headers cannot be sent cross-domain.

XHRs didn't used to be able to access anything cross-domain, which was seen as too big a restriction, hence the advent of CORS. Previously, as forms could access different domains anyway, it was not seen as a particularly risky maneuver. It is still not, provided the appropriate controls are put into place.

There is also an Origin header that the server can use to make sure the request is coming from the right place -- but oops, it is set inconsistently across browsers, and if it is NOT set, you can't be quite sure if it was because of a same-origin request, or a request type that just didn't get it for certain browsers (maybe an IMG tag?). Keep reading.

Quite true. See this answer.

why does the browser send the session cookie in a request that originates from a page that is not the origin of the cookie?

Because lots of things would break otherwise. There are countless forms that are designed to be submitted from static sites to dynamic sites that do the back-end handling.

There is a new standard for "same-site" cookies. A less dry explanation is here.

Basically cookies can be set with a new attribute SameSite. In strict mode, cookies are not sent when the origin is different. In lax mode, they are only withheld if the method is e.g. POST, which is where CSRF vulnerabilities mainly lie.

The one you linked to was an early draft of this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do user agents / browsers lie

From Dev

Why do browsers provide an option to disable Javascript?

From Java

Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?

From Java

Why do browsers match CSS selectors from right to left?

From Dev

Why do no browsers apart from Firefox support animated PNGs?

From Dev

why do browsers style a predefined margin into all webpages?

From Dev

Why do browsers inefficiently make 2 requests here?

From Dev

Why do browsers not send the Authentication header when the credentials are provided in the URL?

From Dev

Why do browsers display grafics different and how to change that?

From Dev

Why do my web browsers cause my entire computer to hang?

From Dev

Browsers Back button Issues with CSRF Prevention Mechanisam

From Dev

Django CSRF cookie not set in several browsers

From Dev

Browsers Back button Issues with CSRF Prevention Mechanisam

From Dev

Browsers do not set cookie

From Dev

Which (mobile) browsers allow styling of the page title?

From Dev

Which (mobile) browsers allow styling of the page title?

From Dev

why no csrf for GET requests

From Dev

Why do gcc and clang allow me to construct an abstract class?

From Java

Why do C and C++ allow the expression (int) + 4*5?

From Dev

Why do Arc and Mutex allow me to change the value of an immutable variable?

From Dev

Why do Rhino Stubs allow me to set expectations on them?

From Dev

Why Kotlin do not allow any code in primary constructor?

From Dev

Why do variable prefixes in ruby allow omission of parenthesis in method invocations?

From Dev

Why do template classes allow member functions which cannot compile?

From Dev

Why do some programming languages allow semicolons to be automatically included?

From Dev

Why do we allow referring to an enum member of an enum member in Java?

From Dev

Why do `env var=value` allow arbitrary name in var?

From Dev

Why Kotlin do not allow any code in primary constructor?

From Dev

Why many website do not allow to access the "last-modified" header?

Related Related

  1. 1

    Why do user agents / browsers lie

  2. 2

    Why do browsers provide an option to disable Javascript?

  3. 3

    Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?

  4. 4

    Why do browsers match CSS selectors from right to left?

  5. 5

    Why do no browsers apart from Firefox support animated PNGs?

  6. 6

    why do browsers style a predefined margin into all webpages?

  7. 7

    Why do browsers inefficiently make 2 requests here?

  8. 8

    Why do browsers not send the Authentication header when the credentials are provided in the URL?

  9. 9

    Why do browsers display grafics different and how to change that?

  10. 10

    Why do my web browsers cause my entire computer to hang?

  11. 11

    Browsers Back button Issues with CSRF Prevention Mechanisam

  12. 12

    Django CSRF cookie not set in several browsers

  13. 13

    Browsers Back button Issues with CSRF Prevention Mechanisam

  14. 14

    Browsers do not set cookie

  15. 15

    Which (mobile) browsers allow styling of the page title?

  16. 16

    Which (mobile) browsers allow styling of the page title?

  17. 17

    why no csrf for GET requests

  18. 18

    Why do gcc and clang allow me to construct an abstract class?

  19. 19

    Why do C and C++ allow the expression (int) + 4*5?

  20. 20

    Why do Arc and Mutex allow me to change the value of an immutable variable?

  21. 21

    Why do Rhino Stubs allow me to set expectations on them?

  22. 22

    Why Kotlin do not allow any code in primary constructor?

  23. 23

    Why do variable prefixes in ruby allow omission of parenthesis in method invocations?

  24. 24

    Why do template classes allow member functions which cannot compile?

  25. 25

    Why do some programming languages allow semicolons to be automatically included?

  26. 26

    Why do we allow referring to an enum member of an enum member in Java?

  27. 27

    Why do `env var=value` allow arbitrary name in var?

  28. 28

    Why Kotlin do not allow any code in primary constructor?

  29. 29

    Why many website do not allow to access the "last-modified" header?

HotTag

Archive