I need to sanitize an unsafe URL on my site. Trying to use the DomSanitizer's sanitize method, but am getting unexpected results. The docs seem clear that some kind of sanitization should be taking place for the method sanitize(), but I'm not seeing anything. "The implementation is responsible to make sure that the value can definitely be safely used in the given context."
Why would sanitizing a safe url to RESOURCE_URL throw an error?
Why would sanitizing an unsafe url to URL not sanitize the string?
Bonus: Why do I see some doing bypassSecurityTrustUrl( sanitize() ) ? Shouldn't the sanitize method make a string safe?
safeURL:string = "android.com";
unsafeURL:string = "android.com?param=<script>alert('xss!');</script>";
outputURL:string | null;
constructor(private sanitizer: DomSanitizer) {
this.outputURL = sanitizer.sanitize(SecurityContext.URL, this.safeURL);
console.log(this.outputURL) //android.com
this.outputURL = sanitizer.sanitize(SecurityContext.RESOURCE_URL, this.safeURL);
console.log(this.outputURL) //Error: unsafe value used in a resource URL context
this.outputURL = sanitizer.sanitize(SecurityContext.URL, this.unsafeURL);
console.log(this.outputURL) //android.com?param=<script>alert('xss!');</script>
this.outputURL = sanitizer.sanitize(SecurityContext.RESOURCE_URL, this.unsafeURL);
console.log(this.outputURL) //Error: unsafe value used in a resource URL context
}