InterviewHubPRO
Section Overview
Q168

Question 168 of 283

Part 7: Change Detection, Security (XSS/Sanitizer) & Interceptors (151 - 176)

59%
Angular & Signals (283 Qs)•Part 7: Change Detection, Security (XSS/Sanitizer) & Interceptors (151 - 176)

How do you prevent automatic sanitization?

Sometimes the applications genuinely need to include executable code such as displaying <iframe> from an URL. In this case, you need to prevent automatic sanitization in Angular by saying that you inspected a value, checked how it was generated, and made sure it will always be secure. Basically it involves 2 steps,

  1. Inject DomSanitizer: You can inject DomSanitizer in component as parameter in constructor

  2. Mark the trusted value by calling some of the below methods

  3. bypassSecurityTrustHtml

  4. bypassSecurityTrustScript

  5. bypassSecurityTrustStyle

  6. bypassSecurityTrustUrl

  7. bypassSecurityTrustResourceUrl

For example,The usage of dangerous url to trusted url would be as below,

javascript
constructor(private sanitizer: DomSanitizer) {
this.dangerousUrl = 'javascript:alert("XSS attack")';
this.trustedUrl = sanitizer.bypassSecurityTrustUrl(this.dangerousUrl);