2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								block includes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  include ../_util-fns
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  This section describes Angular's built-in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  protections against common web application vulnerabilities and attacks such as cross-site
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  scripting attacks. It does not cover application-level security, such as authentication (_Who is
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  this user?_) or authorization (_What can this user do?_).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Web应用程序的安全涉及到很多方面。针对常见的漏洞和攻击, , , , ( ? ) ? 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For more information about the attacks and mitigations described below, see [OWASP Guide Project](https://www.owasp.org/index.php/Category:OWASP_Guide_Project).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  要了解更多攻防信息,参见[开放式Web应用程序安全项目(OWASP)](https://www.owasp.org/index.php/Category:OWASP_Guide_Project)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  # Contents:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  # 目录
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [Reporting vulnerabilities](#report-issues).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [举报漏洞](#report-issues)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [Best practices](#best-practices).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [最佳实践](#best-practices)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [Preventing cross-site scripting (XSS)](#xss).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [防范跨站脚本(XSS)攻击](#xss)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [Trusting safe values](#bypass-security-apis).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [信任安全值](#bypass-security-apis)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [HTTP-Level vulnerabilities](#http).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [HTTP级别的漏洞](#http)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * [Auditing Angular applications](#code-review).
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * [审计Angular应用程序](#code-review).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Try the <live-example></live-example> of the code shown in this page.
							 
						 
					
						
							
								
									
										
										
										
											2016-07-07 08:15:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  运行<live-example></live-example>来试用本页的代码。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#report-issues Reporting vulnerabilities
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#report-issues 举报漏洞
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Email us at [security@angular.io](mailto:security@angular.io) to report vulnerabilities in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular itself.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  给我们([security@angular.io](mailto:security@angular.io)) , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For more information about how Google handles security issues, see [Google's security
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  philosophy](https://www.google.com/about/appsecurity/).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  要了解关于“谷歌如何处理安全问题”的更多信息,参见[谷歌的安全哲学](https://www.google.com/about/appsecurity/)。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#best-practices Best practices
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								h2#best-practices 最佳实践
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * **Keep current with the latest Angular library releases.**
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  We regularly update our Angular libraries, and these updates may fix security defects discovered in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  previous versions. Check the Angular [change
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  log](https://github.com/angular/angular/blob/master/CHANGELOG.md) for security-related updates.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **及时把Angular包更新到最新版本。**
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  我们会频繁的更新Angular库, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * **Don't modify your copy of Angular.**
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Private, customized versions of Angular tend to fall behind the current version and may not include
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  important security fixes and enhancements. Instead, share your Angular improvements with the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  community and make a pull request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **不要修改你的Angular副本。**
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  私有的、定制版的Angular往往跟不上最新版本, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **Avoid Angular APIs marked in the documentation as “[_Security Risk_](#bypass-security-apis).”**
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * **避免使用本文档中带“[_安全风险_](#bypass-security-apis)”标记的Angular API。**
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#xss Preventing cross-site scripting (XSS)
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#xss 防范跨站脚本(XSS)攻击
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [Cross-site scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting) enables attackers
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  to inject malicious code into web pages. Such code can then, for example, steal user data (in
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  particular, their login data) or perform actions impersonating the user. This is one of the most
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  common attacks on the web.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [跨站脚本(XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting)允许攻击者将恶意代码注入到页面中。这些代码可以偷取用户数据
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  ( ) , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  To block XSS attacks, you must prevent malicious code from entering the DOM(Document Object Model). For example, if an
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  attacker can trick you into inserting a `<script>` tag in the DOM, they can run arbitrary code on
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  your website. The attack is not limited to `<script>` tags—many elements and properties in the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  DOM allow code execution, for example, `<img onerror="...">` and `<a href="javascript:...">`. If
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  attacker-controlled data enters the DOM, expect security vulnerabilities.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  为了防范XSS攻击, , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  除了`<script>`, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  如果攻击者所控制的数据混进了DOM, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  ### Angular’ 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### Angular的“跨站脚本安全模型”
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  To systematically block XSS bugs, Angular treats all values as untrusted by default. When a value
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  is inserted into the DOM from a template, via property, attribute, style, class binding, or interpolation, Angular sanitizes and escapes untrusted values.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  为了系统性的防范XSS问题, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  当值从模板中以属性( ) ( , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  Angular将对这些值进行无害化处理( ) , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  _Angular templates are the same as executable code_: HTML, attributes, and binding expressions
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  (but not the values bound!) in templates are trusted to be safe. This means that applications must
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  prevent values that an attacker can control from ever making it into the source code of a
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  template. Never generate template source code by concatenating user input and templates! Using
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the [offline template compiler](#offline-template-compiler) is an effective way to prevent these
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  vulnerabilities, also known as _template injection_.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  **Angular的模板同样是可执行的**: ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  这意味着应用必须防止把可能被攻击者控制的值直接编入模板的源码中。永远不要根据用户的输入和原始模板动态生成模板源码!
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  使用[离线模板编译器](#offline-template-compiler)是防范这类“模板注入”漏洞的有效途径。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ### Sanitization and security contexts
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### 无害化处理与安全环境
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  _Sanitization_ is the inspection of an untrusted value, turning it into a value that is safe to insert into
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the DOM. In many cases, sanitization does not change a value at all. Sanitization depends on context:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  a value that is harmless in CSS is potentially dangerous in a URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  无害化处理会审查不可信的值, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  无害化处理的方式取决于所在的环境: , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular defines four security contexts—HTML, style, URL, and resource URL:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular定义了四个安全环境 - HTML, , , : 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **HTML** is used when interpreting a value as HTML, for example, when binding to `innerHtml`
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  * **HTML**: , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **Style** is used when binding CSS into the `style` property
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  * **样式**: 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **URL** is used for URL properties such as `<a href>`
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  * **URL**: , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **Resource URL** is a URL that will be loaded and executed as code, for example, in `<script src>`
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  * **资源URL**:值需要被当做代码而加载并执行时使用,比如`<script src>`中的URL。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular sanitizes untrusted values for the first three items; sanitizing resource URLs is not
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  possible because they contain arbitrary code. In development mode, Angular prints a console warning
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  when it has to change a value during sanitization.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular会对前三项中种不可信的值进行无害化处理。但Angular无法对第四种资源URL进行无害化, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  如果Angular在进行无害化处理时需要被迫改变一个值, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ### Sanitization example
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### 无害化示例
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The template below binds the value of `htmlSnippet`, once by interpolating it into an element's
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  content, and once by binding it to the `innerHTML` property of an element:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  下面的例子绑定了`htmlSnippet`的值,一次把它放进插值表达式里,另一次把它绑定到元素的`innerHTML`属性上。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExample('app/inner-html-binding.component.html')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Interpolated content is always escaped—the HTML is not interpreted, and the browser displays
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  angle brackets in the element's text content.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  插值表达式的内容总会被编码 - 其中的HTML不会被解释, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For the HTML to be interpreted, you must bind it to an HTML property such as `innerHTML`. But binding
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  a value that an attacker might control into `innerHTML` normally causes an XSS
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  vulnerability. For example, code contained in a `<script>` tag is executed:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  如果希望这段HTML被正常解释, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  比如,包含在`<script>`标签的代码就会被执行:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExcerpt('app/inner-html-binding.component.ts ()', 'inner-html-controller')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular recognizes the value as unsafe and automatically sanitizes it, which removes the `<script>`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  tag but keeps safe content such as the text content of the `<script>` tag, or the `<b>` element.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular认为这些值是不安全的, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    img(src='/resources/images/devguide/security/binding-inner-html.png'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        alt='A screenshot showing interpolated and bound HTML values')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ### Avoid direct use of the DOM APIs
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### 避免直接使用DOM API
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The built-in browser DOM APIs do not automatically protect you from security vulnerabilities.
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For example, `document`, the node available through `ElementRef`, and many third-party APIs
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  contain unsafe methods. Avoid directly interacting with the DOM and instead use Angular
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  templates where possible.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  浏览器内置的DOM API不会自动针对安全漏洞进行防护。比如, ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  要避免直接与DOM交互, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### Content security policy
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### 内容安全策略
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [Content Security Policy (CSP)](http://www.html5rocks.com/en/tutorials/security/content-security-policy/) is a defense-in-depth
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  technique to prevent XSS. To enable CSP, configure your web server to return an appropriate
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `Content-Security-Policy` HTTP header.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  [内容安全策略(CSP)](https://developer.mozilla.org/en-
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  US/docs/Web/Security/CSP/Introducing_Content_Security_Policy)是用来防范XSS的纵深防御技术。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  要打开CSP, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  <a id="offline-template-compiler"></a>
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### Use the offline template compiler
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### 使用离线模板编译器
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The offline template compiler prevents a whole class of vulnerabilities called template injection,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  and also greatly improves application performance. Use the offline template compiler in production
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  deployments; do not dynamically generate templates. Angular trusts template code, so generating
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  templates, in particular templates containing user data, circumvents Angular's built-in protections. For information about how to dynamically construct forms in a safe way, see 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  [Dynamic Forms Cookbook](../cookbook/dynamic-form.html).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  离线模板编译器阻止了一整套被称为“模板注入”的漏洞,并能显著增强应用程序的性能。尽量在产品发布时使用离线模板编译器,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  而不要动态生成模板( ) , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  要了解如何用安全的方式动态创建表单,请参见[动态表单烹饪宝典](../cookbook/dynamic-form.html)一章。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  ### Server-side XSS protection
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  ### 服务器端XSS保护
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  HTML constructed on the server is vulnerable to injection attacks. Injecting template code into an
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular application is the same as injecting executable code into the
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  application: it gives the attacker full control over the application. To prevent this, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  use a templating language that automatically escapes values to prevent XSS vulnerabilities on
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the server. Do not generate Angular templates on the server side using a templating language; doing this
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  carries a high risk of introducing template-injection vulnerabilities.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  服务器端构造的HTML很容易受到注入攻击。当需要在服务器端生成HTML时( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  务必使用一个能够自动进行无害化处理以防范XSS漏洞的后端模板语言。不要在服务器端使用模板语言生成Angular模板, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  这样会带来很高的“模板注入”风险。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#bypass-security-apis Trusting safe values
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								h2#bypass-security-apis 信任安全的值
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Sometimes applications genuinely need to include executable code, display an `<iframe>` from some
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  URL, or construct potentially dangerous URLs. To prevent automatic sanitization in any of these 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  situations, you can tell Angular that you inspected a value, checked how it was generated, and made 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  sure it will always be secure. But **be careful**! If you trust a value that might be malicious, you 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  are introducing a security vulnerability into your application. If in doubt, find a professional 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  security reviewer.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  有时候, , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  为了防止在这种情况下被自动无害化, : , , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  但是**千万要小心**!如果你信任了一个可能是恶意的值,就会在应用中引入一个安全漏洞。如果你有疑问,请找一个安全专家复查下。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-01 02:08:57 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  You can mark a value as trusted by injecting `DomSanitizer`, and calling one of the
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  following methods:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-08 17:20:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  注入`DomSanitizer`服务,然后调用下面的方法之一,你就可以把一个值标记为可信任的。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * `bypassSecurityTrustHtml`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * `bypassSecurityTrustScript`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * `bypassSecurityTrustStyle`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * `bypassSecurityTrustUrl`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  * `bypassSecurityTrustResourceUrl`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Remember, whether a value is safe depends on context, so you need to choose the right context for
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  your intended use of the value. Imagine that the following template needs to bind a URL to a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `javascript:alert(...)` call:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  记住,一个值是否安全取决于它所在的环境,所以你要为这个值按预定的用法选择正确的环境。假设下面的模板需要把`javascript.alert(...)`方法绑定到URL。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExcerpt('app/bypass-security.component.html ()', 'dangerous-url')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Normally, Angular automatically sanitizes the URL, disables the dangerous code, and
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  in development mode, logs this action to the console. To prevent
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  this, you can mark the URL value as a trusted URL using the `bypassSecurityTrustUrl` call:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  通常, , : 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExcerpt('app/bypass-security.component.ts ()', 'trust-url')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    img(src='/resources/images/devguide/security/bypass-security-component.png'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        alt='A screenshot showing an alert box created from a trusted URL')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  If you need to convert user input into a trusted value, use a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  controller method. The template below allows users to enter a YouTube video ID and load the
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  corresponding video in an `<iframe>`. The `<iframe src>` attribute is a resource URL security
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  context, because an untrusted source can, for example, smuggle in file downloads that unsuspecting users 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  could execute. So call a method on the controller to construct a trusted video URL, that causes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular to then allow binding into `<iframe src>`:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  如果需要把用户输入转换为一个可信任的值, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  然后把相应的视频加载到`<iframe>`中。`<iframe src>`是一个“资源URL”的安全环境, , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  所以我们要调用一个控制器方法来构造一个新的、可信任的视频URL, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExcerpt('app/bypass-security.component.html ()', 'iframe-videoid')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 09:35:06 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								+makeExcerpt('app/bypass-security.component.ts ()', 'trust-video-url')
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#http HTTP-level vulnerabilities
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#http HTTP级别的漏洞
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular has built-in support to help prevent two common HTTP vulnerabilities, cross-site request
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  forgery (CSRF or XSRF) and cross-site script inclusion (XSSI). Both of these must be mitigated primarily 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  on the server side, but Angular ships helpers to make integration on the client side easier.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular内建了一些支持来防范两个常见的HTTP漏洞: ( ) ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  这两个漏洞主要在服务器端防范, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h3#xsrf Cross-site request forgery
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								h3#xsrf 跨站请求伪造( ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  In a cross-site request forgery, an attacker tricks the user into visiting a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  _different_ page and has them, for example, submit a form that sends a request to your application's
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  web server. If the user is logged into your application, the browser will send authentication
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  cookies, and the attacker could—for example—cause a bank transfer in the user's name with
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  the right request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  在跨站请求伪造( ) , , , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  向你应用程序的Web服务器发送一个请求。如果用户已经登录到你的应用程序, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  这样攻击者就可以发送一个正确的请求,以该用户的名义发起一次银行转账。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 07:21:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  To prevent this, your application must ensure that user requests originate in your own
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  application, not on a different site. A common technique is that the server sends a randomly
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  generated authentication token in a cookie, often with the name `XSRF-TOKEN`. Only the website 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  on which cookies are set can read the cookies, so only your own application can read this token. On
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  each API request, the server then validates the client by checking that the token is sent back,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  usually in an HTTP header called `X-XSRF-TOKEN`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  为了防止这种情况,你必须确保每个用户的请求都是从你自己的应用中发出的,而不是从另一个网站发出的。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  一个常见的技术是服务器随机生成一个用户认证令牌到cookie中, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  由于Cookie只能被创建它的网站访问, , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  服务器就会通过检查这个发回来的令牌对客户端进行验证, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  The Angular `http` client has built-in support for this technique. The default
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `CookieXSRFStrategy` looks for a cookie called `XSRF-TOKEN` and sets an HTTP request header named
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `X-XSRF-TOKEN` with the value of that cookie on every request. The server must set the
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `XSRF-TOKEN` cookie and validate the response header for each state-modifying request.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular的`http`客户端具有对这项技术的内建支持。默认的`CookieXSRFStrategy`会寻找一个名叫`XSFR-TOKEN`的cookie。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  在每个请求中,设置一个名为`X-XSRF-TOKEN`的HTTP请求头, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  服务器必须设置`XSRF-TOKEN` cookie, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  CSRF tokens should be unique per user and session, have a large random value generated by a
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  cryptographically secure random number generator, and expire.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 15:23:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  CSRF令牌应该对每个用户和session是唯一的, , 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular applications can customize cookie and header names by binding their own
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  `CookieXSRFStrategy` value or implement an entirely custom `XSRFStrategy` through providing a custom
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  binding for that type by adding either of the following to your providers list:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular应用程序可以通过绑定它们自己的`CookieXSRFStrategy`值来自定义cookie和HTTP头的名字, 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-01 08:38:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  也可以通过提供一个自定义类型绑定来完全制定`XSRFStrategy`, 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-07 08:22:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  只要把下列代码之一加到你的提供商列表里就可以了:
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 07:21:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								code-example(language="typescript").
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  { provide: XSRFStrategy, useValue: new CookieXSRFStrategy('myCookieName', 'My-Header-Name')}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  { provide: XSRFStrategy, useClass: MyXSRFStrategy}
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 07:21:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For information about CSRF at the Open Web Application Security Project (OWASP) see
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  [Cross-Site Request Forgery (CSRF)](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29) and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  [Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet](https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet). The Stanford University
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  paper [Robust Defenses for Cross-Site Request Forgery](https://seclab.stanford.edu/websec/csrf/csrf.pdf) is also a rich source of detail.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  到开放式Web应用程序安全项目(OWASP)的[这里](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29)
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  和[这里](https://www.owasp.org/index.php/CSRF_Prevention_Cheat_Sheet)学习更多关于跨站请求伪造( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  这个[斯坦福大学论文](https://seclab.stanford.edu/websec/csrf/csrf.pdf)有详尽的细节。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h3#xssi Cross-site script inclusion (XSSI)
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								h3#xssi 跨站脚本包含(XSSI)
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Cross-site script inclusion, also known as JSON vulnerability, can allow an attacker's website to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  read data from a JSON API. The attack works on older browsers by overriding native JavaScript
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  object constructors, and then including an API URL using a `<script>` tag.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  跨站脚本包含, , , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  它重写原生JavaScript对象的构造函数, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  This attack is only successful if the returned JSON is executable as JavaScript. Servers can
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  prevent an attack by prefixing all JSON responses to make them non-executable, by convention, using the
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  well-known string `")]}',\n"`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  只有在返回的JSON能像JavaScript一样可以被执行时, 
							 
						 
					
						
							
								
									
										
										
										
											2016-06-30 08:17:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  以防范这种攻击,
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  Angular's `Http` library recognizes this convention and automatically strips the string
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  `")]}',\n"` from all responses before further parsing.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular的`Http`库会识别这种约定,并在进一步解析之前,自动把字符串`")]}',\n"`从所有响应中去掉。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  For more information, see the XSSI section of this [Google web security blog
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  post](https://security.googleblog.com/2011/05/website-security-for-webmasters.html).
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  要学习更多这方面的知识,请参见[谷歌Web安全博客文章](https://security.googleblog.com/2011/05/website-security-for-webmasters.html)的XSSI小节。
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								h2#code-review Auditing angular applications
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								h2#code-review 审计Angular应用程序
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-09-14 12:11:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  Angular applications must follow the same security principles as regular web applications, and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  must be audited as such. Angular-specific APIs that should be audited in a security review,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  such as the [_bypassSecurityTrust_](#bypass-security-apis) methods, are marked in the documentation
							 
						 
					
						
							
								
									
										
										
										
											2016-06-20 23:34:14 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  as security sensitive.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-23 14:32:12 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 18:28:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  Angular应用应该遵循和常规Web应用一样的安全原则并按照这些原则进行审计。Angular中某些应该在安全评审中被审计的API( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								  比如[_bypassSecurityTrust_](#bypass-security-apis) API)