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

Commit 0dfd37e

Browse files
chalinfilipesilva
authored andcommitted
docs(security): hide TOC for Dart; other minor copyedits (#3017)
1 parent a74f8fb commit 0dfd37e

File tree

3 files changed

+157
-154
lines changed

3 files changed

+157
-154
lines changed
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<!--#docregion -->
22
<h3>Bypass Security Component</h3>
33

4-
<!--#docregion dangerous-url -->
4+
<!--#docregion URL -->
55
<h4>An untrusted URL:</h4>
66
<p><a class="e2e-dangerous-url" [href]="dangerousUrl">Click me</a></p>
77
<h4>A trusted URL:</h4>
88
<p><a class="e2e-trusted-url" [href]="trustedUrl">Click me</a></p>
9-
<!--#enddocregion dangerous-url -->
9+
<!--#enddocregion URL -->
1010

11-
<!--#docregion iframe-videoid -->
11+
<!--#docregion iframe -->
1212
<h4>Resource URL:</h4>
1313
<p><label>Showing: <input (input)="updateVideoUrl($event.target.value)"></label></p>
1414
<p>Trusted:</p>
1515
<iframe class="e2e-iframe-trusted-src" width="640" height="390" [src]="videoUrl"></iframe>
1616
<p>Untrusted:</p>
1717
<iframe class="e2e-iframe-untrusted-src" width="640" height="390" [src]="dangerousVideoUrl"></iframe>
18-
<!--#enddocregion iframe-videoid -->
19-
20-
<!--#enddocregion -->

public/docs/_examples/security/ts/app/inner-html-binding.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Component } from '@angular/core';
66
selector: 'inner-html-binding',
77
templateUrl: 'inner-html-binding.component.html',
88
})
9-
// #docregion inner-html-controller
9+
// #docregion class
1010
export class InnerHtmlBindingComponent {
1111
// For example, a user/attacker-controlled value from a URL.
1212
htmlSnippet = 'Template <script>alert("0wned")</script> <b>Syntax</b>';

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

Lines changed: 153 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ block includes
88

99
For more information about the attacks and mitigations described below, see [OWASP Guide Project](https://www.owasp.org/index.php/Category:OWASP_Guide_Project).
1010

11-
.l-main-section
12-
:marked
13-
# Contents:
14-
15-
* [Reporting vulnerabilities](#report-issues).
16-
* [Best practices](#best-practices).
17-
* [Preventing cross-site scripting (XSS)](#xss).
18-
* [Trusting safe values](#bypass-security-apis).
19-
* [HTTP-Level vulnerabilities](#http).
20-
* [Auditing Angular applications](#code-review).
11+
+ifDocsFor('ts')
12+
.l-main-section
13+
:marked
14+
# Contents:
15+
16+
* [Reporting vulnerabilities](#report-issues).
17+
* [Best practices](#best-practices).
18+
* [Preventing cross-site scripting (XSS)](#xss).
19+
* [Trusting safe values](#bypass-security-apis).
20+
* [HTTP-Level vulnerabilities](#http).
21+
* [Auditing Angular applications](#code-review).
2122

23+
:marked
2224
Try the <live-example></live-example> of the code shown in this page.
2325

2426
.l-main-section
@@ -103,15 +105,17 @@ h2#xss Preventing cross-site scripting (XSS)
103105
a value that an attacker might control into `innerHTML` normally causes an XSS
104106
vulnerability. For example, code contained in a `<script>` tag is executed:
105107

106-
+makeExcerpt('app/inner-html-binding.component.ts ()', 'inner-html-controller')
108+
+makeExcerpt('app/inner-html-binding.component.ts', 'class')
107109

108-
:marked
109-
Angular recognizes the value as unsafe and automatically sanitizes it, which removes the `<script>`
110-
tag but keeps safe content such as the text content of the `<script>` tag, or the `<b>` element.
110+
block html-sanitization
111+
:marked
112+
Angular recognizes the value as unsafe and automatically sanitizes it, which removes the `<script>`
113+
tag but keeps safe content such as the text content of the `<script>` tag, or the `<b>` element.
114+
115+
figure.image-display
116+
img(src='/resources/images/devguide/security/binding-inner-html.png'
117+
alt='A screenshot showing interpolated and bound HTML values')
111118

112-
figure.image-display
113-
img(src='/resources/images/devguide/security/binding-inner-html.png'
114-
alt='A screenshot showing interpolated and bound HTML values')
115119
:marked
116120
### Avoid direct use of the DOM APIs
117121

@@ -144,136 +148,138 @@ figure.image-display
144148
the server. Do not generate Angular templates on the server side using a templating language; doing this
145149
carries a high risk of introducing template-injection vulnerabilities.
146150

147-
.l-main-section
148-
h2#bypass-security-apis Trusting safe values
149-
:marked
150-
Sometimes applications genuinely need to include executable code, display an `<iframe>` from some
151-
URL, or construct potentially dangerous URLs. To prevent automatic sanitization in any of these
152-
situations, you can tell Angular that you inspected a value, checked how it was generated, and made
153-
sure it will always be secure. But **be careful**! If you trust a value that might be malicious, you
154-
are introducing a security vulnerability into your application. If in doubt, find a professional
155-
security reviewer.
156-
157-
You can mark a value as trusted by injecting `DomSanitizer` and calling one of the
158-
following methods:
159-
160-
* `bypassSecurityTrustHtml`
161-
* `bypassSecurityTrustScript`
162-
* `bypassSecurityTrustStyle`
163-
* `bypassSecurityTrustUrl`
164-
* `bypassSecurityTrustResourceUrl`
165-
166-
Remember, whether a value is safe depends on context, so you need to choose the right context for
167-
your intended use of the value. Imagine that the following template needs to bind a URL to a
168-
`javascript:alert(...)` call:
169-
170-
+makeExcerpt('app/bypass-security.component.html ()', 'dangerous-url')
171-
172-
:marked
173-
Normally, Angular automatically sanitizes the URL, disables the dangerous code, and
174-
in development mode, logs this action to the console. To prevent
175-
this, you can mark the URL value as a trusted URL using the `bypassSecurityTrustUrl` call:
176-
177-
+makeExcerpt('app/bypass-security.component.ts ()', 'trust-url')
178-
179-
figure.image-display
180-
img(src='/resources/images/devguide/security/bypass-security-component.png'
181-
alt='A screenshot showing an alert box created from a trusted URL')
182-
183-
:marked
184-
If you need to convert user input into a trusted value, use a
185-
controller method. The template below allows users to enter a YouTube video ID and load the
186-
corresponding video in an `<iframe>`. The `<iframe src>` attribute is a resource URL security
187-
context, because an untrusted source can, for example, smuggle in file downloads that unsuspecting users
188-
could execute. So call a method on the controller to construct a trusted video URL, that causes
189-
Angular to then allow binding into `<iframe src>`:
190-
191-
+makeExcerpt('app/bypass-security.component.html ()', 'iframe-videoid')
192-
+makeExcerpt('app/bypass-security.component.ts ()', 'trust-video-url')
193-
194-
.l-main-section
195-
h2#http HTTP-level vulnerabilities
196-
:marked
197-
Angular has built-in support to help prevent two common HTTP vulnerabilities, cross-site request
198-
forgery (CSRF or XSRF) and cross-site script inclusion (XSSI). Both of these must be mitigated primarily
199-
on the server side, but Angular ships helpers to make integration on the client side easier.
200-
201-
h3#xsrf Cross-site request forgery
202-
:marked
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`:
248-
249-
code-example(language="typescript").
250-
{ provide: XSRFStrategy, useClass: MyXSRFStrategy }
251-
252-
:marked
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>.
261-
262-
h3#xssi Cross-site script inclusion (XSSI)
263-
:marked
264-
Cross-site script inclusion, also known as JSON vulnerability, can allow an attacker's website to
265-
read data from a JSON API. The attack works on older browsers by overriding native JavaScript
266-
object constructors, and then including an API URL using a `<script>` tag.
267-
268-
This attack is only successful if the returned JSON is executable as JavaScript. Servers can
269-
prevent an attack by prefixing all JSON responses to make them non-executable, by convention, using the
270-
well-known string `")]}',\n"`.
271-
272-
Angular's `Http` library recognizes this convention and automatically strips the string
273-
`")]}',\n"` from all responses before further parsing.
274-
275-
For more information, see the XSSI section of this [Google web security blog
276-
post](https://security.googleblog.com/2011/05/website-security-for-webmasters.html).
151+
block bypass-security-apis
152+
.l-main-section
153+
h2#bypass-security-apis Trusting safe values
154+
:marked
155+
Sometimes applications genuinely need to include executable code, display an `<iframe>` from some
156+
URL, or construct potentially dangerous URLs. To prevent automatic sanitization in any of these
157+
situations, you can tell Angular that you inspected a value, checked how it was generated, and made
158+
sure it will always be secure. But **be careful**! If you trust a value that might be malicious, you
159+
are introducing a security vulnerability into your application. If in doubt, find a professional
160+
security reviewer.
161+
162+
You can mark a value as trusted by injecting `DomSanitizer` and calling one of the
163+
following methods:
164+
165+
* `bypassSecurityTrustHtml`
166+
* `bypassSecurityTrustScript`
167+
* `bypassSecurityTrustStyle`
168+
* `bypassSecurityTrustUrl`
169+
* `bypassSecurityTrustResourceUrl`
170+
171+
Remember, whether a value is safe depends on context, so you need to choose the right context for
172+
your intended use of the value. Imagine that the following template needs to bind a URL to a
173+
`javascript:alert(...)` call:
174+
175+
+makeExcerpt('app/bypass-security.component.html', 'URL')
176+
177+
:marked
178+
Normally, Angular automatically sanitizes the URL, disables the dangerous code, and
179+
in development mode, logs this action to the console. To prevent
180+
this, you can mark the URL value as a trusted URL using the `bypassSecurityTrustUrl` call:
181+
182+
+makeExcerpt('app/bypass-security.component.ts ()', 'trust-url')
183+
184+
figure.image-display
185+
img(src='/resources/images/devguide/security/bypass-security-component.png'
186+
alt='A screenshot showing an alert box created from a trusted URL')
187+
188+
:marked
189+
If you need to convert user input into a trusted value, use a
190+
controller method. The template below allows users to enter a YouTube video ID and load the
191+
corresponding video in an `<iframe>`. The `<iframe src>` attribute is a resource URL security
192+
context, because an untrusted source can, for example, smuggle in file downloads that unsuspecting users
193+
could execute. So call a method on the controller to construct a trusted video URL, that causes
194+
Angular to then allow binding into `<iframe src>`:
195+
196+
+makeExcerpt('app/bypass-security.component.html', 'iframe')
197+
+makeExcerpt('app/bypass-security.component.ts ()', 'trust-video-url')
198+
199+
block http
200+
.l-main-section
201+
h2#http HTTP-level vulnerabilities
202+
:marked
203+
Angular has built-in support to help prevent two common HTTP vulnerabilities, cross-site request
204+
forgery (CSRF or XSRF) and cross-site script inclusion (XSSI). Both of these must be mitigated primarily
205+
on the server side, but Angular ships helpers to make integration on the client side easier.
206+
207+
h3#xsrf Cross-site request forgery
208+
:marked
209+
In a cross-site request forgery (CSRF or XSRF), an attacker tricks the user into visiting
210+
a different web page (e.g. `evil.com`) with malignant code that secretly sends a malicious request
211+
to your application's web server (e.g. `example-bank.com`).
212+
213+
Assume the user is logged into the application at `example-bank.com`.
214+
The user opens an email and clicks a link to `evil.com` which opens in a new tab.
215+
216+
The `evil.com` page immediately sends a malicious request to `example-bank.com`.
217+
Perhaps it's a request to transfer money from the user's account to the attacker's account.
218+
The browser automatically sends the `example-bank.com` cookies (including the authentication cookie) with this request.
219+
220+
The `example-bank.com` server, if it lacks XSRF protection, can't tell the difference between a legitimate request from the application
221+
and the forged request from `evil.com`.
222+
223+
To prevent this, the application must ensure that a user request originates from the real
224+
application, not from a different site.
225+
The server and client must cooperate to thwart this attack.
226+
227+
In a common anti-XSRF technique, the application server sends a randomly
228+
generated authentication token in a cookie.
229+
The client code reads the cookie and adds a custom request header with the token in all subsequent requests.
230+
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.
231+
232+
This technique is effective because all browsers implement the _same origin policy_. Only code from the website
233+
on which cookies are set can read the cookies from that site and set custom headers on requests to that site.
234+
That means only your application can read this cookie token and set the custom header. The malicious code on `evil.com` can't.
235+
236+
Angular's `http` has built-in support for the client-side half of this technique in its `XSRFStrategy`.
237+
The default `CookieXSRFStrategy` is turned on automatically.
238+
Before sending an HTTP request, the `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and
239+
sets a header named `X-XSRF-TOKEN` with the value of that cookie.
240+
241+
The server must do its part by setting the
242+
initial `XSRF-TOKEN` cookie and confirming that each subsequent state-modifying request
243+
includes a matching `XSRF-TOKEN` cookie and `X-XSRF-TOKEN` header.
244+
245+
XSRF/CSRF tokens should be unique per user and session, have a large random value generated by a
246+
cryptographically secure random number generator, and should expire in a day or two.
247+
248+
Your server may use a different cookie or header name for this purpose.
249+
An Angular application can customize cookie and header names by providing its own `CookieXSRFStrategy` values.
250+
code-example(language="typescript").
251+
{ provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name') }
252+
:marked
253+
Or you can implement and provide an entirely custom `XSRFStrategy`:
254+
255+
code-example(language="typescript").
256+
{ provide: XSRFStrategy, useClass: MyXSRFStrategy }
257+
258+
:marked
259+
For information about CSRF at the Open Web Application Security Project (OWASP), see
260+
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29" target="_blank">Cross-Site Request Forgery (CSRF)</a> and
261+
<a href="https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet" target="_blank">Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet</a>.
262+
The Stanford University paper
263+
<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.
264+
265+
See also Dave Smith's easy-to-understand
266+
<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>.
267+
268+
h3#xssi Cross-site script inclusion (XSSI)
269+
:marked
270+
Cross-site script inclusion, also known as JSON vulnerability, can allow an attacker's website to
271+
read data from a JSON API. The attack works on older browsers by overriding native JavaScript
272+
object constructors, and then including an API URL using a `<script>` tag.
273+
274+
This attack is only successful if the returned JSON is executable as JavaScript. Servers can
275+
prevent an attack by prefixing all JSON responses to make them non-executable, by convention, using the
276+
well-known string `")]}',\n"`.
277+
278+
Angular's `Http` library recognizes this convention and automatically strips the string
279+
`")]}',\n"` from all responses before further parsing.
280+
281+
For more information, see the XSSI section of this [Google web security blog
282+
post](https://security.googleblog.com/2011/05/website-security-for-webmasters.html).
277283

278284
.l-main-section
279285
h2#code-review Auditing angular applications

0 commit comments

Comments
 (0)