Repository navigation
Conversation
…tchContinue: false` This option enables this module to be used for basic CSRF protection as recommended by the OWASP: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Checking_the_Origin_Header, while remaining strictly compatible with the CORS specification by default. When `mismatchComplete === false` and a request's origin header is either missing or not allowed, per the module's configuration, the middleware will terminate the request rather than execute the next handler. If `mismatchComplete` is any other value, the middleware will execute the next handler as before. When the middleware terminates the request, it does so with a 400 by default since https://httpstatuses.com/400 says that can be used for "deceptive request routing"; this status code is configurable with `mismatchStatus`. Fixes expressjs#109.
| // When a user-specified handler returns `false`, we skip applying | ||
| // the CORS headers and run the request handler (see below). But | ||
| // the default handler should apply the headers even in case of | ||
| // a mismatch unless `mismatchContinue === false`. |
There was a problem hiding this comment.
I avoid simply returning cb(null, isAllowed ? options.origin : false) because previously, a response of false would cause next to be executed without applying the CORS headers. I'd like the behavior to be unified between this default handler and user-provided handlers but I also don't want to cause breaking changes. I added documentation for this behavior of user-provided handlers here.
| - `RegExp` - set `origin` to a regular expression pattern which will be used to test the request origin. If it's a match, the request origin will be reflected. For example the pattern `/example\.com$/` will reflect any request that is coming from an origin ending with "example.com". | ||
| - `Array` - set `origin` to an array of valid origins. Each origin can be a `String` or a `RegExp`. For example `["http://example1.com", /\.example2\.com$/]` will accept any request from "http://example1.com" or from a subdomain of "example2.com". | ||
| - `Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (which expects the signature `err [object], allow [bool]`) as the second. | ||
| - `Function` - set `origin` to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (which expects the signature `err [object], allow [bool]`) as the second. If the function calls back with `allow === false`, CORS will not be enabled for the response, but the next handler will still run unless `mismatchContinue === false`. |
There was a problem hiding this comment.
As described below, this is merely documentation of pre-existing behavior.
dougwilson
left a comment
There was a problem hiding this comment.
Unless the PR is adjusted such that a missing Origin is not considered a mismatch, I'm not going to accept this, I'm sorry. I noted that back in the original issue. The CORS specification iterates multiple times that a missing Origin header puts the request out of scope for CORS.
|
P.S. the PR is failing the automated checks because the changes do not have adequate test coverage. |
|
|
||
| ### CORS and CSRF | ||
|
|
||
| The default configuration of this module implements the CORS specification as outlined by the W3C. That specification is designed to allow clients to make cross-origin requests, not to prevent servers from responding to requests. Thus, in the case of a CORS "failure", where the request's origin is not allowed, the browser will refuse to deliver the response _after_ the server responds as if the request had been valid. |
There was a problem hiding this comment.
@dougwilson I tried to accurately capture your discussion from #109 here, please let me know if I misrepresented anything!
There was a problem hiding this comment.
It's not delivering the response (for example you can cross domain load an image without cors and cross domain post a form without cors), it's specifically the access to the contents of the response by the JavaScript code in the differing origin. The response can still be delivered in other ways.
Ah I think that's ok @dougwilson, will update. What we're trying to protect against are forged AJAX requests and those should always have
Hm… do you have anything you specifically would like to see tested? Or can you describe how I might deduce that from https://coveralls.io/builds/10783376/source?filename=lib%2Findex.js? I'm not familiar with that sort of readout. |
Sure, but your blob of CSRF above is going to be very misleading. For example, the image tag is a real threat, and so is programmatically creating an iframe and form element and submitting the form element cross origin, which wont have an origin header, either, and what you wrote is going to be very misleading to people who think they are getting protection. If you want to keep that blurb there, I will want to pass it by a security team from my company before I want to add it to the docs to ensure it is not misleading, probably a couple weeks turn around time on that.
I'm not at a computer, so not easy to look. When you run npm test, a coverage folder is created and there is a lcov folder with an html report. Every single line of code you add needs to be tested in order to accept the PR. |
|
Anyway, this is the end of my day; I will take a closer look at the PR tomorrow or so and see what additional information I can gather. Looking at the OWASP like you provided, the proposed protection mechanism you're trying to implement here actually just builds off the Origin header that CORS added; it is not related to CORS itself in general. A quick search online reveals that actually implementing the proposal will significantly limit your browser compatibility, particularly it will excluded even current Firefox web browsers from properly validating (tracking bug https://bugzilla.mozilla.org/show_bug.cgi?id=446344). I think certainly all the extensive browser compatibility issues and limitations of the use as a CSRF protection need to be detailed more if this module is going to lead people down that road in the README. |
|
Thanks for all the discussion @dougwilson.
I think you're right, we need to reject missing headers for this to have any hope of protecting against CSRF. If you feel that policy cannot be enforced by this module, even via an opt-in configuration option, then I guess I'll need to make another module. Which would not be the end of the world. You're absolutely right that
but my feeling was, why check the origin in two places—to set CORS headers, and then also to prevent the server from responding? The intent is the same IMO—that only a whitelisted set of origins should be able to access the resource. I bet that others share my initial confusion over the limitations of CORS, and the relationship between CORS and CSRF protection; and it would be greatly beneficial to have this module, the canonical CORS library for Node, describe that relationship and offer a solution even if it's not the library's default behavior. Speaking of offering a solution, what is the recommended way to solve my use case (preventing CSRF while also allowing cross-domain access) in the Express community? https://github.com/expressjs/csurf appears to suggest that users disable it for cross-domain requests:
And, that module also does not verify the origin of the request, only check the CSRF token. Perhaps that is a stronger check than verifying the origin of the request but it is also more complicated, perhaps not generally applicable per the above quote, and OWASP suggests that the two strategies be used in tandem. So my hope was that this module, as augmented by this PR, could serve as a complement to csurf. Btw
does not trouble me because, again, users would be opting into this behavior. They would be choosing to bias towards security over compatibility. A "fully-secured" API could be opened up later, if even necessary—it may be the case (as it is for me) that the application only supports compatible browsers; and browsers will get more compatible with time. Agreed that such compatibility issues should be documented though. |
Right, I do, because the absence of the
Sure, but even if it was two modules, it would be trivial to simply share a variable in your code between them.
They are not the same, and I think that's where you are getting hung up; the intent of CORS is to allow JavaScript running in a different origin to access the contents of a response, while the proposal you linked to is to try and get more parts of the web platform to send the
What you are describing is a general education problem with how to program on the web platform. This hypothetical confusion would be the same for .NET developers, PHP developers, Python developers, etc. not just Node.js developers. Also, it's pretty nice to hear someone say "canonical CORS library for Node" but unfortunately that is not the case at all--this is a middleware module that implements the CORS spec for Express and similar frameworks. You have even a second middleware that does this: https://github.com/agrueneberg/Corser . That doesn't even count that every framework has their own CORS module as well, since the frameworks are not compatible. For example https://www.npmjs.com/package/kcors for Koa, https://hapijs.com/api#route-options for Hapi.js, and you can go on, just within Node.js itself.
This is because I don't think you actually understand what CSRF does. The name itself is Cross-Site Request Forgery; CSRF itself it made to prevent cross-site requests, so it really has nothing to do with allowing cross site requests. I think this goes back to the general state of web platform education. I'm not sure how to solve that, but it is certainly not an issue that is Node.js-specific and many of these modules are simply implementations assuming you already have the pre-requisite knowledge in why you want to leverage them. I think that from this conversation, and one I had with a college today, the best place you'll want to land this type of protection is in a module dedicated to access controls like https://github.com/krakenjs/lusca |
|
Additional conversations I had today brought up that there are several scenarios that you want this Origin-based CSRF protection but NOT want CORS. Fundamentally they provide security in different directions: the CSRF is locking down access but CORS is opening up access. A given use-case was SSO, where you only want SiteB to post to SiteA, but you do not want to open up SiteA's responses via CORS to SiteB, just allow it to make the request. In order to enable this, you'd need to add another option to this PR to enable "when the origin matches, don't add CORS" and at that point, it's very clear this is not CORS, but an Origin check for CSRF. I think the best place you'll want to land this type of protection is in a module dedicated to access controls like https://github.com/krakenjs/lusca |
This option enables this module to be used for basic CSRF protection as
recommended by the OWASP: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Checking_the_Origin_Header,
while remaining strictly compatible with the CORS specification by default.
When
mismatchComplete === falseand a request's origin header is eithermissing or not allowed, per the module's configuration, the middleware will
terminate the request rather than execute the next handler. If
mismatchCompleteis any other value, the middleware will execute the nexthandler as before.
When the middleware terminates the request, it does so with a 400 by default
since https://httpstatuses.com/400 says that can be used for "deceptive
request routing"; this status code is configurable with
mismatchStatus.Fixes #109.