8000 Extend -SkipHeaderValidation to include -UserAgent to support non-sta… · PowerShell/PowerShell@fc77c79 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit fc77c79

Browse files
markekrausTravisEz13
authored andcommitted
Extend -SkipHeaderValidation to include -UserAgent to support non-standard User-Agent headers (#4479)
1 parent 879b7da commit fc77c79

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,15 @@ internal virtual HttpRequestMessage GetRequest(Uri uri, bool stripAuthorization)
283283
}
284284
else
285285
{
286-
request.Headers.Add(HttpKnownHeaderNames.UserAgent, WebSession.UserAgent);
286+
if (SkipHeaderValidation)
287+
{
288+
request.Headers.TryAddWithoutValidation(HttpKnownHeaderNames.UserAgent, WebSession.UserAgent);
289+
}
290+
else
291+
{
292+
request.Headers.Add(HttpKnownHeaderNames.UserAgent, WebSession.UserAgent);
293+
}
294+
287295
}
288296

289297
// Set 'Keep-Alive' to false. This means set the Connection to 'Close'.

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,50 @@ function ExecuteRequestWithCustomHeaders
227227
return $result
228228
}
229229

230+
# This function calls either Invoke-WebRequest or Invoke-RestMethod with the given uri
231+
# using the custom UserAgent and the optional SkipHeaderValidation switch.
232+
function ExecuteRequestWithCustomUserAgent {
233+
param (
234+
[Parameter(Mandatory)]
235+
[string]
236+
$Uri,
237+
238+
[ValidateSet('Invoke-WebRequest', 'Invoke-RestMethod')]
239+
[string] $Cmdlet = 'Invoke-WebRequest',
240+
241+
[Parameter(Mandatory)]
242+
[ValidateNotNull()]
243+
[string] $UserAgent,
244+
245+
[switch] $SkipHeaderValidation
246+
)
247+
$result = [PSObject]@{Output = $null; Error = $null; Content = $null}
248+
249+
try {
250+
$Params = @{
251+
Uri = $Uri
252+
TimeoutSec = 5
253+
UserAgent = $UserAgent
254+
SkipHeaderValidation = $SkipHeaderValidation.IsPresent
255+
}
256+
if ($Cmdlet -eq 'Invoke-WebRequest') {
257+
$result.Output = Invoke-WebRequest @Params
258+
$result.Content = $result.Output.Content | ConvertFrom-Json
259+
}
260+
else {
261+
$result.Output = Invoke-RestMethod @Params
262+
# NOTE: $result.Output should already be a PSObject (Invoke-RestMethod converts the returned json automatically)
263+
# so simply reference $result.Output
264+
$result.Content = $result.Output
265+
}
266+
}
267+
catch {
268+
$result.Error = $_
269+
}
270+
271+
return $result
272+
}
273+
230274
<#
231275
Defines the list of redirect codes to test as well as the
232276
expected Method when the redirection is handled.
@@ -732,6 +776,31 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" {
732776
$response.Content.Headers -contains "If-Match" | Should Be $true
733777
}
734778

779+
It "Verifies Invoke-WebRequest default UserAgent handling with no errors" {
780+
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer
781+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
782+
783+
$response.Error | Should BeNullOrEmpty
784+
$response.Content.Headers.'User-Agent' | Should Match $UserAgent
785+
}
786+
787+
It "Verifies Invoke-WebRequest default UserAgent handling reports an error is returned for an invalid UserAgent value" {
788+
$UserAgent = 'Invalid:Agent'
789+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-WebRequest"
790+
791+
$response.Error | Should Not BeNullOrEmpty
792+
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
793+
$response.Error.Exception.Message | Should Be "The format of value 'Invalid:Agent' is invalid."
794+
}
795+
796+
It "Verifies Invoke-WebRequest UserAgent handling does not report an error when using -SkipHeaderValidation" {
797+
$UserAgent = 'Invalid:Agent'
798+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-WebRequest"
799+
800+
$response.Error | Should BeNullOrEmpty
801+
$response.Content.Headers.'User-Agent' | Should Match $UserAgent
802+
}
803+
735804
#endregion SkipHeaderVerification Tests
736805

737806
BeforeEach {
@@ -1223,6 +1292,31 @@ Describe "Invoke-RestMethod tests" -Tags "Feature" {
12231292
$response.Content.Headers -contains "If-Match" | Should Be $true
12241293
}
12251294

1295+
It "Verifies Invoke-RestMethod default UserAgent handling with no errors" {
1296+
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer
1297+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
1298+
1299+
$response.Error | Should BeNullOrEmpty
1300+
$response.Content.Headers.'User-Agent' | Should Match $UserAgent
1301+
}
1302+
1303+
It "Verifies Invoke-RestMethod default UserAgent handling reports an error is returned for an invalid UserAgent value" {
1304+
$UserAgent = 'Invalid:Agent'
1305+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -Cmdlet "Invoke-RestMethod"
1306+
1307+
$response.Error | Should Not BeNullOrEmpty
1308+
$response.Error.FullyQualifiedErrorId | Should Be "System.FormatException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand"
1309+
$response.Error.Exception.Message | Should Be "The format of value 'Invalid:Agent' is invalid."
1310+
}
1311+
1312+
It "Verifies Invoke-RestMethod UserAgent handling does not report an error when using -SkipHeaderValidation" {
1313+
$UserAgent = 'Invalid:Agent'
1314+
$response = ExecuteRequestWithCustomUserAgent -Uri "http://localhost:8081/PowerShell?test=echo" -UserAgent $UserAgent -SkipHeaderValidation -Cmdlet "Invoke-RestMethod"
1315+
1316+
$response.Error | Should BeNullOrEmpty
1317+
$response.Content.Headers.'User-Agent' | Should Match $UserAgent
1318+
}
1319+
12261320
#endregion SkipHeaderVerification tests
12271321

12281322
BeforeEach {

0 commit comments

Comments
 (0)
0