Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

docs(security): improve xsrf description and add it to http chapter as well #2652

Merged
merged 1 commit into from
Oct 24, 2016

Conversation

wardbell
Copy link
Contributor

@wardbell wardbell commented Oct 22, 2016

Improves the English description of XSRF in the Security chapter and adds a shorter version (with references) to the Http chapter where developers would expect to find it..

@mprobst Please review and confirm that the XSRFStrategy override, described in the original text, actually works in AoT:

{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name') }

I fear that it might not because it involves executing code (newing the class). It may be necessary to do this:

export const xsrfStrategy  = () =>  new CookieXSRFStrategy('myCookieName', 'My-Header-Name');
...
{ provide: XSRFStrategy, useFactory: xsrfStrategy  }

Please confirm which approach is best.

@johnpapa
Copy link
Contributor

Perhaps it would be worth mentioning why the malicious code cant simply read the cookie and do the same thing.

cookies, and the attacker could—for example—cause a bank transfer in the user's name with
the right request.
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
a different web page with malicious code that secretly sends a request to your application's web server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be clearer if you give the websites names, e.g. evil.com and example.com.

the right request.
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
a different web page with malicious code that secretly sends a request to your application's web server.
If the user is logged into your application, the browser sends the request along with authentication cookies.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it sends authentication cookies along with the request, right?

Copy link
Contributor Author

@wardbell wardbell Oct 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I think I'm saying here. Did I leave that in doubt? That is, after all, how this attack works. I'll try to clarify.

If the user is logged into your application, the browser sends the request along with authentication cookies.
An _unprotected_ server can't tell the difference between a legitimate request and a forged request.

In this manner, for example, a knowledgable attacker could transfer money from the user's account to the attacker's account.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... , if the bank's website does not protect itself against XSRF.?

CSRF tokens should be unique per user and session, have a large random value generated by a
cryptographically secure random number generator, and expire.
XSRF/CSRF tokens should be unique per user and session, have a large random value generated by a
cryptographically secure random number generator, and should expire after a reasonably short period of time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe suggest a day or so

`XSRF-TOKEN` cookie and validate the response header for each state-modifying request.
Angular's `http` has built-in support for the client-side half of this technique in its `XSRFStrategy`.
The default `CookieXSRFStrategy` is turned on automatically.
During each HTTP request, the `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The during is a bit misleading, this all happens before the request is sent. Maybe:

When sending an HTTP request, `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and sets a header named `X-XSRF-TOKEN` with the value of that cookie. 

[Cross-Site Request Forgery (CSRF)](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29) and
[Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet](https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet). The Stanford University
paper [Robust Defenses for Cross-Site Request Forgery](https://seclab.stanford.edu/websec/csrf/csrf.pdf) is also a rich source of detail.
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank">Cross-Site Request Forgery (CSRF)</a> and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason why you're changing the link syntax here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We want links to external sites to appear in a new browser tab by default. Can't specify that in markdown

<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank">Cross-Site Request Forgery (CSRF)</a> and
<a href="https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet" target="_blank">Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</a>.
The Stanford University paper
<a href="" target="_blank">Robust Defenses for Cross-Site Request Forgery</a> is a rich source of detail.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

The Stanford University paper
<a href="" target="_blank">Robust Defenses for Cross-Site Request Forgery</a> is a rich source of detail.

See also Dave Smith's masterful, easy-to-understand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy that you like this, but maybe a more neutral tone of voice would be appropriate in our docs?

:marked
## Guarding against Cross-Site Request Forgery

In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we repeat the same content here? should this link to the original doc, so we don't have two sources of truth that can diverge?

Copy link
Contributor Author

@wardbell wardbell Oct 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about that. We need some context here. I only repeat the intro and this is material that shouldn't change even if our implementation does. I didn't think making up a new summary description would make a difference. I'll crisp it up in the Http chapter.

@mprobst
Copy link
Contributor

mprobst commented Oct 22, 2016

Re whether this works with AoT, it should. The static reflector understands class instantiations and string literals, AFAIK.

@wardbell
Copy link
Contributor Author

wardbell commented Oct 23, 2016

@johnpapa I didn't want to get into this depth in our Angular docs. But I took a crack at it in an update on the security page

@wardbell
Copy link
Contributor Author

@mprobst I wasn't sure about AoT's ability to instantiate with string literals. Not sure what the rules are. Good to know. Thanks. And thanks for your other comments which I will incorporate.

@wardbell
Copy link
Contributor Author

wardbell commented Oct 23, 2016

@mprobst @johnpapa Changes have been made and force-pushed to this branch. I'll merge in a day or two if you have no more objections.

@wardbell wardbell merged commit ea7dff4 into angular:master Oct 24, 2016
@wardbell wardbell deleted the docs-xsrf branch October 24, 2016 09:57
@wardbell
Copy link
Contributor Author

wardbell commented Oct 24, 2016

Thanks @mprobst @johnpapa. If you have more changes, time for a new PR

@mprobst
Copy link
Contributor

mprobst commented Oct 26, 2016

@wardbell please note that most folks don't work on weekends, so might be nice to leave a workday at least for comments. I think we also do mandatory code reviews in most Angular repos now, whether that's useful here for docs is probably your call.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants