mohit_basantani
mohit_basantani

Reputation: 3

Angular 7 How To Access Domsanitizer Object inside a static function

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

Upvotes: 0

Views: 777

Answers (1)

ashutosh tomar
ashutosh tomar

Reputation: 36

You need to make sanitizer: DomSanitizer as private to make it accessible in code as : private sanitizer: DomSanitizer

Upvotes: 2

Related Questions