PoolByte

HTTP security headers: Configure CSP, HSTS and browser protections

· PoolByte Technology

HTTP security headers: Configure CSP, HSTS and browser protections

HTTP security headers are instructions that the server sends to the browser with each response, limiting how the page behaves. A header can prevent the page from opening within a frame on another site, force the browser to use HTTPS only, or determine from which source a script can be loaded. These topics do not by themselves close the gaps in the application; However, when an error occurs, it creates an additional layer that narrows the impact of the attack.

It is tempting to add HTTP security headers all at once with a ready-made block found on the internet. However, especially when Content-Security-Policy is set up incorrectly, the payment form, live support window or analytics tag may silently stop working. Therefore, it is necessary to first measure the current situation and then add the titles one by one and by testing.

Check out the existing answer first

You can see the response headers when you open the Network tab in the browser's developer tools and select the first HTML request of the page. The curl -I https://alanadiniz.com command on the command line also gives the same information. Take note of which headers are already included: the hosting infrastructure, CDN, or a security plugin may be adding some of these themselves.

Having the same header with different values from two different layers leads to behavior that is difficult to predict. MDN's HTTP Observatory tool also lists missing headers and suggested values, providing a good starting checkpoint.

Start with low-risk headlines

Some of the HTTP security headers rarely affect the operation of the site and are suitable as a first step:

  • X-Content-Type-Options: nosniff prevents the browser from guessing the file type and ensures that the content type reported by the server is respected.
  • Referrer-Policy: strict-origin-when-cross-origin ensures that only domain name information is shared instead of the full address when moving to other sites. It is important for pages that contain an order number or search term in the address.
  • Permissions-Policy is used to turn off features such as camera, microphone and location on sites that do not use them. For example camera=(), microphone=(), geolocation=().

Once you've added these three, checking the forms, checkout step, and embedded content once is enough in most cases.

Limit opening within frame

In clickbait (clickjacking) attacks, your site is placed on another page in an invisible frame and the user unknowingly clicks a button. The old way to avoid this is with the X-Frame-Options: SAMEORIGIN header. The current method is the frame-ancestors 'self' directive in CSP. If your site needs to be displayed via iframe on a partner's page, you must explicitly list that domain name.

Turn on HSTS knowing its lasting impact

The Strict-Transport-Security header tells the browser to connect to your site only over HTTPS for a certain period of time. So even if someone types the address with http://, the browser switches to a secure connection. Before adding this header, make sure the certificate is valid on the main domain and the subdomains used. Our HTTPS and SSL certificate guide explains this check step by step.

Because HSTS is remembered in the browser, it takes time to undo when a mistake is made. A safer approach is to start with a short max-age value and gradually extend the time if no problems occur. Only add the includeSubDomains option if all your subdomains are HTTPS ready; Otherwise, a test or panel address without a certificate may become inaccessible. Entering the preload list of browsers is an even more difficult commitment to return and should be evaluated separately.

Install Content-Security-Policy with report mode

CSP defines from which sources the page can load scripts, styles, images, fonts and links. Even if an attacker manages to inject malicious code into the page, it may be blocked from executing from an unauthorized source. Provides strong protection; But this is the title that is most at risk of breaking.

For a safe start, first use the Content-Security-Policy-Report-Only header. In this mode, the browser does not block anything, it only shows resources that do not comply with the policy in the console or reports them to the address you define. Take a look at the intrusions over a period of time and list the resources actually used: payment provider, analytics and tag manager, map or video embeds, font provider, etc.

Once the list is clear, put the policy in enforcement mode. Allowing inline scripts and the use of eval greatly weakens the protection provided by CSP; If possible, move these scripts to separate files or use the nonce or hash method. This conversion usually requires changes to the theme or app code; Trying in a staging environment does not put the live site at risk.

Manage titles in one place

In hosting environments using Apache or LiteSpeed, HTTP security headers are usually defined by lines added to the .htaccess file. If your application generates its own headers, do not define the same header in two places; Choose a single location as the primary source. An example starting block might look like this:

<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
  Header always set X-Frame-Options "SAMEORIGIN"
</IfModule>

After each change, retry the site's main flows: login, cart, payment and contact form. Also check if any new errors appear in the browser console.

Frequently asked questions

Do HTTP security headers affect SEO?

It has not been documented as a direct ranking signal. However, consistent use of HTTPS and error-free operation of pages are important for user experience. If an incorrectly installed CSP corrupts the analytics tag or content loading, it can negatively impact your metrics and the appearance of the page.

Is using a WordPress plugin enough?

Plugins allow you to easily add titles; but you need to check if the same headers are also added by the server or CDN. When choosing the plugin, also follow the update and access steps in the WordPress security checklist.

Official sources