I have an Angular(10) component with html with an iframe.
Whatever I do with sanitizing the URL (bypassSecurityTrustResourceUrl
) I get a cross site scripting error:
Error: unsafe value used in a resource URL context (see http://g.co/ng/security#xss)
Below is the important parts of my code.
Beside the code below I have tried hard coding empty string, valid html, null, # and whatnot.
I have tried manipulating my mocked DomSanitizer; including turning it off.
I have verified my mock is called.
Right now I guess it is Karma that uses an iframe and then my code uses another/inner iframe and somewhere karma's setup does not allow for anything in my iframe.
(The only way I get Angular to not complain about the xss the iframe src/URL is to hard code it in the template.)
Template:
<iframe id="inlineFrameExample" [src]="embeddedLink">
</iframe>
.ts:
private url: string // Set elsewhere.
constructor(
private sanitizer: DomSanitizer,
) { }
public get embeddedLink(): SafeResourceUrl {
return this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
}
.ts.spec:
...
providers: [
{
provide: DomSanitizer,
useValue: {
bypassSecurityTrustResourceUrl: (val: string) => val,
},
},
...