Does anyone have any experience inserting HTML into your Ionic 2 application and bypassing Angular 2’s Cross-site Scripting Security Model by using the DomSanitizationService
? I am trying to do so, but it does not appear to be working as my HTML is still being sanitized. My console reads:
WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).
I was wondering if anyone has successfully managed to get this to work? My approach is based on the solution mentioned in this stackoverflow post.
I import the DomSanitizationService
as follows:
import {DomSanitizationService} from '@angular/platform-browser/esm/src/security/dom_sanitization_service';
I add it to my constructor:
constructor(private _sanitizer: DomSanitizationService) { }
I then construct my function as follows:
assembleHTMLItem() {
var strHTML = '<input type="text" name="name">';
return this._sanitizer.bypassSecurityTrustHtml(strHTML);
}
And call it during the ionViewLoaded function
this.htmlValue = this.assembleHTMLItem();
My HTML looks like this:
<div [innerHTML]="htmlValue">
As mentioned, this isn’t working for me as Angular 2 is still sanitizing the htmlValue value. Has anyone successfully managed to get this to work?
EDIT: I’m also getting the following error in my CLI, so perhaps I’m not importing DomSanitizationService
correctly?
C:\<<project location>>\node_modules\@angular\platform-browser\esm\src\security\dom_sanitization_service.js:8
import { Injectable } from '@angular/core';
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'