{"id":245366,"date":"2025-08-10T02:13:27","date_gmt":"2025-08-10T02:13:27","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/strict-csp\/"},"modified":"2025-11-30T03:19:34","modified_gmt":"2025-11-30T03:19:34","slug":"strict-csp","status":"publish","type":"plugin","link":"https:\/\/tuk.wordpress.org\/plugins\/strict-csp\/","author":186678,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_crdt_document":"","version":"0.3.2","stable_tag":"0.3.2","tested":"6.9.4","requires":"6.4","requires_php":"7.2","requires_plugins":null,"header_name":"Strict CSP","header_author":"Weston Ruter","header_description":"Enables a <a href=\"https:\/\/web.dev\/articles\/strict-csp\">Strict Content Security Policy<\/a> on the frontend and login screen; the policy cannot yet be applied to the WP Admin yet (see <a href=\"https:\/\/core.trac.wordpress.org\/ticket\/59446\">#59446<\/a>).","assets_banners_color":"333333","last_updated":"2025-11-30 03:19:34","external_support_url":"","external_repository_url":"https:\/\/github.com\/westonruter\/strict-csp","donate_link":"","header_plugin_uri":"https:\/\/github.com\/westonruter\/strict-csp","header_author_uri":"https:\/\/weston.ruter.net\/","rating":0,"author_block_rating":0,"active_installs":20,"downloads":586,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation","changelog"],"tags":{"0.3.0":{"tag":"0.3.0","author":"westonruter","date":"2025-08-10 02:13:02"},"0.3.1":{"tag":"0.3.1","author":"westonruter","date":"2025-08-10 21:01:54"},"0.3.2":{"tag":"0.3.2","author":"westonruter","date":"2025-11-30 03:19:34"}},"upgrade_notice":[],"ratings":[],"assets_icons":{"icon-128x128.png":{"filename":"icon-128x128.png","revision":3342167,"resolution":"128x128","location":"assets","locale":""},"icon-256x256.png":{"filename":"icon-256x256.png","revision":3342167,"resolution":"256x256","location":"assets","locale":""},"icon.svg":{"filename":"icon.svg","revision":3342167,"resolution":false,"location":"assets","locale":false}},"assets_banners":{"banner-1544x500.png":{"filename":"banner-1544x500.png","revision":3342167,"resolution":"1544x500","location":"assets","locale":""},"banner-772x250.png":{"filename":"banner-772x250.png","revision":3342167,"resolution":"772x250","location":"assets","locale":""},"banner.svg":{"filename":"banner.svg","revision":3342167,"resolution":false,"location":"assets","locale":false}},"assets_blueprints":{},"all_blocks":[],"tagged_versions":["0.3.0","0.3.1","0.3.2"],"block_files":[],"assets_screenshots":[],"screenshots":[],"jetpack_post_was_ever_published":false},"plugin_section":[],"plugin_tags":[600],"plugin_category":[54],"plugin_contributors":[78438],"plugin_business_model":[216239],"class_list":["post-245366","plugin","type-plugin","status-publish","hentry","plugin_tags-security","plugin_category-security-and-spam-protection","plugin_contributors-westonruter","plugin_business_model-community","plugin_committers-westonruter"],"banners":{"banner":"https:\/\/ps.w.org\/strict-csp\/assets\/banner-772x250.png?rev=3342167","banner_2x":"https:\/\/ps.w.org\/strict-csp\/assets\/banner-1544x500.png?rev=3342167","banner_rtl":false,"banner_2x_rtl":false},"icons":{"svg":"https:\/\/ps.w.org\/strict-csp\/assets\/icon.svg?rev=3342167","icon":"https:\/\/ps.w.org\/strict-csp\/assets\/icon.svg?rev=3342167","icon_2x":false,"generated":false},"screenshots":[],"raw_content":"<!--section=description-->\n<p>This plugin enforces a <a href=\"https:\/\/web.dev\/articles\/strict-csp\">Strict Content Security Policy<\/a> (CSP) on the frontend and login screen. This helps mitigate <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Security\/Attacks\/XSS\">cross-site scripting<\/a> (XSS) vulnerabilities. The policy cannot yet be applied to the WP Admin (see <a href=\"https:\/\/core.trac.wordpress.org\/ticket\/59446\">#59446<\/a>).<\/p>\n\n<p>In <a href=\"https:\/\/core.trac.wordpress.org\/ticket\/58664\">#58664<\/a>, the manual construction of script tags was eliminated from <code>WP_Scripts<\/code> and inline scripts on frontend\/login screen, thanks to the helper functions which had previously been introduced in <a href=\"https:\/\/core.trac.wordpress.org\/ticket\/39941\">#39941<\/a>.  This made it possible to apply Strict CSP, as long as themes and plugins are not directly printing <code>&lt;script&gt;<\/code> tags. Some bundled WordPress core themes <a href=\"https:\/\/github.com\/search?q=repo%3AWordPress%2Fwordpress-develop+path%3A%2F%5Esrc%5C%2Fwp-content%5C%2Fthemes%5C%2F%2F+%2F%3Cscript%5B%5E%3E%5D*%3E%2F&amp;type=code\">still do this<\/a> incorrectly (which has been reported in Trac as <a href=\"https:\/\/core.trac.wordpress.org\/ticket\/63806\">#63806<\/a>). For example, do not do this:<\/p>\n\n<pre>function my_theme_supports_js() {\n    echo &#039;&lt;script&gt;document.body.classList.remove(&quot;no-js&quot;);&lt;\/script&gt;&#039;; \/\/ \u274c\n}\nadd_action( &#039;wp_footer&#039;, &#039;my_theme_supports_js&#039; );<\/pre>\n\n<p>Instead, do this:<\/p>\n\n<pre>function my_theme_supports_js() {\n    wp_print_inline_script_tag( &#039;document.body.classList.remove(&quot;no-js&quot;);&#039; ); \/\/ \u2705\n}\nadd_action( &#039;wp_footer&#039;, &#039;my_theme_supports_js&#039; );<\/pre>\n\n<p>So in order for scripts to execute, they must be printed using the relevant APIs in WordPress for adding scripts, including <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_script\/\"><code>wp_enqueue_script()<\/code><\/a>, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_add_inline_script\/\"><code>wp_add_inline_script()<\/code><\/a>, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_localize_script\/\"><code>wp_localize_script()<\/code><\/a>, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_print_script_tag\/\"><code>wp_print_script_tag()<\/code><\/a>, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_print_inline_script_tag\/\"><code>wp_print_inline_script_tag()<\/code><\/a>, and <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_script_module\/\"><code>wp_enqueue_script_module()<\/code><\/a>. Otherwise, a script's execution will be blocked and an error will appear in the console, for example:<\/p>\n\n<blockquote>\n  <p>Refused to execute inline script because it violates the following Content Security Policy directive: \"script-src 'nonce-9b539cfe47' 'unsafe-inline' 'strict-dynamic' https: http:\". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.<\/p>\n<\/blockquote>\n\n<p>This also blocks scripts inside of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Reference\/Attributes#event_handler_attributes\">event handler attributes<\/a>, such as <code>onclick<\/code>, <code>onchange<\/code>, <code>onsubmit<\/code>, and <code>onload<\/code>. As noted on MDN:<\/p>\n\n<blockquote>\n  <p>Warning: The use of event handler content attributes is discouraged. The mix of HTML and JavaScript often produces unmaintainable code, and the execution of event handler attributes may also be blocked by content security policies.<\/p>\n<\/blockquote>\n\n<p>This plugin also ensures that scripts added to the page from embeds (e.g. Tweets) also get the <code>nonce<\/code> attribute added.<\/p>\n\n<!--section=installation-->\n<h4>Automatic<\/h4>\n\n<ol>\n<li>Visit <strong>Plugins &gt; Add New<\/strong> in the WordPress Admin.<\/li>\n<li>Search for <strong>Strict CSP<\/strong>.<\/li>\n<li>Install and activate the <strong>Strict CSP<\/strong> plugin.<\/li>\n<li>Log out of WordPress and log back in with the \u201cRemember Me\u201d checkbox checked.<\/li>\n<\/ol>\n\n<p>You may also install and update via <a href=\"https:\/\/git-updater.com\/\">Git Updater<\/a> using the <a href=\"https:\/\/github.com\/westonruter\/strict-csp\">plugin's GitHub URL<\/a>.<\/p>\n\n<h4>Manual<\/h4>\n\n<ol>\n<li>Download the plugin ZIP either <a href=\"https:\/\/downloads.wordpress.org\/plugin\/strict-csp.zip\">from WordPress.org<\/a> or <a href=\"https:\/\/github.com\/westonruter\/strict-csp\/archive\/refs\/heads\/main.zip\">from GitHub<\/a>. Alternatively, if you have a local clone of the repo, run <code>npm run plugin-zip<\/code>.<\/li>\n<li>Visit <strong>Plugins &gt; Add New Plugin<\/strong> in the WordPress Admin.<\/li>\n<li>Click <strong>Upload Plugin<\/strong>.<\/li>\n<li>Select the <code>strict-csp.zip<\/code> file on your system from step 1 and click <strong>Install Now<\/strong>.<\/li>\n<li>Click the <strong>Activate Plugin<\/strong> button.<\/li>\n<\/ol>\n\n<!--section=changelog-->\n<h4>0.3.2<\/h4>\n\n<ul>\n<li>Use <code>wp_generate_password()<\/code> to create CSP nonce instead of using <code>wp_create_nonce()<\/code>. Props <a href=\"https:\/\/profiles.wordpress.org\/kasparsd\/\">kasparsd<\/a>. (<a href=\"https:\/\/github.com\/westonruter\/strict-csp\/pull\/13\">#13<\/a>)<\/li>\n<\/ul>\n\n<h4>0.3.1<\/h4>\n\n<ul>\n<li>Update required PHP version to 7.2 instead of 8.1.<\/li>\n<\/ul>\n\n<h4>0.3.0<\/h4>\n\n<ul>\n<li>Add <code>nonce<\/code> attributes to scripts added by embeds.<\/li>\n<\/ul>\n\n<h4>0.2.0<\/h4>\n\n<ul>\n<li>Disable Strict CSP from Site Editor.<\/li>\n<li>Restrict policy to frontend and login screen.<\/li>\n<\/ul>\n\n<h4>0.1.0<\/h4>\n\n<ul>\n<li>Initial release.<\/li>\n<\/ul>","raw_excerpt":"Enforces a Strict Content Security Policy on the frontend and login screen to help mitigate any XSS vulnerabilities.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/245366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=245366"}],"author":[{"embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/westonruter"}],"wp:attachment":[{"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=245366"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=245366"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=245366"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=245366"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=245366"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/tuk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=245366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}