{"id":11705,"date":"2023-03-22T13:27:48","date_gmt":"2023-03-22T13:27:48","guid":{"rendered":"https:\/\/www.icdsoft.com\/blog\/?p=11705"},"modified":"2023-03-24T12:00:38","modified_gmt":"2023-03-24T12:00:38","slug":"top-5-security-headers","status":"publish","type":"post","link":"https:\/\/www.icdsoft.com\/blog\/top-5-security-headers\/","title":{"rendered":"Top 5 Security Headers for Extra Website Protection"},"content":{"rendered":"\n<p>Websites are constantly under attack, and many of them become exploited through vulnerabilities, usually due to outdated software. The safety of a website can greatly benefit from security headers. There are many types of attacks and security headers, so for the time being we will try to only mention the most important ones.<\/p>\n\n\n\n<ul class=\"wp-block-advgb-summary advgb-toc alignnone\"><li class=\"toc-level-1\"><a href=\"#how-do-security-headers-protect-my-website-362deb3d-696d-4fcf-bee4-84d791147de9\">How do security headers protect my website?<\/a><\/li><li class=\"toc-level-1\"><a href=\"#what-are-security-headers-2f06b380-8db1-41fd-8a7a-a8be18c03c9a\">What Are Security Headers?<\/a><\/li><li class=\"toc-level-1\"><a href=\"#xframeoptions-7e0437e6-e7a6-4b50-ac0e-7408cae84d91\">X-Frame-Options<\/a><\/li><li class=\"toc-level-1\"><a href=\"#contentsecuritypolicy-csp-a5b505e4-08ff-413d-9137-cd02e2f5bb9b\">Content-Security-Policy (CSP)<\/a><\/li><li class=\"toc-level-3\"><a href=\"#crosssite-scripting-xss-84f55b7c-1498-4551-8d83-040ca2b87c98\">Cross-site scripting (XSS)<\/a><\/li><li class=\"toc-level-3\"><a href=\"#packet-sniffing-and-forced-https-81b2082c-f75b-4762-9680-dda1421c0031\">Packet sniffing and forced HTTPS<\/a><\/li><li class=\"toc-level-2\"><a href=\"#csp-header-properties-with-examples-7fa38d4c-0c14-459c-adc4-620f315b2991\">Main CSP security header properties<\/a><\/li><li class=\"toc-level-2\"><a href=\"#setting-up-a-basic-csp-351fdb59-fff4-49c0-be98-ed8973871746\">Setting up a Basic CSP<\/a><\/li><li class=\"toc-level-1\"><a href=\"#xcontenttypeoptions-67af99b1-5c10-4c15-9a5c-9eebb8c08c95\">X-Content-Type-Options<\/a><\/li><li class=\"toc-level-1\"><a href=\"#secure-cookies-b3bf4980-5746-464f-996a-b5842f0f101e\">Secure Cookies<\/a><\/li><li class=\"toc-level-1\"><a href=\"#referrerpolicy-113cd0bf-7327-40c4-b323-2a72c1a8ba4b\">Referrer-Policy<\/a><\/li><li class=\"toc-level-1\"><a href=\"#conclusion-330c35e9-c351-42d8-9e8c-ba7f72408237\">Conclusion<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-security-headers-protect-my-website-362deb3d-696d-4fcf-bee4-84d791147de9\">How do security headers protect my website?<\/h2>\n\n\n\n<p>You can add an extra layer of security by using HTTP security headers, which can block common malicious activity from negatively affecting your website's performance, stability, and security. Modern browsers support a large set of HTTP headers that can protect against clickjacking, cross-site scripting, and other common attacks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-security-headers-2f06b380-8db1-41fd-8a7a-a8be18c03c9a\">What Are Security Headers?<\/h2>\n\n\n\n<p>The HTTP headers are used to pass additional information between the clients and the server through the request and response header. The security headers are passed through the HTTP header response as rules which browsers have to follow. A header response can inform your browser of a \"Page Not Found\" for example. As to security headers, those are the directives which protect your website in some way, for example by allowing the browser to only load certain types of files from your own domain and reject downloads from third parties. A security directive like that will block a browser from downloading malicious files from another website. The restrictions imposed by the security headers allow you to prevent malicious activities, such as XSS, code injection, clickjacking, etc.<\/p>\n\n\n\n<p>Let\u2019s have a look at some of the most important security headers that will give your site some much-needed protection. <strong>Please bear in mind that all our servers run Apache as web server, so all examples are for this type of web environment.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"xframeoptions-7e0437e6-e7a6-4b50-ac0e-7408cae84d91\">X-Frame-Options<\/h2>\n\n\n\n<p>The so-called Clickjacking spoofing technique was very popular back in the day and still is. Attackers still try to trick users into clicking on malicious links by making emails or websites appear as official and legitimate. X-Frame-Options help guard against such attacks by simply limiting or fully disabling iframes present on the site, so your pages cannot be embedded on third-party websites. Here are several examples.<\/p>\n\n\n\n<p>Fully disable the use of iframes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set X-Frame-Options \"deny\"<\/code><\/pre>\n\n\n\n<p>The page can only be embedded in a frame on a page with the same origin as itself, which means you can only embed pages within your own website:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set X-Frame-Options \"sameorigin\"<\/code><\/pre>\n\n\n\n<p>The embedded page can be displayed in a frame on the specified origin, meaning that your content can be embedded only on a website which you have specified as trusted. This only works in browsers that support this security header and does not work with multiple domains:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set X-Frame-Options \"allow-from https:\/\/www.trusted-site.com\/\"\n<\/code><\/pre>\n\n\n\n<p>The added security is provided only if the user accessing the document is using a browser that supports <strong>X-Frame-Options<\/strong>. The <strong>Content-Security-Policy<\/strong> HTTP header has a <em>frame-ancestors<\/em> directive which obsoletes this security header for supporting browsers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"contentsecuritypolicy-csp-a5b505e4-08ff-413d-9137-cd02e2f5bb9b\">Content-Security-Policy (CSP)<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"800\" height=\"400\" data-src=\"https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Content-Security-Policy-Simple-Example.webp\" alt=\"Laptop checks server status with green success and red failure icons in a network connection diagram.\" class=\"wp-image-11791 lazyload\" data-srcset=\"https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Content-Security-Policy-Simple-Example.webp 800w, https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Content-Security-Policy-Simple-Example-300x150.webp 300w\" data-sizes=\"(max-width: 800px) 100vw, 800px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 800px; --smush-placeholder-aspect-ratio: 800\/400;\" \/><figcaption class=\"wp-element-caption\">A very basic example of a Content-Security-Policy<\/figcaption><\/figure>\n\n\n\n<p>The HTTP <strong>Content-Security-Policy<\/strong> response header allows website administrators to control resources the browser is allowed to load for a given page. In other words, you can whitelist your site\u2019s content sources. Content Security Policy evaluates and blocks requests for assets.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"crosssite-scripting-xss-84f55b7c-1498-4551-8d83-040ca2b87c98\">Cross-site scripting (XSS)<\/h4>\n\n\n\n<p>The CSP mainly detects and mitigates XSS attacks. The user's browser can be exploited by having it load seemingly trusted and secure content, when the loaded assets could in fact expose the victim's browser to the execution of malicious scripts downloaded from an unknown source. The website administrator has the ability to impose restrictions and specify which Internet domains should be considered as safe sources of executables. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"packet-sniffing-and-forced-https-81b2082c-f75b-4762-9680-dda1421c0031\">Packet sniffing and forced HTTPS<\/h4>\n\n\n\n<p>In addition to specifying the trusted sources of content loaded on your pages, you can also specify the allowed protocols, e.g. \"always use HTTPS\". However, a good data transfer protection policy consists of not only the implementation of HTTPS but also marking all cookies with the secure attribute as well as redirecting all HTTP requests to HTTPS. Additionally, sites may use <a href=\"https:\/\/www.icdsoft.com\/blog\/how-to-use-hsts-and-protect-your-website\/\" target=\"_blank\" rel=\"noreferrer noopener\">HTTP Strict-Transport-Security<\/a> (HSTS) to ensure that the connection between the browsers and the website is always encrypted.<\/p>\n\n\n\n<p>Let's take a look at the list of CSP properties you can use to protect your pages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"csp-header-properties-with-examples-7fa38d4c-0c14-459c-adc4-620f315b2991\">Main CSP security header properties<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>default-src<\/li>\n\n\n\n<li>script-src<\/li>\n\n\n\n<li>img-src<\/li>\n\n\n\n<li>media-src<\/li>\n\n\n\n<li>object-src<\/li>\n\n\n\n<li>manifest-src<\/li>\n\n\n\n<li>frame-ancestors<\/li>\n\n\n\n<li>form-action<\/li>\n\n\n\n<li>plugin-types<\/li>\n\n\n\n<li>base-uri<\/li>\n<\/ul>\n\n\n\n<p>For a complete and up-to-date list of properties, refer to <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CSP\" target=\"_blank\" rel=\"noopener\">Mozilla<\/a>.<\/p>\n\n\n\n<p>One of the most commonly used request headers is \"upgrade-insecure-requests\". To utilize it, you just need to add the following line to the <strong>.htaccess<\/strong> file present in the document root of your website:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set Content-Security-Policy: upgrade-insecure-requests<\/code><\/pre>\n\n\n\n<p>In a nutshell, this converts all HTTP requests to HTTPS.<\/p>\n\n\n\n<p>Here is another example in which your pages are generally restricted to loading assets from your own domain, but embedded elements from Stripe and YouTube can be loaded on your pages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set Content-Security-Policy \"default-src 'self'; frame-ancestors 'self' *.trusted-domain.com; frame-src 'self' https:\/\/js.stripe.com https:\/\/www.youtube.com\"\n<\/code><\/pre>\n\n\n\n<p><strong>frame-src<\/strong>&nbsp;allows you to specify the locations from which content can be embedded to your pages. This differs from&nbsp;<strong>frame-ancestors<\/strong>, which enables you to specify the locations that are allowed to embed content from your website.<\/p>\n\n\n\n<p>For any policy, it is recommended that you first run it in report-only mode, so that you first review your setup. It is also recommended that you always use <strong>report-uri<\/strong> (deprecated but still used) and <strong>report-to<\/strong> (successor of report-uri) when deploying a policy to understand what is being blocked:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nHeader always set Reporting-Endpoints default=\"https:\/\/my-domain.com\/reports\"\n\nHeader always set Content-Security-Policy-Report-Only \"default-src 'self'; frame-ancestors 'self' *.trusted-domain.com; frame-src 'self' https:\/\/js.stripe.com https:\/\/www.youtube.com; report-uri \/some-report-uri; report-to default\"\n<\/code><\/pre>\n\n\n\n<p>For the reporting to work, you need to configure an endpoint to which the reports will be sent. You can check the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Security-Policy\/report-uri\" data-type=\"URL\" data-id=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Security-Policy\/report-uri\" target=\"_blank\" rel=\"noreferrer noopener\">Mozilla documentation on Content-Security-Policy<\/a> for more information on setting up the script running at the endpoint address, which will actually log the JSON detailing the violation(s).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"setting-up-a-basic-csp-351fdb59-fff4-49c0-be98-ed8973871746\">Setting up a Basic CSP<\/h3>\n\n\n\n<p>The following configuration assumes that: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>All resources are hosted by the same domain of the document. <\/li>\n\n\n\n<li>There are no inlines or evals for scripts and style resources. <\/li>\n\n\n\n<li>There is no need for other websites to frame the website. <\/li>\n\n\n\n<li>There are no form-submissions to external websites.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set Content-Security-Policy \"default-src 'self'; frame-ancestors 'self'; form-action 'self';\"<\/code><\/pre>\n\n\n\n<p>This policy should be carefully considered allowing the content that you intend to load but block everything else.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"xcontenttypeoptions-67af99b1-5c10-4c15-9a5c-9eebb8c08c95\">X-Content-Type-Options<\/h2>\n\n\n\n<p>Blocks a request if the request destination is of type&nbsp;<strong>style<\/strong>&nbsp;and the MIME type is not&nbsp;<strong>text\/css<\/strong>, or of type&nbsp;<strong>script<\/strong>&nbsp;and the MIME type is not a&nbsp;<a href=\"https:\/\/html.spec.whatwg.org\/multipage\/scripting.html#javascript-mime-type\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript MIME type<\/a>. In a nutshell, the following tells the web browser that the Content-Type security headers are deliberately set and should be followed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set X-Content-Type-Options \"nosniff\"\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"secure-cookies-b3bf4980-5746-464f-996a-b5842f0f101e\">Secure Cookies<\/h2>\n\n\n\n<p>To protect your cookies by only sending them via HTTPS (HTTP over SSL\/TLS) and effectively stop attackers from having any chance of eavesdropping on the communication channel between the browser and the server, just add the following to your .htaccess file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always edit Set-Cookie (.*) \"$1;HttpOnly;Secure\"\nRewriteEngine On\nRewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)\nRewriteRule .* - &#91;F]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"referrerpolicy-113cd0bf-7327-40c4-b323-2a72c1a8ba4b\">Referrer-Policy<\/h2>\n\n\n\n<p>Using this HTTP response security header, you can limit the amount of information that may be shared with third parties.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>no-referrer<\/strong>: No referrer information is sent. Keep in mind that this will block referrer information in your own logs. If you need this information for analytics purposes, you may want to choose other options.<\/li>\n\n\n\n<li><strong>no-referrer-when-downgrade<\/strong>: Sends full referrer information unless the security protocol is being downgraded from https to http. <strong>This is the default setting for older browsers.<\/strong> <\/li>\n\n\n\n<li><strong>origin<\/strong>: Only sends the origin (https:\/\/example.com) as the referrer and strips out the path and query string. <\/li>\n\n\n\n<li><strong>origin-when-cross-origin<\/strong>: Same as \"origin\" except when the origin matches the request, full referrer information is sent. <\/li>\n\n\n\n<li><strong>same-origin<\/strong>: Same as \"no-referrer\", except when the origin matches the request, full referrer information is sent. <\/li>\n\n\n\n<li><strong>strict-origin<\/strong>: Same as \"origin\" except if the security protocol is different, don't send anything. <\/li>\n\n\n\n<li><strong>strict-origin-when-cross-origin<\/strong>: Sends full referrer information if the origin matches the request. If not, send only the origin unless the security protocol is being downgraded, in which case send nothing. <strong>This is the default setting for modern browsers.<\/strong> <\/li>\n\n\n\n<li><strong>unsafe-url<\/strong>: Sends full referrer information on all requests (not recommended).<\/li>\n<\/ul>\n\n\n\n<p>To always set the default setting for modern browsers, add the following to your .htaccess file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set Referrer-Policy: \"strict-origin-when-cross-origin\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion-330c35e9-c351-42d8-9e8c-ba7f72408237\">Conclusion<\/h2>\n\n\n\n<p>In modern days, with all the automatic bots crawling your pages in search of vulnerabilities, it is definitely worth it to add security headers as one more layer of protection. The security headers, however, should be added on top of up-to-date software and hardened server environment. Always keep in mind that some security headers require a customized configuration, so you should carefully examine the assets which your website loads to avoid content-related issues. We recommend that you regularly run updates on your applications and revise your custom scripts, so they can remain secure and free of security holes, because keeping the Internet users safe should be of utmost importance for any website. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The safety of a website can greatly benefit from security headers. We will introduce to you the top 5 security headers, so you can protect your website.<\/p>\n","protected":false},"author":1,"featured_media":11715,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","footnotes":""},"categories":[6],"tags":[],"class_list":{"0":"post-11705","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-security"},"author_meta":{"display_name":"ICDSoft","author_link":"https:\/\/www.icdsoft.com\/blog\/author\/icdsoft\/"},"featured_img":"https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Important-Security-Headers-300x150.webp","featured_image_src":"https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Important-Security-Headers.webp","featured_image_src_square":"https:\/\/www.icdsoft.com\/blog\/wp-content\/uploads\/2023\/03\/Important-Security-Headers.webp","author_info":{"display_name":"ICDSoft","author_link":"https:\/\/www.icdsoft.com\/blog\/author\/icdsoft\/"},"coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/www.icdsoft.com\/blog\/category\/security\/\" class=\"advgb-post-tax-term\">Security<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">Security<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 3 years ago","modified":"Updated 3 years ago"},"absolute_dates":{"created":"Posted on March 22, 2023","modified":"Updated on March 24, 2023"},"absolute_dates_time":{"created":"Posted on March 22, 2023 1:27 pm","modified":"Updated on March 24, 2023 12:00 pm"},"featured_img_caption":"","series_order":"","_links":{"self":[{"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/posts\/11705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=11705"}],"version-history":[{"count":18,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/posts\/11705\/revisions"}],"predecessor-version":[{"id":11809,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/posts\/11705\/revisions\/11809"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/media\/11715"}],"wp:attachment":[{"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=11705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=11705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icdsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=11705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}