| | 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 | | #> |
| | 30 | | function 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 |
| 0 | 58 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 59 | | if ($null -eq $Server) { |
| 0 | 60 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 61 | | } |
| | 62 | | } |
| | 63 | | process { |
| 0 | 64 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| 0 | 65 | | $Options = [Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions]::new() |
| 0 | 66 | | if ($null -ne $Cookie) { |
| 0 | 67 | | $Options.Cookie = $Cookie |
| | 68 | | } |
| 0 | 69 | | if (-not [string]::IsNullOrEmpty($FormFieldName)) { |
| 0 | 70 | | $Options.FormFieldName = $FormFieldName |
| | 71 | | } |
| 0 | 72 | | if (-not [string]::IsNullOrEmpty($HeaderName)) { |
| 0 | 73 | | $Options.HeaderName = $HeaderName |
| | 74 | | } |
| 0 | 75 | | if ($SuppressXFrameOptionsHeader.IsPresent) { |
| 0 | 76 | | $Options.SuppressXFrameOptionsHeader = $true |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| | 80 | | # Add the Antiforgery service to the server |
| 0 | 81 | | [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddAntiforgery($Server, $Options) | Out-Null |
| | 82 | |
|
| 0 | 83 | | if ($PassThru.IsPresent) { |
| | 84 | | # if the PassThru switch is specified, return the server instance |
| | 85 | | # Return the modified server instance |
| 0 | 86 | | return $Server |
| | 87 | | } |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|