curb 1.5.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d59fbe309c871b35fe03022151783ee2f88a2a348847758034c4ef83034e868
4
- data.tar.gz: 3c143d3940a09679b69bd9d975342bbdbe1a200d1cf24fa6756714570d977d1b
3
+ metadata.gz: e58057d0eaf447ccbf4071247bd909f6c2cda4f7542a137a6c388fed885c82ee
4
+ data.tar.gz: bd250dddc484e8e3f810cccecb966152aa651709ed60e6f161fb98771d5f392a
5
5
  SHA512:
6
- metadata.gz: 41ecf3ceed954e7a79b19db4b4969e2186407cf9d071b570471e4867d1a20521adf1a0091fa2c2c98902817f3c6c2ed435d11d0f9c3b9137ffd9923a11792468
7
- data.tar.gz: 04cdc459106867e381df0c89db22448636cca5dbe6f7073ba0f7b70d9124030bd5a909b3c90de6242d3e2fe865ea6a296fcbd1fd35960baba5d6ca51f241c6c0
6
+ metadata.gz: c0a3eb2bb249298da932afb628b7d7f413d34d4090278cc263a645dc43ba091d819e561652868ba43987c5e64f721cc1884172c06b4c0647e94d445c53d7ae73
7
+ data.tar.gz: 5e625655853ca0221bf69cde15856a9fea379a2540c2b654918e0b27365a7b4efa8e91ab9748195ac2eca469d4144aaee5974e33fe337fc09a228f1678e93c36
data/ChangeLog.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # ChangeLog
2
+ ## 1.5.1
3
+
4
+ * Fix GC rooting during `Curl::Easy` allocation and cloning, preserve POST buffers on older libcurl builds, and harden allowlist parsing and native object cleanup. Oversized inputs to URL encoding and decoding now raise `RangeError`.
5
+
2
6
  ## 1.5.0
3
7
  * Behavior changes (may affect code that does not use the safety features): redirects are now capped at 30 when `max_redirects` is unset, where curb previously passed unlimited (set `max_redirects = -1` to restore unlimited); CR, LF, and NUL in headers, proxy headers, `useragent`, `cookies`, custom request methods, and `CURLOPT_REQUEST_TARGET` now raise `ArgumentError` instead of being sent; `easy.cookies=` is no longer sent to other hosts after a cross-host redirect on libcurl 7.83+; and replacing `post_body`/`put_data` or calling `http_post`/`http_put`/`http_patch`/`http_delete`/`http(verb)` while a transfer is active now raises `RuntimeError`. Under `network_policy = :public` (including `Curl.safe!`), `file://` URLs without a protocol allowlist, non-IP-literal DoH URLs, and Unix sockets without `allow_unix_socket` are refused, and safe mode now defaults to a 60s timeout and 20 redirects. Details for each are below.
4
8
  * Read `Curl::Multi.default_timeout` once per `Curl::Multi#perform` instead of on every wait-loop pass. The setting lives in Ractor-local storage, so each read cost two Ruby method calls, and the polling loop read it up to twice per pass (50 MB download: 132–166 `Ractor.current` calls per perform → 14, independent of transfer length). Applies to both the polling and Fiber-scheduler socket-action loops. Changing `default_timeout` from a callback or perform block now takes effect on the next `perform`.
data/ext/curb.h CHANGED
@@ -28,11 +28,11 @@
28
28
  #include "curb_macros.h"
29
29
 
30
30
  // These should be managed from the Rake 'release' task.
31
- #define CURB_VERSION "1.5.0"
32
- #define CURB_VER_NUM 1050
31
+ #define CURB_VERSION "1.5.1"
32
+ #define CURB_VER_NUM 1051
33
33
  #define CURB_VER_MAJ 1
34
34
  #define CURB_VER_MIN 5
35
- #define CURB_VER_MIC 0
35
+ #define CURB_VER_MIC 1
36
36
  #define CURB_VER_PATCH 0
37
37
 
38
38
 
data/ext/curb_easy.c CHANGED
@@ -11,6 +11,7 @@
11
11
  #include "curb_multi.h"
12
12
 
13
13
  #include <errno.h>
14
+ #include <limits.h>
14
15
  #include <stdlib.h>
