< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrHostFiltering
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrHostFiltering.ps1
Tag: Kestrun/Kestrun@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 11
Coverable lines: 11
Total lines: 77
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 12/01/2025 - 20:55:19 Line coverage: 0% (0/13) Total lines: 95 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa05/09/2026 - 21:51:36 Line coverage: 0% (0/11) Total lines: 77 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrHostFiltering.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds Host Filtering middleware to a Kestrun server instance.
 4    .DESCRIPTION
 5        This cmdlet adds the Host Filtering middleware to a Kestrun server instance, allowing you to configure host filt
 6    .PARAMETER Options
 7        A Microsoft.AspNetCore.HostFiltering.HostFilteringOptions object that defines the configuration options for the 
 8        If this parameter is provided, it takes precedence over the individual configuration parameters (AllowedHosts, A
 9    .PARAMETER AllowedHosts
 10        The hosts headers that are allowed to access this site. At least one value is required.
 11        Port numbers must be excluded.
 12        A top level wildcard "*" allows all non-empty hosts.
 13        Subdomain wildcards are permitted. E.g. "*.example.com" matches subdomains like foo.example.com, but not the par
 14        Unicode host names are allowed but will be converted to punycode for matching.
 15        IPv6 addresses must include their bounding brackets and be in their normalized form.
 16    .PARAMETER NotAllowEmptyHosts
 17        A switch indicating whether requests with an empty Host header should be allowed.
 18        If this switch is present, requests with an empty Host header will be rejected.
 19    .PARAMETER ExcludeFailureMessage
 20        A switch indicating whether to exclude the failure message in the response when a request is rejected due to hos
 21        If this switch is present, the failure message will be excluded from the response.
 22    .EXAMPLE
 23        Add-KrHostFiltering -AllowedHosts "example.com", "www.example.com" -PassThru
 24        This example adds Host Filtering middleware to the current Kestrun server instance, allowing only requests with 
 25        matching "example.com" or "www.example.com", and returns the modified server instance.
 26    .EXAMPLE
 27        $options = [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]::new()
 28        $options.AllowedHosts.Add("example.com")
 29        $options.AllowEmptyHosts = $true
 30        Add-KrHostFiltering -Options $options -PassThru
 31        This example creates a HostFilteringOptions object that allows requests with the Host header "example.com"
 32        and allows empty Host headers, then adds the Host Filtering middleware to the current Kestrun server instance an
 33    .NOTES
 34        This cmdlet is part of the Kestrun PowerShell module.
 35
 36 #>
 37function Add-KrHostFiltering {
 38    [KestrunRuntimeApi('Definition')]
 39    [CmdletBinding(defaultParameterSetName = 'Items')]
 40    param(
 41        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 42        [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]$Options,
 43
 44        [Parameter(ParameterSetName = 'Items')]
 45        [string[]] $AllowedHosts,
 46
 47        [Parameter(ParameterSetName = 'Items')]
 48        [switch] $NotAllowEmptyHosts,
 49
 50        [Parameter(ParameterSetName = 'Items')]
 51        [switch] $ExcludeFailureMessage
 52    )
 53    # Ensure the server instance is resolved
 054    $Server = Resolve-KestrunServer
 55
 056    if ($PSCmdlet.ParameterSetName -eq 'Items') {
 57        # Create options from individual parameters
 058        $Options = [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]::new()
 59
 060        if ($PSBoundParameters.ContainsKey('AllowedHosts')) {
 61            # Validate that at least one host is provided
 062            if ($AllowedHosts.Count -eq 0) {
 063                throw 'At least one AllowedHost must be specified when using the AllowedHosts parameter.'
 64            }
 065            foreach ($h in $AllowedHosts) {
 066                $Options.AllowedHosts.Add($h);
 67            }
 68        }
 69        # Validate the options
 070        $Options.AllowEmptyHosts = -not $NotAllowEmptyHosts.IsPresent
 071        $Options.IncludeFailureMessage = -not $ExcludeFailureMessage.IsPresent
 72    }
 73
 74    # Add the Host Filtering middleware
 075    [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddHostFiltering($Server, $Options) | Out-Null
 76}
 77

Methods/Properties

Add-KrHostFiltering()