Skip to content

Commit c8ced33

Browse files
committed
Merge branch 'insecure'
2 parents c5026f6 + b000a7a commit c8ced33

File tree

11 files changed

+181
-12
lines changed

11 files changed

+181
-12
lines changed

src/targets/go/native.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module.exports = function (source, options) {
4747
code.push(indent, '"time"')
4848
}
4949

50+
if (opts.insecureSkipVerify) {
51+
code.push(indent, '"crypto/tls"')
52+
}
53+
5054
if (source.postData.text) {
5155
code.push(indent, '"strings"')
5256
}
@@ -63,14 +67,28 @@ module.exports = function (source, options) {
6367
.blank()
6468
}
6569

70+
// Create an insecure transport for the client
71+
if (opts.insecureSkipVerify) {
72+
code.push(indent, 'insecureTransport := http.DefaultTransport.(*http.Transport).Clone()')
73+
code.push(indent, 'insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}')
74+
}
75+
6676
// Create client
6777
let client
68-
if (opts.timeout > 0) {
78+
if (opts.timeout > 0 || opts.insecureSkipVerify) {
6979
client = 'client'
7080
code.push(indent, 'client := http.Client{')
71-
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
72-
.push(indent, '}')
73-
.blank()
81+
82+
if (opts.timeout > 0) {
83+
code.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
84+
}
85+
86+
if (opts.insecureSkipVerify) {
87+
code.push(indent + 1, 'Transport: insecureTransport,')
88+
}
89+
90+
code.push(indent, '}')
91+
code.blank()
7492
} else {
7593
client = 'http.DefaultClient'
7694
}

src/targets/node/native.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports = function (source, options) {
2828
headers: source.allHeaders
2929
}
3030

31+
if (options.insecureSkipVerify) {
32+
reqOpts.rejectUnauthorized = false
33+
}
34+
3135
code.push('const http = require("%s");', source.uriObj.protocol.replace(':', ''))
3236

3337
code.blank()

src/targets/python/python3.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ module.exports = function (source, options) {
1616
const code = new CodeBuilder()
1717
// Start Request
1818
code.push('import http.client')
19-
.blank()
19+
20+
if (options.insecureSkipVerify) {
21+
code.push('import ssl')
22+
}
23+
24+
code.blank()
2025

2126
// Check which protocol to be used for the client connection
2227
const protocol = source.uriObj.protocol
2328
if (protocol === 'https:') {
24-
code.push('conn = http.client.HTTPSConnection("%s")', source.uriObj.host)
25-
.blank()
29+
if (options.insecureSkipVerify) {
30+
code.push(
31+
'conn = http.client.HTTPSConnection("%s", context = ssl._create_unverified_context())',
32+
source.uriObj.host
33+
).blank()
34+
} else {
35+
code.push('conn = http.client.HTTPSConnection("%s")', source.uriObj.host)
36+
.blank()
37+
}
2638
} else {
2739
code.push('conn = http.client.HTTPConnection("%s")', source.uriObj.host)
2840
.blank()

src/targets/ruby/native.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ module.exports = function (source, options) {
3434

3535
if (source.uriObj.protocol === 'https:') {
3636
code.push('http.use_ssl = true')
37-
.push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE')
37+
38+
if (options.insecureSkipVerify) {
39+
code.push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE')
40+
}
3841
}
3942

4043
code.blank()

src/targets/shell/curl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module.exports = function (source, options) {
3636
}
3737
code.push(util.format('%s%s', opts.short ? '' : '--url ', formattedUrl))
3838

39+
if (opts.insecureSkipVerify) {
40+
code.push(opts.short ? '-k' : '--insecure')
41+
}
42+
3943
if (source.httpVersion === 'HTTP/1.0') {
4044
code.push(opts.short ? '-0' : '--http1.0')
4145
}

test/fixtures/output/ruby/native/https.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
http = Net::HTTP.new(url.host, url.port)
88
http.use_ssl = true
9-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
109

1110
request = Net::HTTP::Get.new(url)
1211

test/targets/go/native.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,51 @@ func main() {
132132
\tfmt.Println(res)
133133
\tfmt.Println(string(body))
134134
135+
}`)
136+
})
137+
138+
it('should support insecureSkipVerify option', function () {
139+
const result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
140+
insecureSkipVerify: true
141+
})
142+
143+
result.should.be.a.String()
144+
result.should.eql(`package main
145+
146+
import (
147+
\t"fmt"
148+
\t"crypto/tls"
149+
\t"strings"
150+
\t"net/http"
151+
\t"io/ioutil"
152+
)
153+
154+
func main() {
155+
156+
\tinsecureTransport := http.DefaultTransport.(*http.Transport).Clone()
157+
\tinsecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
158+
\tclient := http.Client{
159+
\t\tTransport: insecureTransport,
160+
\t}
161+
162+
\turl := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
163+
164+
\tpayload := strings.NewReader("foo=bar")
165+
166+
\treq, _ := http.NewRequest("POST", url, payload)
167+
168+
\treq.Header.Add("cookie", "foo=bar; bar=baz")
169+
\treq.Header.Add("accept", "application/json")
170+
\treq.Header.Add("content-type", "application/x-www-form-urlencoded")
171+
172+
\tres, _ := client.Do(req)
173+
174+
\tdefer res.Body.Close()
175+
\tbody, _ := ioutil.ReadAll(res.Body)
176+
177+
\tfmt.Println(res)
178+
\tfmt.Println(string(body))
179+
135180
}`)
136181
})
137182
}

test/targets/node/native.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
11
'use strict'
22

3-
module.exports = function (snippet, fixtures) {}
3+
module.exports = function (HTTPSnippet, fixtures) {
4+
it('should support the insecureSkipVerify option', function () {
5+
const result = new HTTPSnippet(fixtures.requests.https).convert('node', 'native', {
6+
insecureSkipVerify: true
7+
})
8+
9+
result.should.be.a.String()
10+
result.should.eql(`const http = require("https");
11+
12+
const options = {
13+
"method": "GET",
14+
"hostname": "mockbin.com",
15+
"port": null,
16+
"path": "/har",
17+
"headers": {},
18+
"rejectUnauthorized": false
19+
};
20+
21+
const req = http.request(options, function (res) {
22+
const chunks = [];
23+
24+
res.on("data", function (chunk) {
25+
chunks.push(chunk);
26+
});
27+
28+
res.on("end", function () {
29+
const body = Buffer.concat(chunks);
30+
console.log(body.toString());
31+
});
32+
});
33+
34+
req.end();`)
35+
})
36+
}

test/targets/python/python3.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,23 @@
22

33
require('should')
44

5-
module.exports = function (snippet, fixtures) {}
5+
module.exports = function (HTTPSnippet, fixtures) {
6+
it('should support insecureSkipVerify', function () {
7+
const result = new HTTPSnippet(fixtures.requests.https).convert('python', 'python3', {
8+
insecureSkipVerify: true
9+
})
10+
11+
result.should.be.a.String()
12+
result.should.eql(`import http.client
13+
import ssl
14+
15+
conn = http.client.HTTPSConnection("mockbin.com", context = ssl._create_unverified_context())
16+
17+
conn.request("GET", "/har")
18+
19+
res = conn.getresponse()
20+
data = res.read()
21+
22+
print(data.decode("utf-8"))`)
23+
})
24+
}

test/targets/ruby/native.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
'use strict'
22

3-
module.exports = function (snippet, fixtures) {}
3+
module.exports = function (HTTPSnippet, fixtures) {
4+
it('should support insecureSkipVerify', function () {
5+
const result = new HTTPSnippet(fixtures.requests.https).convert('ruby', 'native', {
6+
insecureSkipVerify: true
7+
})
8+
9+
result.should.be.a.String()
10+
result.should.eql(`require 'uri'
11+
require 'net/http'
12+
require 'openssl'
13+
14+
url = URI("https://mockbin.com/har")
15+
16+
http = Net::HTTP.new(url.host, url.port)
17+
http.use_ssl = true
18+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19+
20+
request = Net::HTTP::Get.new(url)
21+
22+
response = http.request(request)
23+
puts response.read_body`)
24+
})
25+
}

0 commit comments

Comments
 (0)