Skip to content

Commit bc81fa5

Browse files
committed
Reject range starting above resource length
Closes: gh-23576
1 parent 70bbe71 commit bc81fa5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

spring-web/src/main/java/org/springframework/http/HttpRange.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public ResourceRegion toResourceRegion(Resource resource) {
6565
long contentLength = getLengthFor(resource);
6666
long start = getRangeStart(contentLength);
6767
long end = getRangeEnd(contentLength);
68+
Assert.isTrue(start < contentLength, "'position' exceeds the resource length " + contentLength);
6869
return new ResourceRegion(resource, start, end - start + 1);
6970
}
7071

spring-web/src/test/java/org/springframework/http/HttpRangeTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,23 @@ public void toResourceRegionIllegalLength() {
158158
ByteArrayResource resource = mock(ByteArrayResource.class);
159159
given(resource.contentLength()).willReturn(-1L);
160160
HttpRange range = HttpRange.createByteRange(0, 9);
161-
assertThatIllegalArgumentException().isThrownBy(() ->
162-
range.toResourceRegion(resource));
161+
assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource));
163162
}
164163

165164
@Test
166165
public void toResourceRegionExceptionLength() throws IOException {
167166
InputStreamResource resource = mock(InputStreamResource.class);
168167
given(resource.contentLength()).willThrow(IOException.class);
169168
HttpRange range = HttpRange.createByteRange(0, 9);
170-
assertThatIllegalArgumentException().isThrownBy(() ->
171-
range.toResourceRegion(resource));
169+
assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource));
170+
}
171+
172+
@Test // gh-23576
173+
public void toResourceRegionStartingAtResourceByteCount() {
174+
byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8);
175+
ByteArrayResource resource = new ByteArrayResource(bytes);
176+
HttpRange range = HttpRange.createByteRange(resource.contentLength());
177+
assertThatIllegalArgumentException().isThrownBy(() -> range.toResourceRegion(resource));
172178
}
173179

174180
@Test

0 commit comments

Comments
 (0)