json 2.19.5 → 2.19.8

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: 0bbb85e1521c171ca73ab935cc00899ff3eb7086921a64542a3937fa9589848d
4
- data.tar.gz: 328853f7f164d91522d6791a51cca5ccb3ae418410752a3fb72189bc14c157d5
3
+ metadata.gz: 5f07e2772537eccf97069c6a7d7290aceba9a18c6b635e8e822bc7b92137c678
4
+ data.tar.gz: cbb25a9d0e3434eba5d9fa2be9af5a69e1fe062c5113c461d854d129122adeed
5
5
  SHA512:
6
- metadata.gz: 326b0621e562eebc66f155cee358ca295dde7584e7f60268a354da3e8a1a02e13bfcd9fef7f9b2f9f2ea196987b07bb9e50578347231f14d9fb0e863ac9445ac
7
- data.tar.gz: 0e9e2ab4c91b5544ac8440613a0fd63aab2f7b7ce5a39f41e786af736e80ca28de8d3388057134dde05ba53f4f150d24aa1a8cde92dac82abbb4104740409631
6
+ metadata.gz: 2783e5483b100728ae73451008d3d5f880cc6e9dc663d773ed9dcdf27c23c3523c345f7ca70ae0e3687ab3b11262b380a7ecf455906d72b996f07eedbb1b14f8
7
+ data.tar.gz: 84ab5ff3feb2d961b4bd5d759d444b006e8ccaa6802465203ed04a8718db43b4473859fdd613c106b0cc53d4e96d1da8b4a16550361f455964a4a1a75ae3cb23
data/CHANGES.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2026-06-03 (2.19.8)
6
+
7
+ * Fix 1-byte buffer overread on EOS errors.
8
+ * Handle invalid types passed as `max_nesting` option.
9
+
10
+ ### 2026-05-28 (2.19.7)
11
+
12
+ * Fix some more edge cases with out of range floats.
13
+ * Ensure the string provided to `JSON.parse` can't be mutated during parsing.
14
+ * Add missing write barriers in `State#dup`.
15
+ * Further validate generator `depth` config.
16
+
17
+ ### 2026-05-28 (2.19.6)
18
+
19
+ * Cleanly handle overly large `depth` generator argument.
20
+ * Add missing write barrier in `ParserConfig`.
21
+
5
22
  ### 2026-05-04 (2.19.5)
6
23
 
7
24
  * Cap the parser to emit a maximum of 5 deprecation warnings per document. Emitting more is not helpful.
