< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 13
Coverable lines: 13
Total lines: 95
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 10/17/2025 - 15:48:30 Line coverage: 0% (0/13) Total lines: 95 Tag: Kestrun/Kestrun@b8199aff869a847b75e185d0527ba45e04a43d86

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 Server
 7        The Kestrun server instance to which the Host Filtering middleware will be added. If not specified, the cmdlet w
 8    .PARAMETER Options
 9        A Microsoft.AspNetCore.HostFiltering.HostFilteringOptions object that defines the configuration options for the 
 10        If this parameter is provided, it takes precedence over the individual configuration parameters (AllowedHosts, A
 11    .PARAMETER AllowedHosts
 12        The hosts headers that are allowed to access this site. At least one value is required.
 13        Port numbers must be excluded.
 14        A top level wildcard "*" allows all non-empty hosts.
 15        Subdomain wildcards are permitted. E.g. "*.example.com" matches subdomains like foo.example.com, but not the par
 16        Unicode host names are allowed but will be converted to punycode for matching.
 17        IPv6 addresses must include their bounding brackets and be in their normalized form.
 18    .PARAMETER NotAllowEmptyHosts
 19        A switch indicating whether requests with an empty Host header should be allowed.
 20        If this switch is present, requests with an empty Host header will be rejected.
 21    .PARAMETER ExcludeFailureMessage
 22        A switch indicating whether to exclude the failure message in the response when a request is rejected due to hos
 23        If this switch is present, the failure message will be excluded from the response.
 24    .PARAMETER PassThru
 25        If this switch is specified, the cmdlet will return the modified Kestrun server instance
 26        after adding the Host Filtering middleware. This allows for further chaining of cmdlets or inspection of
 27        the server instance.
 28    .EXAMPLE
 29        Add-KrHostFiltering -AllowedHosts "example.com", "www.example.com" -PassThru
 30        This example adds Host Filtering middleware to the current Kestrun server instance, allowing only requests with 
 31        matching "example.com" or "www.example.com", and returns the modified server instance.
 32    .EXAMPLE
 33        $options = [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]::new()
 34        $options.AllowedHosts.Add("example.com")
 35        $options.AllowEmptyHosts = $true
 36        Add-KrHostFiltering -Options $options -PassThru
 37        This example creates a HostFilteringOptions object that allows requests with the Host header "example.com"
 38        and allows empty Host headers, then adds the Host Filtering middleware to the current Kestrun server instance an
 39    .NOTES
 40        This cmdlet is part of the Kestrun PowerShell module.
 41
 42 #>
 43function Add-KrHostFiltering {
 44    [KestrunRuntimeApi('Definition')]
 45    [CmdletBinding(defaultParameterSetName = 'Items')]
 46    [OutputType([Kestrun.Hosting.KestrunHost])]
 47    param(
 48        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 49        [Kestrun.Hosting.KestrunHost]$Server,
 50
 51        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 52        [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]$Options,
 53
 54        [Parameter(ParameterSetName = 'Items')]
 55        [string[]] $AllowedHosts,
 56        [Parameter(ParameterSetName = 'Items')]
 57        [switch] $NotAllowEmptyHosts,
 58        [Parameter(ParameterSetName = 'Items')]
 59        [switch] $ExcludeFailureMessage,
 60        [Parameter()]
 61        [switch]$PassThru
 62    )
 63    begin {
 64        # Ensure the server instance is resolved
 065        $Server = Resolve-KestrunServer -Server $Server
 66    }
 67    process {
 068        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 69            # Create options from individual parameters
 070            $Options = [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]::new()
 71
 072            if ($PSBoundParameters.ContainsKey('AllowedHosts')) {
 73                # Validate that at least one host is provided
 074                if ($AllowedHosts.Count -eq 0) {
 075                    throw 'At least one AllowedHost must be specified when using the AllowedHosts parameter.'
 76                }
 077                foreach ($h in $AllowedHosts) {
 078                    $Options.AllowedHosts.Add($h);
 79                }
 80            }
 81            # Validate the options
 082            $Options.AllowEmptyHosts = -not $NotAllowEmptyHosts.IsPresent
 083            $Options.IncludeFailureMessage = -not $ExcludeFailureMessage.IsPresent
 84        }
 85
 86        # Add the Host Filtering middleware
 087        [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddHostFiltering($Server, $Options) | Out-Null
 88
 089        if ($PassThru.IsPresent) {
 90            # if the PassThru switch is specified, return the modified server instance
 091            return $Server
 92        }
 93    }
 94}
 95

Methods/Properties

Add-KrHostFiltering()