< Summary - Kestrun — Combined Coverage

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

Metrics

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds an Antiforgery service to the server.
 4    .DESCRIPTION
 5        This cmdlet allows you to configure the Antiforgery service for the Kestrun server.
 6        It can be used to protect against Cross-Site Request Forgery (CSRF) attacks by generating and validating antifor
 7    .PARAMETER Server
 8        The Kestrun server instance to which the Antiforgery service will be added.
 9    .PARAMETER Options
 10        The Antiforgery options to configure the service.
 11    .PARAMETER CookieName
 12        The name of the cookie to use for the Antiforgery token. Default is ".Kestrun.AntiXSRF".
 13    .PARAMETER FormFieldName
 14        The name of the form field to use for the Antiforgery token. If not specified, the default will be used.
 15    .PARAMETER HeaderName
 16        The name of the header to use for the Antiforgery token. Default is "X-CSRF-TOKEN".
 17    .PARAMETER SuppressXFrameOptionsHeader
 18        If specified, the X-Frame-Options header will not be added to responses.
 19    .PARAMETER SuppressReadingTokenFromFormBody
 20        If specified, the Antiforgery service will not read tokens from the form body. This option is only available in 
 21    .PARAMETER PassThru
 22        If specified, the cmdlet will return the modified server instance after adding the Antiforgery service.
 23    .EXAMPLE
 24        $server | Add-KrAntiforgeryMiddleware -Cookie $cookieBuilder -FormField '__RequestVerificationToken' -HeaderName
 25        This example adds an Antiforgery service to the server with a custom cookie builder, form field name, and header
 26    .EXAMPLE
 27        $server | Add-KrAntiforgeryMiddleware -Options $options
 28        This example adds an Antiforgery service to the server using the specified Antiforgery options.
 29    .LINK
 30        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.antiforgery.antiforgeryoptions?view=aspnetcore
 31#>
 32function Add-KrAntiforgeryMiddleware {
 33    [KestrunRuntimeApi('Definition')]
 34    [CmdletBinding(defaultParameterSetName = 'Items')]
 35    [OutputType([Kestrun.Hosting.KestrunHost])]
 36    param(
 37        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 38        [Kestrun.Hosting.KestrunHost]$Server,
 39
 40        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 41        [Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions]$Options,
 42
 43        [Parameter(ParameterSetName = 'Items')]
 44        [string]$FormFieldName,
 45
 46        [Parameter(ParameterSetName = 'Items')]
 47        [string]$CookieName = ".Kestrun.AntiXSRF",
 48
 49        [Parameter(ParameterSetName = 'Items')]
 50        [string]$HeaderName = "X-CSRF-TOKEN",
 51
 52        [Parameter(ParameterSetName = 'Items')]
 53        [switch]$SuppressXFrameOptionsHeader,
 54
 55        [Parameter(ParameterSetName = 'Items')]
 56        [switch]$SuppressReadingTokenFromFormBody,
 57
 58        [Parameter()]
 59        [switch]$PassThru
 60    )
 61    begin {
 62        # Ensure the server instance is resolved
 063        $Server = Resolve-KestrunServer -Server $Server
 64    }
 65    process {
 066        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 067            $Options = [Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions]::new()
 68
 69            # build default cookie
 070            $cookie = [Microsoft.AspNetCore.Http.CookieBuilder]::new()
 071            $cookie.Name = $CookieName
 072            $cookie.SameSite = [Microsoft.AspNetCore.Http.SameSiteMode]::Lax
 073            $cookie.HttpOnly = $true
 074            $cookie.SecurePolicy = [Microsoft.AspNetCore.Http.CookieSecurePolicy]::Always
 075            $cookie.Path = "/"
 76
 077            $Options.Cookie = $cookie
 78
 079            if (-not [string]::IsNullOrEmpty($FormFieldName)) {
 080                $Options.FormFieldName = $FormFieldName
 81            }
 082            if (-not [string]::IsNullOrEmpty($HeaderName)) {
 083                $Options.HeaderName = $HeaderName
 84            }
 085            if ($SuppressXFrameOptionsHeader.IsPresent) {
 086                $Options.SuppressXFrameOptionsHeader = $true
 87            }
 088            if (Test-KrCapability -Feature "SuppressReadingTokenFromFormBody") {
 089                if ($SuppressReadingTokenFromFormBody.IsPresent) {
 090                    $Options.SuppressReadingTokenFromFormBody = $true
 91                }
 92            }
 93        }
 94
 95        # Add the Antiforgery service to the server
 096        [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddAntiforgery($Server, $Options) | Out-Null
 97
 098        if ($PassThru.IsPresent) {
 99            # if the PassThru switch is specified, return the modified server instance
 0100            return $Server
 101        }
 102    }
 103}
 104

Methods/Properties

Add-KrAntiforgeryMiddleware()