PHP code example of altis / browser-security

1. Go to this page and download the library: Download altis/browser-security library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

altis / browser-security example snippets


// Setting hashes for scripts.
use Altis\Security\Browser;
wp_enqueue_script( 'my-handle', 'https://...' );
Browser\set_hash_for_script( 'my-handle', 'sha384-...' );

// Setting hashes for styles.
use Altis\Security\Browser;
wp_enqueue_style( 'my-handle', 'https://...' );
Browser\set_hash_for_style( 'my-handle', 'sha384-...' );

add_filter( 'altis.security.browser.content_security_policies', function ( array $policies ) : array {
	// Policies can be set as strings.
	$policies['object-src'] = 'none';
	$policies['base-uri'] = 'self';

	// Policies can also be set as arrays.
	$policies['font-src'] = [
		'https://fonts.gstatic.com',
		'https://cdnjs.cloudflare.com',
	];

	// Special directives (such as `unsafe-inline`) are handled for you.
	$policies['script-src'] = [
		'https:',
		'unsafe-inline',
	];

	return $policies;
} );

// You can filter specific keys via the filter name.
add_filter( 'altis.security.browser.filter_policy_value.font-src', function ( array $values ) : array {
	$values[] = 'https://fonts.gstatic.com';
	return $values;
} );

// A filter is also available with the directive name in a parameter.
add_filter( 'altis.security.browser.filter_policy_value', function ( array $values, string $name ) : array {
	if ( $name === 'font-src' ) {
		$values[] = 'https://cdnjs.cloudflare.com';
	}

	return $values;
} );

add_filter( 'altis.security.browser.report_only_content_security_policies', function ( array $policies ) : array {
	$policies['report-uri'] = 'https://example.uriports.com/reports';
	return $policies;
} );

define( 'ABS_HSTS', 'max-age=31536000; 

define( 'ABS_HSTS', false );

define( 'ABS_NOSNIFF_HEADER', false );

define( 'ABS_FRAME_OPTIONS_HEADER', false );

define( 'ABS_XSS_PROTECTION_HEADER', false );

add_filter( 'altis.security.browser.rest_allow_origin', '__return_false' );

add_filter( 'altis.security.browser.rest_allow_origin', function ( $allow, $origin ) {

	$allowed_origins = [
		'https://www.example.com',
	];

	if ( in_array( $origin, $allowed_origins, true ) ) {
		return true;
	}

    return false;
}, 10, 2 );

add_filter( 'altis.security.browser.rest_allow_origin', function ( $allow, $origin ) {
    if ( false !== strpos( $origin, '.local' ) ) {
        return false;
    }

    return $allow;
}, 10, 2 );