Git Product home page Git Product logo

Comments (2)

mcaskill avatar mcaskill commented on August 23, 2024

A dedicated filter to customize the policies before they are output as HTTP Headers would indeed be appreciated.

Until then, I've created the following workaround that ensures any dynamically changed directives/policies are not persisted to the database (when saving changes from the plugin's settings page):

/**
 * Determines if the CSP Manager is preparing to output HTTP Headers.
 *
 * Uses a PHP backtrace to locate calls from {@see \CSP_Manager\Core::csp_init()}.
 */
function is_csp_manager_doing_http_headers(): bool {
	foreach ( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 6 ) as $call ) {
		if (
			isset( $call['function'], $call['class'] ) &&
			'csp_init' === $call['function'] &&
			'CSP_Manager\Core' === $call['class']
		) {
			return true;
		}
	}

	return false;
}

/**
 * Customizes the CSP Manager policies to include
 * the staging environment for remote assets.
 *
 * @listens filter:option_csp_manager_admin
 * @listens filter:option_csp_manager_loggedin
 * @listens filter:option_csp_manager_frontend
 *
 * @param  mixed  $settings The CSP directives, policies, and options.
 * @return mixed
 */
function filter_csp_manager_customize_settings( mixed $settings ): mixed {
	if (
		$settings === false ||
		! isset( $settings['mode'] ) ||
		'disabled' === $settings['mode'] ||
		! is_csp_manager_doing_http_headers()
	) {
		return $settings;
	}

	$directives = [
		'img-src' => 'https://example.com',
	];

	foreach ( $directives as $directive => $policy ) {
		if (
			isset( $settings[ 'enable_' . $directive ] ) &&
			$settings[ 'enable_' . $directive ]
		) {
			$settings[ $directive ] = (
				isset( $settings[ $directive ] )
				? $settings[ $directive ] . ' '
				: ''
			) . $policy;
		}
	}

	return $settings;
}

add_action( 'option_csp_manager_admin',    'filter_csp_manager_customize_settings', 10, 1 );
add_action( 'option_csp_manager_loggedin', 'filter_csp_manager_customize_settings', 10, 1 );
add_action( 'option_csp_manager_frontend', 'filter_csp_manager_customize_settings', 10, 1 );

What this does is mutate the three main options of the CSP Manager:

  • csp_manager_admin
  • csp_manager_loggedin
  • csp_manager_frontend

Via the option_{$option} hook.

The function is_csp_manager_doing_http_headers() is used to search through a slice of the PHP backtrace to determine if the filter function filter_csp_manager_customize_settings() was called from CSP_Manager\Core::csp_init() which handles the output of HTTP Headers.

The main purpose of this filter function, for my use case, is to append our staging environment's hostname (using example.com for demonstration) to the img-src directive to allow my localhost to fetch images from the staging environment if they are absent locally.

from wordpress-csp-manager.

buzi avatar buzi commented on August 23, 2024

Fantastic. i will try it out ...

from wordpress-csp-manager.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.