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

Commit 46f9cfe

Browse files
committed
docs(security): improve xsrf description and add it to http chapter as well
1 parent 4c85f0a commit 46f9cfe

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

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

+29-19
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ 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.
203+
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
204+
a different web page with malicious code that secretly sends a request to your application's web server.
205+
If the user is logged into your application, the browser sends the request along with authentication cookies.
206+
An _unprotected_ server can't tell the difference between a legitimate request and a forged request.
207+
208+
In this manner, for example, a knowledgable attacker could transfer money from the user's account to the attacker's account.
208209

209210
To prevent this, your application must ensure that user requests originate in your own
210211
application, not on a different site. A common technique is that the server sends a randomly
@@ -213,27 +214,36 @@ h3#xsrf Cross-site request forgery
213214
each API request, the server then validates the client by checking that the token is sent back,
214215
usually in an HTTP header called `X-XSRF-TOKEN`.
215216

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.
217+
Angular's `http` has built-in support for the client-side half of this technique in its `XSRFStrategy`.
218+
The default `CookieXSRFStrategy` is turned on automatically.
219+
During each HTTP request, the `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and
220+
sets a header named `X-XSRF-TOKEN` with the value of that cookie.
221+
222+
The server must do its part by setting the
223+
initial `XSRF-TOKEN` cookie and confirming that each subsequent state-modifying request
224+
includes a matching `XSRF-TOKEN` cookie and `X-XSRF-TOKEN` header.
220225

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.
226+
XSRF/CSRF tokens should be unique per user and session, have a large random value generated by a
227+
cryptographically secure random number generator, and should expire after a reasonably short period of time.
223228

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:
229+
Angular applications can customize cookie and header names by providing their own `CookieXSRFStrategy` values.
230+
code-example(language="typescript").
231+
{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name') }
232+
:marked
233+
Or you can implement and provide an entirely custom `XSRFStrategy`:
227234

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

232238
:marked
233239
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.
240+
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank">Cross-Site Request Forgery (CSRF)</a> and
241+
<a href="https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet" target="_blank">Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</a>.
242+
The Stanford University paper
243+
<a href="" target="_blank">Robust Defenses for Cross-Site Request Forgery</a> is a rich source of detail.
244+
245+
See also Dave Smith's masterful, easy-to-understand
246+
<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>.
237247

238248
h3#xssi Cross-site script inclusion (XSSI)
239249
:marked

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

+24-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,26 @@ 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 malicious code that secretly sends a request to your application's web server.
639+
If the user is logged into your application, the browser sends the request along with authentication cookies.
640+
An _unprotected_ server can't tell the difference between a legitimate request and a forged request.
641+
642+
In this manner, for example, a knowledgable attacker could transfer money from the user's account to the attacker's account.
643+
644+
The server and client application must work together to thwart this attack.
645+
In one common technique, the server sends a randomly
646+
generated authentication token in a cookie, often named `XSRF-TOKEN`.
647+
The HTTP client adds that token to the request header, typically named `X-XSRF-TOKEN`, of subsequent requests.
648+
The server receives both the cookie and the header, compares them, and processes the request only if the cookie and header match.
649+
650+
Angular's `Http` client supports this strategy automatically. See the [XSRF topic on the Security page](security.html#xsrf) for more information.
651+
631652
a#in-mem-web-api
632653
.l-main-section
633654
:marked

0 commit comments

Comments
 (0)