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

Commit ea7dff4

Browse files
authored
docs(security): improve xsrf description and add it to http chapter as well (#2652)
1 parent c746c73 commit ea7dff4

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

public/docs/ts/latest/guide/security.jade

+54-30
Original file line numberDiff line numberDiff line change
@@ -200,40 +200,64 @@ h2#http HTTP-level vulnerabilities
200200

201201
h3#xsrf Cross-site request forgery
202202
:marked
203-
In a cross-site request forgery, an attacker tricks the user into visiting a
204-
_different_ page and has them, for example, submit a form that sends a request to your application's
205-
web server. If the user is logged into your application, the browser will send authentication
206-
cookies, and the attacker could—for example—cause a bank transfer in the user's name with
207-
the right request.
208-
209-
To prevent this, your application must ensure that user requests originate in your own
210-
application, not on a different site. A common technique is that the server sends a randomly
211-
generated authentication token in a cookie, often with the name `XSRF-TOKEN`. Only the website
212-
on which cookies are set can read the cookies, so only your own application can read this token. On
213-
each API request, the server then validates the client by checking that the token is sent back,
214-
usually in an HTTP header called `X-XSRF-TOKEN`.
215-
216-
The Angular `http` client has built-in support for this technique. The default
217-
`CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and sets an HTTP request header named
218-
`X-XSRF-TOKEN` with the value of that cookie on every request. The server must set the
219-
`XSRF-TOKEN` cookie and validate the response header for each state-modifying request.
220-
221-
CSRF tokens should be unique per user and session, have a large random value generated by a
222-
cryptographically secure random number generator, and expire.
223-
224-
Angular applications can customize cookie and header names by binding their own
225-
`CookieXSRFStrategy` value or implement an entirely custom `XSRFStrategy` through providing a custom
226-
binding for that type by adding either of the following to your providers list:
203+
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
204+
a different web page (e.g. `evil.com`) with malignant code that secretly sends a malicious request
205+
to your application's web server (e.g. `example-bank.com`).
206+
207+
Assume the user is logged into the application at `example-bank.com`.
208+
The user opens an email and clicks a link to `evil.com` which opens in a new tab.
209+
210+
The `evil.com` page immediately sends a malicious request to `example-bank.com`.
211+
Perhaps it's a request to transfer money from the user's account to the attacker's account.
212+
The browser automatically sends the `example-bank.com` cookies (including the authentication cookie) with this request.
213+
214+
The `example-bank.com` server, if it lacks XSRF protection, can't tell the difference between a legitimate request from the application
215+
and the forged request from `evil.com`.
216+
217+
To prevent this, the application must ensure that a user request originates from the real
218+
application, not from a different site.
219+
The server and client must cooperate to thwart this attack.
220+
221+
In a common anti-XSRF technique, the application server sends a randomly
222+
generated authentication token in a cookie.
223+
The client code reads the cookie and adds a custom request header with the token in all subsequent requests.
224+
The server compares the received cookie value to the request header value and rejects the request if the values are missing or don't match.
225+
226+
This technique is effective because all browsers implement the _same origin policy_. Only code from the website
227+
on which cookies are set can read the cookies from that site and set custom headers on requests to that site.
228+
That means only your application can read this cookie token and set the custom header. The malicious code on `evil.com` can't.
229+
230+
Angular's `http` has built-in support for the client-side half of this technique in its `XSRFStrategy`.
231+
The default `CookieXSRFStrategy` is turned on automatically.
232+
Before sending an HTTP request, the `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and
233+
sets a header named `X-XSRF-TOKEN` with the value of that cookie.
234+
235+
The server must do its part by setting the
236+
initial `XSRF-TOKEN` cookie and confirming that each subsequent state-modifying request
237+
includes a matching `XSRF-TOKEN` cookie and `X-XSRF-TOKEN` header.
238+
239+
XSRF/CSRF tokens should be unique per user and session, have a large random value generated by a
240+
cryptographically secure random number generator, and should expire in a day or two.
241+
242+
Your server may use a different cookie or header name for this purpose.
243+
An Angular application can customize cookie and header names by providing its own `CookieXSRFStrategy` values.
244+
code-example(language="typescript").
245+
{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name') }
246+
:marked
247+
Or you can implement and provide an entirely custom `XSRFStrategy`:
227248

228249
code-example(language="typescript").
229-
{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name')}
230-
{ provide: XSRFStrategy, useClass: MyXSRFStrategy}
250+
{ provide: XSRFStrategy, useClass: MyXSRFStrategy }
231251

232252
:marked
233-
For information about CSRF at the Open Web Application Security Project (OWASP) see
234-
[Cross-Site Request Forgery (CSRF)](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29) and
235-
[Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet](https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet). The Stanford University
236-
paper [Robust Defenses for Cross-Site Request Forgery](https://seclab.stanford.edu/websec/csrf/csrf.pdf) is also a rich source of detail.
253+
For information about CSRF at the Open Web Application Security Project (OWASP), see
254+
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank">Cross-Site Request Forgery (CSRF)</a> and
255+
<a href="https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet" target="_blank">Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</a>.
256+
The Stanford University paper
257+
<a href="https://seclab.stanford.edu/websec/csrf/csrf.pdf" target="_blank">Robust Defenses for Cross-Site Request Forgery</a> is a rich source of detail.
258+
259+
See also Dave Smith's easy-to-understand
260+
<a href="https://www.youtube.com/watch?v=9inczw6qtpY" target="_blank" title="Cross Site Request Funkery Securing Your Angular Apps From Evil Doers">talk on XSRF at AngularConnect 2016</a>.
237261

238262
h3#xssi Cross-site script inclusion (XSSI)
239263
:marked

public/docs/ts/latest/guide/server-communication.jade

+22-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ block includes
2727
- [Always handle errors](#error-handling).
2828
- [Send data to the server](#update).
2929
<li if-docs="ts"> [Fall back to promises](#promises).</li>
30-
- [Cross-origin requests: Wikipedia example](#cors).
30+
- [Cross-Origin Requests: Wikipedia example](#cors).
3131
<ul if-docs="ts">
3232
<li> [Search parameters](#search-parameters).</li>
3333
<li> [More fun with observables](#more-observables).</li>
3434
</ul>
35+
- [Guarding against Cross-Site Request Forgery](#xsrf)
3536
- [Appendix: Tour of Heroes in-memory server](#in-mem-web-api).
3637

3738
A <live-example>live example</live-example> illustrates these topics.
@@ -46,7 +47,7 @@ block demos-list
4647
:marked
4748
- [The Tour of Heroes *HTTP* client demo](#http-client).
4849
- [Fall back to !{_Promise}s](#promises).
49-
- [Cross-origin requests: Wikipedia example](#cors).
50+
- [Cross-Origin Requests: Wikipedia example](#cors).
5051
- [More fun with observables](#more-observables).
5152

5253
:marked
@@ -446,7 +447,7 @@ block hero-list-comp-add-hero
446447

447448
To understand the implications and consequences of subscriptions, watch [Ben Lesh's talk on observables](https://www.youtube.com/watch?v=3LKMwkuK0ZE) or his video course on [egghead.io](https://egghead.io/lessons/rxjs-rxjs-observables-vs-promises).
448449

449-
h2#cors Cross-origin requests: Wikipedia example
450+
h2#cors Cross-Origin Requests: Wikipedia example
450451
:marked
451452
You just learned how to make `XMLHttpRequests` using the !{_Angular_Http} service.
452453
This is the most common approach for server communication, but it doesn't work in all scenarios.
@@ -628,6 +629,24 @@ block wikipedia-jsonp+
628629
You added the `debounceTime`, `distinctUntilChanged`, and `switchMap` operators to the RxJS `Observable` class
629630
in `rxjs-operators` as [described above](#rxjs).
630631

632+
a#xsrf
633+
.l-main-section
634+
:marked
635+
## Guarding against Cross-Site Request Forgery
636+
637+
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
638+
a different web page with malignant code that secretly sends a malicious request to your application's web server,
639+
640+
The server and client application must work together to thwart this attack.
641+
Angular's `Http` client does its part by applying a default `CookieXSRFStrategy` automatically to all requests.
642+
643+
The `CookieXSRFStrategy` supports a common anti-XSRF technique in which the server sends a randomly
644+
generated authentication token in a cookie named `XSRF-TOKEN`.
645+
The HTTP client adds an `X-XSRF-TOKEN` header with that token value to subsequent requests.
646+
The server receives both the cookie and the header, compares them, and processes the request only if the cookie and header match.
647+
648+
See the [XSRF topic on the Security page](security.html#xsrf) for more information about XSRF and Angular's `XSRFStrategy` counter measures.
649+
631650
a#in-mem-web-api
632651
.l-main-section
633652
:marked

0 commit comments

Comments
 (0)