Skip to content

Commit 3ecf848

Browse files
authored
Merge pull request #18989 from rwinch/gh-18970-null-oncommitted
Merge Handle null value in OnCommittedResponseWrapper header methods
2 parents 057e518 + 0039bc0 commit 3ecf848

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void checkContentLengthHeader(String name, int value) {
8787
}
8888

8989
private void checkContentLengthHeader(String name, String value) {
90-
if ("Content-Length".equalsIgnoreCase(name)) {
90+
if (value != null && "Content-Length".equalsIgnoreCase(name)) {
9191
setContentLength(Long.parseLong(value));
9292
}
9393
}
@@ -505,7 +505,7 @@ public PrintWriter format(Locale l, String format, Object... args) {
505505

506506
@Override
507507
public PrintWriter append(CharSequence csq) {
508-
checkContentLength(csq.length());
508+
checkContentLength((csq != null) ? csq.length() : 4);
509509
return this.delegate.append(csq);
510510
}
511511

web/src/test/java/org/springframework/security/web/util/OnCommittedResponseWrapperTests.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.mockito.Mock;
2929
import org.mockito.junit.jupiter.MockitoExtension;
3030

31+
import org.springframework.http.HttpHeaders;
32+
3133
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.assertThatNoException;
3235
import static org.mockito.BDDMockito.given;
3336
import static org.mockito.Mockito.verify;
3437

@@ -1006,6 +1009,16 @@ public void addHeaderContentLengthPrintWriterWriteStringCommits() throws Excepti
10061009
assertThat(this.committed).isTrue();
10071010
}
10081011

1012+
@Test
1013+
public void addHeaderNullNameDoesNotThrow() {
1014+
assertThatNoException().isThrownBy(() -> this.response.addHeader(null, "value"));
1015+
}
1016+
1017+
@Test
1018+
public void addHeaderNullValueDoesNotThrow() {
1019+
assertThatNoException().isThrownBy(() -> this.response.addHeader(HttpHeaders.CONTENT_LENGTH, null));
1020+
}
1021+
10091022
@Test
10101023
public void addIntHeaderContentLengthPrintWriterWriteStringCommits() throws Exception {
10111024
givenGetWriterThenReturn();
@@ -1015,6 +1028,11 @@ public void addIntHeaderContentLengthPrintWriterWriteStringCommits() throws Exce
10151028
assertThat(this.committed).isTrue();
10161029
}
10171030

1031+
@Test
1032+
public void addIntHeaderNullNameDoesNotThrow() {
1033+
assertThatNoException().isThrownBy(() -> this.response.addIntHeader(null, 1));
1034+
}
1035+
10181036
@Test
10191037
public void setHeaderContentLengthPrintWriterWriteStringCommits() throws Exception {
10201038
givenGetWriterThenReturn();
@@ -1024,6 +1042,16 @@ public void setHeaderContentLengthPrintWriterWriteStringCommits() throws Excepti
10241042
assertThat(this.committed).isTrue();
10251043
}
10261044

1045+
@Test
1046+
public void setHeaderNullNameDoesNotThrow() {
1047+
assertThatNoException().isThrownBy(() -> this.response.setHeader(null, "value"));
1048+
}
1049+
1050+
@Test
1051+
public void setHeaderNullValueDoesNotThrow() {
1052+
assertThatNoException().isThrownBy(() -> this.response.setHeader(HttpHeaders.CONTENT_LENGTH, null));
1053+
}
1054+
10271055
@Test
10281056
public void setIntHeaderContentLengthPrintWriterWriteStringCommits() throws Exception {
10291057
givenGetWriterThenReturn();
@@ -1033,6 +1061,11 @@ public void setIntHeaderContentLengthPrintWriterWriteStringCommits() throws Exce
10331061
assertThat(this.committed).isTrue();
10341062
}
10351063

1064+
@Test
1065+
public void setIntHeaderNullNameDoesNotThrow() {
1066+
assertThatNoException().isThrownBy(() -> this.response.setIntHeader(null, 1));
1067+
}
1068+
10361069
@Test
10371070
public void bufferSizePrintWriterWriteCommits() throws Exception {
10381071
givenGetWriterThenReturn();
@@ -1054,4 +1087,86 @@ public void bufferSizeCommitsOnce() throws Exception {
10541087
assertThat(this.committed).isFalse();
10551088
}
10561089

1090+
@Test
1091+
public void printWriterPrintNullStringDoesNotThrow() throws Exception {
1092+
givenGetWriterThenReturn();
1093+
String s = null;
1094+
assertThatNoException().isThrownBy(() -> this.response.getWriter().print(s));
1095+
verify(this.writer).print(s);
1096+
}
1097+
1098+
@Test
1099+
public void printWriterPrintlnNullStringDoesNotThrow() throws Exception {
1100+
givenGetWriterThenReturn();
1101+
String s = null;
1102+
assertThatNoException().isThrownBy(() -> this.response.getWriter().println(s));
1103+
verify(this.writer).println(s);
1104+
}
1105+
1106+
@Test
1107+
public void printWriterPrintNullObjectDoesNotThrow() throws Exception {
1108+
givenGetWriterThenReturn();
1109+
Object obj = null;
1110+
assertThatNoException().isThrownBy(() -> this.response.getWriter().print(obj));
1111+
verify(this.writer).print(obj);
1112+
}
1113+
1114+
@Test
1115+
public void printWriterPrintlnNullObjectDoesNotThrow() throws Exception {
1116+
givenGetWriterThenReturn();
1117+
Object obj = null;
1118+
assertThatNoException().isThrownBy(() -> this.response.getWriter().println(obj));
1119+
verify(this.writer).println(obj);
1120+
}
1121+
1122+
@Test
1123+
public void printWriterWriteNullStringDoesNotThrow() throws Exception {
1124+
givenGetWriterThenReturn();
1125+
String s = null;
1126+
assertThatNoException().isThrownBy(() -> this.response.getWriter().write(s));
1127+
verify(this.writer).write(s);
1128+
}
1129+
1130+
@Test
1131+
public void printWriterAppendNullCharSequenceDoesNotThrow() throws Exception {
1132+
givenGetWriterThenReturn();
1133+
CharSequence csq = null;
1134+
assertThatNoException().isThrownBy(() -> this.response.getWriter().append(csq));
1135+
verify(this.writer).append(csq);
1136+
}
1137+
1138+
@Test
1139+
public void printWriterAppendNullCharSequenceIntIntDoesNotThrow() throws Exception {
1140+
givenGetWriterThenReturn();
1141+
CharSequence csq = null;
1142+
assertThatNoException().isThrownBy(() -> this.response.getWriter().append(csq, 0, 3));
1143+
verify(this.writer).append(csq, 0, 3);
1144+
}
1145+
1146+
@Test
1147+
public void outputStreamPrintNullStringDoesNotThrow() throws Exception {
1148+
givenGetOutputStreamThenReturn();
1149+
String s = null;
1150+
assertThatNoException().isThrownBy(() -> this.response.getOutputStream().print(s));
1151+
verify(this.out).print(s);
1152+
}
1153+
1154+
@Test
1155+
public void outputStreamPrintlnNullStringDoesNotThrow() throws Exception {
1156+
givenGetOutputStreamThenReturn();
1157+
String s = null;
1158+
assertThatNoException().isThrownBy(() -> this.response.getOutputStream().println(s));
1159+
verify(this.out).println(s);
1160+
}
1161+
1162+
@Test
1163+
public void sendErrorWithNullMsgDoesNotThrow() throws Exception {
1164+
assertThatNoException().isThrownBy(() -> this.response.sendError(400, null));
1165+
}
1166+
1167+
@Test
1168+
public void sendRedirectWithNullLocationDoesNotThrow() throws Exception {
1169+
assertThatNoException().isThrownBy(() -> this.response.sendRedirect(null));
1170+
}
1171+
10571172
}

0 commit comments

Comments
 (0)