Skip to content

Commit

Permalink
Consistently check for Content-Length value
Browse files Browse the repository at this point in the history
This commit makes sure to consistently check that the content length
is not set above 2GB. Previously it was only checked in
setContentLength.

Closes gh-33256
  • Loading branch information
snicoll committed Jul 23, 2024
1 parent 83ff8e4 commit 6e9a192
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,15 @@ public void setContentLength(int len) {

@Override
public void setContentLengthLong(long len) {
if (len > Integer.MAX_VALUE) {
setContentLength(toContentLengthInt(len));
}

private int toContentLengthInt(long contentLength) {
if (contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Content-Length exceeds ContentCachingResponseWrapper's maximum (" +
Integer.MAX_VALUE + "): " + len);
Integer.MAX_VALUE + "): " + contentLength);
}
setContentLength((int) len);
return (int) contentLength;
}

@Override
Expand All @@ -160,7 +164,7 @@ public boolean containsHeader(String name) {
@Override
public void setHeader(String name, String value) {
if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
this.contentLength = Integer.valueOf(value);
this.contentLength = toContentLengthInt(Long.parseLong(value));
}
else {
super.setHeader(name, value);
Expand All @@ -170,7 +174,7 @@ public void setHeader(String name, String value) {
@Override
public void addHeader(String name, String value) {
if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
this.contentLength = Integer.valueOf(value);
this.contentLength = toContentLengthInt(Long.parseLong(value));
}
else {
super.addHeader(name, value);
Expand All @@ -180,7 +184,7 @@ public void addHeader(String name, String value) {
@Override
public void setIntHeader(String name, int value) {
if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
this.contentLength = Integer.valueOf(value);
this.contentLength = value;
}
else {
super.setIntHeader(name, value);
Expand All @@ -190,7 +194,7 @@ public void setIntHeader(String name, int value) {
@Override
public void addIntHeader(String name, int value) {
if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
this.contentLength = Integer.valueOf(value);
this.contentLength = value;
}
else {
super.addIntHeader(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Named.named;
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
Expand Down Expand Up @@ -233,6 +234,43 @@ void copyBodyToResponseWithTransferEncoding() throws Exception {
assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
}

@Test
void setContentLengthAbove2GbViaSetContentLengthLong() {
MockHttpServletResponse response = new MockHttpServletResponse();

ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
long overflow = (long) Integer.MAX_VALUE + 1;
assertThatIllegalArgumentException()
.isThrownBy(() -> responseWrapper.setContentLengthLong(overflow))
.withMessageContaining("Content-Length exceeds ContentCachingResponseWrapper's maximum")
.withMessageContaining(String.valueOf(overflow));
}

@Test
void setContentLengthAbove2GbViaAddHeader() {
MockHttpServletResponse response = new MockHttpServletResponse();

ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
String overflow = String.valueOf((long) Integer.MAX_VALUE + 1);
assertThatIllegalArgumentException()
.isThrownBy(() -> responseWrapper.addHeader(CONTENT_LENGTH, overflow))
.withMessageContaining("Content-Length exceeds ContentCachingResponseWrapper's maximum")
.withMessageContaining(overflow);
}

@Test
void setContentLengthAbove2GbViaSetHeader() {
MockHttpServletResponse response = new MockHttpServletResponse();

ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
String overflow = String.valueOf((long) Integer.MAX_VALUE + 1);
assertThatIllegalArgumentException()
.isThrownBy(() -> responseWrapper.setHeader(CONTENT_LENGTH, overflow))
.withMessageContaining("Content-Length exceeds ContentCachingResponseWrapper's maximum")
.withMessageContaining(overflow);
}


private void assertHeader(HttpServletResponse response, String header, int value) {
assertHeader(response, header, Integer.toString(value));
}
Expand Down

0 comments on commit 6e9a192

Please sign in to comment.