Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a SafeUrl or a SafeResourceUrl?

Tags:

angular

I'm loading a trusted URL into an iframe which works fine. I also want to display that URL as a string on the page. I've tried <div>{{url}}</div> but it displays:

SafeValue must use [property]=binding: /my/resource/path.html (see http://g.co/ng/security#xss)

I also tried using <div [ngModel]="url"></div>, but got

Error: No value accessor for form control with unspecified name attribute

How can I display it?

like image 492
adamdport Avatar asked May 02 '17 14:05

adamdport


People also ask

What is SafeResourceUrl in angular?

SafeResourceUrllink Marker interface for a value that's safe to use as a URL to load executable code from.

What is SafeUrl?

A SafeUrl is a string-like object that carries the security type contract that its value as a string will not cause untrusted script execution when evaluated as a hyperlink URL in a browser.

Which method can you use to bypass security and to trust that the given value is a safe resource URL?

bypassSecurityTrustResourceUrl()link Bypass security and trust the given value to be a safe resource URL, i.e. a location that may be used to load executable code from, like <script src> , or <iframe src> .


1 Answers

  1. The error says use [property] binding - it means [innerHTML] for html
  2. You want to display this value as html (not url or resource) - use bypassSecurityTrustHtml

     @Component({
      selector: 'my-app',
      template: `
        <div [innerHTML]="url"><div>
      `
     })
    export class App {
    
      dangerousVideoUrl = "href='&#x3000;javascript:alert(1)'";
    
      constructor(private sanitizer: DomSanitizer) {
        this.url =
        this.sanitizer.bypassSecurityTrustHtml(this.dangerousVideoUrl);
     }
    }
    
like image 88
Julia Passynkova Avatar answered Nov 15 '22 06:11

Julia Passynkova