Angular 7 How To Access Domsanitizer Object inside a static function

Published

I am trying to resolve the checkmarx issue which says application embeds untrusted data in the generated output.This untrusted data is embedded straight into the output without proper sanitization or encoding, enabling an attacker to inject malicious code into the output.So to prevent this I am using Angular 7 Domsanitizer. So the checkmarx error appear on this line

const search = new URLSearchParams(window.location.search)

My complete code-:

Utils.ts

 import { DomSanitizer } from '@angular/platform-browser';

export class Utils {

    static _sanitize:any;
    constructor(private cookieService: CookieService,sanitizer: DomSanitizer) { 
          Utils._sanitize = sanitizer
    }

static getParam(param: string): string | null {
        
        if (window.location.search) {
            const search = new URLSearchParams(Utils._sanitize.bypassSecurityTrustUrl(window.location.search));
            const value = search.get(param);
            return value && value !== 'null' && value !== 'undefined' ? value : null;
        }
        return null;
    }

But in this case I am getting error that bypassSecurityTrustUrl is not defined as inside the static function DomSanitizer object is not obtained.
Also I cannot pass Utils._sanitizer in params as this function is accessed by many files in complete project. Please help and give some suggestions

Source: Angular Questions

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq