|
6 | 6 | *
|
7 | 7 | * @element html
|
8 | 8 | * @description
|
9 |
| - * Enables [CSP (Content Security Policy)](https://developer.mozilla.org/en/Security/CSP) support. |
| 9 | + * |
| 10 | + * Angular has some features that can break certain |
| 11 | + * [CSP (Content Security Policy)](https://developer.mozilla.org/en/Security/CSP) rules. |
| 12 | + * |
| 13 | + * If you intend to implement these rules then you must tell Angular not to use these features. |
10 | 14 | *
|
11 | 15 | * This is necessary when developing things like Google Chrome Extensions or Universal Windows Apps.
|
12 | 16 | *
|
13 |
| - * CSP forbids apps to use `eval` or `Function(string)` generated functions (among other things). |
14 |
| - * For Angular to be CSP compatible there are only two things that we need to do differently: |
15 | 17 | *
|
16 |
| - * - don't use `Function` constructor to generate optimized value getters |
17 |
| - * - don't inject custom stylesheet into the document |
| 18 | + * The following rules affect Angular: |
18 | 19 | *
|
19 |
| - * AngularJS uses `Function(string)` generated functions as a speed optimization. Applying the `ngCsp` |
20 |
| - * directive will cause Angular to use CSP compatibility mode. When this mode is on AngularJS will |
21 |
| - * evaluate all expressions up to 30% slower than in non-CSP mode, but no security violations will |
22 |
| - * be raised. |
| 20 | + * * `unsafe-eval`: this rule forbids apps to use `eval` or `Function(string)` generated functions |
| 21 | + * (among other things). Angular makes use of this in the {@link $parse} service to provide a 30% |
| 22 | + * increase in the speed of evaluating Angular expressions. |
23 | 23 | *
|
24 |
| - * CSP forbids JavaScript to inline stylesheet rules. In non CSP mode Angular automatically |
25 |
| - * includes some CSS rules (e.g. {@link ng.directive:ngCloak ngCloak}). |
26 |
| - * To make those directives work in CSP mode, include the `angular-csp.css` manually. |
| 24 | + * * `unsafe-inline`: this rule forbids apps from inject custom styles into the document. Angular |
| 25 | + * makes use of this to include some CSS rules (e.g. {@link ngCloak} and {@link ngHide}). |
| 26 | + * To make these directives work when a CSP rule is blocking inline styles, you must link to the |
| 27 | + * `angular-csp.css` in your HTML manually. |
27 | 28 | *
|
28 |
| - * Angular tries to autodetect if CSP is active and automatically turn on the CSP-safe mode. This |
29 |
| - * autodetection however triggers a CSP error to be logged in the console: |
| 29 | + * If you do not provide `ngCsp` then Angular tries to autodetect if CSP is blocking unsafe-eval |
| 30 | + * and automatically deactivates this feature in the {@link $parse} service. This autodetection, |
| 31 | + * however, triggers a CSP error to be logged in the console: |
30 | 32 | *
|
31 | 33 | * ```
|
32 | 34 | * Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of
|
|
35 | 37 | * ```
|
36 | 38 | *
|
37 | 39 | * This error is harmless but annoying. To prevent the error from showing up, put the `ngCsp`
|
38 |
| - * directive on the root element of the application or on the `angular.js` script tag, whichever |
39 |
| - * appears first in the html document. |
| 40 | + * directive on an element of the HTML document that appears before the `<script>` tag that loads |
| 41 | + * the `angular.js` file. |
40 | 42 | *
|
41 | 43 | * *Note: This directive is only available in the `ng-csp` and `data-ng-csp` attribute form.*
|
42 | 44 | *
|
| 45 | + * You can specify which of the CSP related Angular features should be deactivated by providing |
| 46 | + * a value for the `ng-csp` attribute. The options are as follows: |
| 47 | + * |
| 48 | + * * no-inline-style: this stops Angular from injecting CSS styles into the DOM |
| 49 | + * |
| 50 | + * * no-unsafe-eval: this stops Angular from optimising $parse with unsafe eval of strings |
| 51 | + * |
| 52 | + * You can use these values in the following combinations: |
| 53 | + * |
| 54 | + * |
| 55 | + * * No declaration means that Angular will assume that you can do inline styles, but it will do |
| 56 | + * a runtime check for unsafe-eval. E.g. `<body>`. |
| 57 | + * |
| 58 | + * * A simple `ng-csp` (or `data-ng-csp`) attribute will tell Angular to deactivate both inline |
| 59 | + * styles and unsafe eval. E.g. `<body ng-csp>`. |
| 60 | + * |
| 61 | + * * Specifying only `no-unsafe-eval` tells Angular that we must not use eval, but that we can inject |
| 62 | + * inline styles. E.g. `<body ng-csp="no-unsafe-eval">`. |
| 63 | + * |
| 64 | + * * Specifying only `no-inline-style` tells Angular that we must not inject styles, but that we can |
| 65 | + * run eval - no automcatic check for unsafe eval will occur. E.g. `<body ng-csp="no-inline-style">` |
| 66 | + * |
| 67 | + * * Specifying both `no-unsafe-eval` and `no-inline-style` tells Angular that we must not inject |
| 68 | + * styles nor use eval, which is the same as an empty: ng-csp, except that no runtime check for |
| 69 | + * unsafe eval will occur. E.g.`<body ng-csp="no-inline-style;no-unsafe-eval">` |
| 70 | + * |
43 | 71 | * @example
|
44 | 72 | * This example shows how to apply the `ngCsp` directive to the `html` tag.
|
45 | 73 | ```html
|
|
171 | 199 |
|
172 | 200 | // ngCsp is not implemented as a proper directive any more, because we need it be processed while we
|
173 | 201 | // bootstrap the system (before $parse is instantiated), for this reason we just have
|
174 |
| -// the csp.isActive() fn that looks for ng-csp attribute anywhere in the current doc |
| 202 | +// the csp() fn that looks for the `ng-csp` attribute anywhere in the current doc |
0 commit comments