11block includes
22 include ../_util-fns
33:marked
4- Web application security has many aspects. This documentation describes Angular's built in
4+ Web application security has many aspects. This chapter describes Angular's built in
55 protections against common web application vulnerabilities and attacks, such as Cross Site
66 Scripting Attacks. It does not cover application level security, such as authentication (_Who is
77 this user?_) or authorization (_What can this user do?_).
@@ -50,7 +50,7 @@ h2#best-practices Best Practices
5050h2#xss Preventing Cross-Site Scripting (XSS)
5151:marked
5252 [Cross-Site Scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting) enables attackers
53- to inject malicious code into web pages. Such code can then for example steal user's data (in
53+ to inject malicious code into web pages. Such code can then, for example, steal user's data (in
5454 particular their login data), or perform actions impersonating the user. This is one of the most
5555 common attacks on the web.
5656
@@ -81,10 +81,10 @@ h2#xss Preventing Cross-Site Scripting (XSS)
8181
8282 Angular defines four security contexts: HTML, style, URL, and resource URL.
8383
84- * HTML is used when interpreting a value as HTML, e.g. when binding to `innerHtml`
84+ * HTML is used when interpreting a value as HTML, e.g., when binding to `innerHtml`
8585 * Style is used when binding CSS into the `style` property
8686 * URL is used for URL properties such as `<a href>`
87- * Resource URLs are URLs that will be loaded and executed as code, e.g. in `<script src>`
87+ * Resource URLs are URLs that will be loaded and executed as code, e.g., in `<script src>`
8888
8989 Angular sanitizes untrusted values for the first three items; sanitizing resource URLs is not
9090 possible as they contain arbitrary code. In development mode, Angular prints a console warning
@@ -95,7 +95,8 @@ h2#xss Preventing Cross-Site Scripting (XSS)
9595 The template below binds the value of `htmlSnippet`, once by interpolating it into an element's
9696 content, and once by binding it to the `innerHTML` property of an element.
9797
98- + makeExample('security/ts/app/inner-html-binding.component.html' )( format ="." )
98+ + makeExcerpt('app/inner-html-binding.component.html' )
99+
99100:marked
100101 Interpolated content is always escaped - the HTML is not interpreted, and the browser displays
101102 angle brackets in the elements text content.
@@ -121,8 +122,7 @@ figure.image-display
121122
122123 ### Content Security Policy
123124
124- A [Content Security Policy (CSP)]
125- (http://www.html5rocks.com/en/tutorials/security/content-security-policy/) is a defense-in-depth
125+ A [Content Security Policy (CSP)](http://www.html5rocks.com/en/tutorials/security/content-security-policy/) is a defense-in-depth
126126 technique to prevent XSS. To enable CSP, configure your web server to return an appropriate
127127 `Content-Security-Policy` HTTP header.
128128
@@ -139,7 +139,7 @@ figure.image-display
139139 ### Server side XSS protection
140140
141141 HTML constructed on the server is vulnerable to injection attacks. Injecting template code into an
142- Angular application is the same as injecting executable code (e.g. JavaScript) into the
142+ Angular application is the same as injecting executable code (e.g., JavaScript) into the
143143 application; it gives the attacker full control over the application. To prevent this, make sure
144144 to use a templating language that automatically escapes values to prevent XSS vulnerabilities on
145145 the server. Do not generate Angular templates on the server side using a templating language, this
@@ -181,7 +181,7 @@ figure.image-display
181181 If we need to convert user input into a trusted value, it can be convenient to do so in a
182182 controller method. The template below allows users to enter a YouTube video ID, and load the
183183 corresponding video in an `<iframe>`. The `<iframe src>` attribute is a resource URL security
184- context, because an untrusted source can e.g. smuggle in file downloads that unsuspecting users
184+ context, because an untrusted source can, e.g., smuggle in file downloads that unsuspecting users
185185 would execute. So we call a method on the controller to construct a trusted video URL, which
186186 Angular then allows binding into `<iframe src>`.
187187
@@ -198,12 +198,12 @@ h2#http HTTP-level Vulnerabilities
198198h3#xsrf Cross-site Request Forgery (XSRF)
199199:marked
200200 In a Cross-site Request Forgery (XSRF or CSRF), an attacker tricks the user into visiting a
201- _different_ page, and has them e.g. submit a form that sends a request to your application's
201+ _different_ page, and has them, e.g., submit a form that sends a request to your application's
202202 web server. If the user is logged into your application, the browser will send authentication
203- cookies, and the attacker could - for example - cause a bank transfer in the user's name with
203+ cookies, and the attacker could — for example — cause a bank transfer in the user's name with
204204 the right request.
205205
206- To prevent this, your application must make sure that user requests originate in your own
206+ To prevent this, your application must ensure that user requests originate in your own
207207 application, not on a different site. A common technique is that the server sends a randomly
208208 generated authentication token in a cookie, often with the name `XSRF-TOKEN`. Cookies can only
209209 be read by the website on which they are set, so only your own application can read this token. On
@@ -220,14 +220,17 @@ h3#xsrf Cross-site Request Forgery (XSRF)
220220
221221 Angular applications can customize cookie and header names by binding their own
222222 `CookieXSRFStrategy` value, or implement an entirely custom `XSRFStrategy` by providing a custom
223- binding for that type, by adding
224- `provide(XSRFStrategy, {useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name')})` or
225- `provide(XSRFStrategy, {useClass: MyXSRFStrategy})` to your providers list.
223+ binding for that type, by adding either of the following to your providers list:
224+
225+ code-example( language ="typescript" ) .
226+ { provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name')}
227+ { provide: XSRFStrategy, useClass: MyXSRFStrategy}
226228
229+ :marked
227230 Learn about Cross Site Request Forgery (XSRF) at the Open Web Application Security Project (OWASP)
228231 [here](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29) and
229232 [here](https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet). This [Stanford University
230- paper](https://seclab.stanford.edu/websec/csrf/csrf.pdf) is a rich source of detail.
233+ paper](https://seclab.stanford.edu/websec/csrf/csrf.pdf) is also a rich source of detail.
231234
232235h3#xssi Cross-site Script Inclusion (XSSI)
233236:marked
0 commit comments