Browse Source

Reject range starting above resource length

Closes: gh-23576
pull/23582/head
Rossen Stoyanchev 7 years ago
parent
commit
bc81fa520e
  1. 1
      spring-web/src/main/java/org/springframework/http/HttpRange.java
  2. 14
      spring-web/src/test/java/org/springframework/http/HttpRangeTests.java

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

@ -65,6 +65,7 @@ public abstract class HttpRange { @@ -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);
}

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

@ -158,8 +158,7 @@ public class HttpRangeTests { @@ -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 { @@ -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

Loading…
Cancel
Save