From d202847b66fe2c104bd1dde56e3716dd4727345c Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 15 Nov 2026 22:56:44 +0800 Subject: [PATCH] fix(mock): avoid overflow in inclusive integer ranges --- .../shenyu/plugin/mock/util/RandomUtil.java | 3 ++- .../shenyu/plugin/mock/MockPluginTest.java | 20 ++++++++++++++ .../shenyu/plugin/mock/util/MockUtilTest.java | 27 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/RandomUtil.java b/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/RandomUtil.java index d2e303327fa9..b74eeed88f0d 100644 --- a/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/RandomUtil.java +++ b/shenyu-plugin/shenyu-plugin-mock/src/main/java/org/apache/shenyu/plugin/mock/util/RandomUtil.java @@ -35,7 +35,8 @@ public class RandomUtil { */ public static int randomInt(final int min, final int max) { ThreadLocalRandom random = ThreadLocalRandom.current(); - return random.nextInt(max - min + 1) + min; + // Widen the exclusive upper bound to preserve inclusive ranges across integer overflow. + return (int) random.nextLong(min, (long) max + 1); } /** diff --git a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/MockPluginTest.java b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/MockPluginTest.java index e7ed9b1f4728..46928ca01ece 100644 --- a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/MockPluginTest.java +++ b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/MockPluginTest.java @@ -31,6 +31,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -45,6 +46,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -147,6 +149,24 @@ public void testDoExecuteWithoutCachedHandle() { verify(chain).execute(any()); } + @ParameterizedTest + @CsvSource({"0, 2147483647", "2147483647, 2147483647", "0, 0", "10, 15"}) + public void testIntegerResponseSupportsInclusiveBounds(final int min, final int max) { + RuleData ruleData = buildRuleData(); + MockHandle mockHandle = new MockHandle(); + mockHandle.setResponseContent(String.format("${int|%d-%d}", min, max)); + MockPluginHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), mockHandle); + + StepVerifier.create(mockPlugin.doExecute(exchange, chain, mock(SelectorData.class), ruleData)).verifyComplete(); + + String body = exchange.getResponse().getBodyAsString().block(); + int value = Integer.parseInt(body); + assertTrue(value >= min && value <= max); + assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode()); + assertEquals(body.getBytes(StandardCharsets.UTF_8).length, exchange.getResponse().getHeaders().getContentLength()); + verify(chain, never()).execute(any()); + } + @Test public void testGetOrder() { final int result = mockPlugin.getOrder(); diff --git a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java index e411d0912912..92dae1ad5dba 100644 --- a/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java +++ b/shenyu-plugin/shenyu-plugin-mock/src/test/java/org/apache/shenyu/plugin/mock/util/MockUtilTest.java @@ -18,7 +18,10 @@ package org.apache.shenyu.plugin.mock.util; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -26,6 +29,30 @@ */ public final class MockUtilTest { + @ParameterizedTest + @CsvSource({ + "0, 2147483647", + "-2147483648, 2147483647", + "-2147483648, 0", + "-2147483648, -1", + "2147483647, 2147483647", + "-2147483648, -2147483648", + "0, 0", + "10, 15" + }) + public void randomIntShouldSupportInclusiveIntegerBounds(final int min, final int max) { + for (int i = 0; i < 100; i++) { + int value = MockUtil.randomInt(min, max); + assertTrue(value >= min && value <= max, "Generated value must remain within the inclusive bounds"); + } + } + + @ParameterizedTest + @CsvSource({"1, 0", "2147483647, -2147483648"}) + public void randomIntShouldRejectReversedBounds(final int min, final int max) { + assertThrows(IllegalArgumentException.class, () -> MockUtil.randomInt(min, max)); + } + @Test public void zhShouldReturnLengthsWithinTheInclusiveRange() { for (int i = 0; i < 500; i++) {