Comments (2)
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.
Fantastic. i will try it out ...
from wordpress-csp-manager.
Related Issues (8)
- CSP breaks when new line characters are in the fields HOT 4
- Implement CSP presets (and a more reasonable default)
- Allow using same policy across several of admin/logged-in/frontend (and also inheriting?)
- Undefined $option['mode'] error HOT 1
- Headers already sent warning on cron run
- Valid JSON cannot be saved into the Report-To header option HOT 1
- PHP 8.2 Deprecation: Dynamic Property
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wordpress-csp-manager.