我试图弄清楚DomSanitizerAngular 10 中的真正工作原理。
我一直在尝试清理以下 CSS 字符串,但没有任何运气:
const testString = 'transform: rotate(70deg);color: blue;';
const result = this.sanitizer.sanitize(SecurityContext.STYLE, testString);
在上面的代码片段中,result常量变量总是等于testString. 我期望发生的是:result应该只包含color: blue;并且transform从字符串中过滤掉 CSS 属性。
我还尝试清理一些通过如下字符串提供的 JavaScript 代码:
const testString = 'alert("hacked!");console.log("logged");';
const result = this.sanitizer.sanitize(SecurityContext.SCRIPT, testString);
但是,在此示例中,该函数使用脚本上下文中使用sanitize()的消息 unsafe value 引发错误。
我期望在这里发生的是:result常量变量应该是一个空字符串。