| | | 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 | | #> |
| | | 43 | | function 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 |
| | 0 | 65 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 66 | | } |
| | | 67 | | process { |
| | 0 | 68 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 69 | | # Create options from individual parameters |
| | 0 | 70 | | $Options = [Microsoft.AspNetCore.HostFiltering.HostFilteringOptions]::new() |
| | | 71 | | |
| | 0 | 72 | | if ($PSBoundParameters.ContainsKey('AllowedHosts')) { |
| | | 73 | | # Validate that at least one host is provided |
| | 0 | 74 | | if ($AllowedHosts.Count -eq 0) { |
| | 0 | 75 | | throw 'At least one AllowedHost must be specified when using the AllowedHosts parameter.' |
| | | 76 | | } |
| | 0 | 77 | | foreach ($h in $AllowedHosts) { |
| | 0 | 78 | | $Options.AllowedHosts.Add($h); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | # Validate the options |
| | 0 | 82 | | $Options.AllowEmptyHosts = -not $NotAllowEmptyHosts.IsPresent |
| | 0 | 83 | | $Options.IncludeFailureMessage = -not $ExcludeFailureMessage.IsPresent |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | # Add the Host Filtering middleware |
| | 0 | 87 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddHostFiltering($Server, $Options) | Out-Null |
| | | 88 | | |
| | 0 | 89 | | if ($PassThru.IsPresent) { |
| | | 90 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 91 | | return $Server |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |