-
Notifications
You must be signed in to change notification settings - Fork 61
/
PreventUnEscapeDotsAndSlashesOnUri.ps1
51 lines (38 loc) · 1.67 KB
/
PreventUnEscapeDotsAndSlashesOnUri.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
if (-not $UnEscapeDotsAndSlashes) { Set-Variable -Scope Script -name UnEscapeDotsAndSlashes -value 0x2000000 }
function GetUriParserFlags
{
$getSyntax = [System.UriParser].GetMethod("GetSyntax", 40)
$flags = [System.UriParser].GetField("m_Flags", 36)
$parser = $getSyntax.Invoke($null, "http")
return $flags.GetValue($parser)
}
function SetUriParserFlags([int]$newValue)
{
$getSyntax = [System.UriParser].GetMethod("GetSyntax", 40)
$flags = [System.UriParser].GetField("m_Flags", 36)
$parser = $getSyntax.Invoke($null, "http")
$flags.SetValue($parser, $newValue)
}
function PreventUnEscapeDotsAndSlashesOnUri
{
if (-not $uriUnEscapesDotsAndSlashes) { return }
Write-Verbose "Switching off UnEscapesDotsAndSlashes flag on UriParser."
$newValue = $defaultUriParserFlagsValue -bxor $UnEscapeDotsAndSlashes
SetUriParserFlags $newValue
}
function RestoreUriParserFlags
{
if (-not $uriUnEscapesDotsAndSlashes) { return }
Write-Verbose "Restoring UriParser flags - switching on UnEscapesDotsAndSlashes flag."
try
{
SetUriParserFlags $defaultUriParserFlagsValue
}
catch [System.Exception]
{
Write-Error "Failed to restore UriParser flags. This may cause your scripts to behave unexpectedly. You can find more at get-help about_UnEsapingDotsAndSlashes."
throw
}
}
if (-not $defaultUriParserFlagsValue) { Set-Variable -Scope Script -name defaultUriParserFlagsValue -value (GetUriParserFlags) }
if (-not $uriUnEscapesDotsAndSlashes) { Set-Variable -Scope Script -name uriUnEscapesDotsAndSlashes -value (($defaultUriParserFlagsValue -band $UnEscapeDotsAndSlashes) -eq $UnEscapeDotsAndSlashes) }