{"id":5028,"date":"2021-03-18T14:55:32","date_gmt":"2021-03-18T14:55:32","guid":{"rendered":"https:\/\/chemicloud.com\/kb\/?post_type=ht_kb&#038;p=5028"},"modified":"2021-03-22T09:55:47","modified_gmt":"2021-03-22T09:55:47","slug":"content-security-policy","status":"publish","type":"ht_kb","link":"https:\/\/chemicloud.com\/kb\/article\/content-security-policy\/","title":{"rendered":"How do I Create a Content Security Policy?"},"content":{"rendered":"<p>A Content Security Policy, or CSP, is an additional layer of security delivered via an HTTP Header, similar to HSTS technology. This policy helps prevent various kinds of attacks, including Cross-Site Scripting (XSS) and other code injection attacks by defining content sources that are approved, therefore allowing the browser to load them.<\/p>\n<p>Without a Content Security Policy, the browser will just load all files on the page without considering their source, which could be a harmful site. That puts the site and visitors at higher risk of malicious activity.<\/p>\n<h2 id=\"what-browsers-support-content-security-policies\">What browsers support Content Security Policies?<\/h2>\n<p>All major browsers offer full or partial support for Content Security Policies.<\/p>\n<p>However, in the event that someone&#8217;s using a really old browser, the Content Security Policy won&#8217;t be applied. Content Security Policies are backward compatible which means that older browsers are still able to view webpages that are protected by said Content Security Policies, and vice-versa.<\/p>\n<h2 id=\"what-directives-are-supported-in-a-content-security-policy\">What directives are supported in a Content Security Policy?<\/h2>\n<p>There are many directives available to website owners who want to implement a Content Security Policy. A server can also define which directives within its own security header. The following list outlines the directives available for use along with their description:<\/p>\n<ul>\n<li><code>default-src<\/code>\u00a0Define loading policy for all resources type in case of a resource type dedicated directive is not defined (fallback),<\/li>\n<li><code>script-src<\/code>\u00a0Define which scripts the protected resource can execute,<\/li>\n<li><code>object-src<\/code>\u00a0Define from where the protected resource can load plugins,<\/li>\n<li><code>style-src<\/code>\u00a0Define which styles (CSS) the user applies to the protected resource,<\/li>\n<li><code>img-src<\/code>\u00a0Define from where the protected resource can load images,<\/li>\n<li><code>media-src<\/code>\u00a0Define from where the protected resource can load video and audio,<\/li>\n<li><code>frame-src<\/code>\u00a0Define from where the protected resource can embed frames,<\/li>\n<li><code>frame-ancestors<\/code>\u00a0Specifies valid parents that may embed a page using\u00a0<code>&lt;frame&gt;<\/code>,\u00a0<code>&lt;iframe&gt;<\/code>,\u00a0<code>&lt;object&gt;<\/code>,\u00a0<code>&lt;embed&gt;<\/code>, or\u00a0<code>&lt;applet&gt;<\/code>.<\/li>\n<li><code>font-src<\/code>\u00a0Define from where the protected resource can load fonts,<\/li>\n<li><code>connect-src<\/code>\u00a0Define which URIs the protected resource can load using script interfaces,<\/li>\n<li><code>form-action<\/code>\u00a0Define which URIs can be used as the action of HTML form elements,<\/li>\n<li><code>sandbox<\/code>\u00a0Specifies an HTML sandbox policy that the user agent applies to the protected resource,<\/li>\n<li><code>script-nonce<\/code>\u00a0Define script execution by requiring the presence of the specified nonce on script elements,<\/li>\n<li><code>plugin-types<\/code>\u00a0Define the set of plugins that can be invoked by the protected resource by limiting the types of resources that can be embedded,<\/li>\n<li><code>reflected-xss<\/code>\u00a0Instructs a user agent to activate or deactivate any heuristics used to filter or block reflected cross-site scripting attacks, equivalent to the effects of the non-standard\u00a0<code>X-XSS-Protection<\/code>\u00a0header,<\/li>\n<li><code>report-uri<\/code> Specifies a URI to which the user agent sends reports about a policy violation<\/li>\n<\/ul>\n<p>The above directives can all be used when Creating a Content Security Policy, depending on what you want to accomplish.<\/p>\n<h2 id=\"how-to-create-a-content-security-policy\">How to Create a Content Security Policy<\/h2>\n<p>Considering the number of directives in the list in the above section, there&#8217;s a lot of options available for Administrators to create their Content Security Policy. A CSP format is defined as <code>Content-Security-Policy: policy<\/code>. The following shows a few examples for configuring your\u00a0<code>Content-Security-Policy<\/code>\u00a0header.<\/p>\n<div>Example #1<\/div>\n<p>This CSP will allow scripts from both the current domain (defined by\u00a0<code>'self'<\/code>) as well as\u00a0<code>https:\/\/www.google-analytics.com<\/code>.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: script-src 'self' https:\/\/www.google-analytics.com<\/code><\/pre>\n<div>Example #2<\/div>\n<p>The\u00a0<code>default-src<\/code>\u00a0directive set to\u00a0<code>https:<\/code> will allow the browser to load the resource from any origin using\u00a0<code>https:\/\/<\/code>.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: default-src https:<\/code><\/pre>\n<div>Example #3<\/div>\n<p>This CSP allows for any resource to be loaded from the current domain as well as any subdomain of <code>example.com<\/code>\u00a0(both HTTP and HTTPS).<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: default-src 'self' *.example.com<\/code><\/pre>\n<div>Example #4<\/div>\n<p>The following CSP makes use of the\u00a0<code>frame-ancestors<\/code> the directive which defines which sources are allowed to embed a page using <code>&lt;frame&gt;<\/code>,\u00a0<code>&lt;iframe&gt;<\/code>,\u00a0<code>&lt;object&gt;<\/code>,\u00a0<code>&lt;embed&gt;<\/code>, or\u00a0<code>&lt;applet&gt;<\/code>. To allow only your site use the following.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: frame-ancestors 'self'<\/code><\/pre>\n<div>Example #5<\/div>\n<p>Ports can also be defined in content security policies. This example restricts resources to be loaded only from\u00a0<code>https:\/\/www.keycdn.com<\/code>\u00a0using port\u00a0<code>443<\/code>.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: default-src https:\/\/www.keycdn.com:443<\/code><\/pre>\n<div>Example #6<\/div>\n<p>The first part of this example\u00a0<code>default-src 'none';<\/code>\u00a0tells the browser not to load any resources from any sources. While the second part\u00a0<code>script-src https:\/\/www.keycdn.com<\/code>\u00a0tells the browser to allow scripts from\u00a0<code>www.keycdn.com<\/code>\u00a0over\u00a0<code>https:\/\/<\/code>.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: default-src 'none'; script-src https:\/\/www.keycdn.com<\/code><\/pre>\n<p>For a detailed list of examples and references, visit\u00a0<a href=\"http:\/\/content-security-policy.com\/\" target=\"_blank\" rel=\"nofollow noopener\">content-security-policy.com<\/a>.<\/p>\n<p>You can also use this really cool tool called <a href=\"http:\/\/cspisawesome.com\/\" target=\"_blank\" rel=\"nofollow noopener\">cspisawesome.com<\/a> to easily create a CSP specific to your site&#8217;s needs.<\/p>\n<h2 id=\"testing-your-new-content-security-policy\">Testing Your New Content Security Policy<\/h2>\n<p>Once you&#8217;ve determined how you want to define your Content Security Policy, it&#8217;s time to put it to the test and make sure it works as described on the tin.<\/p>\n<p>For testing purposes, instead of defining your CSP as\u00a0<code>Content-Security-Policy:<\/code>\u00a0you may use\u00a0<code>Content-Security-Policy-Report-Only:<\/code> instead. This <strong>won&#8217;t<\/strong> <strong>enforce the policy rules<\/strong> on the web page but will simply provide you with feedback as to how the policy will react.<\/p>\n<p>This example uses the following CSP. For\u00a0<strong>Nginx users<\/strong>, this snippet is placed within the configuration file.<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">add_header Content-Security-Policy-Report-Only: \"default-src 'none'; script-src http:\/\/wordpress.keycdn.net\";<\/code><\/pre>\n<p>For\u00a0<strong>Apache &amp; Litespeed users<\/strong>, the following would be placed in the configuration file<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Header set Content-Security-Policy-Report-Only \"default-src 'none'; script-src http:\/\/wordpress.keycdn.net;\"<\/code><\/pre>\n<p>Once this CSP has been set on your origin server, you can open up your browser&#8217;s console and will see feedback based on the directives set.<\/p>\n<p>When you&#8217;re happy with the results of your CSP, you can remove the Report-Only section of the header so that the Content Security Policy will be taken into affect.<\/p>\n<h2 id=\"csp-reporting\">CSP Reporting<\/h2>\n<p>Now that you&#8217;ve got your Content Security Policy properly configured and in place, your site will be much less vulnerable to XSS attacks. However, in the event that the Content Security Policy does trigger an unwanted action, the report-uri directive can be utilized to keep track of any activity that is in violation of the Content Security Policy.<\/p>\n<p>Using this directive, the browser will post a JSON formatted report to the defined URL of your choosing. This directive can be appended to the end of your Content Security Policy like this:<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">Content-Security-Policy: \"default-src 'none'; script-src https:\/\/example.com; report-uri https:\/\/report.example.com\"<\/code><\/pre>\n<p>When a report is triggered, it will look something like this:<\/p>\n<pre class=\" language-none\"><code class=\" language-none\">{\r\n    \"csp-report\": {\r\n        \"document-uri\": \"https:\/\/example.com\",\r\n        \"referrer\": \"https:\/\/malicious.com\",\r\n        \"blocked-uri\": \"https:\/\/malicious.com\/assets\/js\/xss.js\",\r\n        \"violated-directive\": \"script-src https:\/\/example.com\",\r\n        \"original-policy\": \"default-src 'none'; script-src https:\/\/example.com; report-uri https:\/\/report.example.com\"\r\n    }\r\n}<\/code><\/pre>\n<p>Once Reporting is properly configured, you will be able to keep a closer eye on which sources are in violation of your new Content Security Policy.<\/p>\n<p>If you enjoyed this tutorial, then you\u2019ll love our support! All ChemiCloud\u2019s hosting plans include 24\/7 support from our amazing support team. Check out our <strong><a href=\"https:\/\/chemicloud.com\/web-hosting\/?utm_source=kb&amp;utm_medium=tutorial&amp;utm_content=bottom\" target=\"_blank\" rel=\"noopener\">Web hosting<\/a><\/strong>\u00a0plans and have your website migrated today!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Content Security Policy, or CSP, is an additional layer of security delivered via an HTTP Header, similar to HSTS technology. This policy helps prevent various kinds of attacks, including Cross-Site Scripting (XSS) and other code injection attacks by defining content sources that are approved, therefore allowing the browser to&#8230;<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"ht-kb-category":[192],"ht-kb-tag":[],"class_list":["post-5028","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-website-security"],"_links":{"self":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/comments?post=5028"}],"version-history":[{"count":4,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5028\/revisions"}],"predecessor-version":[{"id":5104,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/5028\/revisions\/5104"}],"wp:attachment":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/media?parent=5028"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-category?post=5028"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-tag?post=5028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}