I am using Angular 13 for my development.
Recently I created a simple angular form with a user input field, I tried providing different kinds of inputs, where I saw that providing input as <b>Hack</b><img src="some_url" > was working and is not escaped by angular.
I tried doing the POC on the same and created a live example here, but could find out that using angular's DOM Sanitizer service also these inputs are not being escaped by angular.
For reference,
.ts
import { Component, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
userName = 'Angular';
constructor(private domSanitizer: DomSanitizer) {}
onSave(): void {
console.log('save clicked api call');
this.sanitizeUserName();
}
sanitizeUserName(): string {
const domSanitize = this.domSanitizer.sanitize(1, this.userName);
console.log('sanitized text/html', domSanitize);
return domSanitize;
}
}
.html
<form #userForm="ngForm">
<label>Username: </label>
<input type="text" [(ngModel)]="userName" name="username" />
<div>
<span>View : </span>
<div [innerHTML]="userName"></div>
</div>
<br />
<br />
<button type="button" (click)="onSave()">Save</button>
</form>
