• Stars
    star
    276
  • Rank 148,635 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Inject OWASP recommended HTTP Headers for increased security in a single line

OwaspHeaders.Core

A collection of ASP.NET Core middleware classes designed to increase web application security by adopting the recommended OWASP settings.

Build Status Release Status License used Changelog Code of Conduct
Build status Release License: MIT changelog Code of Conduct.md

Please note: this middleware DOES NOT SUPPORT BLAZOR OR WEBASSEMBLY APPLICATIONS. This is because setting up secure HTTP headers in a WebAssembly context is a non-trivial task.

Tools Required to Build This Repo

  • .NET vLatest
  • an IDE (VS Code, Rider, or Visual Studio)
  • dotnet-format global tool.

That's it.

Pull Requests

PRs Welcome

Pull requests are welcome, but please take a moment to read the Code of Conduct before submitting them or commenting on any work in this repo.

Also please make sure to run dotnet format OwaspHeaders.Core.sln in the root of the repo before submitting a PR. This repo uses an editorconfig file to enforce certain formatting rules on this repo. Any PRs which don't adhere to these formatting rules will fail a PR action (for checking the code against the rules). So to save time, please run dotnet format OwaspHeaders.Core.sln ahead of submitting your PR.

Getting Started

Assuming that you have an ASP .NET Core project, add the NuGet package:

dotnet add package OwaspHeaders.Core

Alter the Startup (pre .NET 6) or program (post .NET 6) class to include the following:

app.UseSecureHeadersMiddleware();

This will add a number of default HTTP headers to all responses from your server component.

The following is an example of the response headers from version 6.0.2 (taken on May 15th, 2023)

cache-control: max-age=31536000, private
strict-transport-security: max-age=63072000;includeSubDomains
x-frame-options: DENY
x-xss-protection: 0
x-content-type-options: nosniff
content-security-policy: script-src 'self';object-src 'self';block-all-mixed-content;upgrade-insecure-requests;
x-permitted-cross-domain-policies: none;
referrer-policy: no-referrer

Please note: The above example contains only the headers added by the Middleware.

Secure Headers

The SecureHeadersMiddleware is used to inject the HTTP headers recommended by the OWASP Secure Headers project into all responses generated by the ASP.NET Core pipeline.

Listing and commenting on the default values that this middleware provides is out of scope for this readme. Please note that you will need to read through the above link to the Secure Headers Project in order to understand what these headers do, and the affect their presence will have on your applications when running in a web browser.

Configuration

This Middleware uses the builder pattern to set up the header information, which is a compile time dependency.

In your Startup class (or Program.cs for .NET 6 onwards):

app.UseSecureHeadersMiddleware(RealisticContentSecurityPolicyGenerators.GenerateOwaspHomePageCsp());

This will use the default configuration for the OwaspHeaders.Core middleware. The method (found in /src/Extensions/SecureHeadersMiddlewareExtensions.cs) looks like this:

public static SecureHeadersMiddlewareConfiguration BuildDefaultConfiguration()
{
return SecureHeadersMiddlewareBuilder
.CreateBuilder()
.UseHsts()
.UseXFrameOptions()
.UseContentTypeOptions()
.UseContentDefaultSecurityPolicy()
.UsePermittedCrossDomainPolicies()
.UseReferrerPolicy()
.UseCacheControl()
.RemovePoweredByHeader()
.UseXssProtection()
.UseCrossOriginResourcePolicy()
.Build();
}

Custom Configuration

In order to use a custom configuration, follow the same pattern (perhaps creating your own extension method to encapsulate it):

public static SecureHeadersMiddlewareConfiguration CustomConfiguration()
{
    return SecureHeadersMiddlewareBuilder
        .CreateBuilder()
        .UseHsts(1200, false)
        .UseContentDefaultSecurityPolicy()
        .UsePermittedCrossDomainPolicies(XPermittedCrossDomainOptionValue.masterOnly)
        .UseReferrerPolicy(ReferrerPolicyOptions.sameOrigin)
        .Build();
}

Then consume it in the following manner:

app.UseSecureHeadersMiddleware(CustomSecureHeaderExtensions.CustomConfiguration());

Testing the Middleware

An example ASP .NET Core application - with the middleware installed - is provided as part of this repo (see the code in the example directory). As such, you can run this example application to see the middleware in use.

Or you could add the middleware to an existing application and run through the following Run the application, request one of the pages that it serves and view the headers for the page.

This can be done in Google Chrome, using the Dev tools and checking the network tab.

secure headers shown in network tab

Shown above in the Response Headers section of the Values response.

Server Header: A Warning

The default configuration for this middleware removes the X-Powered-By header, as this can help malicious users to use targeted attacks for specific server infrastructure. However, since the Server header is added by the reverse proxy used when hosting an ASP .NET Core application, removing this header is out of scope for this middleware.

In order to remove this header, a web.config file is required, and the following should be added to it:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering removeServerHeader="true" />
        </security>
    </system.webServer>
</configuration>

The above XML is taken from this answer on ServerFault.

The web.config file will need to be copied to the server when the application is deployed.

More Repositories

1

OnionArch

A .NET Core demo application which uses the Onion Architecture
C#
102
star
2

dwCheckApi

An ASP.NET Core WebAPI project for all Discworld novels
C#
27
star
3

BugTracker

A basic bug tracker, implemented in ASP NET Core
C#
19
star
4

FullStackTemplate

A full stack ASP.NET Core template
C#
13
star
5

UmBlazor

An example of client side Blazor using Headless Umbraco
C#
11
star
6

PokeBlazor

A Blazor app for requesting data from the Pokemon API
HTML
8
star
7

NET-Core-Podast-Transcriptions

Transcriptions for all released episodes of The .NET Core Podcast - used for generating all show notes for the website
HTML
8
star
8

ClacksMiddleware

An ASP.NET Core middlware for injecting the GNU Terry Pratchett header into all HTTP responses
C#
7
star
9

Bro-As-A-Service

An extremely simple ReactJs Client and .NET Core Server application for giving Hi Fives
C#
5
star
10

LaTeXDocuments

A collectin of my documents prepared in LaTeX
TeX
4
star
11

RaffleIdGenerator

A .NET Core global tool for generating a pseudo-random winner id - for raffle draws
C#
2
star
12

Talks

Slide decks for talks and presentations that I have given
JavaScript
2
star
13

bookApi

An API and UI for a fake book search engine - built entirely in the open, on Twitch
C#
2
star
14

dwCheckUI

A collection of .NET Core MVVM applications which use the dwCheckApi project
TypeScript
2
star
15

Hi-n

Code listings for the Esoteric Language Hi\n
JavaScript
1
star
16

Narrativia

A blog written in ASP.NET Core and Angular
C#
1
star
17

NazeBesto

TeX
1
star
18

WDTV-Live-Meta-Gen

A small C# app to generate XML metadata for media on WDTV Live (and compatibles)
C#
1
star
19

Blog-Tutorials

A repository for full code listings used in tutorials on my .NET Core blog
C#
1
star
20

Wilf

A .NET Core MVC application for choosing a random TV show within a given TV series
C#
1
star
21

SmsFixer

A C# console application to remove duplicate sms text messages contained within a SMSBackupAndRestore XML file
C#
1
star
22

Wordpress-x-Docker-Talk

Code generated for my Wordpress x Docker talk
Shell
1
star