11'use strict'
22
33const CodeBuilder = require ( '../../helpers/code-builder' )
4- const { escape } = require ( '../../helpers/format' )
54const 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+
714module . 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}
0 commit comments