data/README.md CHANGED
@@ -249,6 +249,17 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
249
249
  `pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
250
250
  the `pp` library's `pp` methods.
251
251
 
252
+ ## Security
253
+
254
+ When parsing or serializing untrusted input, parser and generator options should never be user controlled.
255
+
256
+ ```ruby
257
+ # Dangerous, DO NOT DO THIS.
258
+ JSON.generate(params[:data], params[:options])
259
+ ```
260
+
261
+ Security vulnerability reports relying on attacker controlled parsing or generator options will be handled as regular bug fixes.
262
+
252
263
  ## Development
253
264
 
254
265
  ### Prerequisites
@@ -131,6 +131,15 @@ static inline void fbuffer_inc_capa(FBuffer *fb, size_t requested)
131
131
  }
132
132
  }
133
133
 
134
+ static inline size_t fbuffer_size_mul_or_raise(size_t a, size_t b)
135
+ {
136
+ size_t result = a * b;
137
+ if (RB_UNLIKELY(a != 0 && (result / a) != b)) {
138
+ rb_raise(rb_eArgError, "Buffer overflow, the resulting document is too large to be generated");
139
+ }
140
+ return result;
141
+ }
142
+
134
143
  static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, size_t len)
135
144
  {
136
145
  MEMCPY(fb->ptr + fb->len, newstr, char, len);
@@ -175,7 +184,7 @@ static void fbuffer_append_str_repeat(FBuffer *fb, VALUE str, size_t repeat)
175
184
  size_t len;
176
185
  RSTRING_GETMEM(str, ptr, len);
177
186
 
178
- fbuffer_inc_capa(fb, repeat * len);
187
+ fbuffer_inc_capa(fb, fbuffer_size_mul_or_raise(repeat, len));
179
188
  while (repeat) {
180
189
  #if JSON_DEBUG
181
190
  fb->requested = len;
@@ -1367,12 +1367,14 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
1367
1367
  if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
1368
1368
 
1369
1369
  MEMCPY(objState, origState, JSON_Generator_State, 1);
1370
- objState->indent = origState->indent;
1371
- objState->space = origState->space;
1372
- objState->space_before = origState->space_before;
1373
- objState->object_nl = origState->object_nl;
1374
- objState->array_nl = origState->array_nl;
1375
- objState->as_json = origState->as_json;
1370
+
1371
+ RB_OBJ_WRITTEN(obj, Qundef, objState->indent);
1372
+ RB_OBJ_WRITTEN(obj, Qundef, objState->space);
1373
+ RB_OBJ_WRITTEN(obj, Qundef, objState->space_before);
1374
+ RB_OBJ_WRITTEN(obj, Qundef, objState->object_nl);
1375
+ RB_OBJ_WRITTEN(obj, Qundef, objState->array_nl);
1376
+ RB_OBJ_WRITTEN(obj, Qundef, objState->as_json);
1377
+
1376
1378
  return obj;
1377
1379
  }
1378
1380
 
@@ -1579,7 +1581,7 @@ static VALUE cState_max_nesting(VALUE self)
1579
1581
 
1580
1582
  static long long_config(VALUE num)
1581
1583
  {
1582
- return RTEST(num) ? FIX2LONG(num) : 0;
1584
+ return RTEST(num) ? NUM2LONG(num) : 0;
1583
1585
  }
1584
1586
 
1585
1587
  // depth must never be negative; reject early with a clear error.
@@ -1590,6 +1592,9 @@ static long depth_config(VALUE num)
1590
1592
  if (RB_UNLIKELY(d < 0)) {
1591
1593
  rb_raise(rb_eArgError, "depth must be >= 0 (got %ld)", d);
1592
1594
  }
1595
+ if (RB_UNLIKELY(d > INT_MAX)) {
1596
+ rb_raise(rb_eArgError, "depth is too large (got %ld)", d);
1597
+ }
1593
1598
  return d;
1594
1599
  }
1595
1600
 
@@ -385,6 +385,13 @@ static inline char peek(JSON_ParserState *state)
385
385
 
386
386
  static void cursor_position(JSON_ParserState *state, long *line_out, long *column_out)
387
387
  {
388
+ JSON_ASSERT(state->cursor <= state->end);
389
+
390
+ // Redundant but helpful for hardening
391
+ if (RB_UNLIKELY(state->cursor > state->end)) {
392
+ state->cursor = state->end;
393
+ }
394
+
388
395
  const char *cursor = state->cursor;
389
396
  long column = 0;
390
397
  long line = 1;
@@ -1022,6 +1029,13 @@ ALWAYS_INLINE(static) bool string_scan(JSON_ParserState *state)
1022
1029
  }
1023
1030
  state->cursor++;
1024
1031
  }
1032
+
1033
+ // If the string ended with an unterminated escape sequence, we might
1034
+ // have gone past the end.
1035
+ if (RB_UNLIKELY(state->cursor > state->end)) {
1036
+ state->cursor = state->end;
1037
+ }
1038
+
1025
1039
  return false;
1026
1040
  }
1027
1041
 
@@ -1202,7 +1216,11 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1202
1216
  raise_parse_error_at("invalid number: %s", state, start);
1203
1217
  }
1204
1218
 
1205
- exponent = negative_exponent ? -abs_exponent : abs_exponent;
1219
+ if (RB_UNLIKELY(exponent_digits >= 20 || abs_exponent > (uint64_t)INT64_MAX)) {
1220
+ exponent = negative_exponent ? INT64_MIN : INT64_MAX;
1221
+ } else {
1222
+ exponent = negative_exponent ? -(int64_t)abs_exponent : (int64_t)abs_exponent;
1223
+ }
1206
1224
  }
1207
1225
 
1208
1226
  if (integer) {
@@ -1457,23 +1475,39 @@ static void json_ensure_eof(JSON_ParserState *state)
1457
1475
 
1458
1476
  static VALUE convert_encoding(VALUE source)
1459
1477
  {
1460
- int encindex = RB_ENCODING_GET(source);
1478
+ StringValue(source);
1479
+ int encindex = RB_ENCODING_GET(source);
1480
+
1481
+ if (RB_LIKELY(encindex == utf8_encindex)) {
1482
+ return source;
1483
+ }
1484
+
1485
+ if (encindex == binary_encindex) {
1486
+ // For historical reason, we silently reinterpret binary strings as UTF-8
1487
+ return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
1488
+ }
1461
1489
 
1462
- if (RB_LIKELY(encindex == utf8_encindex)) {
1490
+ source = rb_funcall(source, i_encode, 1, Encoding_UTF_8);
1491
+ StringValue(source);
1463
1492
  return source;
1464
- }
1493
+ }
1465
1494
 
1466
- if (encindex == binary_encindex) {
1467
- // For historical reason, we silently reinterpret binary strings as UTF-8
1468
- return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
1469
- }
1495
+ struct parser_config_init_args {
1496
+ JSON_ParserConfig *config;
1497
+ VALUE self;
1498
+ };
1470
1499
 
1471
- return rb_funcall(source, i_encode, 1, Encoding_UTF_8);
1500
+ static void parser_config_wb_write(VALUE self, VALUE *dest, VALUE val)
1501
+ {
1502
+ *dest = val;
1503
+ if (self) RB_OBJ_WRITTEN(self, Qundef, val);
1472
1504
  }
1473
1505
 
1474
1506
  static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1475
1507
  {
1476
- JSON_ParserConfig *config = (JSON_ParserConfig *)data;
1508
+ struct parser_config_init_args *args = (struct parser_config_init_args *)data;
1509
+ JSON_ParserConfig *config = args->config;
1510
+ VALUE self = args->self;
1477
1511
 
1478
1512
  if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
1479
1513
  else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
@@ -1482,15 +1516,15 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1482
1516
  else if (key == sym_allow_invalid_escape) { config->allow_invalid_escape = RTEST(val); }
1483
1517
  else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
1484
1518
  else if (key == sym_freeze) { config->freeze = RTEST(val); }
1485
- else if (key == sym_on_load) { config->on_load_proc = RTEST(val) ? val : Qfalse; }
1519
+ else if (key == sym_on_load) { parser_config_wb_write(self, &config->on_load_proc, RTEST(val) ? val : Qfalse); }
1486
1520
  else if (key == sym_allow_duplicate_key) { config->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1487
1521
  else if (key == sym_decimal_class) {
1488
1522
  if (RTEST(val)) {
1489
1523
  if (rb_respond_to(val, i_try_convert)) {
1490
- config->decimal_class = val;
1524
+ parser_config_wb_write(self, &config->decimal_class, val);
1491
1525
  config->decimal_method_id = i_try_convert;
1492
1526
  } else if (rb_respond_to(val, i_new)) {
1493
- config->decimal_class = val;
1527
+ parser_config_wb_write(self, &config->decimal_class, val);
1494
1528
  config->decimal_method_id = i_new;
1495
1529
  } else if (RB_TYPE_P(val, T_CLASS)) {
1496
1530
  VALUE name = rb_class_name(val);
@@ -1499,7 +1533,7 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1499
1533
  if (last_colon) {
1500
1534
  const char *mod_path_end = last_colon - 1;
1501
1535
  VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
1502
- config->decimal_class = rb_path_to_class(mod_path);
1536
+ parser_config_wb_write(self, &config->decimal_class, rb_path_to_class(mod_path));
1503
1537
 
1504
1538
  const char *method_name_beg = last_colon + 1;
1505
1539
  long before_len = method_name_beg - name_cstr;
@@ -1507,7 +1541,7 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1507
1541
  VALUE method_name = rb_str_substr(name, before_len, len);
1508
1542
  config->decimal_method_id = SYM2ID(rb_str_intern(method_name));
1509
1543
  } else {
1510
- config->decimal_class = rb_mKernel;
1544
+ parser_config_wb_write(self, &config->decimal_class, rb_mKernel);
1511
1545
  config->decimal_method_id = SYM2ID(rb_str_intern(name));
1512
1546
  }
1513
1547
  }
@@ -1517,16 +1551,21 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1517
1551
  return ST_CONTINUE;
1518
1552
  }
1519
1553
 
1520
- static void parser_config_init(JSON_ParserConfig *config, VALUE opts)
1554
+ static void parser_config_init(JSON_ParserConfig *config, VALUE opts, VALUE self)
1521
1555
  {
1522
1556
  config->max_nesting = 100;
1523
1557
 
1558
+ struct parser_config_init_args args = {
1559
+ .config = config,
1560
+ .self = self,
1561
+ };
1562
+
1524
1563
  if (!NIL_P(opts)) {
1525
1564
  Check_Type(opts, T_HASH);
1526
1565
  if (RHASH_SIZE(opts) > 0) {
1527
1566
  // We assume in most cases few keys are set so it's faster to go over
1528
1567
  // the provided keys than to check all possible keys.
1529
- rb_hash_foreach(opts, parser_config_init_i, (VALUE)config);
1568
+ rb_hash_foreach(opts, parser_config_init_i, (VALUE)&args);
1530
1569
  }
1531
1570
 
1532
1571
  }
@@ -1560,17 +1599,21 @@ static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
1560
1599
  rb_check_frozen(self);
1561
1600
  GET_PARSER_CONFIG;
1562
1601
 
1563
- parser_config_init(config, opts);
1564
-
1565
- RB_OBJ_WRITTEN(self, Qundef, config->decimal_class);
1602
+ parser_config_init(config, opts, self);
1566
1603
 
1567
1604
  return self;
1568
1605
  }
1569
1606
 
1570
- static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
1607
+ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE src)
1571
1608
  {
1572
- Vsource = convert_encoding(StringValue(Vsource));
1573
- StringValue(Vsource);
1609
+ VALUE Vsource = convert_encoding(src);
1610
+
1611
+ // Ensure the string isn't mutated under us.
1612
+ // The classic API to use is `rb_str_locktmp`, but then we'd
1613
+ // need to use `rb_protect` to make sure we always unlock.
1614
+ if (Vsource == src) {
1615
+ Vsource = rb_str_new_frozen(Vsource);
1616
+ }
1574
1617
 
1575
1618
  VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
1576
1619
  rvalue_stack stack = {
@@ -1581,6 +1624,7 @@ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
1581
1624
 
1582
1625
  long len;
1583
1626
  const char *start;
1627
+
1584
1628
  RSTRING_GETMEM(Vsource, start, len);
1585
1629
 
1586
1630
  VALUE stack_handle = 0;
@@ -1599,6 +1643,7 @@ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE Vsource)
1599
1643
  // it won't cause a leak.
1600
1644
  rvalue_stack_eagerly_release(stack_handle);
1601
1645
  RB_GC_GUARD(stack_handle);
1646
+ RB_GC_GUARD(Vsource);
1602
1647
  json_ensure_eof(state);
1603
1648
 
1604
1649
  return result;
@@ -1619,12 +1664,9 @@ static VALUE cParserConfig_parse(VALUE self, VALUE Vsource)
1619
1664
 
1620
1665
  static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
1621
1666
  {
1622
- Vsource = convert_encoding(StringValue(Vsource));
1623
- StringValue(Vsource);
1624
-
1625
1667
  JSON_ParserConfig _config = {0};
1626
1668
  JSON_ParserConfig *config = &_config;
1627
- parser_config_init(config, opts);
1669
+ parser_config_init(config, opts, false);
1628
1670
 
1629
1671
  return cParser_parse(config, Vsource);
1630
1672
  }
@@ -307,6 +307,9 @@ module JSON
307
307
  if !opts.key?(:max_nesting) # defaults to 100
308
308
  @max_nesting = 100
309
309
  elsif opts[:max_nesting]
310
+ unless opts[:max_nesting].is_a?(Integer)
311
+ raise TypeError, ":max_nesting must be an Integer, got: #{opts[:max_nesting].class}"
312
+ end
310
313
  @max_nesting = opts[:max_nesting]
311
314
  else
312
315
  @max_nesting = 0
data/lib/json/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSON
4
- VERSION = '2.19.5'
4
+ VERSION = '2.19.8'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.19.5
4
+ version: 2.19.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 4.0.6
87
+ rubygems_version: 4.0.12
88
88
  specification_version: 4
89
89
  summary: JSON Implementation for Ruby
90
90
  test_files: []