diff --git a/spring-web/src/main/java/org/springframework/http/HttpRange.java b/spring-web/src/main/java/org/springframework/http/HttpRange.java index 65444c40dd..ae591cd335 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRange.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRange.java @@ -65,6 +65,7 @@ public abstract class HttpRange { long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); + Assert.isTrue(start < contentLength, "'position' exceeds the resource length " + contentLength); return new ResourceRegion(resource, start, end - start + 1); } diff --git a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java index 0c5b701c6a..2a22b87d80 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java @@ -158,8 +158,7 @@ public class HttpRangeTests { ByteArrayResource resource = mock(ByteArrayResource.class); given(resource.contentLength()).willReturn(-1L); HttpRange range = HttpRange.createByteRange(0, 9); - assertThatIllegalArgumentException().isThrownBy(() -> - range.toResourceRegion(resource)); + assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource)); } @Test @@ -167,8 +166,15 @@ public class HttpRangeTests { InputStreamResource resource = mock(InputStreamResource.class); given(resource.contentLength()).willThrow(IOException.class); HttpRange range = HttpRange.createByteRange(0, 9); - assertThatIllegalArgumentException().isThrownBy(() -> - range.toResourceRegion(resource)); + assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource)); + } + + @Test // gh-23576 + public void toResourceRegionStartingAtResourceByteCount() { + byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8); + ByteArrayResource resource = new ByteArrayResource(bytes); + HttpRange range = HttpRange.createByteRange(resource.contentLength()); + assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource)); } @Test