< 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@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 98
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/14) Total lines: 115 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa05/09/2026 - 21:51:36 Line coverage: 0% (0/12) Total lines: 98 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

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 Options
 11        A Microsoft.AspNetCore.HttpsPolicy.HstsOptions object that defines the configuration options for
 12        the HSTS middleware. If this parameter is provided, it takes precedence over the individual configuration
 13        parameters (MaxAgeDays, IncludeSubDomains, Preload, ExcludedHosts).
 14    .PARAMETER MaxAgeDays
 15        The maximum duration (in days) that the browser should remember that a site is only to be accessed using HTTPS.
 16        The default value is 30 days.
 17    .PARAMETER IncludeSubDomains
 18        A switch indicating whether the HSTS policy should also apply to all subdomains of the site.
 19        If this switch is present, the IncludeSubDomains directive will be included in the HSTS header.
 20    .PARAMETER Preload
 21        A switch indicating whether the site should be included in browsers' HSTS preload list.
 22        If this switch is present, the Preload directive will be included in the HSTS header.
 23    .PARAMETER ExcludedHosts
 24        An array of hostnames that should be excluded from the HSTS policy. These hosts will not receive the HSTS header
 25    .PARAMETER AllowInDevelopment
 26        A switch that allows HSTS to work in development environments by clearing the default excluded hosts.
 27        By default, ASP.NET Core excludes localhost and development hosts from HSTS for security.
 28        Use this switch to enable HSTS for testing and development scenarios.
 29    .EXAMPLE
 30        Add-KrHsts -MaxAgeDays 60 -IncludeSubDomains -Preload -PassThru
 31        This example adds HSTS middleware to the current Kestrun server instance with a max age of 60 days,
 32        includes subdomains, enables preload, and returns the modified server instance.
 33    .EXAMPLE
 34        Add-KrHsts -MaxAgeDays 30 -IncludeSubDomains -Preload -AllowInDevelopment
 35        This example enables HSTS for development/testing by clearing default excluded hosts.
 36        Useful for testing HSTS behavior in non-production environments.
 37    .EXAMPLE
 38        $options = [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]::new()
 39        $options.MaxAge = [TimeSpan]::FromDays(90)
 40        $options.IncludeSubDomains = $true
 41        Add-KrHsts -Options $options -PassThru
 42        This example creates a HstsOptions object with a max age of 90 days and includes subdomains,
 43        then adds the HSTS middleware to the current Kestrun server instance and returns the modified server instance.
 44    .NOTES
 45        This cmdlet is part of the Kestrun PowerShell module.
 46 #>
 47function Add-KrHsts {
 48    [KestrunRuntimeApi('Definition')]
 49    [CmdletBinding(defaultParameterSetName = 'Items')]
 50    param(
 51        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 52        [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]$Options,
 53
 54        [Parameter(ParameterSetName = 'Items')]
 55        [ValidateRange(1, [int]::MaxValue)]
 56        [int] $MaxAgeDays = 30,
 57
 58        [Parameter(ParameterSetName = 'Items')]
 59        [switch] $IncludeSubDomains,
 60
 61        [Parameter(ParameterSetName = 'Items')]
 62        [switch] $Preload,
 63
 64        [Parameter(ParameterSetName = 'Items')]
 65        [string[]] $ExcludedHosts,
 66
 67        [Parameter(ParameterSetName = 'Items')]
 68        [switch] $AllowInDevelopment
 69    )
 70    # Ensure the server instance is resolved
 071    $Server = Resolve-KestrunServer
 72
 073    if ($PSCmdlet.ParameterSetName -eq 'Items') {
 74        # Create options from individual parameters
 075        $Options = [Microsoft.AspNetCore.HttpsPolicy.HstsOptions]::new()
 76        # Set default values
 077        $Options.MaxAge = [TimeSpan]::FromDays($MaxAgeDays)
 78
 079        if ($PSBoundParameters.ContainsKey('IncludeSubDomains')) { $Options.IncludeSubDomains = $IncludeSubDomains.IsPre
 080        if ($PSBoundParameters.ContainsKey('Preload')) { $Options.Preload = $Preload.IsPresent }
 81
 82        # Handle AllowInDevelopment switch - clears default excluded hosts first
 083        if ($AllowInDevelopment.IsPresent) {
 084            $Options.ExcludedHosts.Clear()
 85        }
 86
 87        # Add any explicitly specified excluded hosts
 088        if ($PSBoundParameters.ContainsKey('ExcludedHosts')) {
 089            foreach ($h in $ExcludedHosts) {
 090                $Options.ExcludedHosts.Add($h);
 91            }
 92        }
 93    }
 94
 95    # Add the HTTPS redirection middleware
 096    [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddHsts($Server, $Options) | Out-Null
 97}
 98

Methods/Properties

Add-KrHsts()