Skip to content

Commit 24847f2

Browse files
committed
Fix powershell test cases for in-depth escaping scenario
This also handles a few other cases not specifically covered by the new scenario but which are obviously easily fixable
1 parent 91ba1b4 commit 24847f2

26 files changed

+83
-40
lines changed

src/targets/powershell/common.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
'use strict'
22

33
const CodeBuilder = require('../../helpers/code-builder')
4-
const { escape } = require('../../helpers/format')
54
const helpers = require('../../helpers/headers')
65

6+
// Within a single quote, the ONLY character to worry about is the single quote
7+
// itself (escaped by doubling). Newlines, backticks, slashes etc are all treated
8+
// as literal characters.
9+
const psSqEscape = function (input) {
10+
return input
11+
.replace(/'/g, "''")
12+
}
13+
714
module.exports = function (command) {
815
return function (source, options) {
916
const code = new CodeBuilder()
@@ -23,7 +30,10 @@ module.exports = function (command) {
2330
code.push('$headers=@{}')
2431
headers.forEach(function (key) {
2532
if (key !== 'connection') { // Not allowed
26-
code.push('$headers.Add("%s", "%s")', key, escape(source.headersObj[key], { escapeChar: '`' }))
33+
code.push("$headers.Add('%s', '%s')",
34+
psSqEscape(key),
35+
psSqEscape(source.headersObj[key])
36+
)
2737
}
2838
})
2939
commandOptions.push('-Headers $headers')
@@ -36,21 +46,32 @@ module.exports = function (command) {
3646
source.cookies.forEach(function (cookie) {
3747
code.push('$cookie = New-Object System.Net.Cookie')
3848

39-
code.push("$cookie.Name = '%s'", cookie.name)
40-
code.push("$cookie.Value = '%s'", cookie.value)
41-
code.push("$cookie.Domain = '%s'", source.uriObj.host)
49+
code.push("$cookie.Name = '%s'", psSqEscape(cookie.name))
50+
code.push("$cookie.Value = '%s'", psSqEscape(cookie.value))
51+
code.push("$cookie.Domain = '%s'", psSqEscape(source.uriObj.host))
4252

4353
code.push('$session.Cookies.Add($cookie)')
4454
})
4555
commandOptions.push('-WebSession $session')
4656
}
4757

4858
if (source.postData.text) {
49-
commandOptions.push("-ContentType '" + helpers.getHeader(source.allHeaders, 'content-type') + "'")
50-
commandOptions.push("-Body '" + source.postData.text + "'")
59+
const contentType = helpers.getHeader(source.allHeaders, 'content-type')
60+
if (contentType) {
61+
commandOptions.push("-ContentType '" + psSqEscape(contentType) + "'")
62+
}
63+
64+
commandOptions.push(
65+
"-Body '" + psSqEscape(source.postData.text) + "'"
66+
)
5167
}
5268

53-
code.push("$response = %s -Uri '%s' -Method %s %s", command, source.fullUrl, source.method, commandOptions.join(' '))
69+
code.push("$response = %s -Uri '%s' -Method %s %s",
70+
command,
71+
psSqEscape(source.fullUrl),
72+
source.method,
73+
commandOptions.join(' ')
74+
)
5475
return code.join()
5576
}
5677
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
$headers=@{}
2-
$headers.Add("content-type", "application/x-www-form-urlencoded")
2+
$headers.Add('content-type', 'application/x-www-form-urlencoded')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/x-www-form-urlencoded' -Body 'foo=bar&hello=world'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
$headers=@{}
2-
$headers.Add("content-type", "application/json")
2+
$headers.Add('content-type', 'application/json')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
$headers=@{}
2-
$headers.Add("accept-encoding", "deflate, gzip, br")
2+
$headers.Add('accept-encoding', 'deflate, gzip, br')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method GET -Headers $headers

test/fixtures/output/powershell/restmethod/full.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$headers=@{}
2-
$headers.Add("accept", "application/json")
3-
$headers.Add("content-type", "application/x-www-form-urlencoded")
2+
$headers.Add('accept', 'application/json')
3+
$headers.Add('content-type', 'application/x-www-form-urlencoded')
44
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
55
$cookie = New-Object System.Net.Cookie
66
$cookie.Name = 'foo'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$headers=@{}
2-
$headers.Add("accept", "application/json")
3-
$headers.Add("x-foo", "Bar")
4-
$headers.Add("quoted-value", "`"quoted`" 'string'")
2+
$headers.Add('accept', 'application/json')
3+
$headers.Add('x-foo', 'Bar')
4+
$headers.Add('quoted-value', '"quoted" ''string''')
55
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method GET -Headers $headers
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$headers=@{}
2-
$headers.Add("content-type", "application/json")
2+
$headers.Add('content-type', 'application/json')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/json' -Body '{
44
"foo": "bar"
55
}'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
$headers=@{}
2-
$headers.Add("content-type", "application/json")
2+
$headers.Add('content-type', 'application/json')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"foo":null}'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$headers=@{}
2+
$headers.Add('squote-value-test', '''')
3+
$headers.Add('dquote-value-test', '"')
4+
$headers.Add('backtick-value-test', '`')
5+
$headers.Add('dollar-parenthesis-value-test', '$(')
6+
$headers.Add('hash-brace-value-test', '#{')
7+
$headers.Add('percent-parenthesis-value-test', '%(')
8+
$headers.Add('percent-brace-value-test', '%{')
9+
$headers.Add('double-brace-value-test', '{{')
10+
$headers.Add('null-value-test', '\0')
11+
$headers.Add('string-fmt-value-test', '%s')
12+
$headers.Add('slash-value-test', '\')
13+
$response = Invoke-RestMethod -Uri 'http://example.test/%27%22%60$(%(%%7B%7B%7B/0%s//?''=squote-key-test&squote-value-test=''&%22=dquote-key-test&dquote-value-test=%22&%60=backtick-key-test&backtick-value-test=%60&%24(=dollar-parenthesis-key-test&dollar-parenthesis-value-test=%24(&%23%7B=hash-brace-key-test&hash-brace-value-test=%23%7B&%25(=percent-parenthesis-key-test&percent-parenthesis-value-test=%25(&%25%7B=percent-brace-key-test&percent-brace-value-test=%25%7B&%7B%7B=double-brace-key-test&double-brace-value-test=%7B%7B&%5C0=null-key-test&null-value-test=%5C0&%25s=string-fmt-key-test&string-fmt-value-test=%25s&%5C=slash-key-test&slash-value-test=%5C' -Method POST -Headers $headers -Body ''' " ` $( #{ %( %{ {{ \0 %s \'

test/fixtures/output/powershell/restmethod/multipart-data.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
$headers=@{}
2-
$headers.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
2+
$headers.Add('content-type', 'multipart/form-data; boundary=---011000010111000001101001')
33
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'multipart/form-data; boundary=---011000010111000001101001' -Body '-----011000010111000001101001
44
Content-Disposition: form-data; name="foo"; filename="hello.txt"
55
Content-Type: text/plain

0 commit comments

Comments
 (0)