如何在Angular 2中设置iframe src而不会导致`unsafe value`异常?

TJ.*_*TJ. 144 angular

我是没有AngularJS经验的Angular 2的新手,并且正在编写一个涉及设置iframe src属性的教程:

<iframe width="100%" height="300" src="{{video.url}}"></iframe>
Run Code Online (Sandbox Code Playgroud)

这引发了一个异常:

Error: unsafe value used in a resource URL context
at DomSanitizationServiceImpl.sanitize...
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用绑定但iframe没有成功.我可能错过了一些东西,我很难找到解决方案.

yur*_*zui 303

更新

对于RC.6 ^版本使用DomSanitizer

Plunker

一个很好的选择是使用纯管道:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer} from '@angular/platform-browser';

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {}
  transform(url) {
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
} 
Run Code Online (Sandbox Code Playgroud)

记得把你的新东西添加this.sanitizer.bypassSecurityTrustResourceUrl(url)this.sanitizer.sanitize(SecurityContext.URL, url)AppModule 的数组中.(如文档所示)

@NgModule({
   declarations : [
     ...
     SafePipe
   ],
})
Run Code Online (Sandbox Code Playgroud)

HTML

<iframe width="100%" height="300" [src]="url | safe"></iframe>
Run Code Online (Sandbox Code Playgroud)

Plunker

如果您使用SafePipe标签,这可能对您有意义:


旧版RC.5

你可以declarations像这样利用:

export class YourComponent {
  url: SafeResourceUrl;
  constructor(sanitizer: DomSanitizationService) {
    this.url = sanitizer.bypassSecurityTrustResourceUrl('your url');
  }
}
Run Code Online (Sandbox Code Playgroud)

然后embed在模板中绑定:

<iframe width="100%" height="300" [src]="url"></iframe>
Run Code Online (Sandbox Code Playgroud)

不要忘记添加以下导入:

import { SafeResourceUrl, DomSanitizationService } from '@angular/platform-browser';
Run Code Online (Sandbox Code Playgroud)

Plunker样品

  • `this.sanitizer.sanitize(SecurityContext.URL, url)` 不起作用,而 `this.sanitizer.bypassSecurityTrustResourceUrl(url)` 起作用,但在静态代码分析中引发了高安全漏洞问题,这阻止了我将其转移到生产环境。需要一种方法来解决这个问题 (5认同)
  • 我正在使用 Ionic2。我声明了一个管道`Pipe({ name: 'safe' }) export class SafePipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) {} transform(url): any { return this.sanitizer.bypassSecurityTrustResourceUrl(url); } } ` 并且在模板中我调用 `&lt;iframe width="100%" height="315" src="{{url}} | safe" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;`。但它不适用于错误“不安全值”。请帮忙 (2认同)
  • @yurzui我遵循了您对更新v8的建议。但是,当我使用this.sanitizer.sanitize(SecurityContext.URL,url)时,出现错误“错误:资源URL上下文中使用的不安全值” II将其更改为`this.sanitizer.bypassSecurityTrustResourceUrl(url) `工作正常。知道有什么问题吗? (2认同)

小智 25

这个对我有用.

import { Component,Input,OnInit} from '@angular/core';
import {DomSanitizer,SafeResourceUrl,} from '@angular/platform-browser';

@Component({
    moduleId: module.id,
    selector: 'player',
    templateUrl: './player.component.html',
    styleUrls:['./player.component.scss'],

})
export class PlayerComponent implements OnInit{
    @Input()
    id:string; 
    url: SafeResourceUrl;
    constructor (public sanitizer:DomSanitizer) {
    }
    ngOnInit() {
        this.url = this.sanitizer.bypassSecurityTrustResourceUrl(this.id);      
    }
}
Run Code Online (Sandbox Code Playgroud)


Lro*_*z84 13

这适用于Angular 5.2.0

sarasa.Component.ts

import { Component, OnInit, Input } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

@Component({
  selector: 'app-sarasa',
  templateUrl: './sarasa.component.html',
  styleUrls: ['./sarasa.component.scss']
})

export class Sarasa implements OnInit {
  @Input()
  url: string = "https://www.mmlpqtpkasjdashdjahd.com";
  urlSafe: SafeResourceUrl;

  constructor(public sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.urlSafe= this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
  }

}
Run Code Online (Sandbox Code Playgroud)

sarasa.Component.html

<iframe width="100%" height="100%" frameBorder="0" [src]="urlSafe"></iframe>
Run Code Online (Sandbox Code Playgroud)

多数民众赞成!

  • 试过这个。它可以工作,但控制台不断抛出错误错误:资源 URL 上下文中使用的不安全值 (2认同)

小智 8

老实说,所有答案似乎都是错误的。仅使用this.sanitizer.bypassSecurityTrustResourceUrl(url)会绕过安全性并将 url 视为SafeResourceUrl. 但是,给定的 url 仍然可能是恶意的,从而导致安全违规。文档也这么说:https://angular.io/api/platform-b ​​rowser/DomSanitizer#bypassSecurityTrustResourceUrl

解决方案是首先调用并将返回值sanitizer传递给如下所示:sanitizerbypassSecurityTrustResourceUrl

this.sanitizer.bypassSecurityTrustResourceUrl(this.sanitizer.sanitize(SecurityContext.URL, url))

这样您就可以清除任何恶意代码,然后绕过安全性,表明这确实是一个安全的网址。


Kum*_*mal 7

constructor(
 public sanitizer: DomSanitizer, ) {

 }
Run Code Online (Sandbox Code Playgroud)

我一直在挣扎4个小时.问题出在img标签上.当你使用方括号'src'ex:[src]时.你不能使用这个角度表达式{{}}.您只需直接从下面的对象示例中提供.如果你给角度表达式{{}}.你会得到插值错误.

  1. 首先,我使用ngFor迭代这些国家

    *ngFor="let country of countries"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 第二,你把它放在img标签中.就是这个.

    <img [src]="sanitizer.bypassSecurityTrustResourceUrl(country.flag)"
    height="20" width="20" alt=""/>
    
    Run Code Online (Sandbox Code Playgroud)