curb 1.4.1 → 1.5.1

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.
@@ -52,6 +52,30 @@ class TestCurbCurlEasy < Test::Unit::TestCase
52
52
  assert_match(/^# DO NOT REMOVE THIS COMMENT/, easy.body_str)
53
53
  end
54
54
 
55
+ def test_options_survive_gc_between_allocation_and_initialization
56
+ subclass = Class.new(Curl::Easy) do
57
+ attr_reader :initial_options
58
+
59
+ def initialize(url)
60
+ @initial_options = [self.url, proxy_url]
61
+ super
62
+ end
63
+ end
64
+
65
+ previous_stress = GC.stress
66
+ begin
67
+ GC.stress = true
68
+ allocated = Curl::Easy.allocate
69
+ initialized = subclass.new($TEST_URL)
70
+ ensure
71
+ GC.stress = previous_stress
72
+ end
73
+
74
+ assert_nil allocated.url
75
+ assert_equal [nil, nil], initialized.initial_options
76
+ assert_equal $TEST_URL, initialized.url
77
+ end
78
+
55
79
  def test_connection_info_before_perform
56
80
  easy = Curl::Easy.new
57
81
  CONNECTION_INFO_METHODS.each do |method|
@@ -179,6 +203,32 @@ class TestCurbCurlEasy < Test::Unit::TestCase
179
203
  assert_match(/GET/, http.body)
180
204
  end
181
205
 
206
+ # The default body/header buffers used to reserve 32 KB/16 KB up front, and
207
+ # that capacity stayed attached to every returned string.
208
+ def test_small_response_strings_do_not_retain_large_buffers
209
+ require 'objspace'
210
+ easy = Curl::Easy.new(TestServlet.url)
211
+ easy.perform
212
+
213
+ body = easy.body_str
214
+ header = easy.header_str
215
+ assert_operator body.bytesize, :>, 0
216
+ assert_operator ObjectSpace.memsize_of(body), :<, 4096, 'body_str keeps excess capacity'
217
+ assert_operator ObjectSpace.memsize_of(header), :<, 4096, 'header_str keeps excess capacity'
218
+ end
219
+
220
+ def test_large_response_body_is_complete
221
+ require 'objspace'
222
+ server = CurbTinyHTTPServer.new
223
+ expected = CurbTinyHTTPServer.stream_body(64, 4096)
224
+ easy = Curl::Easy.new(server.url('/stream/64/4096'))
225
+ easy.perform
226
+ assert_equal expected, easy.body_str
227
+ assert_operator ObjectSpace.memsize_of(easy.body_str), :<, expected.bytesize + 4096
228
+ ensure
229
+ server.close if server
230
+ end
231
+
182
232
  def test_curlopt_stderr_with_file
183
233
  # does not work with Tempfile directly
184
234
  path = Tempfile.new('curb_test_curlopt_stderr').path
@@ -239,6 +289,92 @@ class TestCurbCurlEasy < Test::Unit::TestCase
239
289
  tmp.close! if defined?(tmp) && tmp
240
290
  end
241
291
 
292
+ # libcurl used to write through the IO's own FILE*, which Ruby frees on
293
+ # close. curb now owns a stream on a duplicate descriptor instead.
294
+ def test_curlopt_stderr_survives_block_form_file_close
295
+ tmp = Tempfile.new('curb_test_curlopt_stderr_closed')
296
+ easy = Curl::Easy.new(TestServlet.url)
297
+ easy.verbose = true
298
+ File.open(tmp.path, 'w') { |file| easy.setopt(Curl::CURLOPT_STDERR, file) }
299
+
300
+ 2.times { easy.perform }
301
+
302
+ output = File.read(tmp.path)
303
+ assert_equal 2, output.scan(/HTTP\/1\.1 200 OK/).length
304
+ ensure
305
+ tmp.close! if tmp
306
+ end
307
+
308
+ def test_curlopt_stderr_survives_close_after_multi_add
309
+ tmp = Tempfile.new('curb_test_curlopt_stderr_multi')
310
+ file = File.open(tmp.path, 'w')
311
+ easy = Curl::Easy.new(TestServlet.url)
312
+ easy.verbose = true
313
+ easy.setopt(Curl::CURLOPT_STDERR, file)
314
+ multi = Curl::Multi.new
315
+ multi.add(easy)
316
+ file.close
317
+ GC.start
318
+
319
+ multi.perform
320
+
321
+ assert_equal 200, easy.response_code
322
+ assert_match(/HTTP\/1\.1 200 OK/, File.read(tmp.path))
323
+ ensure
324
+ multi.close if multi
325
+ tmp.close! if tmp
326
+ end
327
+
328
+ def test_curlopt_stderr_clone_owns_its_stream
329
+ tmp = Tempfile.new('curb_test_curlopt_stderr_clone')
330
+ easy = Curl::Easy.new(TestServlet.url)
331
+ easy.verbose = true
332
+ File.open(tmp.path, 'w') { |file| easy.setopt(Curl::CURLOPT_STDERR, file) }
333
+ copy = easy.clone
334
+
335
+ # Releasing the original's stream must not affect the clone's.
336
+ easy.close
337
+ GC.start
338
+ copy.perform
339
+
340
+ assert_equal 200, copy.response_code
341
+ assert_match(/HTTP\/1\.1 200 OK/, File.read(tmp.path))
342
+ ensure
343
+ tmp.close! if tmp
344
+ end
345
+
346
+ def test_curlopt_stderr_streams_are_released
347
+ fd_dir = '/proc/self/fd'
348
+ omit('needs /proc/self/fd to count open descriptors') unless File.directory?(fd_dir)
349
+ tmp = Tempfile.new('curb_test_curlopt_stderr_release')
350
+ file = File.open(tmp.path, 'w')
351
+ easy = Curl::Easy.new(TestServlet.url)
352
+ before = Dir.children(fd_dir).length
353
+
354
+ # Replacement, reset, and close each release curb's descriptor.
355
+ 20.times { easy.setopt(Curl::CURLOPT_STDERR, file) }
356
+ easy.reset
357
+ easy.setopt(Curl::CURLOPT_STDERR, file)
358
+ easy.close
359
+ easy.setopt(Curl::CURLOPT_STDERR, file)
360
+ easy.clone.close
361
+ easy.close
362
+
363
+ assert_equal before, Dir.children(fd_dir).length
364
+ ensure
365
+ file.close if file && !file.closed?
366
+ tmp.close! if tmp
367
+ end
368
+
369
+ def test_curlopt_stderr_rejects_unwritable_io
370
+ tmp = Tempfile.new('curb_test_curlopt_stderr_readonly')
371
+ File.open(tmp.path, 'r') do |file|
372
+ assert_raise(IOError) { Curl::Easy.new.setopt(Curl::CURLOPT_STDERR, file) }
373
+ end
374
+ ensure
375
+ tmp.close! if tmp
376
+ end
377
+
242
378
  def test_head_request_no_body_and_no_timeout
243
379
  # Ensure a HEAD request completes and does not attempt to read a body
244
380
  # even when the server advertises a Content-Length.
@@ -667,6 +803,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
667
803
 
668
804
  def test_escape
669
805
  c = Curl::Easy.new
806
+ assert_equal "", c.escape("")
670
807
 
671
808
  assert_equal "one%20two", c.escape('one two')
672
809
  assert_equal "one%00two%20three", c.escape("one\000two three")
@@ -674,6 +811,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
674
811
 
675
812
  def test_unescape
676
813
  c = Curl::Easy.new
814
+ assert_equal "", c.unescape("")
677
815
 
678
816
  assert_equal "one two", c.unescape('one%20two')
679
817
 
@@ -683,6 +821,19 @@ class TestCurbCurlEasy < Test::Unit::TestCase
683
821
  end
684
822
  end
685
823
 
824
+ def test_url_conversion_rejects_lengths_larger_than_libcurl_int
825
+ int_size = [0].pack('i!').bytesize
826
+ omit('Ruby string lengths fit in int on this platform') if [0].pack('l!').bytesize <= int_size
827
+ omit('set CURB_LARGE_STRING_TESTS=1 to allocate an INT_MAX + 1 byte string') unless ENV['CURB_LARGE_STRING_TESTS'] == '1'
828
+
829
+ oversized = "a" * (1 << (int_size * 8 - 1))
830
+ easy = Curl::Easy.new
831
+ assert_raise(RangeError) { easy.escape(oversized) }
832
+ assert_raise(RangeError) { easy.unescape(oversized) }
833
+ assert_raise(RangeError) { Curl::PostField.content(oversized, "v").to_s }
834
+ assert_raise(RangeError) { Curl::PostField.content("k", oversized).to_s }
835
+ end
836
+
686
837
  def test_escape_with_non_string_to_s_result_does_not_crash
687
838
  c = Curl::Easy.new
688
839
  result = nil
@@ -1423,8 +1574,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1423
1574
  end
1424
1575
 
1425
1576
  def test_post_body_preserves_assigned_snapshot_when_original_string_is_mutated
1426
- omit('CURLOPT_COPYPOSTFIELDS is not available in this build') unless Curl.const_defined?(:CURLOPT_COPYPOSTFIELDS)
1427
-
1428
1577
  curl = Curl::Easy.new(TestServlet.url)
1429
1578
  post_body = String.new('alpha=1')
1430
1579
  curl.post_body = post_body
@@ -1454,8 +1603,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1454
1603
  end
1455
1604
 
1456
1605
  def test_setopt_postfields_preserves_assigned_snapshot_when_original_string_is_mutated
1457
- omit('CURLOPT_COPYPOSTFIELDS is not available in this build') unless Curl.const_defined?(:CURLOPT_COPYPOSTFIELDS)
1458
-
1459
1606
  curl = Curl::Easy.new(TestServlet.url)
1460
1607
  post_body = String.new('alpha=1')
1461
1608
  curl.set(Curl::CURLOPT_POSTFIELDS, post_body)
@@ -1471,6 +1618,18 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1471
1618
  assert_equal 'alpha=1', curl.post_body
1472
1619
  end
1473
1620
 
1621
+ def test_cloned_handle_retains_binary_post_body_after_original_is_reset
1622
+ original = Curl::Easy.new(TestServlet.url)
1623
+ original.post_body = "a\0b"
1624
+ copy = original.dup
1625
+ original.reset
1626
+ copy.post_body.replace("new value")
1627
+ GC.compact if GC.respond_to?(:compact)
1628
+
1629
+ copy.perform
1630
+ assert_equal "POST\na\0b", copy.body_str
1631
+ end
1632
+
1474
1633
  def test_setopt_postfields_nil_preserves_post_request
1475
1634
  curl = Curl::Easy.new(TestServlet.url)
1476
1635
  curl.set(Curl::CURLOPT_POST, true)
@@ -2119,4 +2278,80 @@ class TestCurbCurlEasy < Test::Unit::TestCase
2119
2278
  server_setup
2120
2279
  end
2121
2280
 
2281
+ # Replacing request state while libcurl is still sending the old state
2282
+ # freed buffers libcurl was reading (use-after-free). Every entry point that
2283
+ # hands libcurl new request state must refuse while the handle is active.
2284
+ ACTIVE_HANDLE_MUTATORS = {
2285
+ 'post_body=' => ->(easy) { easy.post_body = 'replacement' },
2286
+ 'put_data=' => ->(easy) { easy.put_data = 'replacement' },
2287
+ 'autoreferer=' => ->(easy) { easy.autoreferer = true },
2288
+ 'http_post' => ->(easy) { easy.http_post('replacement') },
2289
+ 'http_put' => ->(easy) { easy.http_put('replacement') },
2290
+ 'http_patch' => ->(easy) { easy.http_patch('replacement') },
2291
+ 'http_delete' => ->(easy) { easy.http_delete },
2292
+ 'http(:OPTIONS)' => ->(easy) { easy.http(:OPTIONS) },
2293
+ '__curb_native_setup!' => ->(easy) { easy.__send__(:__curb_native_setup!) },
2294
+ }.freeze
2295
+
2296
+ def test_request_state_cannot_be_replaced_during_own_transfer
2297
+ ACTIVE_HANDLE_MUTATORS.each do |name, mutate|
2298
+ easy = Curl::Easy.new(TestServlet.url)
2299
+ easy.post_body = 'original'
2300
+ errors = []
2301
+ easy.on_body do |data|
2302
+ begin
2303
+ mutate.call(easy)
2304
+ rescue RuntimeError => e
2305
+ errors << e
2306
+ end
2307
+ data.bytesize
2308
+ end
2309
+
2310
+ easy.perform
2311
+
2312
+ assert_equal 1, errors.length, name
2313
+ assert_match(/Cannot modify an active curl handle/, errors.first.message, name)
2314
+ assert_equal 200, easy.response_code, name
2315
+ assert_equal 'original', easy.post_body, name
2316
+
2317
+ # Once idle, the same mutation is allowed again.
2318
+ easy.on_body
2319
+ assert_nothing_raised(name) { mutate.call(easy) }
2320
+ end
2321
+ end
2322
+
2323
+ def test_request_state_cannot_be_replaced_on_active_sibling_transfer
2324
+ ACTIVE_HANDLE_MUTATORS.each do |name, mutate|
2325
+ # The sibling's response is delayed so it is still in flight when the
2326
+ # first transfer's body callback runs.
2327
+ server = CurbTinyHTTPServer.new
2328
+ multi = Curl::Multi.new
2329
+ first = Curl::Easy.new(TestServlet.url)
2330
+ sibling = Curl::Easy.new(server.url('/delay/0.3'))
2331
+ sibling.post_body = 'original'
2332
+ errors = []
2333
+ first.on_body do |data|
2334
+ begin
2335
+ mutate.call(sibling)
2336
+ rescue RuntimeError => e
2337
+ errors << e
2338
+ end
2339
+ data.bytesize
2340
+ end
2341
+
2342
+ multi.add(first)
2343
+ multi.add(sibling)
2344
+ multi.perform
2345
+
2346
+ assert_operator errors.length, :>=, 1, name
2347
+ errors.each { |e| assert_match(/Cannot modify an active curl handle/, e.message, name) }
2348
+ assert_equal 200, sibling.response_code, name
2349
+ assert_equal 'delayed', sibling.body_str, name
2350
+ assert_equal 'original', sibling.post_body, name
2351
+ ensure
2352
+ multi.close if multi
2353
+ server.close if server
2354
+ end
2355
+ end
2356
+
2122
2357
  end
@@ -799,6 +799,7 @@ class TestCurbCurlMulti < Test::Unit::TestCase
799
799
  end
800
800
 
801
801
  def test_native_safety_signatures_are_pruned_after_perform
802
+ Curl.safe! { |config| config.network_policy = :none }
802
803
  m = Curl::Multi.new
803
804
  easy = Curl::Easy.new($TEST_URL)
804
805
 
@@ -811,6 +812,37 @@ class TestCurbCurlMulti < Test::Unit::TestCase
811
812
 
812
813
  assert_equal 0, m.requests.length
813
814
  assert_empty signatures
815
+ ensure
816
+ m.close if m
817
+ Curl.__send__(:clear_safe!)
818
+ end
819
+
820
+ def test_unconstrained_easy_skips_safety_signature_bookkeeping
821
+ m = Curl::Multi.new
822
+ easy = Curl::Easy.new(TestServlet.url)
823
+
824
+ m.add(easy)
825
+ assert_empty m.__send__(:__curb_native_safety_signatures)
826
+
827
+ m.perform
828
+ assert_equal 200, easy.response_code
829
+ ensure
830
+ m.close if m
831
+ end
832
+
833
+ def test_native_policy_set_after_unconstrained_add_is_applied
834
+ m = Curl::Multi.new
835
+ easy = Curl::Easy.new(TestServlet.url)
836
+ m.add(easy)
837
+ begin
838
+ easy.network_policy = :public
839
+ rescue NotImplementedError
840
+ omit('network_policy=:public requires CURLOPT_OPENSOCKETFUNCTION support')
841
+ end
842
+
843
+ m.perform
844
+
845
+ assert_match(/127\.0\.0\.1/, easy.unsafe_destination_error)
814
846
  ensure
815
847
  m.close if m
816
848
  end
@@ -95,7 +95,13 @@ class TestCurbCurlNativeCoverage < Test::Unit::TestCase
95
95
  easy = Curl::Easy.new(TestServlet.url)
96
96
  easy.put_data = 'clone-data'
97
97
 
98
- clone = easy.clone
98
+ previous_stress = GC.stress
99
+ begin
100
+ GC.stress = true
101
+ clone = easy.clone
102
+ ensure
103
+ GC.stress = previous_stress
104
+ end
99
105
  easy.put_data = 'other-data'
100
106
 
101
107
  clone.perform
@@ -1018,43 +1018,36 @@ class TestCurbCurlNetworkPolicy < Test::Unit::TestCase
1018
1018
  Curl.__send__(:clear_safe!) if Curl.respond_to?(:clear_safe!, true)
1019
1019
  end
1020
1020
 
1021
- def test_public_network_policy_blocks_private_peer_after_live_dns_answer_changes
1022
- omit('live DNS rebinding test requires CURLOPT_DOH_URL') unless Curl.const_defined?(:CURLOPT_DOH_URL)
1023
- omit('live DNS rebinding test requires CURLOPT_DOH_SSL_VERIFYPEER') unless Curl.const_defined?(:CURLOPT_DOH_SSL_VERIFYPEER)
1024
- omit('live DNS rebinding test requires CURLOPT_DOH_SSL_VERIFYHOST') unless Curl.const_defined?(:CURLOPT_DOH_SSL_VERIFYHOST)
1021
+ # The peer address is vetted at connect time for every resolution, so a
1022
+ # name that resolves to an allowed address once and a private one later is
1023
+ # still blocked. (This used a loopback DoH server as the changing resolver;
1024
+ # public policy now refuses non-public DoH endpoints, so CURLOPT_RESOLVE
1025
+ # supplies the two answers instead.)
1026
+ def test_public_network_policy_blocks_private_peer_after_dns_answer_changes
1025
1027
  require_public_network_policy!
1026
1028
 
1027
- host = 'curb-doh-rebind.invalid'
1028
- with_rebinding_doh_server(host, '1.1.1.1') do |doh_url, doh_state|
1029
- Curl.safe! do |config|
1030
- config.network_policy = :public
1031
- config.allow_doh = true
1032
- config.allowed_cidrs = ['8.8.8.0/24']
1033
- end
1034
-
1035
- first = Curl::Easy.new("http://#{host}/dns-rebind-target")
1036
- configure_live_doh_easy(first, doh_url)
1037
-
1038
- first_error = assert_raise(Curl::Err::UnsafeDestinationError) do
1039
- first.perform
1040
- end
1041
-
1042
- assert_include doh_a_answers(doh_state), '1.1.1.1'
1043
- assert_match(/outside allowed CIDR ranges/, first_error.message)
1044
- assert_match(/1\.1\.1\.1/, first_error.message)
1045
-
1046
- set_doh_answer(doh_state, '127.0.0.1')
1047
- second = Curl::Easy.new("http://#{host}/dns-rebind-target")
1048
- configure_live_doh_easy(second, doh_url)
1029
+ host = 'curb-dns-rebind.invalid'
1030
+ Curl.safe! do |config|
1031
+ config.network_policy = :public
1032
+ config.allow_resolve = true
1033
+ config.allowed_cidrs = ['8.8.8.0/24']
1034
+ end
1049
1035
 
1050
- error = assert_raise(Curl::Err::UnsafeDestinationError) do
1051
- second.perform
1052
- end
1036
+ first = Curl::Easy.new("http://#{host}/dns-rebind-target")
1037
+ first.resolve = ["#{host}:80:1.1.1.1"]
1038
+ first_error = assert_raise(Curl::Err::UnsafeDestinationError) do
1039
+ first.perform
1040
+ end
1041
+ assert_match(/outside allowed CIDR ranges/, first_error.message)
1042
+ assert_match(/1\.1\.1\.1/, first_error.message)
1053
1043
 
1054
- assert_include doh_a_answers(doh_state), '127.0.0.1'
1055
- assert_match(/public network policy/, error.message)
1056
- assert_match(/127\.0\.0\.1/, error.message)
1044
+ second = Curl::Easy.new("http://#{host}/dns-rebind-target")
1045
+ second.resolve = ["#{host}:80:127.0.0.1"]
1046
+ error = assert_raise(Curl::Err::UnsafeDestinationError) do
1047
+ second.perform
1057
1048
  end
1049
+ assert_match(/public network policy/, error.message)
1050
+ assert_match(/127\.0\.0\.1/, error.message)
1058
1051
  ensure
1059
1052
  Curl.__send__(:clear_safe!) if Curl.respond_to?(:clear_safe!, true)
1060
1053
  end
@@ -131,6 +131,11 @@ class TestCurbCurlPostfield < Test::Unit::TestCase
131
131
  assert_equal "foo=bar%20ton", pf.to_s
132
132
  end
133
133
 
134
+ def test_to_s_encodes_names_and_values_by_byte
135
+ assert_equal "=", Curl::PostField.content("", "").to_s
136
+ assert_equal "%C3%A9%00=%00%2B", Curl::PostField.content("é\0", "\0+").to_s
137
+ end
138
+
134
139
  def test_to_s_03
135
140
  pf = Curl::PostField.content('foo') { |field| field.name.upcase + "BAR" }
136
141
  assert_equal "foo=FOOBAR", pf.to_s