15
16
  #include <string.h>
16
17
  #ifndef _WIN32
@@ -402,7 +403,7 @@ static char *curb_normalize_host_value_impl(const char *value, int raise_errors)
402
403
  }
403
404
 
404
405
  for (scan = start; scan < authority_end; scan++) {
405
- if (*scan == ':') colon_count++;
406
+ if (*scan == ':' && ++colon_count == 2) break;
406
407
  }
407
408
 
408
409
  if (colon_count <= 1) {
@@ -442,6 +443,7 @@ static VALUE curb_normalize_host_list(VALUE hosts) {
442
443
  VALUE item = rb_ary_entry(list, i);
443
444
  VALUE host_value = rb_obj_as_string(item);
444
445
  char *host = curb_normalize_host_value(StringValueCStr(host_value));
446
+ RB_GC_GUARD(host_value);
445
447
  VALUE normalized_host = rb_str_new_cstr(host);
446
448
 
447
449
  if (!curb_array_includes_string(normalized, normalized_host)) {
@@ -493,6 +495,7 @@ static void curb_prepare_network_allowed_hosts(ruby_curl_easy *rbce) {
493
495
  for (i = 0; i < count; i++) {
494
496
  VALUE host = rb_ary_entry(hosts, i);
495
497
  rules[i] = curb_strdup_cstr(StringValueCStr(host));
498
+ RB_GC_GUARD(host);
496
499
  }
497
500
 
498
501
  rbce->network_allowed_hosts = rules;
@@ -531,6 +534,7 @@ static void curb_prepare_network_allowed_cidr_rules(ruby_curl_easy *rbce) {
531
534
  for (i = 0; i < count; i++) {
532
535
  VALUE cidr = rb_ary_entry(cidrs, i);
533
536
  curb_parse_cidr_rule(StringValueCStr(cidr), &rules[i]);
537
+ RB_GC_GUARD(cidr);
534
538
  }
535
539
 
536
540
  rbce->network_allowed_cidr_rules = rules;
@@ -1304,6 +1308,9 @@ static void curl_easy_mark(void *ptr) {
1304
1308
  if (!NIL_P(rbce->opts)) { rb_gc_mark(rbce->opts); }
1305
1309
  if (!NIL_P(rbce->multi)) { rb_gc_mark(rbce->multi); }
1306
1310
  if (!NIL_P(rbce->callback_error)) { rb_gc_mark(rbce->callback_error); }
1311
+ #ifndef HAVE_CURLOPT_COPYPOSTFIELDS
1312
+ rb_gc_mark(rbce->borrowed_post_body);
1313
+ #endif
1307
1314
  }
1308
1315
  }
1309
1316
 
@@ -1505,7 +1512,7 @@ static void curl_easy_free(void *ptr) {
1505
1512
  ruby_curl_easy *rbce = (ruby_curl_easy *)ptr;
1506
1513
  if (rbce) {
1507
1514
  ruby_curl_easy_free(rbce);
1508
- free(rbce);
1515
+ xfree(rbce);
1509
1516
  }
1510
1517
  }
1511
1518
 
@@ -1586,6 +1593,9 @@ static ruby_curl_easy *ruby_curl_easy_get_idle_handle(VALUE self) {
1586
1593
 
1587
1594
  static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
1588
1595
  rbce->opts = rb_hash_new();
1596
+ #ifndef HAVE_CURLOPT_COPYPOSTFIELDS
1597
+ rbce->borrowed_post_body = Qnil;
1598
+ #endif
1589
1599
 
1590
1600
  memset(rbce->err_buf, 0, CURL_ERROR_SIZE);
1591
1601
  memset(rbce->unsafe_destination_error, 0, CURL_ERROR_SIZE);
@@ -1662,15 +1672,21 @@ static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
1662
1672
  */
1663
1673
  static VALUE ruby_curl_easy_allocate(VALUE klass) {
1664
1674
  ruby_curl_easy *rbce;
1665
- rbce = ALLOC(ruby_curl_easy);
1666
- if (!rbce) {
1667
- rb_raise(rb_eNoMemError, "Failed to allocate memory for Curl::Easy");
1668
- }
1669
- rbce->curl = NULL;
1675
+ /* Wrap a zeroed struct before ruby_curl_easy_zero allocates the opts hash:
1676
+ * the hash is only reachable by the GC once a Ruby object owns the struct.
1677
+ * Zeroed native pointers also keep the free function safe if that
1678
+ * allocation raises. */
1679
+ VALUE self = TypedData_Make_Struct(klass, ruby_curl_easy, &ruby_curl_easy_data_type, rbce);
1670
1680
  rbce->opts = Qnil;
1671
1681
  rbce->multi = Qnil;
1682
+ rbce->self = Qnil;
1683
+ rbce->callback_error = Qnil;
1684
+ #ifndef HAVE_CURLOPT_COPYPOSTFIELDS
1685
+ rbce->borrowed_post_body = Qnil;
1686
+ #endif
1672
1687
  ruby_curl_easy_zero(rbce);
1673
- return TypedData_Wrap_Struct(klass, &ruby_curl_easy_data_type, rbce);
1688
+ RB_GC_GUARD(self);
1689
+ return self;
1674
1690
  }
1675
1691
 
1676
1692
  /*
@@ -1780,6 +1796,7 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
1780
1796
  ruby_curl_easy *rbce, *newrbce;
1781
1797
 
1782
1798
  FILE *clone_stderr_file = NULL;
1799
+ VALUE clone_opts = Qnil;
1783
1800
 
1784
1801
  rbce = ruby_curl_easy_get_handle(self);
1785
1802
 
@@ -1838,7 +1855,9 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
1838
1855
  newrbce->native_active = 0;
1839
1856
 
1840
1857
  if (rbce->opts != Qnil) {
1841
- newrbce->opts = rb_funcall(rbce->opts, rb_intern("dup"), 0);
1858
+ /* Not reachable through newrbce until it is wrapped below. */
1859
+ clone_opts = rb_funcall(rbce->opts, rb_intern("dup"), 0);
1860
+ newrbce->opts = clone_opts;
1842
1861
  }
1843
1862
 
1844
1863
  /* Set the error buffer on the new curl handle using the new err_buf */
@@ -1860,6 +1879,7 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
1860
1879
  }
1861
1880
 
1862
1881
  VALUE clone = TypedData_Wrap_Struct(cCurlEasy, &ruby_curl_easy_data_type, newrbce);
1882
+ RB_GC_GUARD(clone_opts);
1863
1883
  newrbce->self = clone;
1864
1884
  curl_easy_setopt(newrbce->curl, CURLOPT_PRIVATE, (void*)newrbce);
1865
1885
 
@@ -2300,6 +2320,14 @@ struct post_body_set_args {
2300
2320
  int force_http_get_on_nil;
2301
2321
  };
2302
2322
 
2323
+ static void clear_postfields(ruby_curl_easy *rbce) {
2324
+ curl_easy_setopt(rbce->curl, CURLOPT_POSTFIELDS, NULL);
2325
+ curl_easy_setopt(rbce->curl, CURLOPT_POSTFIELDSIZE, 0L);
2326
+ #ifndef HAVE_CURLOPT_COPYPOSTFIELDS
2327
+ rbce->borrowed_post_body = Qnil;
2328
+ #endif
2329
+ }
2330
+
2303
2331
  static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
2304
2332
  struct post_body_set_args *args = (struct post_body_set_args *)argp;
2305
2333
  VALUE self = args->self;
@@ -2310,17 +2338,16 @@ static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
2310
2338
 
2311
2339
  VALUE body_str;
2312
2340
  VALUE retained_body_str;
2313
- char *data;
2314
- long len;
2341
+ VALUE transfer_body;
2342
+ CURLcode result;
2315
2343
 
2316
2344
  rbce = ruby_curl_easy_get_idle_handle(self);
2317
2345
 
2318
2346
  curl = rbce->curl;
2319
2347
 
2320
2348
  if ( post_body == Qnil ) {
2349
+ clear_postfields(rbce);
2321
2350
  rb_easy_del("postdata_buffer");
2322
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
2323
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
2324
2351
  if (force_http_get_on_nil) {
2325
2352
  curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
2326
2353
  }
@@ -2338,30 +2365,31 @@ static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
2338
2365
 
2339
2366
  StringValue(body_str);
2340
2367
 
2341
- // Store the string, since it has to hang around for the duration of the
2342
- // request. See CURLOPT_POSTFIELDS in the libcurl docs.
2343
- #ifdef HAVE_CURLOPT_COPYPOSTFIELDS
2344
- /*
2345
- * libcurl copies the bytes immediately for COPYPOSTFIELDS, so retain a
2346
- * matching Ruby snapshot for post_body instead of the caller's mutable
2347
- * source string.
2348
- */
2349
2368
  retained_body_str = rb_str_dup(body_str);
2369
+ #ifdef HAVE_CURLOPT_COPYPOSTFIELDS
2370
+ transfer_body = retained_body_str;
2350
2371
  #else
2351
- retained_body_str = body_str;
2372
+ /* libcurl borrows this pointer. Neither the source nor the mutable
2373
+ * post_body getter can own the bytes. Pin the private copy in mark. */
2374
+ transfer_body = rb_str_tmp_new(RSTRING_LEN(retained_body_str));
2375
+ memcpy(RSTRING_PTR(transfer_body), RSTRING_PTR(retained_body_str), RSTRING_LEN(transfer_body));
2352
2376
  #endif
2353
- data = StringValuePtr(retained_body_str);
2354
- len = RSTRING_LEN(retained_body_str);
2355
2377
  rb_easy_set("postdata_buffer", retained_body_str);
2356
2378
 
2357
- curl_easy_setopt(curl, CURLOPT_POST, 1);
2358
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
2379
+ result = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, RSTRING_LEN(transfer_body));
2380
+ if (result == CURLE_OK) {
2359
2381
  #ifdef HAVE_CURLOPT_COPYPOSTFIELDS
2360
- curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, data);
2382
+ result = curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, RSTRING_PTR(transfer_body));
2361
2383
  #else
2362
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
2384
+ result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, RSTRING_PTR(transfer_body));
2385
+ rbce->borrowed_post_body = transfer_body;
2363
2386
  #endif
2364
-
2387
+ }
2388
+ if (result != CURLE_OK) {
2389
+ clear_postfields(rbce);
2390
+ rb_easy_del("postdata_buffer");
2391
+ raise_curl_easy_error_exception(result);
2392
+ }
2365
2393
  return post_body;
