🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

AspNetHeaderReplicator

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

AspNetHeaderReplicator

AspNetHeaderReplicator: Middleware for replicating HTTP request headers into response headers efficiently with the rules you determined.

0.9.1
Source
NuGet
Version published
Maintainers
1
Created
Source

AspNetHeaderReplicator

This is a simple library that exposes a middleware for ASP.NET Core applications that replicates headers from the request to the response with the ability to include or exclude specific headers.

Installation

AspNetHeaderReplicator is available as a NuGet package. You can install it using the NuGet Package Manager Console:

dotnet add package AspNetHeaderReplicator

Or with the nuget command:

nuget install AspNetHeaderReplicator

Usage

To use the middleware, you need to add it to the pipeline in the Configure method of your Startup class (Or you can use without the Startup class if you are using the generic host):

Default configuration

By default, the middleware will replicate and ignore the following headers:

Replicated headers

new[] { "X-", "My-", "Req-", "Trace-", "Debug-", "Verbose-" }

Ignored headers

new[] { "auth", "credential", "token", "pass", "secret", "hash", "cert" };

Example 01: Use default configuration

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator();
}

Example 02: Use default configuration AND add custom allowed headers

Following code snippet will allow headers starting with Allowed-Prefix to be replicated.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Allow headers starting with
        opt.AllowHeaderPrefix("Allowed-Prefix");
    });
}

Example 03: Use default configuration AND add custom ignored headers

Following code snippet will ignore headers containing ignored in their name.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Ignore headers containing
        opt.IgnoreHeaderSentence("ignored");
    });
}

Example 04: Clear default configuration AND add custom allowed headers with custom ignored headers

Following code snippet will clear all default settings and allow headers starting with X- and My- and ignore headers containing auth and credential.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Clear all default settings
        opt.ClearAll();

        // Allow headers starting with
        opt.AllowHeaderPrefix("X-");
        opt.AllowHeaderPrefix("My-");

        // Ignore headers containing
        opt.IgnoreHeaderSentence("auth");
        opt.IgnoreHeaderSentence("credential");
    });
}



Demo

You can create a new API project and apply the following changes...

Demo: Startup.cs

...
...
public void ConfigureServices(IServiceCollection services)
{
    services.AddHeaderReplicator(opt =>
    {
        // Clear all default settings
        opt.ClearAll();

        // Allow headers starting with
        opt.AllowHeaderPrefix("X-");
        opt.AllowHeaderPrefix("My-");

        // Ignore headers containing
        opt.IgnoreHeaderSentence("auth");
        opt.IgnoreHeaderSentence("credential");
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHeaderReplicator();
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.Map("/", context =>
        {
            return context.Response.WriteAsync("Please inspect the headers of this response.");
        });
    });
}
...
...

Demo: Test the API

Run the API and make a request to the root URL. You can use curl or any other tool to inspect the headers of the response.

curl --location 'http://localhost:5278/' \
    --header 'X-Burak: Burak Tungut' \
    --header 'My-Some: 123' \
    --header 'Some-Auth-Key: somesecrets' \
    --header 'a-header-credential-demo: somesecrets'

request headers

As you can see, headers are being executing without case sensitivity.

Response headers will be like below:

response headers

As you can see, headers are being replicated and/or ignored according to the configuration.



License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Owner : Burak Tungut

Any contributions are welcome! Please post your issues and pull requests to the repository.

Regards...

Keywords

asp.net

FAQs

Package last updated on 25 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts