1
1
block includes
2
2
include ../_util-fns
3
3
: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
5
5
protections against common web application vulnerabilities and attacks, such as Cross Site
6
6
Scripting Attacks. It does not cover application level security, such as authentication (_Who is
7
7
this user?_) or authorization (_What can this user do?_).
@@ -50,7 +50,7 @@ h2#best-practices Best Practices
50
50
h2#xss Preventing Cross-Site Scripting (XSS)
51
51
:marked
52
52
[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
54
54
particular their login data), or perform actions impersonating the user. This is one of the most
55
55
common attacks on the web.
56
56
@@ -81,10 +81,10 @@ h2#xss Preventing Cross-Site Scripting (XSS)
81
81
82
82
Angular defines four security contexts: HTML, style, URL, and resource URL.
83
83
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`
85
85
* Style is used when binding CSS into the `style` property
86
86
* 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>`
88
88
89
89
Angular sanitizes untrusted values for the first three items; sanitizing resource URLs is not
90
90
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)
95
95
The template below binds the value of `htmlSnippet`, once by interpolating it into an element's
96
96
content, and once by binding it to the `innerHTML` property of an element.
97
97
98
- + makeExample('security/ts/app/inner-html-binding.component.html' )( format ="." )
98
+ + makeExcerpt('app/inner-html-binding.component.html' )
99
+
99
100
:marked
100
101
Interpolated content is always escaped - the HTML is not interpreted, and the browser displays
101
102
angle brackets in the elements text content.
@@ -121,8 +122,7 @@ figure.image-display
121
122
122
123
### Content Security Policy
123
124
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
126
126
technique to prevent XSS. To enable CSP, configure your web server to return an appropriate
127
127
`Content-Security-Policy` HTTP header.
128
128
@@ -139,7 +139,7 @@ figure.image-display
139
139
### Server side XSS protection
140
140
141
141
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
143
143
application; it gives the attacker full control over the application. To prevent this, make sure
144
144
to use a templating language that automatically escapes values to prevent XSS vulnerabilities on
145
145
the server. Do not generate Angular templates on the server side using a templating language, this
@@ -181,7 +181,7 @@ figure.image-display
181
181
If we need to convert user input into a trusted value, it can be convenient to do so in a
182
182
controller method. The template below allows users to enter a YouTube video ID, and load the
183
183
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
185
185
would execute. So we call a method on the controller to construct a trusted video URL, which
186
186
Angular then allows binding into `<iframe src>`.
187
187
@@ -198,12 +198,12 @@ h2#http HTTP-level Vulnerabilities
198
198
h3#xsrf Cross-site Request Forgery (XSRF)
199
199
:marked
200
200
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
202
202
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
204
204
the right request.
205
205
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
207
207
application, not on a different site. A common technique is that the server sends a randomly
208
208
generated authentication token in a cookie, often with the name `XSRF-TOKEN`. Cookies can only
209
209
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)
220
220
221
221
Angular applications can customize cookie and header names by binding their own
222
222
`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}
226
228
229
+ :marked
227
230
Learn about Cross Site Request Forgery (XSRF) at the Open Web Application Security Project (OWASP)
228
231
[here](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29) and
229
232
[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.
231
234
232
235
h3#xssi Cross-site Script Inclusion (XSSI)
233
236
:marked
0 commit comments