< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrHsts
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrHsts.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 115
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/13/2025 - 16:52:37 Line coverage: 0% (0/14) Total lines: 115 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

Method
Add-KrHsts()

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds HTTP Strict Transport Security (HSTS) middleware to a Kestrun server instance.
 4    .DESCRIPTION
 5        The Add-KrHsts cmdlet configures HTTP Strict Transport Security (HSTS)
 6        for a Kestrun server instance. HSTS is a web security policy mechanism that helps
 7        to protect websites against protocol downgrade attacks and cookie hijacking.
 8        It allows web servers to declare that web browsers (or other complying user agents)
 9        should only interact with it using secure HTTPS connections, and never via the insecure HTTP protocol.
 10    .PARAMETER Server
 11        The Kestrun server instance to which the HSTS middleware will be added.
 12        If not specified, the cmdlet will attempt to use the current server context.
 13    .PARAMETER Options
 14        A Microsoft.AspNetCore.HttpsPolicy.HstsOptions object that defines the configuration options for
 15        the HSTS middleware. If this parameter is provided, it takes precedence over the individual configuration
 16        parameters (MaxAgeDays, IncludeSubDomains, Preload, ExcludedHosts).
 17    .PARAMETER MaxAgeDays
 18        The maximum duration (in days) that the browser should remember that a site is only to be accessed using HTTPS.
 19        The default value is 30 days.
 20    .PARAMETER IncludeSubDomains
 21        A switch indicating whether the HSTS policy should also apply to all subdomains of the site.
 22        If this switch is present, the IncludeSubDomains directive will be included in the HSTS header.
 23    .PARAMETER Preload
 24        A switch indicating whether the site should be included in browsers' HSTS preload list.
 25        If this switch is present, the Preload directive will be included in the HSTS header.
 26    .PARAMETER ExcludedHosts
 27        An array of hostnames that should be excluded from the HSTS policy. These hosts will not receive the HSTS header
 28    .PARAMETER AllowInDevelopment
 29        A switch that allows HSTS to work in development environments by clearing the default excluded hosts.
 30        By default, ASP.NET Core excludes localhost and development hosts from HSTS for security.
 31        Use this switch to enable HSTS for testing and development scenarios.
 32    .PARAMETER PassThru
 33        If this switch is specified, the cmdlet will return the modified Kestrun server instance
 34        after adding the HSTS middleware. This allows for further chaining of cmdlets or inspection of
 35        the server instance.
 36    .EXAMPLE
 37        Add-KrHsts -MaxAgeDays 60 -IncludeSubDomains -Preload -PassThru
 38        This example adds HSTS middleware to the current Kestrun server instance with a max age of 60 days,
 39        includes subdomains, enables preload, and returns the modified server instance.
 40    .EXAMPLE
 41        Add-KrHsts -MaxAgeDays 30 -IncludeSubDomains -Preload -AllowInDevelopment
 42        This example enables HSTS for development/testing by clearing default excluded hosts.
 43        Useful for testing HSTS behavior in non-production environments.
 44    .EXAMPLE
 45        $options = [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]::new()
 46        $options.MaxAge = [TimeSpan]::FromDays(90)
 47        $options.IncludeSubDomains = $true
 48        Add-KrHsts -Options $options -PassThru
 49        This example creates a HstsOptions object with a max age of 90 days and includes subdomains,
 50        then adds the HSTS middleware to the current Kestrun server instance and returns the modified server instance.
 51    .NOTES
 52        This cmdlet is part of the Kestrun PowerShell module.
 53 #>
 54function Add-KrHsts {
 55    [KestrunRuntimeApi('Definition')]
 56    [CmdletBinding(defaultParameterSetName = 'Items')]
 57    [OutputType([Kestrun.Hosting.KestrunHost])]
 58    param(
 59        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 60        [Kestrun.Hosting.KestrunHost]$Server,
 61
 62        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 63        [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]$Options,
 64
 65        [Parameter(ParameterSetName = 'Items')]
 66        [ValidateRange(1, [int]::MaxValue)]
 67        [int] $MaxAgeDays = 30,
 68        [Parameter(ParameterSetName = 'Items')]
 69        [switch] $IncludeSubDomains,
 70        [Parameter(ParameterSetName = 'Items')]
 71        [switch] $Preload,
 72        [Parameter(ParameterSetName = 'Items')]
 73        [string[]] $ExcludedHosts,
 74        [Parameter(ParameterSetName = 'Items')]
 75        [switch] $AllowInDevelopment,
 76        [Parameter()]
 77        [switch]$PassThru
 78    )
 79    begin {
 80        # Ensure the server instance is resolved
 081        $Server = Resolve-KestrunServer -Server $Server
 82    }
 83    process {
 084        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 85            # Create options from individual parameters
 086            $Options = [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]::new()
 87            # Set default values
 088            $Options.MaxAge = [TimeSpan]::FromDays($MaxAgeDays)
 89
 090            if ($PSBoundParameters.ContainsKey('IncludeSubDomains')) { $Options.IncludeSubDomains = $IncludeSubDomains.I
 091            if ($PSBoundParameters.ContainsKey('Preload')) { $Options.Preload = $Preload.IsPresent }
 92
 93            # Handle AllowInDevelopment switch - clears default excluded hosts first
 094            if ($AllowInDevelopment.IsPresent) {
 095                $Options.ExcludedHosts.Clear()
 96            }
 97
 98            # Add any explicitly specified excluded hosts
 099            if ($PSBoundParameters.ContainsKey('ExcludedHosts')) {
 0100                foreach ($h in $ExcludedHosts) {
 0101                    $Options.ExcludedHosts.Add($h);
 102                }
 103            }
 104        }
 105
 106        # Add the HTTPS redirection middleware
 0107        [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddHsts($Server, $Options) | Out-Null
 108
 0109        if ($PassThru.IsPresent) {
 110            # if the PassThru switch is specified, return the modified server instance
 0111            return $Server
 112        }
 113    }
 114}
 115

Methods/Properties

Add-KrHsts()