2366
2394
  }
2367
2395
 
@@ -2479,8 +2507,7 @@ static VALUE ruby_curl_easy_put_data_set(VALUE self, VALUE data) {
2479
2507
 
2480
2508
  curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
2481
2509
  curl_easy_setopt(curl, CURLOPT_POST, 0);
2482
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
2483
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
2510
+ clear_postfields(rbce);
2484
2511
  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
2485
2512
  curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback)read_data_handler);
2486
2513
  #ifdef HAVE_CURLOPT_SEEKFUNCTION
@@ -4777,8 +4804,7 @@ static VALUE perform_with_request_restore_ensure(VALUE argp) {
4777
4804
  }
4778
4805
  if (args->clear_postfields) {
4779
4806
  curl_easy_setopt(args->curl, CURLOPT_POST, 0L);
4780
- curl_easy_setopt(args->curl, CURLOPT_POSTFIELDS, NULL);
4781
- curl_easy_setopt(args->curl, CURLOPT_POSTFIELDSIZE, 0L);
4807
+ clear_postfields(args->rbce);
4782
4808
  curl_easy_setopt(args->curl, CURLOPT_HTTPGET, 1L);
4783
4809
  if (args->rbce && !NIL_P(args->rbce->opts)) {
4784
4810
  rb_hash_delete(args->rbce->opts, rb_easy_hkey("postdata_buffer"));
@@ -6449,12 +6475,13 @@ static VALUE ruby_curl_easy_inspect(VALUE self) {
6449
6475
 
6450
6476
  struct curl_string_result {
6451
6477
  char *data;
6452
- long length;
6478
+ size_t length;
6453
6479
  };
6454
6480
 
6455
6481
  static VALUE curl_string_result_body(VALUE argp) {
6456
6482
  struct curl_string_result *args = (struct curl_string_result *)argp;
6457
- return rb_str_new(args->data, args->length);
6483
+ if (args->length > LONG_MAX) rb_raise(rb_eRangeError, "URL conversion result is too large for a Ruby String");
6484
+ return rb_str_new(args->data, (long)args->length);
6458
6485
  }
6459
6486
 
6460
6487
  static VALUE curl_string_result_free(VALUE argp) {
@@ -6463,12 +6490,25 @@ static VALUE curl_string_result_free(VALUE argp) {
6463
6490
  return Qnil;
6464
6491
  }
6465
6492
 
6466
- static VALUE curl_string_result_to_ruby(char *data, long length) {
6493
+ static VALUE curl_string_result_to_ruby(char *data, size_t length) {
6467
6494
  struct curl_string_result args = { data, length };
6468
6495
  return rb_ensure(curl_string_result_body, (VALUE)&args,
6469
6496
  curl_string_result_free, (VALUE)&args);
6470
6497
  }
6471
6498
 
6499
+ VALUE curb_escape_string(CURL *curl, VALUE str, VALUE error_class, const char *error_message) {
6500
+ int length = RSTRING_LENINT(str);
6501
+ char *result;
6502
+ if (!length) return rb_str_new("", 0);
6503
+ #ifdef HAVE_CURL_EASY_ESCAPE
6504
+ result = curl_easy_escape(curl, RSTRING_PTR(str), length);
6505
+ #else
6506
+ result = curl_escape(RSTRING_PTR(str), length);
6507
+ #endif
6508
+ if (!result) rb_raise(error_class, "%s", error_message);
6509
+ return curl_string_result_to_ruby(result, strlen(result));
6510
+ }
6511
+
6472
6512
  /*
6473
6513
  * call-seq:
6474
6514
  * easy.escape("some text") => "some%20text"
@@ -6480,7 +6520,6 @@ static VALUE curl_string_result_to_ruby(char *data, long length) {
6480
6520
  */
6481
6521
  static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
6482
6522
  ruby_curl_easy *rbce;
6483
- char *result;
6484
6523
  VALUE str = svalue;
6485
6524
 
6486
6525
  rbce = ruby_curl_easy_get_handle(self);
@@ -6489,14 +6528,7 @@ static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
6489
6528
  * String, even if the object's to_s misbehaves and returns a non-String. */
6490
6529
  if( rb_type(str) != T_STRING ) { str = rb_obj_as_string(str); }
6491
6530
 
6492
- #if (LIBCURL_VERSION_NUM >= 0x070f04)
6493
- result = (char*)curl_easy_escape(rbce->curl, StringValuePtr(str), (int)RSTRING_LEN(str));
6494
- #else
6495
- result = (char*)curl_escape(StringValuePtr(str), (int)RSTRING_LEN(str));
6496
- #endif
6497
-
6498
- if (!result) rb_raise(eCurlErrError, "Failed to URL-encode string");
6499
- return curl_string_result_to_ruby(result, (long)strlen(result));
6531
+ return curb_escape_string(rbce->curl, str, eCurlErrError, "Failed to URL-encode string");
6500
6532
  }
6501
6533
 
6502
6534
  /*
@@ -6509,27 +6541,30 @@ static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
6509
6541
  */
6510
6542
  static VALUE ruby_curl_easy_unescape(VALUE self, VALUE str) {
6511
6543
  ruby_curl_easy *rbce;
6544
+ int length;
6545
+ #if (LIBCURL_VERSION_NUM >= 0x070f04)
6512
6546
  int rlen;
6547
+ #endif
6513
6548
  char *result;
6514
6549
 
6515
6550
  rbce = ruby_curl_easy_get_handle(self);
6516
6551
 
6517
- /* Coerce as its own statement before use below - StringValuePtr and
6518
- * RSTRING_LEN are sibling args to the same call and C does not guarantee
6519
- * left-to-right evaluation order between them. */
6520
6552
  StringValue(str);
6553
+ length = RSTRING_LENINT(str);
6554
+ if (!length) return rb_str_new("", 0);
6521
6555
 
6522
6556
  #if (LIBCURL_VERSION_NUM >= 0x070f04)
6523
- result = (char*)curl_easy_unescape(rbce->curl, StringValuePtr(str), (int)RSTRING_LEN(str), &rlen);
6557
+ result = curl_easy_unescape(rbce->curl, RSTRING_PTR(str), length, &rlen);
6524
6558
  #else
6525
- result = (char*)curl_unescape(StringValuePtr(str), (int)RSTRING_LEN(str));
6559
+ result = curl_unescape(RSTRING_PTR(str), length);
6526
6560
  #endif
6527
6561
 
6528
6562
  if (!result) rb_raise(eCurlErrError, "Failed to URL-decode string");
6529
6563
  #if (LIBCURL_VERSION_NUM < 0x070f04)
6530
- rlen = (int)strlen(result);
6531
- #endif
6564
+ return curl_string_result_to_ruby(result, strlen(result));
6565
+ #else
6532
6566
  return curl_string_result_to_ruby(result, rlen);
6567
+ #endif
6533
6568
  }
6534
6569
 
6535
6570
 
data/ext/curb_easy.h CHANGED
@@ -56,6 +56,9 @@ typedef struct {
56
56
  VALUE opts; /* rather then allocate everything we might need to store, allocate a Hash and only store objects we actually use... */
57
57
  VALUE multi; /* keep a multi handle alive for each easy handle not being used by a multi handle. This improves easy performance when not within a multi context */
58
58
  VALUE callback_error; /* preserves body/header callback exceptions without mutating the Ruby object */
59
+ #ifndef HAVE_CURLOPT_COPYPOSTFIELDS
60
+ VALUE borrowed_post_body; /* private, pinned storage for CURLOPT_POSTFIELDS, shared by duphandle */
61
+ #endif
59
62
 
60
63
  /* Other opts */
61
64
  unsigned short local_port; // 0 is no port
@@ -133,6 +136,7 @@ extern const rb_data_type_t ruby_curl_easy_data_type;
133
136
 
134
137
  ruby_curl_easy *ruby_curl_easy_get_handle(VALUE self);
135
138
  void ruby_curl_easy_check_access(ruby_curl_easy *rbce);
139
+ VALUE curb_escape_string(CURL *curl, VALUE str, VALUE error_class, const char *error_message);
136
140
 
137
141
  VALUE ruby_curl_easy_setup(ruby_curl_easy *rbce);
138
142
  VALUE ruby_curl_easy_cleanup(VALUE self, ruby_curl_easy *rbce);
data/ext/curb_multi.c CHANGED
@@ -557,7 +557,7 @@ static void curl_multi_free(void *ptr) {
557
557
  rbcm->handle = NULL;
558
558
  }
559
559
 
560
- free(rbcm);
560
+ xfree(rbcm);
561
561
  }
562
562
 
563
563
  static size_t curl_multi_memsize(const void *ptr) {
data/ext/curb_postfield.c CHANGED
@@ -179,10 +179,6 @@ static void curl_postfield_mark(void *ptr) {
179
179
  }
180
180
  }
181
181
 
182
- static void curl_postfield_free(void *ptr) {
183
- if (ptr) free(ptr);
184
- }
185
-
186
182
  static size_t curl_postfield_memsize(const void *ptr) {
187
183
  (void)ptr;
188
184
  return sizeof(ruby_curl_postfield);
@@ -192,7 +188,7 @@ const rb_data_type_t ruby_curl_postfield_data_type = {
192
188
  "Curl::PostField",
193
189
  {
194
190
  curl_postfield_mark,
195
- curl_postfield_free,
191
+ RUBY_TYPED_DEFAULT_FREE,
196
192
  curl_postfield_memsize,
197
193
  #ifdef RUBY_TYPED_FREE_IMMEDIATELY
198
194
  NULL, /* compact */
@@ -431,6 +427,28 @@ static VALUE ruby_curl_postfield_content_proc_set(int argc, VALUE *argv, VALUE s
431
427
  CURB_HANDLER_PROC_SETTER(ruby_curl_postfield, content_proc);
432
428
  }
433
429
 
430
+ struct postfield_encode_args {
431
+ CURL *curl;
432
+ VALUE name;
433
+ VALUE content;
434
+ };
435
+
436
+ static VALUE postfield_encode(VALUE arg) {
437
+ struct postfield_encode_args *args = (struct postfield_encode_args *)arg;
438
+ VALUE result = curb_escape_string(args->curl, args->name, eCurlErrInvalidPostField, "Failed to url-encode name");
439
+ VALUE content = curb_escape_string(args->curl, args->content, eCurlErrInvalidPostField, "Failed to url-encode content");
440
+ rb_str_cat(result, "=", 1);
441
+ return rb_str_concat(result, content);
442
+ }
443
+
444
+ #ifdef HAVE_CURL_EASY_ESCAPE
445
+ static VALUE postfield_encode_cleanup(VALUE arg) {
446
+ struct postfield_encode_args *args = (struct postfield_encode_args *)arg;
447
+ curl_easy_cleanup(args->curl);
448
+ return Qnil;
449
+ }
450
+ #endif
451
+
434
452
  /*
435
453
  * call-seq:
436
454
  * field.to_str => "name=value"
@@ -444,12 +462,7 @@ static VALUE ruby_curl_postfield_content_proc_set(int argc, VALUE *argv, VALUE s
444
462
  */
445
463
  static VALUE ruby_curl_postfield_to_str(VALUE self) {
446
464
  ruby_curl_postfield *rbcpf;
447
- VALUE result = Qnil;
448
465
  VALUE name = Qnil;
449
- char *tmpchrs;
450
- #ifdef HAVE_CURL_EASY_ESCAPE
451
- CURL *curl_handle = NULL;
452
- #endif
453
466
 
454
467
  TypedData_Get_Struct(self, ruby_curl_postfield, &ruby_curl_postfield_data_type, rbcpf);
455
468
 
@@ -500,52 +513,16 @@ static VALUE ruby_curl_postfield_to_str(VALUE self) {
500
513
  /* Force content to UTF-8 before escaping */
501
514
  VALUE content_utf8 = rb_str_export_to_enc(tmpcontent, rb_utf8_encoding());
502
515
 
516
+ struct postfield_encode_args args = { NULL, name_utf8, content_utf8 };
503
517
  #ifdef HAVE_CURL_EASY_ESCAPE
504
- curl_handle = curl_easy_init();
505
- if (!curl_handle) {
518
+ args.curl = curl_easy_init();
519
+ if (!args.curl) {
506
520
  rb_raise(eCurlErrInvalidPostField, "Failed to initialize curl handle for escaping");
507
521
  }
508
- tmpchrs = curl_easy_escape(curl_handle, StringValuePtr(name_utf8), (int)RSTRING_LEN(name_utf8));
509
- if (!tmpchrs) {
510
- curl_easy_cleanup(curl_handle);
511
- rb_raise(eCurlErrInvalidPostField, "Failed to url-encode name");
512
- }
522
+ return rb_ensure(postfield_encode, (VALUE)&args, postfield_encode_cleanup, (VALUE)&args);
513
523
  #else
514
- tmpchrs = curl_escape(StringValuePtr(name_utf8), (int)RSTRING_LEN(name_utf8));
515
- if (!tmpchrs) {
516
- rb_raise(eCurlErrInvalidPostField, "Failed to url-encode name");
517
- }
524
+ return postfield_encode((VALUE)&args);
518
525
  #endif
519
-
520
- VALUE escd_name = rb_str_new2(tmpchrs);
521
- curl_free(tmpchrs);
522
-
523
- #ifdef HAVE_CURL_EASY_ESCAPE
524
- tmpchrs = curl_easy_escape(curl_handle, StringValuePtr(content_utf8), (int)RSTRING_LEN(content_utf8));
525
- if (!tmpchrs) {
526
- curl_easy_cleanup(curl_handle);
527
- rb_raise(eCurlErrInvalidPostField, "Failed to url-encode content");
528
- }
529
- #else
530
- tmpchrs = curl_escape(StringValuePtr(content_utf8), (int)RSTRING_LEN(content_utf8));
531
- if (!tmpchrs) {
532
- rb_raise(eCurlErrInvalidPostField, "Failed to url-encode content");
533
- }
534
- #endif
535
-
536
- VALUE escd_content = rb_str_new2(tmpchrs);
537
- #ifdef HAVE_CURL_EASY_ESCAPE
538
- curl_free(tmpchrs);
539
- curl_easy_cleanup(curl_handle);
540
- #else
541
- curl_free(tmpchrs);
542
- #endif
543
-
544
- result = escd_name;
545
- rb_str_cat(result, "=", 1);
546
- rb_str_concat(result, escd_content);
547
-
548
- return result;
549
526
  }
550
527
 
551
528
 
data/ext/curb_upload.c CHANGED
@@ -15,10 +15,6 @@ static void curl_upload_mark(void *ptr) {
15
15
  if (rbcu && rbcu->stream && !NIL_P(rbcu->stream)) rb_gc_mark(rbcu->stream);
16
16
  }
17
17
 
18
- static void curl_upload_free(void *ptr) {
19
- if (ptr) free(ptr);
20
- }
21
-
22
18
  static size_t curl_upload_memsize(const void *ptr) {
23
19
  (void)ptr;
24
20
  return sizeof(ruby_curl_upload);
@@ -28,7 +24,7 @@ const rb_data_type_t ruby_curl_upload_data_type = {
28
24
  "Curl::Upload",
29
25
  {
30
26
  curl_upload_mark,
31
- curl_upload_free,
27
+ RUBY_TYPED_DEFAULT_FREE,
32
28
  curl_upload_memsize,
33
29
  #ifdef RUBY_TYPED_FREE_IMMEDIATELY
34
30
  NULL, /* compact */
@@ -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|
@@ -779,6 +803,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
779
803
 
780
804
  def test_escape
781
805
  c = Curl::Easy.new
806
+ assert_equal "", c.escape("")
782
807
 
783
808
  assert_equal "one%20two", c.escape('one two')
784
809
  assert_equal "one%00two%20three", c.escape("one\000two three")
@@ -786,6 +811,7 @@ class TestCurbCurlEasy < Test::Unit::TestCase
786
811
 
787
812
  def test_unescape
788
813
  c = Curl::Easy.new
814
+ assert_equal "", c.unescape("")
789
815
 
790
816
  assert_equal "one two", c.unescape('one%20two')
791
817
 
@@ -795,6 +821,19 @@ class TestCurbCurlEasy < Test::Unit::TestCase
795
821
  end
796
822
  end
797
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
+
798
837
  def test_escape_with_non_string_to_s_result_does_not_crash
799
838
  c = Curl::Easy.new
800
839
  result = nil
@@ -1535,8 +1574,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1535
1574
  end
1536
1575
 
1537
1576
  def test_post_body_preserves_assigned_snapshot_when_original_string_is_mutated
1538
- omit('CURLOPT_COPYPOSTFIELDS is not available in this build') unless Curl.const_defined?(:CURLOPT_COPYPOSTFIELDS)
1539
-
1540
1577
  curl = Curl::Easy.new(TestServlet.url)
1541
1578
  post_body = String.new('alpha=1')
1542
1579
  curl.post_body = post_body
@@ -1566,8 +1603,6 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1566
1603
  end
1567
1604
 
1568
1605
  def test_setopt_postfields_preserves_assigned_snapshot_when_original_string_is_mutated
1569
- omit('CURLOPT_COPYPOSTFIELDS is not available in this build') unless Curl.const_defined?(:CURLOPT_COPYPOSTFIELDS)
1570
-
1571
1606
  curl = Curl::Easy.new(TestServlet.url)
1572
1607
  post_body = String.new('alpha=1')
1573
1608
  curl.set(Curl::CURLOPT_POSTFIELDS, post_body)
@@ -1583,6 +1618,18 @@ class TestCurbCurlEasy < Test::Unit::TestCase
1583
1618
  assert_equal 'alpha=1', curl.post_body
1584
1619
  end
1585
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
+
1586
1633
  def test_setopt_postfields_nil_preserves_post_request
1587
1634
  curl = Curl::Easy.new(TestServlet.url)
1588
1635
  curl.set(Curl::CURLOPT_POST, true)
@@ -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
@@ -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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Bamford
8
8
  - Todd A. Fisher
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Curb (probably CUrl-RuBy or something) provides Ruby-language bindings
14
14
  for the libcurl(3), a fully-featured client-side URL transfer library. cURL and
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  requirements: []
120
- rubygems_version: 4.0.10
120
+ rubygems_version: 4.0.20
121
121
  specification_version: 4
122
122
  summary: Ruby libcurl bindings
123
123
  test_files: