@@ -9,6 +9,27 @@ This document explains some of AngularJS's security features and best practices
9
9
keep in mind as you build your application.
10
10
11
11
12
+ ## Reporting a security issue
13
+
14
+ Email us at [
[email protected] ](mailto:
[email protected] ) to report any potential
15
+ security issues in AngularJS.
16
+
17
+ Please keep in mind the points below about Angular's expression language.
18
+
19
+
20
+ ## Use the latest AngularJS possible
21
+
22
+ Like any software library, it is critical to keep AngularJS up to date. Please track the
23
+ [CHANGELOG](https://github.com/angular/angular.js/blob/master/CHANGELOG.md) and make sure you are aware
24
+ of upcoming security patches and other updates.
25
+
26
+ Be ready to update rapidly when new security-centric patches are available.
27
+
28
+ Those that stray from Angular standards (such as modifying Angular's core) may have difficulty updating,
29
+ so keeping to AngularJS standards is not just a functionality issue, it's also critical in order to
30
+ facilitate rapid security updates.
31
+
32
+
12
33
## Expression Sandboxing
13
34
14
35
AngularJS's expressions are sandboxed not for security reasons, but instead to maintain a proper
@@ -25,36 +46,80 @@ But if an attacker can change arbitrary HTML templates, there's nothing stopping
25
46
<script>somethingEvil();</script>
26
47
```
27
48
28
- It's better to design your application in such a way that users cannot change client-side templates.
49
+ **It's better to design your application in such a way that users cannot change client-side templates.**
50
+
29
51
For instance:
30
52
31
53
* Do not mix client and server templates
32
54
* Do not use user input to generate templates dynamically
33
55
* Do not run user input through `$scope.$eval`
34
56
* Consider using {@link ng.directive:ngCsp CSP} (but don't rely only on CSP)
35
57
36
- ## Mixing client-side and server-side templates
58
+
59
+ ### Mixing client-side and server-side templates
37
60
38
61
In general, we recommend against this because it can create unintended XSS vectors.
39
62
40
63
However, it's ok to mix server-side templating in the bootstrap template (`index.html`) as long
41
64
as user input cannot be used on the server to output html that would then be processed by Angular
42
65
in a way that would allow for arbitrary code execution.
43
66
44
- For instance, you can use server-side templating to dynamically generate CSS, URLs, etc, but not
45
- for generating templates that are bootstrapped/compiled by Angular.
67
+ ** For instance, you can use server-side templating to dynamically generate CSS, URLs, etc, but not
68
+ for generating templates that are bootstrapped/compiled by Angular.**
46
69
47
70
48
- ## Reporting a security issue
71
+ ## HTTP Requests
49
72
50
- Email us at [
[email protected] ](mailto:
[email protected] ) to report any potential
51
- security issues in AngularJS.
73
+ Whenever your application makes requests to a server there are potential security issues that need
74
+ to be blocked. Both server and the client must cooperate in order to eliminate these threats.
75
+ Angular comes pre-configured with strategies that address these issues, but for this to work backend
76
+ server cooperation is required.
77
+
78
+
79
+ ### Cross Site Request Forgery (XSRF/CSRF)
80
+
81
+ Protection from XSRF is provided by using the double-submit cookie defense pattern.
82
+ For more information please visit {@link $http#cross-site-request-forgery-xsrf-protection XSRF protection}.
83
+
84
+ ### JSON Hijacking Protection
85
+
86
+ Protection from JSON Hijacking is provided if the server prefixes all JSON requests with following string `")]}',\n"`.
87
+ Angular will automatically strip the prefix before processing it as JSON.
88
+ For more information please visit {@link $http#json-vulnerability-protection JSON Hijacking Protection}.
89
+
90
+
91
+ ## Strict Contextual Escaping
92
+
93
+ Strict Contextual Escaping (SCE) is a mode in which AngularJS requires bindings in certain contexts to require
94
+ a value that is marked as safe to use for that context.
95
+
96
+ This mode is implemented by the {@link $sce} service and various core directives.
97
+
98
+ One example of such a context is rendering arbitrary content via the {@link ngBindHtml} directive. If the content is
99
+ provided by a user there is a chance of Cross Site Scripting (XSS) attacks. The {@link ngBindHtml} directive will not
100
+ render content that is not marked as safe by {@link $sce}. The {@link ngSanitize} module can be used to clean such
101
+ user provided content and mark the content as safe.
102
+
103
+ **Be aware that marking untrusted data as safe via calls to {@link $sce#trustAsHtml `$sce.trustAsHtml`}, etc is
104
+ dangerous and will lead to Cross Site Scripting exploits.**
105
+
106
+ For more information please visit {@link $sce} and {@link ngSanitize.$sanitize}.
107
+
108
+ ## Using Local Caches
109
+
110
+ There are various places that the browser can store (or cache) data. Within Angular there are objects created by
111
+ the {@link $cacheFactory}. These objects, such as {@link $templateCache} are used to store and retrieve data,
112
+ primarily used by {@link $http} and the {@link script} directive to cache templates and other data.
113
+
114
+ Similarly the browser itself offers `localStorage` and `sessionStorage` objects for caching data.
115
+
116
+ **Attackers with local access can retrieve sensitive data from this cache even when users are not authenticated.**
52
117
53
- Please keep in mind the above points about Angular's expression language.
118
+ For instance in a long running Single Page Application (SPA), one user may "log out", but then another user
119
+ may access the application without refreshing, in which case all the cached data is still available.
54
120
121
+ For more information please visit [Web Storage Security](https://www.whitehatsec.com/blog/web-storage-security/).
55
122
56
123
## See also
57
124
58
125
* {@link ng.directive:ngCsp Content Security Policy}
59
- * {@link ng.$sce Strict Contextual Escaping}
60
- * {@link ngSanitize.$sanitize $sanitize}
0 commit comments