Mark Stott
Nov 19, 2021
  2196
(5 votes)

Adding Secure Headers in Optimizely CMS 12

I have previously blogged about this in a pure .NET 5.0 context on my own blog here: Secure Headers in .NET 5.0. In this version of this post I have adapted the solution to work with an Optimizely CMS Build.

I have been building .NET 5.0 websites for personal projects as well as reviewing the move from .NET 4.8 to .NET 5.0 by Optimizely and seeing the evolution of the CMS solution.  In any website build it is best practice to remove any headers that your website may produce which expose the underlying technology stack and version.  This is known as information leakage and provides malicious actors with information that allows them to understand the security flaws in the hosting technologies utilized by your website.  It is also best practice to provide headers which instruct the user's browser as to how your website can use third parties and be used by third parties in order to offer the best protection for the user.

.NET 5.0 and .NET Core 3.1 follow a common pattern in how you build websites.  Web.config is meant to be a thing of the past with configuration of the site moving to appsettings.json and code.  A good place to add security headers to your requests is to create a security header middleware. However the routing of the CMS content does not pass through this middleware so I've fallen back to Action Attributes:

namespace MyProject.Features.Security
{
    using Microsoft.AspNetCore.Mvc.Filters;

    public class SecurityHeaderActionAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            base.OnResultExecuting(context);

            context.HttpContext.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
            context.HttpContext.Response.Headers.Add("X-Xss-Protection", "1; mode=block");
            context.HttpContext.Response.Headers.Add("X-Content-Type-Options", "nosniff");
            context.HttpContext.Response.Headers.Add("Referrer-Policy", "no-referrer");

            context.HttpContext.Response.Headers.Add("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data: https:; frame-src 'self' https://www.youtube-nocookie.com/;");
        }
    }
}

I did find that I couldn't remove the server and x-powered-by headers using this method.  As it turns out, these are added by the hosting technology, in this case IIS.  The only way to remove these headers was to add a minimal web.config file to the web solution that contained just enough configuration to remove these headers.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <system.webServer>
       <httpProtocol>
           <customHeaders>
               <remove name="X-Powered-By" />
           </customHeaders>
       </httpProtocol>
       <security>
           <requestFiltering removeServerHeader="true" />
       </security>
   </system.webServer>
</configuration>

I spent a good while looking for this solution, almost every solution I could find was based on hosting within Kestrel rather than IIS.  If you are hosting with Kestrel, then you can remove the server header by updating your CreateHostBuilder method in program.cs to set the options for Kestrel to exclude the server header.

public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
{
   return Host.CreateDefaultBuilder(args)
              .ConfigureCmsDefaults()
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  webBuilder.UseStartup<Startup>();
                  webBuilder.UseKestrel(options => { options.AddServerHeader = false; });
              });
}
Nov 19, 2021

Comments

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024