我有一个翻译 key ,它实际上是一个 HTML 代码,包括编码的和未编码的。
$scope.translations = {
"html_code" : "<script>alert('Alert!');</script>",
"html_code_full" : "<script>alert('Alert!');</script>",
"greeting" : "Welcome!"
}
当我使用这些值在 View 中显示翻译后的文本时,我使用两种方法:
- 按照指令
<span translate>{{translations.html_code}}</span>
- 作为过滤器
{{translations.html_code|translate}}
我对 translations.html_code_full
尝试同样的操作。
这是 View 的代码:
translations.html_code = {{translations.html_code|translate}}
translations.html_code = <span translate>{{translations.html_code}}</span>
translations.html_code_full = {{translations.html_code_full|translate}}
translations.html_code_full = <span translate>{{translations.html_code_full}}</span>
这是我得到的输出:
translations.html_code = <script>alert('Alert!');</script>
translations.html_code = <script>alert('Alert!');</script>
translations.html_code_full = <script>alert('Alert!');</script>
translations.html_code_full =
很明显,指令实现将翻译 key 编码为 HTML,但过滤器则不然。为什么指令与过滤器实现之间的输出存在差异? 如果它正在渲染 HTML,为什么我没有收到警报?
Here is the plunk我为演示而创建。
最佳答案
AngularJS 框架正在保护您的应用程序免受 XSS 攻击。
Cross-site scripting carried out on websites accounted for roughly 84% of all security vulnerabilities documented by Symantec as of 2007.
--Wikipedia - Cross-site scripting
那么您真正想要做什么?也许我们可以向您展示如何以安全的方式进行操作。
关于angularjs - Angular 翻译指令与过滤器 : is XSS possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894875/