import Logger from '../logger/Logger' ; export default class CSPMonitor { private static loggers : Set < Logger > = new Set < Logger > ( ) ; private static shouldRegisterCSPMonitor : boolean = true ; private static added = false ; static register ( ) { if ( ! ( 'document' in global ) || ! document . addEventListener ) { return ; } if ( CSPMonitor . shouldRegisterCSPMonitor ) { if ( ! CSPMonitor . added ) { document . addEventListener ( 'securitypolicyviolation' , CSPMonitor . listener ) ; CSPMonitor . added = true ; } } } static unregister ( ) { if ( ! ( 'document' in global ) || ! document . removeEventListener ) { return ; } document . removeEventListener ( 'securitypolicyviolation' , CSPMonitor . listener ) ; CSPMonitor . loggers = new Set < Logger > ( ) ; CSPMonitor . added = false ; } static disable ( ) { CSPMonitor . shouldRegisterCSPMonitor = false ; CSPMonitor . unregister ( ) ; } static addLogger ( logger ) { if ( logger ) { CSPMonitor . loggers . add ( logger ) ; } } static removeLogger ( logger ) { if ( logger ) { CSPMonitor . loggers . delete ( logger ) ; } } private static listener = ( event ) : void => { const message = 'Security Policy Violation\n' + `Blocked URI: ${ event . blockedURI }\n` + `Violated Directive: ${ event . violatedDirective }\n` + `Original Policy: ${ event . originalPolicy }\n` + `Document URI: ${ event . documentURI }\n` + `Source File: ${ event . sourceFile }\n` + `Line No.: ${ event . lineNumber }\n` ; for ( const logger of CSPMonitor . loggers ) { logger . error ( message ) ; } if ( CSPMonitor . loggers . size === 0 ) { console . error ( message ) ; } } ; }