PHP Class ParagonIE\CSPBuilder\CSPBuilder

Afficher le fichier Open project: paragonie/csp-builder Class Usage Examples

Protected Properties

Свойство Type Description
$supportOldBrowsers boolean

Méthodes publiques

Méthode Description
__construct ( array $policy = [] )
addDirective ( string $key, mixed $value = null ) : self Add a directive if it doesn't already exist
addSource ( string $directive, string $path ) : self Add a source to our allow white-list
allowPluginType ( string $mime = 'text/plain' ) : self Add a plugin type to be added
compile ( ) : string Compile the current policies into a CSP header
disableOldBrowserSupport ( ) : self Disable old browser support (e.g. Safari)
enableOldBrowserSupport ( ) : self Enable old browser support (e.g. Safari)
fromData ( string $data = '' ) : self Factory method - create a new CSPBuilder object from a JSON data
fromFile ( string $filename = '' ) : self Factory method - create a new CSPBuilder object from a JSON file
getCompiledHeader ( ) : string Get the formatted CSP header
getHeaderArray ( boolean $legacy = true ) : array Get an associative array of headers to return.
hash ( string $directive = 'script-src', string $script = '', string $algorithm = 'sha384' ) : self Add a new hash to the existing CSP
injectCSPHeader ( Psr\Http\Message\MessageInterface $message, boolean $legacy = false ) : Psr\Http\Message\MessageInterface PSR-7 header injection
nonce ( string $directive = 'script-src', string $nonce = '' ) : string Add a new nonce to the existing CSP
preHash ( string $directive = 'script-src', string $hash = '', string $algorithm = 'sha384' ) : self Add a new (pre-calculated) base64-encoded hash to the existing CSP
saveSnippet ( string $outputFile, string $format = self::FORMAT_NGINX ) : boolean Save CSP to a snippet file
sendCSPHeader ( boolean $legacy = true ) : boolean Send the compiled CSP as a header()
setDirective ( string $key, mixed $value = null ) : self Set a directive

Méthodes protégées

Méthode Description
compileSubgroup ( string $directive, mixed $policies = null ) : string Compile a subgroup into a policy string
getHeaderKeys ( boolean $legacy = true ) : array Get an array of header keys to return
isHTTPSConnection ( ) : boolean Is this user currently connected over HTTPS?

Method Details

__construct() public méthode

public __construct ( array $policy = [] )
$policy array

addDirective() public méthode

If it already exists, do nothing
public addDirective ( string $key, mixed $value = null ) : self
$key string
$value mixed
Résultat self

addSource() public méthode

Add a source to our allow white-list
public addSource ( string $directive, string $path ) : self
$directive string
$path string
Résultat self

allowPluginType() public méthode

Add a plugin type to be added
public allowPluginType ( string $mime = 'text/plain' ) : self
$mime string
Résultat self

compile() public méthode

Compile the current policies into a CSP header
public compile ( ) : string
Résultat string

compileSubgroup() protected méthode

Compile a subgroup into a policy string
protected compileSubgroup ( string $directive, mixed $policies = null ) : string
$directive string
$policies mixed
Résultat string

disableOldBrowserSupport() public méthode

Disable old browser support (e.g. Safari)
public disableOldBrowserSupport ( ) : self
Résultat self

enableOldBrowserSupport() public méthode

This is enabled by default
public enableOldBrowserSupport ( ) : self
Résultat self

fromData() public static méthode

Factory method - create a new CSPBuilder object from a JSON data
public static fromData ( string $data = '' ) : self
$data string
Résultat self

fromFile() public static méthode

Factory method - create a new CSPBuilder object from a JSON file
public static fromFile ( string $filename = '' ) : self
$filename string
Résultat self

getCompiledHeader() public méthode

Get the formatted CSP header
public getCompiledHeader ( ) : string
Résultat string

getHeaderArray() public méthode

Get an associative array of headers to return.
public getHeaderArray ( boolean $legacy = true ) : array
$legacy boolean
Résultat array

getHeaderKeys() protected méthode

Get an array of header keys to return
protected getHeaderKeys ( boolean $legacy = true ) : array
$legacy boolean
Résultat array

hash() public méthode

Add a new hash to the existing CSP
public hash ( string $directive = 'script-src', string $script = '', string $algorithm = 'sha384' ) : self
$directive string
$script string
$algorithm string
Résultat self

injectCSPHeader() public méthode

PSR-7 header injection
public injectCSPHeader ( Psr\Http\Message\MessageInterface $message, boolean $legacy = false ) : Psr\Http\Message\MessageInterface
$message Psr\Http\Message\MessageInterface
$legacy boolean
Résultat Psr\Http\Message\MessageInterface

isHTTPSConnection() protected méthode

Is this user currently connected over HTTPS?
protected isHTTPSConnection ( ) : boolean
Résultat boolean

nonce() public méthode

Add a new nonce to the existing CSP
public nonce ( string $directive = 'script-src', string $nonce = '' ) : string
$directive string
$nonce string (if empty, it will be generated)
Résultat string

preHash() public méthode

Add a new (pre-calculated) base64-encoded hash to the existing CSP
public preHash ( string $directive = 'script-src', string $hash = '', string $algorithm = 'sha384' ) : self
$directive string
$hash string
$algorithm string
Résultat self

saveSnippet() public méthode

Save CSP to a snippet file
public saveSnippet ( string $outputFile, string $format = self::FORMAT_NGINX ) : boolean
$outputFile string Output file name
$format string Which format are we saving in?
Résultat boolean

sendCSPHeader() public méthode

Send the compiled CSP as a header()
public sendCSPHeader ( boolean $legacy = true ) : boolean
$legacy boolean Send legacy headers?
Résultat boolean

setDirective() public méthode

Set a directive
public setDirective ( string $key, mixed $value = null ) : self
$key string
$value mixed
Résultat self

Property Details

$supportOldBrowsers protected_oe property

protected bool $supportOldBrowsers
Résultat boolean