angular - Angular 的 bypassSecurityTrustHtml 管道如何在后台工作?
问题描述
我使用了各种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 源中找出来。该bypassSecurityTrustHtml
函数返回一个new SafeHtmlImpl(value);
实例。在此sanitize
过程中,有一个检查:if (value instanceof SafeHtmlImpl)
,如果是,则跳过清理过程
解决方案
我认为你误解了函数的意义。它实际上并没有清理,它甚至不检查 HTML。它所做的只是创建一个设置了标志的对象,因此 Angular 安全性不会阻止它。如果字符串包含不安全的 HTML,则不会被阻止。
开发人员仍然应该自己编写一些函数或使用其他一些工具来确保 HTML 是安全的。
推荐阅读
- r - 将 data.frame 转换为时间序列
- swift - iOS 自定义键盘按钮操作是如何触发的?
- css - 如何修复全屏@keyframes 加载器
- reactjs - 使用 npm init react-app 时,Docker 容器在启动后立即退出
- ruby - 使用 Ruby、PGSql 和 Sinatra 的 new.erb 视图和控制器中的年龄布尔值
- javascript - React 布局帮助 - 无法让我的 SVG 既定尺寸又响应
- numpy - 弃用警告:元素比较失败;这将在未来引发错误。在广播大型阵列时
- ios - APNS 推送通知 iOS 13 - didRegisterForRemoteNotificationsWithDeviceToken 未被调用
- javascript - $.getJSON 中的函数似乎没有运行
- c# - Visual Studio 2019:System.UnauthorizedAccessException:“访问路径“C:/”被拒绝