< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.WebApi.ForwardedHeadersRegistrationExtensions
Assembly: ArturRios.Heimdall.WebApi
File(s): /home/ci/agent/workspace/heimdall-api-docs_main/app/src/Presentation/ArturRios.Heimdall.WebApi/Registration/ForwardedHeadersRegistrationExtensions.cs
Line coverage
47%
Covered lines: 8
Uncovered lines: 9
Coverable lines: 17
Total lines: 47
Line coverage: 47%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage
100%
Covered methods: 1
Fully covered methods: 0
Total methods: 1
Method coverage: 100%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddTrustedForwardedHeaders(...)50%6447.05%

File(s)

/home/ci/agent/workspace/heimdall-api-docs_main/app/src/Presentation/ArturRios.Heimdall.WebApi/Registration/ForwardedHeadersRegistrationExtensions.cs

#LineLine coverage
 1using ArturRios.Data.Relational.Core.DependencyInjection;
 2using ArturRios.Heimdall.WebApi.Security;
 3using ArturRios.Util.WebApi.Configuration;
 4using ArturRios.Util.WebApi.Security.Extensions;
 5using Serilog;
 6
 7namespace ArturRios.Heimdall.WebApi;
 8
 9public static class ForwardedHeadersRegistrationExtensions
 10{
 11    /// <summary>
 12    ///     Believes <c>X-Forwarded-For</c> and <c>X-Forwarded-Proto</c> from the proxies
 13    ///     <c>HEIMDALL_TRUSTED_PROXIES</c> names, and from no one else — see
 14    ///     <see cref="TrustedProxyOptions" />. Applied by <c>UseForwardedHeaders</c>, which runs
 15    ///     ahead of everything that reads the client address: the rate limiter, and the request log.
 16    /// </summary>
 17    public static IServiceCollection AddTrustedForwardedHeaders(this IServiceCollection services)
 18    {
 96619        var proxies = TrustedProxyOptions.FromEnvironment();
 20
 96621        if (proxies.InvalidEntries.Count > 0)
 22        {
 023            Log.Warning(
 024                "Ignoring unusable {Variable} entries {Entries}; they are neither an address nor a CIDR network",
 025                TrustedProxyOptions.ProxiesVariable, proxies.InvalidEntries);
 26        }
 27
 96628        if (!proxies.Configured)
 29        {
 96630            Log.Warning(
 96631                "No trusted proxy is configured ({Variable}); behind a reverse proxy every caller will " +
 96632                "be rate limited and logged under the proxy's address",
 96633                TrustedProxyOptions.ProxiesVariable);
 34
 96635            return services;
 36        }
 37
 038        Log.Information(
 039            "Trusting forwarded headers from {Proxies} and {Networks}",
 040            proxies.Proxies.Select(proxy => proxy.ToString()),
 041            proxies.Networks.Select(network => network.ToString()));
 42
 043        services.Configure<ForwardedHeadersOptions>(proxies.Apply);
 44
 045        return services;
 46    }
 47}