首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告:消毒不安全样式值url

警告:消毒不安全样式值url
EN

Stack Overflow用户
提问于 2016-07-26 15:04:45
回答 10查看 161.5K关注 0票数 128

我想在我的角2应用程序中的组件模板中设置DIV的背景图像。然而,我总是在我的控制台中得到以下警告,但没有达到预期的效果.我不确定动态CSS背景图像是否由于Angular2中的安全限制而被阻塞,或者我的HTML是否被破坏。

这是我在控制台中看到的警告(我已将img url更改为/img/path/is/correct.png )。

警告:清除不安全的样式值url(SafeValue必须使用property=binding: /img/path/is/rectit.png(参见http://g.co/ng/security#xss)) (参见http://g.co/ng/security#xss)。

问题是,我使用DomSanitizationService在Angular2中对注入到模板中的内容进行了消毒。下面是我在模板中的HTML:

代码语言:javascript
复制
<div>
    <div>
        <div class="header"
             *ngIf="image"
             [style.background-image]="'url(' + image + ')'">
        </div>

        <div class="zone">
            <div>
                <div>
                    <h1 [innerHTML]="header"></h1>
                </div>
                <div class="zone__content">
                    <p
                       *ngFor="let contentSegment of content"
                       [innerHTML]="contentSegment"></p>
                </div>
            </div>
        </div>
    </div>
</div>

这是组件..。

代码语言:javascript
复制
Import {
    DomSanitizationService,
    SafeHtml,
    SafeUrl,
    SafeStyle
} from '@angular/platform-browser';

@Component({
               selector: 'example',
               templateUrl: 'src/content/example.component.html'
           })
export class CardComponent implements OnChanges {

    public header:SafeHtml;
    public content:SafeHtml[];
    public image:SafeStyle;
    public isActive:boolean;
    public isExtended:boolean;

    constructor(private sanitization:DomSanitizationService) {
    }

    ngOnChanges():void {
        map(this.element, this);

        function map(element:Card, instance:CardComponent):void {
            if (element) {
                instance.header = instance.sanitization.bypassSecurityTrustHtml(element.header);

                instance.content = _.map(instance.element.content, (input:string):SafeHtml => {
                    return instance.sanitization.bypassSecurityTrustHtml(input);
                });

                if (element.image) {
                    /* Here is the problem... I have also used bypassSecurityTrustUrl */ 
                    instance.image = instance.sanitization.bypassSecurityTrustStyle(element.image);
                } else {
                    instance.image = null;
                }

            }
        }
    }
}

请注意,当我使用src="image“绑定到模板时,例如:

代码语言:javascript
复制
<div *ngIf="image">
    <img [src]="image">
</div>

image是用bypassSecurityTrustUrl通过的一切看起来都很好..。有人能看出我做错了什么吗?

EN

回答 10

Stack Overflow用户

发布于 2016-07-29 16:19:11

必须将整个url语句包装在bypassSecurityTrustStyle中。

代码语言:javascript
复制
<div class="header" *ngIf="image" [style.background-image]="image"></div>

并有

代码语言:javascript
复制
this.image = this.sanitization.bypassSecurityTrustStyle(`url(${element.image})`);

否则,它将不被视为有效的样式属性。

票数 122
EN

Stack Overflow用户

发布于 2018-08-08 12:43:10

使用这个<div [ngStyle]="{'background-image':'url('+imageUrl+')'}"></div>,这为我解决了问题。

票数 77
EN

Stack Overflow用户

发布于 2017-05-02 10:22:37

带线性梯度的中频背景图像(*ngFor)

视图:

代码语言:javascript
复制
<div [style.background-image]="getBackground(trendingEntity.img)" class="trending-content">
</div>

类:

代码语言:javascript
复制
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';

constructor(private _sanitizer: DomSanitizer) {}

getBackground(image) {
    return this._sanitizer.bypassSecurityTrustStyle(`linear-gradient(rgba(29, 29, 29, 0), rgba(16, 16, 23, 0.5)), url(${image})`);
}
票数 60
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38593515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档