Skip to content

Commit 4704aea

Browse files
authored
Merge pull request #18991 from rwinch/main-gh-18970-null-oncommitted
Merge Handle null value in OnCommittedResponseWrapper header methods
2 parents aff7369 + 9f67afe commit 4704aea

2 files changed

Lines changed: 136 additions & 21 deletions

File tree

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,37 @@ public OnCommittedResponseWrapper(HttpServletResponse response) {
5858
}
5959

6060
@Override
61-
public void addHeader(String name, String value) {
61+
public void addHeader(@Nullable String name, @Nullable String value) {
6262
checkContentLengthHeader(name, value);
6363
super.addHeader(name, value);
6464
}
6565

6666
@Override
67-
public void addIntHeader(String name, int value) {
67+
public void addIntHeader(@Nullable String name, int value) {
6868
checkContentLengthHeader(name, value);
6969
super.addIntHeader(name, value);
7070
}
7171

7272
@Override
73-
public void setHeader(String name, String value) {
73+
public void setHeader(@Nullable String name, @Nullable String value) {
7474
checkContentLengthHeader(name, value);
7575
super.setHeader(name, value);
7676
}
7777

7878
@Override
79-
public void setIntHeader(String name, int value) {
79+
public void setIntHeader(@Nullable String name, int value) {
8080
checkContentLengthHeader(name, value);
8181
super.setIntHeader(name, value);
8282
}
8383

84-
private void checkContentLengthHeader(String name, int value) {
84+
private void checkContentLengthHeader(@Nullable String name, int value) {
8585
if ("Content-Length".equalsIgnoreCase(name)) {
8686
setContentLength(value);
8787
}
8888
}
8989

90-
private void checkContentLengthHeader(String name, String value) {
91-
if ("Content-Length".equalsIgnoreCase(name)) {
90+
private void checkContentLengthHeader(@Nullable String name, @Nullable String value) {
91+
if (value != null && "Content-Length".equalsIgnoreCase(name)) {
9292
setContentLength(Long.parseLong(value));
9393
}
9494
}
@@ -150,7 +150,7 @@ public final void sendError(int sc) throws IOException {
150150
* before calling the superclass <code>sendError()</code>
151151
*/
152152
@Override
153-
public final void sendError(int sc, String msg) throws IOException {
153+
public final void sendError(int sc, @Nullable String msg) throws IOException {
154154
doOnResponseCommitted();
155155
super.sendError(sc, msg);
156156
}
@@ -160,7 +160,7 @@ public final void sendError(int sc, String msg) throws IOException {
160160
* before calling the superclass <code>sendRedirect()</code>
161161
*/
162162
@Override
163-
public final void sendRedirect(String location) throws IOException {
163+
public final void sendRedirect(@Nullable String location) throws IOException {
164164
doOnResponseCommitted();
165165
super.sendRedirect(location);
166166
}
@@ -207,7 +207,7 @@ private void trackContentLength(char content) {
207207
}
208208
}
209209

210-
private void trackContentLength(Object content) {
210+
private void trackContentLength(@Nullable Object content) {
211211
if (!this.disableOnCommitted) {
212212
trackContentLength(String.valueOf(content));
213213
}
@@ -249,7 +249,7 @@ private void trackContentLengthLn() {
249249
}
250250
}
251251

252-
private void trackContentLength(String content) {
252+
private void trackContentLength(@Nullable String content) {
253253
if (!this.disableOnCommitted) {
254254
int contentLength = (content != null) ? content.length() : 4;
255255
checkContentLength(contentLength);
@@ -356,7 +356,7 @@ public void write(String s, int off, int len) {
356356
}
357357

358358
@Override
359-
public void write(String s) {
359+
public void write(@Nullable String s) {
360360
trackContentLength(s);
361361
this.delegate.write(s);
362362
}
@@ -404,13 +404,13 @@ public void print(char[] s) {
404404
}
405405

406406
@Override
407-
public void print(String s) {
407+
public void print(@Nullable String s) {
408408
trackContentLength(s);
409409
this.delegate.print(s);
410410
}
411411

412412
@Override
413-
public void print(Object obj) {
413+
public void print(@Nullable Object obj) {
414414
trackContentLength(obj);
415415
this.delegate.print(obj);
416416
}
@@ -471,14 +471,14 @@ public void println(char[] x) {
471471
}
472472

473473
@Override
474-
public void println(String x) {
474+
public void println(@Nullable String x) {
475475
trackContentLength(x);
476476
trackContentLengthLn();
477477
this.delegate.println(x);
478478
}
479479

480480
@Override
481-
public void println(Object x) {
481+
public void println(@Nullable Object x) {
482482
trackContentLength(x);
483483
trackContentLengthLn();
484484
this.delegate.println(x);
@@ -505,13 +505,13 @@ public PrintWriter format(Locale l, String format, Object... args) {
505505
}
506506

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

513513
@Override
514-
public PrintWriter append(CharSequence csq, int start, int end) {
514+
public PrintWriter append(@Nullable CharSequence csq, int start, int end) {
515515
checkContentLength(end - start);
516516
return this.delegate.append(csq, start, end);
517517
}
@@ -596,7 +596,7 @@ public void print(long l) throws IOException {
596596
}
597597

598598
@Override
599-
public void print(String s) throws IOException {
599+
public void print(@Nullable String s) throws IOException {
600600
trackContentLength(s);
601601
this.delegate.print(s);
602602
}
@@ -650,7 +650,7 @@ public void println(long l) throws IOException {
650650
}
651651

652652
@Override
653-
public void println(String s) throws IOException {
653+
public void println(@Nullable String s) throws IOException {
654654
trackContentLength(s);
655655
trackContentLengthLn();
656656
this.delegate.println(s);

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)