export enum PolicyOptions { NotSet , DefaultHeader , CustomValue } export class Content { policy ? : string ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return this . policy ; } } get option ( ) { return this . optionType ; } } export class CSP { policy : HeaderOptions [ ] = [ ] ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } baseUri ( ... sources ) { this . policy . push ( { directive : "base-uri" , sources : sources } ) ; return this ; } blockAllMixedContent ( ) { this . policy . push ( { directive : "block-all-mixed-content" } ) ; return this ; } connectSrc ( ... sources ) { this . policy . push ( { directive : "connect-src" , sources : sources } ) ; return this ; } defaultSrc ( ... sources ) { this . policy . push ( { directive : "default-src" , sources : sources } ) ; return this ; } fontSrc ( ... sources ) { this . policy . push ( { directive : "font-src" , sources : sources } ) ; return this ; } formAction ( ... sources ) { this . policy . push ( { directive : "form-action" , sources : sources } ) ; return this ; } frameAncestors ( ... sources ) { this . policy . push ( { directive : "frame-ancestors" , sources : sources } ) ; return this ; } frameSrc ( ... sources ) { this . policy . push ( { directive : "frame-src" , sources : sources } ) ; return this ; } imgSrc ( ... sources ) { this . policy . push ( { directive : "img-src" , sources : sources } ) ; return this ; } manifestSrc ( ... sources ) { this . policy . push ( { directive : "manifest-src" , sources : sources } ) ; return this ; } mediaSrc ( ... sources ) { this . policy . push ( { directive : "media-src" , sources : sources } ) ; return this ; } objectSrc ( ... sources ) { this . policy . push ( { directive : "object-src" , sources : sources } ) ; return this ; } pluginTypes ( ... types ) { this . policy . push ( { directive : "plugin-types" , sources : types } ) ; return self ; } requireSriFor ( ... values ) { this . policy . push ( { directive : "require-sri-for" , sources : values } ) ; return this ; } reportTo ( json_object ) { this . policy . push ( { directive : "report-to" , sources : json_object } ) ; return this ; } reportUri ( uri ) { this . policy . push ( { directive : "report-uri" , sources : uri } ) ; return this ; } sandbox ( ... values ) { this . policy . push ( { directive : "sandbox" , sources : values } ) ; return this ; } scriptSrc ( ... sources ) { this . policy . push ( { directive : "script-src" , sources : sources } ) ; return this ; } styleSrc ( ... sources ) { this . policy . push ( { directive : "style-src" , sources : sources } ) ; return self ; } upgradeInsecureRequests ( ) { this . policy . push ( { directive : "upgrade-insecure-requests" } ) ; return self ; } workerSrc ( ... sources ) { this . policy . push ( { directive : "worker-src" , sources : sources } ) ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return getPolicyMultiOpt ( this . policy ) ; } } get option ( ) { return this . optionType ; } } export class XFO { policy ? : string ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } allowFrom ( uri ) { this . policy = `allow-from ${ uri }` ; return this ; } deny ( ) { this . policy = "deny" ; return this ; } sameorigin ( ) { this . policy = "sameorigin" ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return this . policy ; } } get option ( ) { return this . optionType ; } } export class XXP { policy ? : string ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } disabled ( ) { this . policy = "0" ; return this ; } enabled ( ) { this . policy = "1" ; return this ; } enabledBlock ( ) { this . policy = "1; mode=block" ; return this ; } enabledReport ( uri ) { this . policy = `1; report=${ uri }` ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return this . policy ; } } get option ( ) { return this . optionType ; } } export class Referrer { policy : string [ ] = [ ] ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } noReferrer ( ) { this . policy . push ( "no-referrer" ) ; return this ; } noReferrerWhenDowngrade ( ) { this . policy . push ( "no-referrer-when-downgrade" ) ; return this ; } origin ( ) { this . policy . push ( "origin" ) ; return this ; } originWhenCrossOrigin ( ) { this . policy . push ( "origin-when-cross-origin" ) ; return this ; } sameOrigin ( ) { this . policy . push ( "same-origin" ) ; return this ; } strictOrigin ( ) { this . policy . push ( "strict-origin" ) ; return this ; } strictOriginWhenCrossOrigin ( ) { this . policy . push ( "strict-origin-when-cross-origin" ) ; return this ; } unsafeUrl ( ) { this . policy . push ( "unsafe-url" ) ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return getPolicy ( this . policy , ", " ) ; } } get option ( ) { return this . optionType ; } } export class HSTS { policy : string [ ] = [ ] ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } includeSubdomains ( ) { this . policy . push ( "includeSubDomains" ) ; return this ; } maxAge ( seconds ) { this . policy . push ( `max-age=${ seconds }` ) ; return this ; } preload ( ) { this . policy . push ( "preload" ) ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return getPolicy ( this . policy , "; " ) ; } } get option ( ) { return this . optionType ; } } export class Cache { policy : string [ ] = [ ] ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } immutable ( ) { this . policy . push ( "immutable" ) ; return this ; } maxAge ( seconds ) { this . policy . push ( `max-age=${ seconds }` ) ; return this ; } maxStale ( seconds ) { this . policy . push ( `max-stale=${ seconds }` ) ; return this ; } minFresh ( seconds ) { this . policy . push ( `min-fresh=${ seconds }` ) ; return this ; } mustRevalidate ( ) { this . policy . push ( "must-revalidate" ) ; return this ; } noCache ( ) { this . policy . push ( "no-cache" ) ; return this ; } noStore ( ) { this . policy . push ( "no-store" ) ; return this ; } noTransform ( ) { this . policy . push ( "no-transform" ) ; return this ; } onlyIfCached ( ) { this . policy . push ( "only-if-cached" ) ; return this ; } private ( ) { this . policy . push ( "private" ) ; return this ; } proxyRevalidate ( ) { this . policy . push ( "proxy-revalidate" ) ; return this ; } public ( ) { this . policy . push ( "public" ) ; return this ; } sMaxage ( seconds ) { this . policy . push ( `s-maxage=${ seconds }` ) ; return this ; } staleIfError ( seconds ) { this . policy . push ( `stale-if-error=${ seconds }` ) ; return this ; } staleWhileRevalidate ( seconds ) { this . policy . push ( `stale-while-revalidate=${ seconds }` ) ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return getPolicy ( this . policy , ", " ) ; } } get option ( ) { return this . optionType ; } } export class Feature { policy : HeaderOptions [ ] = [ ] ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } accelerometer ( ... allowlist ) { this . policy . push ( { directive : "accelerometer" , sources : allowlist } ) ; return this ; } ambientLightSensor ( ... allowlist ) { this . policy . push ( { directive : "ambient-light-sensor " , sources : allowlist } ) ; return this ; } autoplay ( ... allowlist ) { this . policy . push ( { directive : "autoplay" , sources : allowlist } ) ; return this ; } camera ( ... allowlist ) { this . policy . push ( { directive : "camera" , sources : allowlist } ) ; return this ; } documentDomain ( ... allowlist ) { this . policy . push ( { directive : "document-domain" , sources : allowlist } ) ; return this ; } encryptedMedia ( ... allowlist ) { this . policy . push ( { directive : "encrypted-media" , sources : allowlist } ) ; return this ; } fullscreen ( ... allowlist ) { this . policy . push ( { directive : "fullscreen" , sources : allowlist } ) ; return this ; } geolocation ( ... allowlist ) { this . policy . push ( { directive : "geolocation" , sources : allowlist } ) ; return this ; } gyroscope ( ... allowlist ) { this . policy . push ( { directive : "gyroscope" , sources : allowlist } ) ; return this ; } magnetometer ( ... allowlist ) { this . policy . push ( { directive : "magnetometer" , sources : allowlist } ) ; return this ; } microphone ( ... allowlist ) { this . policy . push ( { directive : "microphone" , sources : allowlist } ) ; return this ; } midi ( ... allowlist ) { this . policy . push ( { directive : "midi" , sources : allowlist } ) ; return this ; } payment ( ... allowlist ) { this . policy . push ( { directive : "payment" , sources : allowlist } ) ; return this ; } pictureInPicture ( ... allowlist ) { this . policy . push ( { directive : "picture-in-picture" , sources : allowlist } ) ; return this ; } speaker ( ... allowlist ) { this . policy . push ( { directive : "speaker" , sources : allowlist } ) ; return this ; } syncXhr ( ... allowlist ) { this . policy . push ( { directive : "sync-xhr" , sources : allowlist } ) ; return this ; } usb ( ... allowlist ) { this . policy . push ( { directive : "usb" , sources : allowlist } ) ; return this ; } vibrate ( ... allowlist ) { this . policy . push ( { directive : "vibrate" , sources : allowlist } ) ; return this ; } vr ( ... allowlist ) { this . policy . push ( { directive : "vr" , sources : allowlist } ) ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return getPolicyMultiOpt ( this . policy ) ; } } get option ( ) { return this . optionType ; } } export class Server { policy ? : string ; custom ? : string ; optionType : PolicyOptions = PolicyOptions . CustomValue ; default ( ) { this . optionType = PolicyOptions . DefaultHeader ; return this ; } notSet ( ) { this . optionType = PolicyOptions . NotSet ; return this ; } set ( value ) { this . custom = value ; return this ; } get value ( ) { if ( this . custom ) { return this . custom ; } else if ( this . policy ) { return this . policy ; } } get option ( ) { return this . optionType ; } } export const values = { all : "*" , none : "'none'" , self : "'self'" , src : "'src'" , strictDynamic : "'strict-dynamic'" , unsafeEval : "'unsafe-eval'" , unsafeInline : "'unsafe-inline'" } ; export const seconds = { fiveMinutes : "300" , oneDay : "86400" , oneWeek : "604800" , oneMonth : "2592000" , oneYear : "31536000" , twoYears : "63072000" } ; function getPolicyMultiOpt ( policy ) { const values = [ ] ; let resouces ; for ( let option of policy ) { if ( option . sources ) { if ( Array . isArray ( option . sources ) ) { resouces = option . sources . join ( " " ) ; } else { resouces = option . sources ; } values . push ( `${ option . directive } ${ resouces }` ) ; } else { values . push ( option . directive ) ; } } return values . join ( "; " ) ; } function getPolicy ( policy , separator ) { let value ; if ( Array . isArray ( policy ) ) { value = policy . join ( separator ) ; } else { value = policy ; } return value ; } interface HeaderOptions { directive : string ; sources ? : string [ ] | string | object ; }