我正在使用 contentEditable div 使用户能够格式化他们的文章。我对html内容做了一些处理并持久化。
我正在使用 ng-bind-html
在观众想要阅读文章时呈现结果。我不想使用 $sce.trustAsHtml
因为我仍然希望 AngularJS 清理用户输入并且因为我不信任所有输入。我想要的只是 AngularJS 清理以允许元素上的某些属性。它似乎剥离了 ID 和数据属性。 (但保留等级和头衔)。
数据属性是否被认为是有害的?攻击者如何使用它们来攻击最终用户?有没有一种方法可以安全地使用它们并且让 Angular 不删除它们?
这是一个例子:
article.body = '<p data-guid="afasfa-afasfafas-faf-asasf" class="guid-tagged">Yes this is my article</p>';
<article ng-bind-html='article.body'></article>
这是 Angular 在 article 标签内输出的内容(注意剥离的 data- 属性):
<p class="guid-tagged">Yes this is my article</p>
谢谢
最佳答案
如评论中所述,ng-bind-html
通过 sanitizer 传递数据。这个 sanitizer 从传入它的所有输入中删除了一些属性。这个问题可能有助于解释更多:ngSanitize issue concerning whitelisting attributes .这part of the source code包括所有被认为有效的属性,因此不会被 ngSanitize 触及。
关于javascript - 为什么 AngularJS 在使用 ng-bind-html 时会去掉 data- 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348558/