【发布时间】:2019-04-21 07:14:40
【问题描述】:
我使用了SafeHtml 管道的变体,但我想知道它实际上是如何工作的。 Angular 如何知道应用于 DOM 的文本已经通过管道传递并且是安全的?它只是在编译阶段完成还是运行时检查?
调用任何 bypassSecurityTrust... API 都会禁用 Angular 的 对传入的值进行内置清理
安全 HTML 管道的常见实现:
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Pipe({name: 'sanitizeHtml'})
export class SanitizeHtmlPipe implements PipeTransform {
constructor(private _sanitizer:DomSanitizer) {
}
transform(v:string):SafeHtml {
return this._sanitizer.bypassSecurityTrustHtml(v);
}
}
更新:从dom_sanitization_service.ts source 中找出来。 bypassSecurityTrustHtml 函数返回一个 new SafeHtmlImpl(value); 实例。在sanitize过程中,有一个检查:if (value instanceof SafeHtmlImpl),如果是,则跳过清理过程
【问题讨论】:
标签: angular