< Summary - Kestrun — Combined Coverage

Information
Class: Public.Service.Add-KrAntiforgery
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrAntiforgery.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 90
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

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrAntiforgery.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 Cookie
 12        The cookie builder to use for the Antiforgery service.
 13    .PARAMETER FormFieldName
 14        The name of the form field to use for the Antiforgery token.
 15    .PARAMETER HeaderName
 16        The name of the header to use for the Antiforgery token.
 17    .PARAMETER SuppressXFrameOptionsHeader
 18        If specified, the X-Frame-Options header will not be added to responses.
 19    .PARAMETER PassThru
 20        If specified, the cmdlet will return the modified server instance after adding the Antiforgery service.
 21    .EXAMPLE
 22        $server | Add-KrAntiforgery -Cookie $cookieBuilder -FormField '__RequestVerificationToken' -HeaderName 'X-CSRF-T
 23        This example adds an Antiforgery service to the server with a custom cookie builder, form field name, and header
 24    .EXAMPLE
 25        $server | Add-KrAntiforgery -Options $options
 26        This example adds an Antiforgery service to the server using the specified Antiforgery options.
 27    .LINK
 28        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.antiforgery.antiforgeryoptions?view=aspnetcore
 29#>
 30function Add-KrAntiforgery {
 31    [KestrunRuntimeApi('Definition')]
 32    [CmdletBinding(defaultParameterSetName = 'Items')]
 33    [OutputType([Kestrun.Hosting.KestrunHost])]
 34    param(
 35        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 36        [Kestrun.Hosting.KestrunHost]$Server,
 37
 38        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 39        [Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions]$Options,
 40
 41        [Parameter(ParameterSetName = 'Items')]
 42        [Microsoft.AspNetCore.Http.CookieBuilder]$Cookie = $null,
 43
 44        [Parameter(ParameterSetName = 'Items')]
 45        [string]$FormFieldName,
 46
 47        [Parameter(ParameterSetName = 'Items')]
 48        [string]$HeaderName,
 49
 50        [Parameter(ParameterSetName = 'Items')]
 51        [switch]$SuppressXFrameOptionsHeader,
 52
 53        [Parameter()]
 54        [switch]$PassThru
 55    )
 56    begin {
 57        # Ensure the server instance is resolved
 058        $Server = Resolve-KestrunServer -Server $Server
 059        if ($null -eq $Server) {
 060            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 61        }
 62    }
 63    process {
 064        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 065            $Options = [Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions]::new()
 066            if ($null -ne $Cookie) {
 067                $Options.Cookie = $Cookie
 68            }
 069            if (-not [string]::IsNullOrEmpty($FormFieldName)) {
 070                $Options.FormFieldName = $FormFieldName
 71            }
 072            if (-not [string]::IsNullOrEmpty($HeaderName)) {
 073                $Options.HeaderName = $HeaderName
 74            }
 075            if ($SuppressXFrameOptionsHeader.IsPresent) {
 076                $Options.SuppressXFrameOptionsHeader = $true
 77            }
 78        }
 79
 80        # Add the Antiforgery service to the server
 081        [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddAntiforgery($Server, $Options) | Out-Null
 82
 083        if ($PassThru.IsPresent) {
 84            # if the PassThru switch is specified, return the server instance
 85            # Return the modified server instance
 086            return $Server
 87        }
 88    }
 89}
 90

Methods/Properties

Add-KrAntiforgery()