Created
February 17, 2017 20:51
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: CSP | |
* Description: Uses inline_js() to make CSP happen | |
*/ | |
add_filter('inline_js_attributes', function ($attr) { | |
$nonce = wp_create_nonce('csp'); | |
return $attr . ' nonce="'.esc_attr($nonce).'" '; | |
}); | |
add_action('admin_head', function () { | |
$nonce = wp_create_nonce('csp'); | |
?> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'nonce-<?php echo esc_attr($nonce) ?>'"> | |
<?php | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does wp_create_nonce reuse the value if it has already been generated? Otherwise, we'll probably hit a timing issue if the page takes a couple seconds to load. Also, we need to make sure that not-logged-in users get different nonces even if they load the page at the same time.