加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – Angular2 – 警告:消除不安全的风格值url(SafeVa

发布时间:2020-12-17 07:48:10 所属栏目:安全 来源:网络整理
导读:我想在我的Angular 2应用程序的组件模板中设置DIV的背景图像.但是,我在控制台中收到以下警告,我没有得到所需的效果…我不确定动态CSS背景图像是否被阻止,因为Angular2中的安全限制,或者我的HTML模板是坏的. 这是我在控制台中看到的警告(我已将我的img url更
我想在我的Angular 2应用程序的组件模板中设置DIV的背景图像.但是,我在控制台中收到以下警告,我没有得到所需的效果…我不确定动态CSS背景图像是否被阻止,因为Angular2中的安全限制,或者我的HTML模板是坏的.

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

WARNING: sanitizing unsafe style value url(SafeValue must use [property]=binding: /img/path/is/correct.png (see http://g.co/ng/security#xss)) (see http://g.co/ng/security#xss).

事情是我使用Angular2中的DomSanitizationService来清理注入到我的模板中的内容.这是我在我的模板中的HTML:

<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>

这是组件…

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”绑定到模板时,例如:

<div *ngIf="image">
    <img [src]="image">
</div>

并且图像被传递使用bypassSecurityTrustUrl一切似乎工作正常…任何人都可以看到我做错了什么?

您必须将整个url语句包装在bypassSecurityTrustStyle中:
<div class="header"
             *ngIf="image"
             [style.background-image]="image">
</div>

还有

if (element.image) {             
  instance.image = 
           instance.sanitization.bypassSecurityTrustStyle('url(' + element.image + ')');
} else {
  instance.image = null;
}

否则不会被视为有效的样式属性

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读