httpclient 2.9.0 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -0
- data/lib/httpclient/cacert.pem +2079 -3015
- data/lib/httpclient/jruby_ssl_socket.rb +11 -2
- data/lib/httpclient/session.rb +1 -0
- data/lib/httpclient/ssl_config.rb +100 -1
- data/lib/httpclient/ssl_socket.rb +3 -0
- data/lib/httpclient/version.rb +1 -1
- data/lib/httpclient.rb +20 -8
- data/test/fixtures/emudhra-dv-g3b.pem +47 -0
- data/test/helper.rb +66 -6
- data/test/jruby_ssl_socket/test_mutable_buffers.rb +30 -0
- data/test/jruby_ssl_socket/test_pemutils.rb +4 -3
- data/test/jruby_ssl_socket/test_socket_timeout.rb +19 -0
- data/test/test_auth.rb +19 -7
- data/test/test_auth_fixture.rb +42 -0
- data/test/test_helper_lifecycle.rb +84 -0
- data/test/test_http-access2.rb +1 -0
- data/test/test_httpclient.rb +19 -5
- data/test/test_proxy_auth_scope.rb +292 -0
- data/test/test_ssl.rb +159 -4
- data/test/test_ssl_config.rb +51 -0
- metadata +10 -7
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
|
2
|
+
|
|
3
|
+
class TestSupportLifecycle < Test::Unit::TestCase
|
|
4
|
+
Server = Struct.new(:events, :kind) do
|
|
5
|
+
def shutdown
|
|
6
|
+
events << [kind, :shutdown]
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
Listener = Struct.new(:events, :kind) do
|
|
11
|
+
def join
|
|
12
|
+
events << [kind, :join]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def setup
|
|
17
|
+
@events = []
|
|
18
|
+
@fixture = Object.new.extend(Helper)
|
|
19
|
+
@fixture.setup
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_serverport_lazily_starts_server_once
|
|
23
|
+
install_lazy_server(:server, 18_081)
|
|
24
|
+
|
|
25
|
+
assert_equal(18_081, @fixture.serverport)
|
|
26
|
+
assert_equal(18_081, @fixture.serverport)
|
|
27
|
+
assert_equal([[:server, :start]], @events)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_proxyport_lazily_starts_proxy_once
|
|
31
|
+
install_lazy_server(:proxyserver, 18_082)
|
|
32
|
+
|
|
33
|
+
assert_equal(18_082, @fixture.proxyport)
|
|
34
|
+
assert_equal(18_082, @fixture.proxyport)
|
|
35
|
+
assert_equal([[:proxyserver, :start]], @events)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_teardown_server_joins_listener
|
|
39
|
+
install_server(:server)
|
|
40
|
+
|
|
41
|
+
@fixture.teardown_server
|
|
42
|
+
|
|
43
|
+
assert_equal([[:server, :shutdown], [:server, :join]], @events)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_teardown_proxyserver_joins_listener
|
|
47
|
+
install_server(:proxyserver)
|
|
48
|
+
|
|
49
|
+
@fixture.teardown_proxyserver
|
|
50
|
+
|
|
51
|
+
assert_equal([[:proxyserver, :shutdown], [:proxyserver, :join]], @events)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_client_cleanup_error_still_stops_both_servers
|
|
55
|
+
install_server(:server)
|
|
56
|
+
install_server(:proxyserver)
|
|
57
|
+
client = Object.new
|
|
58
|
+
def client.reset_all
|
|
59
|
+
raise IOError, 'client cleanup failed'
|
|
60
|
+
end
|
|
61
|
+
@fixture.instance_variable_set(:@client, client)
|
|
62
|
+
|
|
63
|
+
assert_raise(IOError) { @fixture.teardown }
|
|
64
|
+
assert_equal([[:proxyserver, :shutdown], [:proxyserver, :join],
|
|
65
|
+
[:server, :shutdown], [:server, :join]], @events)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def install_server(kind)
|
|
71
|
+
@fixture.instance_variable_set("@#{kind}", Server.new(@events, kind))
|
|
72
|
+
@fixture.instance_variable_set("@#{kind}_thread", Listener.new(@events, kind))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def install_lazy_server(kind, port)
|
|
76
|
+
events = @events
|
|
77
|
+
@fixture.define_singleton_method("setup_#{kind}") do
|
|
78
|
+
events << [kind, :start]
|
|
79
|
+
instance_variable_set("@#{kind}", Server.new(events, kind))
|
|
80
|
+
port_variable = kind == :server ? :@serverport : :@proxyport
|
|
81
|
+
instance_variable_set(port_variable, port)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
data/test/test_http-access2.rb
CHANGED
data/test/test_httpclient.rb
CHANGED
|
@@ -9,7 +9,6 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
|
9
9
|
|
|
10
10
|
def setup
|
|
11
11
|
super
|
|
12
|
-
setup_server
|
|
13
12
|
setup_client
|
|
14
13
|
end
|
|
15
14
|
|
|
@@ -65,6 +64,21 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
|
65
64
|
assert(!str.empty?)
|
|
66
65
|
end
|
|
67
66
|
|
|
67
|
+
data('mutable' => false, 'frozen' => true)
|
|
68
|
+
def test_debug_socket_does_not_mutate_binary_input(freeze_payload)
|
|
69
|
+
debug_dev = StringIO.new
|
|
70
|
+
socket = Object.new
|
|
71
|
+
socket.extend(HTTPClient::DebugSocket)
|
|
72
|
+
socket.debug_dev = debug_dev
|
|
73
|
+
payload = "\x00".dup
|
|
74
|
+
payload.freeze if freeze_payload
|
|
75
|
+
|
|
76
|
+
assert_nothing_raised { socket.send(:debug, payload) }
|
|
77
|
+
assert_equal("\x00", payload)
|
|
78
|
+
assert_equal(Encoding::UTF_8, payload.encoding)
|
|
79
|
+
assert(!debug_dev.string.empty?)
|
|
80
|
+
end
|
|
81
|
+
|
|
68
82
|
def test_protocol_version_http09
|
|
69
83
|
@client.protocol_version = 'HTTP/0.9'
|
|
70
84
|
@client.debug_dev = str = ''.dup
|
|
@@ -264,7 +278,6 @@ class TestHTTPClient < Test::Unit::TestCase
|
|
|
264
278
|
end
|
|
265
279
|
|
|
266
280
|
def test_empty_proxy_env
|
|
267
|
-
setup_proxyserver
|
|
268
281
|
escape_env do
|
|
269
282
|
ENV['http_proxy'] = ""
|
|
270
283
|
client = HTTPClient.new
|
|
@@ -768,7 +781,8 @@ EOS
|
|
|
768
781
|
if RUBY_VERSION > "1.9"
|
|
769
782
|
def test_post_async_with_default_internal
|
|
770
783
|
original_encoding = Encoding.default_internal
|
|
771
|
-
|
|
784
|
+
# This test covers the legacy setter even though JRuby deprecates it.
|
|
785
|
+
silent { Encoding.default_internal = Encoding::UTF_8 }
|
|
772
786
|
begin
|
|
773
787
|
post_body = StringIO.new("こんにちは")
|
|
774
788
|
conn = @client.post_async(serverurl + 'servlet', post_body)
|
|
@@ -776,7 +790,7 @@ EOS
|
|
|
776
790
|
res = conn.pop
|
|
777
791
|
assert_equal 'post,こんにちは', res.content.read
|
|
778
792
|
ensure
|
|
779
|
-
Encoding.default_internal = original_encoding
|
|
793
|
+
silent { Encoding.default_internal = original_encoding }
|
|
780
794
|
end
|
|
781
795
|
end
|
|
782
796
|
end
|
|
@@ -934,7 +948,7 @@ EOS
|
|
|
934
948
|
STDOUT.sync = true
|
|
935
949
|
File.open(__FILE__) do |file|
|
|
936
950
|
res = @client.post(serverurl + 'servlet', {1=>2, 3=>file})
|
|
937
|
-
assert_match(/^Content-Disposition: form-data; name="1"\r\n/
|
|
951
|
+
assert_match(/^Content-Disposition: form-data; name="1"\r\n/m, res.content.b)
|
|
938
952
|
assert_match(/^Content-Disposition: form-data; name="3";/, res.content)
|
|
939
953
|
assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)
|
|
940
954
|
end
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
|
2
|
+
require 'webrick/https'
|
|
3
|
+
|
|
4
|
+
class TestProxyAuthScope < Test::Unit::TestCase
|
|
5
|
+
include Helper
|
|
6
|
+
|
|
7
|
+
PROXY_CREDENTIAL = 'Basic ' + ['proxy-user:proxy-secret'].pack('m0')
|
|
8
|
+
ORIGIN_CREDENTIAL = 'Basic ' + ['origin-user:origin-secret'].pack('m0')
|
|
9
|
+
|
|
10
|
+
class HeaderFilter
|
|
11
|
+
attr_reader :requests, :responses
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@requests = @responses = 0
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def filter_request(req)
|
|
18
|
+
@requests += 1
|
|
19
|
+
req.header.add('Proxy-Authorization', 'custom filter value')
|
|
20
|
+
req.header.add('X-Scope-Filter', 'present')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def filter_response(_req, _res)
|
|
24
|
+
@responses += 1
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def setup
|
|
30
|
+
super
|
|
31
|
+
setup_client
|
|
32
|
+
@client.proxy = nil
|
|
33
|
+
@client.no_proxy = nil
|
|
34
|
+
@client.set_proxy_auth('proxy-user', 'proxy-secret')
|
|
35
|
+
@origin_requests = Queue.new
|
|
36
|
+
@proxy_requests = Queue.new
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
data('Basic' => 'Basic', 'Digest' => 'Digest')
|
|
40
|
+
def test_redirect_to_no_proxy(scheme)
|
|
41
|
+
start_origin
|
|
42
|
+
start_proxy(scheme)
|
|
43
|
+
assert_equal('OK', @client.get_content('http://upstream.invalid/redirect'))
|
|
44
|
+
assert_proxy_authenticated(scheme)
|
|
45
|
+
assert_equal([[]], drain(@origin_requests).map { |request| request[:proxy_auth] })
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
data('sync Basic listed' => [:sync, 'Basic', false],
|
|
49
|
+
'async Basic listed' => [:async, 'Basic', false],
|
|
50
|
+
'sync Digest listed' => [:sync, 'Digest', false],
|
|
51
|
+
'async Digest listed' => [:async, 'Digest', false],
|
|
52
|
+
'sync Basic localhost' => [:sync, 'Basic', true],
|
|
53
|
+
'async Basic localhost' => [:async, 'Basic', true])
|
|
54
|
+
def test_direct_request_after_proxy_authentication(settings)
|
|
55
|
+
mode, scheme, implicit_localhost = settings
|
|
56
|
+
start_origin
|
|
57
|
+
start_proxy(scheme)
|
|
58
|
+
assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
|
|
59
|
+
assert_proxy_authenticated(scheme)
|
|
60
|
+
url = @origin_url
|
|
61
|
+
if implicit_localhost
|
|
62
|
+
@client.no_proxy = nil
|
|
63
|
+
url = "http://localhost:#{serverport}/"
|
|
64
|
+
end
|
|
65
|
+
assert_equal(200, request(mode, url).status)
|
|
66
|
+
assert_equal([[]], drain(@origin_requests).map { |record| record[:proxy_auth] })
|
|
67
|
+
assert_equal('OK', @client.get_content('http://upstream.invalid/again'))
|
|
68
|
+
assert_match(/\A#{scheme} /, drain(@proxy_requests).last[:proxy_auth].first)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
data('sync' => :sync, 'async' => :async)
|
|
72
|
+
def test_forced_basic_auth_preserves_origin_auth(mode)
|
|
73
|
+
start_origin
|
|
74
|
+
start_proxy
|
|
75
|
+
@client.force_basic_auth = true
|
|
76
|
+
@client.set_auth(@origin_url, 'origin-user', 'origin-secret')
|
|
77
|
+
assert_equal(200, request(mode, 'http://upstream.invalid/').status)
|
|
78
|
+
assert_equal([PROXY_CREDENTIAL], drain(@proxy_requests).first[:proxy_auth])
|
|
79
|
+
assert_equal(200, request(mode, @origin_url).status)
|
|
80
|
+
record = @origin_requests.pop
|
|
81
|
+
assert_equal([], record[:proxy_auth])
|
|
82
|
+
assert_equal([ORIGIN_CREDENTIAL], record[:origin_auth])
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
data('sync request with proxy' => [:sync, false, true],
|
|
86
|
+
'async request with proxy' => [:async, false, true],
|
|
87
|
+
'sync defaults with proxy' => [:sync, true, true],
|
|
88
|
+
'async defaults with proxy' => [:async, true, true],
|
|
89
|
+
'sync request without proxy' => [:sync, false, false],
|
|
90
|
+
'async request without proxy' => [:async, false, false],
|
|
91
|
+
'sync defaults without proxy' => [:sync, true, false],
|
|
92
|
+
'async defaults without proxy' => [:async, true, false])
|
|
93
|
+
def test_explicit_headers_on_direct_requests(settings)
|
|
94
|
+
mode, defaults, configured_proxy = settings
|
|
95
|
+
start_origin
|
|
96
|
+
start_proxy if configured_proxy
|
|
97
|
+
headers = { 'pRoXy-AuThOrIzAtIoN' => ['first', 'second'],
|
|
98
|
+
'Proxy-Authorization' => 'third' }
|
|
99
|
+
@client.default_header.update(headers) if defaults
|
|
100
|
+
assert_equal(200, request(mode, @origin_url, defaults ? {} : headers).status)
|
|
101
|
+
assert_equal([], @origin_requests.pop[:proxy_auth])
|
|
102
|
+
assert_equal(['first', 'second'], headers['pRoXy-AuThOrIzAtIoN'])
|
|
103
|
+
assert_equal('third', headers['Proxy-Authorization'])
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
data('sync' => :sync, 'async' => :async)
|
|
107
|
+
def test_explicit_header_on_forwarding_request(mode)
|
|
108
|
+
start_origin
|
|
109
|
+
start_proxy
|
|
110
|
+
assert_equal(200, request(mode, 'http://upstream.invalid/', 'Proxy-Authorization' => PROXY_CREDENTIAL).status)
|
|
111
|
+
records = drain(@proxy_requests)
|
|
112
|
+
assert_equal(1, records.size)
|
|
113
|
+
assert_equal([PROXY_CREDENTIAL], records.first[:proxy_auth])
|
|
114
|
+
assert(@origin_requests.empty?)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
data('sync' => :sync, 'async' => :async)
|
|
118
|
+
def test_custom_filters_still_run_on_direct_requests(mode)
|
|
119
|
+
start_origin
|
|
120
|
+
start_proxy
|
|
121
|
+
filter = HeaderFilter.new
|
|
122
|
+
@client.request_filter << filter
|
|
123
|
+
assert_equal(200, request(mode, @origin_url).status)
|
|
124
|
+
record = @origin_requests.pop
|
|
125
|
+
assert_equal([], record[:proxy_auth])
|
|
126
|
+
assert_equal('present', record[:custom_filter])
|
|
127
|
+
assert_equal(1, filter.requests)
|
|
128
|
+
assert_equal(1, filter.responses)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
data('sync' => :sync, 'async' => :async)
|
|
132
|
+
def test_direct_response_cannot_challenge_proxy(mode)
|
|
133
|
+
start_origin
|
|
134
|
+
start_proxy
|
|
135
|
+
assert_equal(407, request(mode, @origin_url + 'proxy_challenge').status)
|
|
136
|
+
assert_equal(1, drain(@origin_requests).size, 'origin 407 must not cause proxy authentication retries')
|
|
137
|
+
assert_nil(@client.proxy_auth.basic_auth.get(nil))
|
|
138
|
+
assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
|
|
139
|
+
assert_proxy_authenticated('Basic')
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
data('sync' => :sync, 'async' => :async)
|
|
143
|
+
def test_loopback_response_does_not_bypass_direct_route_protection(mode)
|
|
144
|
+
@client.proxy = 'http://proxy.invalid:8080'
|
|
145
|
+
@client.no_proxy = 'direct.invalid'
|
|
146
|
+
@client.force_basic_auth = true
|
|
147
|
+
@client.test_loopback_http_response << "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nOK"
|
|
148
|
+
@client.debug_dev = log = ''.dup
|
|
149
|
+
assert_equal(200, request(mode, 'http://direct.invalid/').status)
|
|
150
|
+
assert_not_match(/Proxy-Authorization:/i, log)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
data('sync challenged' => [:sync, false], 'async challenged' => [:async, false],
|
|
154
|
+
'sync forced' => [:sync, true], 'async forced' => [:async, true])
|
|
155
|
+
def test_https_credentials_only_reach_connect(settings)
|
|
156
|
+
mode, force = settings
|
|
157
|
+
start_origin(true)
|
|
158
|
+
start_proxy('Basic', true)
|
|
159
|
+
@client.force_basic_auth = force
|
|
160
|
+
@client.protocol_version = '1.1'
|
|
161
|
+
@client.set_auth(@origin_url, 'origin-user', 'origin-secret')
|
|
162
|
+
@client.www_auth.basic_auth.force_auth = true
|
|
163
|
+
escape_noproxy do
|
|
164
|
+
assert_equal(200, request(:sync, @origin_url).status)
|
|
165
|
+
assert_equal(200, request(mode, @origin_url, 'pRoXy-AuThOrIzAtIoN' => ['first', 'second']).status)
|
|
166
|
+
end
|
|
167
|
+
proxy_requests = drain(@proxy_requests)
|
|
168
|
+
assert_equal(force ? 1 : 2, proxy_requests.size, 'the authenticated tunnel should be reused')
|
|
169
|
+
assert(proxy_requests.all? { |record| record[:method] == 'CONNECT' })
|
|
170
|
+
assert_equal([PROXY_CREDENTIAL], proxy_requests.last[:proxy_auth])
|
|
171
|
+
records = drain(@origin_requests)
|
|
172
|
+
assert_equal([[], []], records.map { |record| record[:proxy_auth] })
|
|
173
|
+
assert_equal([[ORIGIN_CREDENTIAL], [ORIGIN_CREDENTIAL]], records.map { |record| record[:origin_auth] })
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
data('sync' => :sync, 'async' => :async)
|
|
177
|
+
def test_https_origin_response_cannot_challenge_proxy(mode)
|
|
178
|
+
start_origin(true)
|
|
179
|
+
start_proxy('Basic', true)
|
|
180
|
+
@client.protocol_version = '1.1'
|
|
181
|
+
escape_noproxy do
|
|
182
|
+
assert_equal(200, request(:sync, @origin_url).status)
|
|
183
|
+
@client.proxy_auth.reset_challenge
|
|
184
|
+
assert_equal(407, request(mode, @origin_url + 'proxy_challenge').status)
|
|
185
|
+
end
|
|
186
|
+
assert_equal(2, drain(@origin_requests).size)
|
|
187
|
+
assert_nil(@client.proxy_auth.basic_auth.get(nil))
|
|
188
|
+
assert_equal(2, drain(@proxy_requests).size, 'origin 407 must not reopen the CONNECT tunnel')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def test_parallel_direct_and_proxied_requests
|
|
192
|
+
start_origin
|
|
193
|
+
start_proxy
|
|
194
|
+
assert_equal('OK', @client.get_content('http://upstream.invalid/warm'))
|
|
195
|
+
assert_proxy_authenticated('Basic')
|
|
196
|
+
threads = 10.times.map do |index|
|
|
197
|
+
Thread.new do
|
|
198
|
+
url = index.even? ? @origin_url : 'http://upstream.invalid/again'
|
|
199
|
+
@client.get(url).status
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
assert_equal([200] * 10, threads.map(&:value))
|
|
203
|
+
assert_equal([[]] * 5, drain(@origin_requests).map { |record| record[:proxy_auth] })
|
|
204
|
+
assert_equal([[PROXY_CREDENTIAL]] * 5, drain(@proxy_requests).map { |record| record[:proxy_auth] })
|
|
205
|
+
ensure
|
|
206
|
+
threads.each { |thread| thread.join } if threads
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
private
|
|
210
|
+
|
|
211
|
+
def start_origin(ssl = false)
|
|
212
|
+
options = { :BindAddress => '127.0.0.1', :Port => 0, :AccessLog => [], :Logger => @logger }
|
|
213
|
+
if ssl
|
|
214
|
+
options.update(:SSLEnable => true,
|
|
215
|
+
:SSLCertificate => OpenSSL::X509::Certificate.new(File.read(File.join(__dir__, 'server.cert'))),
|
|
216
|
+
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(File.join(__dir__, 'server.key'))))
|
|
217
|
+
@client.ssl_config.add_trust_ca(File.join(__dir__, 'ca-chain.pem'))
|
|
218
|
+
end
|
|
219
|
+
@server = WEBrick::HTTPServer.new(options)
|
|
220
|
+
@serverport = @server.config[:Port]
|
|
221
|
+
@server.mount_proc('/') do |req, res|
|
|
222
|
+
@origin_requests << record(req)
|
|
223
|
+
if req.path == '/proxy_challenge'
|
|
224
|
+
res.status = 407
|
|
225
|
+
res['Proxy-Authenticate'] = 'Basic realm="untrusted origin"'
|
|
226
|
+
end
|
|
227
|
+
res.body = 'OK'
|
|
228
|
+
end
|
|
229
|
+
@server_thread = start_server_thread(@server)
|
|
230
|
+
@origin_url = ssl ? "https://localhost:#{serverport}/" : "http://127.0.0.1:#{serverport}/"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def start_proxy(scheme = 'Basic', tunnel = false)
|
|
234
|
+
@proxy_scheme = scheme
|
|
235
|
+
options = { :BindAddress => '127.0.0.1', :Port => 0, :AccessLog => [], :Logger => @proxylogger }
|
|
236
|
+
if tunnel
|
|
237
|
+
options[:ProxyAuthProc] = method(:authenticate_proxy).to_proc
|
|
238
|
+
@proxyserver = WEBrick::HTTPProxyServer.new(options)
|
|
239
|
+
else
|
|
240
|
+
# A fake forwarding proxy: never resolves or connects to upstream.invalid.
|
|
241
|
+
@proxyserver = WEBrick::HTTPServer.new(options)
|
|
242
|
+
@proxyserver.mount_proc('/') do |req, res|
|
|
243
|
+
authenticate_proxy(req, res)
|
|
244
|
+
if req.path == '/redirect'
|
|
245
|
+
res.status = 302
|
|
246
|
+
res['Location'] = @origin_url
|
|
247
|
+
end
|
|
248
|
+
res.body = 'OK'
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
@proxyport = @proxyserver.config[:Port]
|
|
252
|
+
@proxyserver_thread = start_server_thread(@proxyserver)
|
|
253
|
+
@client.proxy = "http://127.0.0.1:#{proxyport}/"
|
|
254
|
+
@client.no_proxy = "127.0.0.1:#{serverport}"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def authenticate_proxy(req, res)
|
|
258
|
+
@proxy_requests << record(req)
|
|
259
|
+
unless req['proxy-authorization']
|
|
260
|
+
res['Proxy-Authenticate'] = @proxy_scheme == 'Basic' ?
|
|
261
|
+
'Basic realm="proxy"' : 'Digest realm="proxy", nonce="test-nonce", qop="auth"'
|
|
262
|
+
raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def record(req)
|
|
267
|
+
{ :proxy_auth => req.header['proxy-authorization'].dup,
|
|
268
|
+
:origin_auth => req.header['authorization'].dup, :method => req.request_method,
|
|
269
|
+
:custom_filter => req['x-scope-filter'] }
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def drain(queue)
|
|
273
|
+
Array.new(queue.size) { queue.pop }
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def assert_proxy_authenticated(scheme)
|
|
277
|
+
records = drain(@proxy_requests)
|
|
278
|
+
assert_equal(2, records.size)
|
|
279
|
+
assert_equal([], records.first[:proxy_auth])
|
|
280
|
+
assert_match(/\A#{scheme} /, records.last[:proxy_auth].first)
|
|
281
|
+
assert_equal([PROXY_CREDENTIAL], records.last[:proxy_auth]) if scheme == 'Basic'
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def request(mode, url, headers = {})
|
|
285
|
+
return @client.get(url, :header => headers) if mode == :sync
|
|
286
|
+
connection = @client.get_async(url, :header => headers)
|
|
287
|
+
response = connection.pop
|
|
288
|
+
response.body.read
|
|
289
|
+
connection.join
|
|
290
|
+
response
|
|
291
|
+
end
|
|
292
|
+
end
|
data/test/test_ssl.rb
CHANGED
|
@@ -61,6 +61,129 @@ class TestSSL < Test::Unit::TestCase
|
|
|
61
61
|
assert_instance_of(OpenSSL::X509::Store, cfg.cert_store)
|
|
62
62
|
end
|
|
63
63
|
|
|
64
|
+
def test_emudhra_cross_signed_public_key
|
|
65
|
+
certificates = read_path('fixtures/emudhra-dv-g3b.pem').scan(/-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----/m).map { |pem|
|
|
66
|
+
OpenSSL::X509::Certificate.new(pem)
|
|
67
|
+
}
|
|
68
|
+
assert_equal(2, certificates.size)
|
|
69
|
+
assert_not_equal(certificates[0].issuer, certificates[1].issuer)
|
|
70
|
+
assert_equal(8, HTTPClient::SSLConfig::BLOCKED_SPKI_SHA256.size)
|
|
71
|
+
certificates.each do |certificate|
|
|
72
|
+
assert(HTTPClient::SSLConfig.blocked_public_key?(certificate.public_key.to_der))
|
|
73
|
+
end
|
|
74
|
+
assert_equal(false, HTTPClient::SSLConfig.blocked_public_key?(cert('ca.cert').public_key.to_der))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def test_blocked_peer_certificate_before_request
|
|
78
|
+
cfg = @client.ssl_config
|
|
79
|
+
cfg.add_trust_ca(path('ca-chain.pem'))
|
|
80
|
+
requests = Queue.new
|
|
81
|
+
@server.mount_proc('/blocked') { |req, res| requests << req; res.body = 'unexpected' }
|
|
82
|
+
intermediate = cert('subca.cert')
|
|
83
|
+
# This unrelated self-signed certificate cannot be part of the valid path.
|
|
84
|
+
unused = cert('fixtures/verify.foo.cert')
|
|
85
|
+
@server.ssl_context.extra_chain_cert = [intermediate, unused]
|
|
86
|
+
assert_equal('hello', @client.get_content(@url))
|
|
87
|
+
|
|
88
|
+
@client.reset_all
|
|
89
|
+
with_blocked_public_key(intermediate) do
|
|
90
|
+
error = assert_raise(OpenSSL::SSL::SSLError) do
|
|
91
|
+
@client.post("https://localhost:#{serverport}/blocked", 'secret', 'Authorization' => 'secret')
|
|
92
|
+
end
|
|
93
|
+
assert_match(/certificate verify failed|verifycallback failed|revoked|blocked eMudhra/, error.message)
|
|
94
|
+
end
|
|
95
|
+
assert(requests.empty?, 'HTTP request reached a server supplying an intermediate containing a blocked public key')
|
|
96
|
+
|
|
97
|
+
@client.reset_all
|
|
98
|
+
with_blocked_public_key(unused) do
|
|
99
|
+
error = assert_raise(OpenSSL::SSL::SSLError) do
|
|
100
|
+
@client.post("https://localhost:#{serverport}/blocked", 'secret', 'Authorization' => 'secret')
|
|
101
|
+
end
|
|
102
|
+
assert_match(/certificate verify failed|verifycallback failed|revoked|blocked eMudhra/, error.message)
|
|
103
|
+
end
|
|
104
|
+
assert(requests.empty?, 'HTTP request reached a server supplying an unused certificate containing a blocked public key')
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def test_blocked_intermediate_with_default_paths
|
|
108
|
+
escape_env do
|
|
109
|
+
ENV['SSL_CERT_FILE'] = path('ca-chain.pem')
|
|
110
|
+
@client.ssl_config.set_default_paths
|
|
111
|
+
@server.ssl_context.extra_chain_cert = [cert('subca.cert')]
|
|
112
|
+
assert_equal('hello', @client.get_content(@url))
|
|
113
|
+
@client.reset_all
|
|
114
|
+
with_blocked_public_key(cert('subca.cert')) do
|
|
115
|
+
assert_raise(OpenSSL::SSL::SSLError) { @client.get(@url) }
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_blocked_intermediate_with_verification_overrides
|
|
121
|
+
cfg = @client.ssl_config
|
|
122
|
+
cfg.add_trust_ca(path('ca-chain.pem'))
|
|
123
|
+
@server.ssl_context.extra_chain_cert = [cert('subca.cert')]
|
|
124
|
+
assert_equal('hello', @client.get_content(@url))
|
|
125
|
+
@client.reset_all
|
|
126
|
+
with_blocked_public_key(cert('subca.cert')) do
|
|
127
|
+
cfg.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
128
|
+
assert_equal('hello', @client.get_content(@url))
|
|
129
|
+
|
|
130
|
+
cfg.verify_mode = nil
|
|
131
|
+
assert_equal('hello', @client.get_content(@url))
|
|
132
|
+
|
|
133
|
+
cfg.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
134
|
+
callback_results = []
|
|
135
|
+
cfg.verify_callback = proc { |ok, ctx| callback_results << ok; true }
|
|
136
|
+
assert_equal('hello', @client.get_content(@url))
|
|
137
|
+
assert(!callback_results.empty?, 'custom verification callback was not invoked')
|
|
138
|
+
assert(callback_results.all?, 'custom callback did not receive the original verification result')
|
|
139
|
+
|
|
140
|
+
cfg.verify_callback = proc { |ok, ctx| false }
|
|
141
|
+
assert_raise(OpenSSL::SSL::SSLError) { @client.get_content(@url) }
|
|
142
|
+
|
|
143
|
+
cfg.verify_callback = cfg.method(:default_verify_callback)
|
|
144
|
+
assert_raise(OpenSSL::SSL::SSLError) { @client.get_content(@url) }
|
|
145
|
+
|
|
146
|
+
cfg.verify_callback = nil
|
|
147
|
+
assert_raise(OpenSSL::SSL::SSLError) { @client.get_content(@url) }
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
data('VERIFY_NONE with nil' => [OpenSSL::SSL::VERIFY_NONE, nil],
|
|
152
|
+
'VERIFY_NONE with false' => [OpenSSL::SSL::VERIFY_NONE, false],
|
|
153
|
+
'nil with nil' => [nil, nil],
|
|
154
|
+
'nil with false' => [nil, false])
|
|
155
|
+
def test_verification_disabled_ignores_callback_result(settings)
|
|
156
|
+
verify_mode, callback_result = settings
|
|
157
|
+
cfg = @client.ssl_config
|
|
158
|
+
cfg.verify_mode = verify_mode
|
|
159
|
+
callback_certificates = []
|
|
160
|
+
cfg.verify_callback = proc { |ok, ctx|
|
|
161
|
+
callback_certificates << ctx.current_cert
|
|
162
|
+
callback_result
|
|
163
|
+
}
|
|
164
|
+
assert_equal('hello', @client.get_content(@url))
|
|
165
|
+
assert(!callback_certificates.empty?, 'custom verification callback was not invoked')
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def test_custom_verify_callback_with_untrusted_blocked_intermediate
|
|
169
|
+
cfg = @client.ssl_config
|
|
170
|
+
cfg.clear_cert_store
|
|
171
|
+
@server.ssl_context.extra_chain_cert = [cert('subca.cert')]
|
|
172
|
+
callback_results = []
|
|
173
|
+
cfg.verify_callback = proc { |ok, ctx|
|
|
174
|
+
callback_results << [ok, ctx.error]
|
|
175
|
+
true
|
|
176
|
+
}
|
|
177
|
+
with_blocked_public_key(cert('subca.cert')) do
|
|
178
|
+
assert_equal('hello', @client.get_content(@url))
|
|
179
|
+
assert(callback_results.any? { |ok, error| !ok && error && error != 0 },
|
|
180
|
+
'custom callback did not receive the underlying verification error')
|
|
181
|
+
|
|
182
|
+
cfg.verify_callback = proc { |ok, ctx| ok }
|
|
183
|
+
assert_raise(OpenSSL::SSL::SSLError) { @client.get_content(@url) }
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
64
187
|
unless defined?(HTTPClient::JRubySSLSocket)
|
|
65
188
|
# JRubySSLSocket does not support sync mode.
|
|
66
189
|
def test_sync
|
|
@@ -209,7 +332,10 @@ if defined?(HTTPClient::JRubySSLSocket)
|
|
|
209
332
|
assert_match(/No appropriate protocol/, ssle.message)
|
|
210
333
|
end
|
|
211
334
|
#
|
|
212
|
-
|
|
335
|
+
# CI's Temurin 21 disables static-RSA suites such as
|
|
336
|
+
# TLS_RSA_WITH_AES_128_CBC_SHA in jdk.tls.disabledAlgorithms. Use an enabled
|
|
337
|
+
# ECDHE suite to verify explicitly configured ciphers can be negotiated.
|
|
338
|
+
cfg.ciphers = %w(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256)
|
|
213
339
|
assert_equal("hello", @client.get_content(@url))
|
|
214
340
|
#
|
|
215
341
|
cfg.ciphers = HTTPClient::SSLConfig::CIPHERS_DEFAULT
|
|
@@ -263,7 +389,15 @@ end
|
|
|
263
389
|
end
|
|
264
390
|
end
|
|
265
391
|
|
|
266
|
-
if
|
|
392
|
+
if RUBY_ENGINE == 'jruby'
|
|
393
|
+
def test_disallow_tlsv1
|
|
394
|
+
@client.ssl_config.ssl_version = :TLSv1
|
|
395
|
+
@client.ssl_config.verify_mode = nil
|
|
396
|
+
assert_raise(OpenSSL::SSL::SSLError) do
|
|
397
|
+
@client.get(@url)
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
elsif OPENSSL_VERSION < 3
|
|
267
401
|
def test_allow_tlsv1
|
|
268
402
|
teardown_server
|
|
269
403
|
setup_server_with_ssl_version(:TLSv1)
|
|
@@ -280,12 +414,14 @@ end
|
|
|
280
414
|
@client.ssl_config.verify_mode = nil
|
|
281
415
|
@client.get("https://localhost:#{serverport}/hello")
|
|
282
416
|
end
|
|
283
|
-
|
|
417
|
+
# OpenSSL 3.0 may reject TLSv1 with "no suitable signature algorithm",
|
|
418
|
+
# which sends an internal_error alert instead of protocol_version.
|
|
419
|
+
assert_match(/tlsv1 alert (?:protocol version|internal error)/, ssle.message)
|
|
284
420
|
end
|
|
285
421
|
end
|
|
286
422
|
|
|
287
423
|
def test_use_higher_TLS
|
|
288
|
-
omit('TODO: it does not pass with Java 7 or old openssl')
|
|
424
|
+
omit('TODO: it does not pass with Java 7 or old openssl') unless RUBY_ENGINE == 'jruby'
|
|
289
425
|
teardown_server
|
|
290
426
|
setup_server_with_ssl_version('TLSv1_2')
|
|
291
427
|
assert_nothing_raised do
|
|
@@ -323,6 +459,11 @@ end
|
|
|
323
459
|
assert_raises(OpenSSL::SSL::SSLError) do
|
|
324
460
|
@client.get("https://localhost:#{serverport}/hello")
|
|
325
461
|
end
|
|
462
|
+
@client.ssl_config.verify_callback = proc { |ok, ctx| true }
|
|
463
|
+
assert_raises(OpenSSL::SSL::SSLError) do
|
|
464
|
+
@client.get("https://localhost:#{serverport}/hello")
|
|
465
|
+
end
|
|
466
|
+
@client.ssl_config.verify_callback = nil
|
|
326
467
|
@client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
327
468
|
assert_nothing_raised do
|
|
328
469
|
@client.get("https://localhost:#{serverport}/hello")
|
|
@@ -392,6 +533,20 @@ end
|
|
|
392
533
|
|
|
393
534
|
private
|
|
394
535
|
|
|
536
|
+
def with_blocked_public_key(certificate)
|
|
537
|
+
config = HTTPClient::SSLConfig
|
|
538
|
+
original = config::BLOCKED_SPKI_SHA256
|
|
539
|
+
digest = OpenSSL::Digest::SHA256.hexdigest(certificate.public_key.to_der)
|
|
540
|
+
config.send(:remove_const, :BLOCKED_SPKI_SHA256)
|
|
541
|
+
config.const_set(:BLOCKED_SPKI_SHA256, original + [digest])
|
|
542
|
+
yield
|
|
543
|
+
ensure
|
|
544
|
+
if original
|
|
545
|
+
config.send(:remove_const, :BLOCKED_SPKI_SHA256)
|
|
546
|
+
config.const_set(:BLOCKED_SPKI_SHA256, original)
|
|
547
|
+
end
|
|
548
|
+
end
|
|
549
|
+
|
|
395
550
|
def cert(filename)
|
|
396
551
|
OpenSSL::X509::Certificate.new(File.read(File.join(DIR, filename)))
|
|
397
552
|
end
|