From 36bb0015eed1dc189e3331413fc13616b5318b98 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sat, 5 Dec 2026 13:48:52 +0100 Subject: [PATCH] Consistently limit `max_nesting` and similar configs to 32 on all platforms Makes it easier to test and more can't realistically be needed. --- ext/json/ext/generator/generator.c | 18 ++------------ ext/json/ext/json.h | 9 +++++++ ext/json/ext/parser/parser.c | 7 +----- java/src/json/ext/GeneratorState.java | 11 +++------ java/src/json/ext/OptionsReader.java | 2 +- java/src/json/ext/Parser.java | 2 +- lib/json/truffle_ruby/generator.rb | 35 ++++++++++++++------------- test/json/json_generator_test.rb | 6 +++-- test/json/json_parser_test.rb | 3 ++- 9 files changed, 42 insertions(+), 51 deletions(-) diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 4ac6dc20..f1075f6b 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -1693,26 +1693,12 @@ static VALUE cState_max_nesting(VALUE self) static size_t max_nesting_config(VALUE num) { - if (!RTEST(num)) return 0; - ssize_t max_nesting = NUM2SSIZET(num); - if (RB_UNLIKELY(max_nesting < 0)) { - rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting); - } - return (size_t)max_nesting; + return RTEST(num) ? json_num2size(num, "max_nesting") : 0; } -// depth must never be negative; reject early with a clear error. static size_t depth_config(VALUE num) { - if (!RTEST(num)) return 0; - ssize_t d = NUM2SSIZET(num); - if (RB_UNLIKELY(d < 0)) { - rb_raise(rb_eArgError, "depth must be >= 0 (got %"PRIdSIZE")", d); - } - if (RB_UNLIKELY(d > INT_MAX)) { - rb_raise(rb_eArgError, "depth is too large (got %"PRIdSIZE")", d); - } - return (size_t)d; + return RTEST(num) ? json_num2size(num, "depth") : 0; } /* diff --git a/ext/json/ext/json.h b/ext/json/ext/json.h index ba2ac2b7..d54d9f1f 100644 --- a/ext/json/ext/json.h +++ b/ext/json/ext/json.h @@ -206,4 +206,13 @@ static inline void raise_argument_error_on_unknown_keywords(VALUE unknown_keywor } } +static inline size_t json_num2size(VALUE num, const char *name) +{ + int value = NUM2INT(num); + if (RB_UNLIKELY(value < 0)) { + rb_raise(rb_eArgError, "%s must be >= 0 (got %d)", name, value); + } + return (size_t)value; +} + #endif // _JSON_H_ diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index b6dc0cf5..14131c0f 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -2011,12 +2011,7 @@ static void parser_config_wb_write(VALUE self, VALUE *dest, VALUE val) static size_t max_nesting_config(VALUE num) { - if (!RTEST(num)) return 0; - ssize_t max_nesting = NUM2SSIZET(num); - if (RB_UNLIKELY(max_nesting < 0)) { - rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting); - } - return (size_t)max_nesting; + return RTEST(num) ? json_num2size(num, "max_nesting") : 0; } static int parser_config_init_i(VALUE key, VALUE val, VALUE data) diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java index 81a20d79..b186bcb8 100644 --- a/java/src/json/ext/GeneratorState.java +++ b/java/src/json/ext/GeneratorState.java @@ -406,7 +406,7 @@ public RubyInteger max_nesting_get(ThreadContext context) { @JRubyMethod(name="max_nesting=") public IRubyObject max_nesting_set(ThreadContext context, IRubyObject max_nesting) { checkFrozen(); - long value = RubyNumeric.num2long(max_nesting); + long value = RubyNumeric.num2int(max_nesting); if (value < 0) { throw context.runtime.newArgumentError("max_nesting must be >= 0 (got: " + value + ")"); } @@ -562,7 +562,7 @@ public RubyInteger depth_get(ThreadContext context) { @JRubyMethod(name="depth=") public IRubyObject depth_set(ThreadContext context, IRubyObject vDepth) { checkFrozen(); - long value = checkDepth(context, RubyNumeric.num2long(vDepth)); + long value = checkDepth(context, RubyNumeric.num2int(vDepth)); checkFrozen(); depth = value; return vDepth; @@ -613,7 +613,7 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) { this.asJSON = proc; } - maxNesting = opts.getLong("max_nesting", maxNesting); + maxNesting = opts.getInt("max_nesting", (int) maxNesting); if (maxNesting < 0) { throw context.runtime.newArgumentError("max_nesting must be >= 0 (got: " + maxNesting + ")"); } @@ -623,7 +623,7 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) { strict = opts.getBool("strict", strict); bufferInitialLength = opts.getLong("buffer_initial_length", bufferInitialLength); - depth = checkDepth(context, opts.getLong("depth", depth)); + depth = checkDepth(context, opts.getInt("depth", (int) depth)); this.allowDuplicateKey = opts.getBool("allow_duplicate_key", allowDuplicateKey); if (opts.hasKey("sort_keys")) sortKeys = normalizeSortKeys(context, opts.get("sort_keys")); @@ -695,9 +695,6 @@ private static long checkDepth(ThreadContext context, long depth) { if (depth < 0) { throw context.runtime.newArgumentError("depth must be >= 0 (got: " + depth + ")"); } - if (depth > Integer.MAX_VALUE) { - throw context.runtime.newArgumentError("depth is too large (got: " + depth + ")"); - } return depth; } diff --git a/java/src/json/ext/OptionsReader.java b/java/src/json/ext/OptionsReader.java index 086ad437..9c518f7c 100644 --- a/java/src/json/ext/OptionsReader.java +++ b/java/src/json/ext/OptionsReader.java @@ -89,7 +89,7 @@ int getInt(String key, int defaultValue) { IRubyObject value = get(key); if (value == null) return defaultValue; if (!value.isTrue()) return 0; - return RubyNumeric.fix2int(value); + return RubyNumeric.num2int(value); } long getLong(String key, long defaultValue) { diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 9319d6e1..bd4c864e 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -104,7 +104,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject options) { Ruby runtime = context.runtime; OptionsReader opts = new OptionsReader(context, options); - this.maxNesting = opts.getLong("max_nesting", DEFAULT_MAX_NESTING); + this.maxNesting = opts.getInt("max_nesting", DEFAULT_MAX_NESTING); if (maxNesting < 0) { throw runtime.newArgumentError("max_nesting must be >= 0 (got: " + maxNesting + ")"); } diff --git a/lib/json/truffle_ruby/generator.rb b/lib/json/truffle_ruby/generator.rb index 2fe6e897..df05c67e 100644 --- a/lib/json/truffle_ruby/generator.rb +++ b/lib/json/truffle_ruby/generator.rb @@ -196,11 +196,7 @@ def as_json=(value) attr_reader :max_nesting def max_nesting=(max_nesting) - max_nesting = max_nesting ? integer_config(max_nesting) : 0 - if max_nesting.negative? - raise ArgumentError, "max_nesting must be >= 0 (got #{max_nesting})" - end - @max_nesting = max_nesting + @max_nesting = max_nesting ? nesting_config("max_nesting", max_nesting) : 0 end # If this attribute is set to true, script tags and HTML comments will be escaped in @@ -256,17 +252,26 @@ def buffer_initial_length=(length) attr_reader :depth def depth=(depth) - depth = depth ? integer_config(depth) : 0 - if depth.negative? - raise ArgumentError, "depth must be >= 0 (got #{depth})" - end - @depth = depth + @depth = depth ? nesting_config("depth", depth) : 0 end private def integer_config(value) Integer.try_convert(value) or raise TypeError, "no implicit conversion of #{value.class} into Integer" end + # Mirrors NUM2INT in the C extension. + private def nesting_config(name, value) + value = integer_config(value) + if value > 2**31 - 1 + raise RangeError, "integer #{value} too big to convert to 'int'" + elsif value < -(2**31) + raise RangeError, "integer #{value} too small to convert to 'int'" + elsif value.negative? + raise ArgumentError, "#{name} must be >= 0 (got #{value})" + end + value + end + def check_max_nesting # :nodoc: return if @max_nesting.zero? current_nesting = depth + 1 @@ -347,17 +352,13 @@ def configure(options) allow_duplicate_key: @allow_duplicate_key, script_safe: @script_safe, strict: @strict, max_nesting: @max_nesting, rfc8785: @rfc8785 ) - if depth.negative? - raise ArgumentError, "depth must be >= 0 (got #{depth})" - end + depth = depth ? nesting_config("depth", depth) : 0 if max_nesting && !(Integer === max_nesting) raise TypeError, ":max_nesting must be `false` or an Integer, got: #{max_nesting.class}" end - if max_nesting && max_nesting.negative? - raise ArgumentError, "max_nesting must be >= 0 (got #{max_nesting})" - end + max_nesting = max_nesting ? nesting_config("max_nesting", max_nesting) : 0 @indent = indent || '' @space = space || '' @@ -377,7 +378,7 @@ def configure(options) @allow_duplicate_key = allow_duplicate_key @script_safe = script_safe @strict = strict - @max_nesting = max_nesting || 0 + @max_nesting = max_nesting self.rfc8785 = rfc8785 validate_rfc8785 diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 810447e2..a7f803fa 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -498,19 +498,21 @@ def test_depth end def test_large_max_nesting - [2**62, 2**63 - 1].each do |value| + [2**30, 2**31 - 1].each do |value| state = JSON::State.new state.max_nesting = value assert_equal value, state.max_nesting assert_equal value, state.to_h[:max_nesting] assert_equal value, JSON::State.new(max_nesting: value).max_nesting end + assert_raise(RangeError) { JSON::State.new.max_nesting = 2**31 } + assert_raise(RangeError) { JSON::State.new(max_nesting: 2**31) } end def test_negative_max_nesting assert_raise(ArgumentError) { JSON::State.new.max_nesting = -1 } assert_raise(ArgumentError) { JSON::State.new(max_nesting: -1) } - assert_raise(ArgumentError) { JSON::State.new.configure(max_nesting: -(2**63)) } + assert_raise(RangeError) { JSON::State.new.configure(max_nesting: -(2**63)) } assert_raise(ArgumentError) { JSON.generate([], max_nesting: -1) } end diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index b81d9c0b..45a0c146 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -595,8 +595,9 @@ def test_nesting assert_equal too_deep_ary, ok ok = parse too_deep, max_nesting: 0 assert_equal too_deep_ary, ok - ok = parse too_deep, max_nesting: 2**62 + ok = parse too_deep, max_nesting: 2**31 - 1 assert_equal too_deep_ary, ok + assert_raise(RangeError) { parse too_deep, max_nesting: 2**31 } assert_raise(ArgumentError) { parse too_deep, max_nesting: -1 } end