Skip to content

Commit b3a3ff7

Browse files
committed
Fix Swift test cases for in-depth escaping scenario (and others)
1 parent 0a7fa5d commit b3a3ff7

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

src/targets/swift/helpers.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const util = require('util')
4+
const { escape } = require('../../helpers/format')
45

56
/**
67
* Create an string of given length filled with blank spaces
@@ -72,7 +73,12 @@ module.exports = {
7273
case '[object Object]': {
7374
const keyValuePairs = []
7475
for (const k in value) {
75-
keyValuePairs.push(util.format('"%s": %s', k, this.literalRepresentation(value[k], opts, indentLevel)))
76+
keyValuePairs.push(
77+
util.format('%s: %s',
78+
this.literalRepresentation(k, opts, indentLevel),
79+
this.literalRepresentation(value[k], opts, indentLevel)
80+
)
81+
)
7682
}
7783
return concatArray(keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel)
7884
}
@@ -84,7 +90,7 @@ module.exports = {
8490
if (value === null || value === undefined) {
8591
return ''
8692
}
87-
return '"' + value.toString().replace(/"/g, '\\"') + '"'
93+
return '"' + escape(value.toString()) + '"'
8894
}
8995
}
9096
}

src/targets/swift/nsurlsession.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ module.exports = function (source, options) {
4646
// we make it easier for the user to edit it according to his or her needs after pasting.
4747
// The user can just add/remove lines adding/removing body parameters.
4848
code.blank()
49-
.push('let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value)
49+
.push('let postData = NSMutableData(data: "%qd=%qd".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value)
5050
for (let i = 1, len = source.postData.params.length; i < len; i++) {
51-
code.push('postData.append("&%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[i].name, source.postData.params[i].value)
51+
code.push('postData.append("&%qd=%qd".data(using: String.Encoding.utf8)!)', source.postData.params[i].name, source.postData.params[i].value)
5252
}
5353
break
5454

@@ -68,7 +68,7 @@ module.exports = function (source, options) {
6868
*/
6969
code.push(helpers.literalDeclaration('parameters', source.postData.params, opts))
7070
.blank()
71-
.push('let boundary = "%s"', source.postData.boundary)
71+
.push('let boundary = "%qd"', source.postData.boundary)
7272
.blank()
7373
.push('var body = ""')
7474
.push('var error: NSError? = nil')
@@ -93,13 +93,13 @@ module.exports = function (source, options) {
9393

9494
default:
9595
code.blank()
96-
.push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text)
96+
.push('let postData = NSData(data: "%qd".data(using: String.Encoding.utf8)!)', source.postData.text)
9797
}
9898
}
9999

100100
code.blank()
101101
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
102-
.push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl)
102+
.push('let request = NSMutableURLRequest(url: NSURL(string: "%qd")! as URL,', source.fullUrl)
103103
.push(' cachePolicy: .useProtocolCachePolicy,')
104104
.push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1))
105105
.push('request.httpMethod = "%s"', source.method)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
3+
let headers = [
4+
"squote-value-test": "'",
5+
"dquote-value-test": "\"",
6+
"backtick-value-test": "`",
7+
"dollar-parenthesis-value-test": "$(",
8+
"hash-brace-value-test": "#{",
9+
"percent-parenthesis-value-test": "%(",
10+
"percent-brace-value-test": "%{",
11+
"double-brace-value-test": "{{",
12+
"null-value-test": "\\0",
13+
"string-fmt-value-test": "%s",
14+
"slash-value-test": "\\"
15+
]
16+
17+
let postData = NSData(data: "' \" ` $( #{ %( %{ {{ \\0 %s \\".data(using: String.Encoding.utf8)!)
18+
19+
let request = NSMutableURLRequest(url: NSURL(string: "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")! as URL,
20+
cachePolicy: .useProtocolCachePolicy,
21+
timeoutInterval: 10.0)
22+
request.httpMethod = "POST"
23+
request.allHTTPHeaderFields = headers
24+
request.httpBody = postData as Data
25+
26+
let session = URLSession.shared
27+
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
28+
if (error != nil) {
29+
print(error)
30+
} else {
31+
let httpResponse = response as? HTTPURLResponse
32+
print(httpResponse)
33+
}
34+
})
35+
36+
dataTask.resume()

test/targets.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ const skipMe = {
4747
r: {
4848
httr: ['malicious']
4949
},
50-
swift: {
51-
nsurlsession: ['malicious']
52-
},
5350
'*': {
5451
'*': []
5552
}

0 commit comments

Comments
 (0)