Angular Prevent XSS CSRF Attacks

How To Sanitize a Value Manually in Angular?

As per our project requirement, we are sanitizes a value manually using the below sanitize methods-
1.     SecurityContext.HTML
2.     SecurityContext.SCRIPT
3.     SecurityContext.STYLE
4.     SecurityContext.NONE
5.     SecurityContext.RESOURCE_URL
6.     SecurityContext.URL

Example 1
import {Component, SecurityContext} from '@angular/core'

export class App {
  constructor(private sanitizer: DomSanitizer) {
    this.html = sanitizer.sanitize(SecurityContext.HTML, "<h2>DOM Sanitize</h2><script>alert("XSS")</script>");
  }
}

Example 2
import {Component, SecurityContext} from '@angular/core'

export class App {
  constructor(private sanitizer: DomSanitizer) {
    this.script = sanitizer.sanitize(SecurityContext.SCRIPT, "<h2>DOM Sanitize</h2><script>alert("XSS")</script>");
  }
}

Example 3
import {Component, SecurityContext} from '@angular/core'

export class App {
  constructor(private sanitizer: DomSanitizer) {
    this.url = sanitizer.sanitize(SecurityContext.URL, "<h2>DOM Sanitize</h2><script> Your code also");
  }
}



I hope you are enjoying with this post! Please share with you friends. Thank you so much!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^