使用Angular 2的资源URL上下文中使用的不安全值

Bil*_*ble 87 angular

由于升级到最新的Angular 2发布候选版本,我的img标签引发了错误.

img标签:

<img class='photo-img' [hidden]="!showPhoto1" src='{{theMediaItem.photoURL1}}'>
Run Code Online (Sandbox Code Playgroud)

生成浏览器错误:

http://veeu-images.s3.amazonaws.com/media/userphotos/116_1464645173408_cdv_photo_007.jpg
Run Code Online (Sandbox Code Playgroud)

网址的价值是:

import {DomSanitizationService} from '@angular/platform-browser';

@Component({
  templateUrl: 'build/pages/veeu/veeu.html'
})
export class VeeUPage {
  static get parameters() {
    return [[NavController], [App], [MenuController], [DomSanitizationService]];
  }

  constructor(nav, app, menu, sanitizer) {

    this.app = app;
    this.nav = nav;
    this.menu = menu;
    this.sanitizer = sanitizer;

    this.theMediaItem.photoURL1 = this.sanitizer.bypassSecurityTrustUrl(this.mediaItems[1].url);
  }
Run Code Online (Sandbox Code Playgroud)

编辑:

我已经尝试过在另一个解决方案中提出的建议,这个问题应该是重复的,但我得到了同样的错误.

我已将以下代码添加到控制器:

<img class='photo-img' [hidden]="!showPhoto1" [src]='theMediaItem.photoURL1'>
Run Code Online (Sandbox Code Playgroud)

我仍然收到相同的错误消息.

EDIT2:

我还将html更改为:

<img class='photo-img' [hidden]="!showPhoto1" src='{{theMediaItem.photoURL1}}'>
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的错误消息

Hel*_*ate 128

方法1:

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

...

constructor(public sanitizer: DomSanitizer){}
Run Code Online (Sandbox Code Playgroud)

...然后在HTML中:

    <iframe [src]='sanitizer.bypassSecurityTrustResourceUrl(myurl)' width="640" height="360" frameborder="0"
        webkitallowfullscreen mozallowfullscreen allowfullscreen>
    </iframe>
Run Code Online (Sandbox Code Playgroud)

方法2:

我不得不首先在代码中进行清理,因为Vimeo不会在Edge上最大化

这只是一个例子,重点是,首先在代码中清理,但是你喜欢

对象界面:

import { SafeUrl } from '@angular/platform-browser';
export class Book {
   constructor(public title: string, public videoURL?: SafeUrl) {}
}
Run Code Online (Sandbox Code Playgroud)

服务(例如):

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { Book } from '../features/models/book';
import { DomSanitizer } from '@angular/platform-browser';

@Injectable()

export class BookService {
    constructor(
        private sanitizer: DomSanitizer
    ) {}
    getBooks (): Observable<Book[]> {    
        return new Observable( observer => {
            observer.next(
                    new Book(
                        'Some Book Title', 
                        this.sanitizer.bypassSecurityTrustResourceUrl(
                           'https://player.vimeo.com/video/12345678'
                        )
                    ),
                    new Book(
                        'Second BookTitle',
                        this.sanitizer.bypassSecurityTrustResourceUrl(
                            'https://player.vimeo.com/video/91011121'
                        )
                    )
                )
            });

    }
}
Run Code Online (Sandbox Code Playgroud)


elk*_*elk 82

我正在使用rc.4,这种方法适用于ES2015(ES6):

import {DomSanitizationService} from '@angular/platform-browser';

@Component({
  templateUrl: 'build/pages/veeu/veeu.html'
})
export class VeeUPage {
  static get parameters() {
    return [NavController, App, MenuController, DomSanitizationService];
  }

  constructor(nav, app, menu, sanitizer) {

    this.app = app;
    this.nav = nav;
    this.menu = menu;
    this.sanitizer = sanitizer;    
  }

  photoURL() {
    return this.sanitizer.bypassSecurityTrustUrl(this.mediaItems[1].url);
  }
}
Run Code Online (Sandbox Code Playgroud)

在HTML中:

<iframe [src]='photoURL()' width="640" height="360" frameborder="0"
    webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>
Run Code Online (Sandbox Code Playgroud)

使用函数将确保在清理后值不会更改.另请注意,您使用的清理功能取决于上下文.

对于图像,bypassSecurityTrustUrl将起作用,但对于其他用途,您需要参考文档:

https://angular.io/docs/ts/latest/api/platform-b​​rowser/index/DomSanitizer-class.html

  • 什么是“rc4”(后来 [Helzgate 指的是 RC3](http://stackoverflow.com/a/38079724/1175496))?我的意思是,如何将其映射到 github 版本?在 github 和 npm 中,我只看到 2.4.4 或 2.4.5 之类的版本。我目前在 2.4.4 上,似乎 DomSanitizer 改变了;所以这是你需要的导入:`import {DomSanitizer} from '@angular/platform-b​​rowser';` (3认同)
  • 使用此方法之前请仔细阅读文档: **bypassSecurityTrustUrl()** **警告:** *使用不受信任的用户数据调用此方法会使您的应用程序面临 XSS 安全风险!* 在我看来,这样做并不安全,除非您确实确定图像源是可信的。即使它来自服务器,如果它是由用户上传的,那么就有可能利用这样的解决方案。 (3认同)

Fil*_*cik 23

解决这个问题最优雅的方法是:使用管道.这是一个例子(我的博客).因此,您可以简单地使用url | safe管道来绕过安全性.

<iframe [src]="url | safe"></iframe>
Run Code Online (Sandbox Code Playgroud)


小智 13

您可以将清理程序公开给视图,或者公开将调用转发给bypassSecurityTrustUrl的方法

<img class='photo-img' [hidden]="!showPhoto1" 
    [src]='sanitizer.bypassSecurityTrustUrl(theMediaItem.photoURL1)'>
Run Code Online (Sandbox Code Playgroud)


小智 11

使用安全管道对其进行修复。

  • 如果您还没有,请创建一个安全的管道。

    ng gc管道安全

  • 在app.module.ts中添加安全管道

    声明:[SafePipe]

  • 在您的ts中声明安全管道

导入Dom消毒剂和安全管道以安全地访问URL

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)

-使用src网址添加安全

<iframe width="900" height="500" [src]="link | safe"/>
Run Code Online (Sandbox Code Playgroud)

  • 伟大的!一件事,不应该是“ng g pipeline safe”而不是“ng gc pipeline safe”,这显然不起作用吗? (4认同)

Sun*_*arg 5

默认情况下,Angular 将所有值视为不受信任。当一个值从模板插入到 DOM 中时,通过属性、属性、样式、类绑定或插值,Angular 会清理和转义不受信任的值。

因此,如果您直接操作 DOM 并插入内容,则需要对其进行清理,否则 Angular 会出错。

我为此创建了管道SanitizeUrlPipe

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

@Pipe({
    name: "sanitizeUrl"
})
export class SanitizeUrlPipe implements PipeTransform {

    constructor(private _sanitizer: DomSanitizer) { }

    transform(v: string): SafeHtml {
        return this._sanitizer.bypassSecurityTrustResourceUrl(v);
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是你可以使用的方式

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

如果您想添加 HTML,那么SanitizeHtmlPipe可以提供帮助

import { PipeTransform, Pipe } 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处阅读有关 Angular 安全性的更多信息。

  • “因此,如果您直接操作 DOM 并向其中插入内容,则需要对其进行清理,否则 Angular 会抛出错误。” 但你并没有对你的管道进行消毒,而是绕过了消毒。 (2认同)