在 Angular 中处理用户输入时,我对安全性 (XSS) 的担忧程度如何?
To what degree do i have the worry about security (XSS) when handling user input in Angular?
To systematically block XSS bugs, Angular treats all values as untrusted by default. When a value is inserted into the DOM from a template, via property, attribute, style, class binding, or interpolation, Angular sanitizes and escapes untrusted values.
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 template. Never generate template source code by concatenating user input and templates. To prevent these vulnerabilities, use the offline template compiler, also known as template injection.
这就是 Angular 关于 XSS 的说法。然而,第一段似乎与第二段矛盾(如果我错了请纠正我)。所以我的问题是:我是否需要转义和清理我的用户输入?
代码是可信的,但值不是。如果您使用正常的 Angular 数据绑定插入值,那么它们将被适当地编码,因为 Angular 可以识别什么是值,什么是代码。
例如,这不需要任何额外的转义:
<h1>Hello {{yourName}}!</h1>
但是如果您尝试使用字符串连接创建模板。例如:
'<h1>Hello ' + yourName + '!</h1>'
或使用服务器端语言,如 PHP:
<h1>Hello <?= $_GET['name'] ?> !</h1>
那么Angular就分不清文字html和插入值的区别了。像这样插入的数据现在可以注入 Javascript 以及 Angular 语法。
只要您坚持正常的 Angular 插入值机制,就不需要额外的编码。这些漏洞来自绕过 angular 并以其他方式直接插入值。
To systematically block XSS bugs, Angular treats all values as untrusted by default. When a value is inserted into the DOM from a template, via property, attribute, style, class binding, or interpolation, Angular sanitizes and escapes untrusted values.
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 template. Never generate template source code by concatenating user input and templates. To prevent these vulnerabilities, use the offline template compiler, also known as template injection.
这就是 Angular 关于 XSS 的说法。然而,第一段似乎与第二段矛盾(如果我错了请纠正我)。所以我的问题是:我是否需要转义和清理我的用户输入?
代码是可信的,但值不是。如果您使用正常的 Angular 数据绑定插入值,那么它们将被适当地编码,因为 Angular 可以识别什么是值,什么是代码。
例如,这不需要任何额外的转义:
<h1>Hello {{yourName}}!</h1>
但是如果您尝试使用字符串连接创建模板。例如:
'<h1>Hello ' + yourName + '!</h1>'
或使用服务器端语言,如 PHP:
<h1>Hello <?= $_GET['name'] ?> !</h1>
那么Angular就分不清文字html和插入值的区别了。像这样插入的数据现在可以注入 Javascript 以及 Angular 语法。
只要您坚持正常的 Angular 插入值机制,就不需要额外的编码。这些漏洞来自绕过 angular 并以其他方式直接插入值。