Skip to content

Commit a78e263

Browse files
author
james
committed
1 parent da232ac commit a78e263

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

service/src/main/java/com/bnc/sbjb/rest/DefaultExceptionHandler.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,27 @@ public CustomError handleExceptionMethodNotAllowed(final Exception ex) {
8686
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
8787
@ResponseBody
8888
public CustomError handleException(HttpServletRequest httpServletRequest, Exception ex) {
89-
logger.error("Unknown exception [queryString=\"{}\" errorMessage=\"{}\"]", httpServletRequest.getQueryString(), ex.getMessage(), ex);
89+
final String queryString = sanitizedQueryString(httpServletRequest.getQueryString(), 250);
90+
logger.error("Unknown exception [queryString=\"{}\" errorMessage=\"{}\"]", queryString, ex.getMessage(), ex);
9091
return new CustomError(HttpStatus.INTERNAL_SERVER_ERROR,
9192
"Oops something went wrong. Please contact us if this keeps occurring.");
9293
}
94+
95+
/**
96+
* Logging should not be vulnerable to injection attacks.
97+
*
98+
* @param queryString The query string to sanitize.
99+
* @param maxLength The max acceptable length
100+
* @return A string that is no more characters than the max length requested, with new lines and tabs replaced.
101+
*/
102+
private String sanitizedQueryString(String queryString, int maxLength) {
103+
if (queryString == null) {
104+
return "";
105+
}
106+
return queryString
107+
.substring(0, Math.min(queryString.length(), maxLength))
108+
.replaceAll("[\n]+", "\\\\n")
109+
.replaceAll("[\r]+", "\\\\r")
110+
.replaceAll("[\t]+", "\\\\t");
111+
}
93112
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.bnc.sbjb.rest;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.verifyNoMoreInteractions;
6+
import static org.mockito.Mockito.when;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import org.junit.jupiter.api.Test;
10+
11+
class DefaultExceptionHandlerTest {
12+
13+
@Test
14+
void testHandleExceptionWithBigQueryString() {
15+
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
16+
when(mockRequest.getQueryString()).thenReturn(
17+
"This is a string with a lot of text and several extra whitespaces\n"
18+
+ "This is a string with a lot of text and several extra whitespaces\n"
19+
+ "This is a string with a lot of text and several extra whitespaces\n"
20+
+ "This is a string with a lot of text and several extra whitespaces\n"
21+
+ "This is a string with a lot of text and several extra whitespaces\n");
22+
new DefaultExceptionHandler().handleException(mockRequest, new Exception("Just testing"));
23+
// Just to include a test.
24+
verify(mockRequest).getQueryString();
25+
verifyNoMoreInteractions(mockRequest);
26+
}
27+
28+
@Test
29+
void testHandleExceptionWithLittleQueryString() {
30+
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
31+
when(mockRequest.getQueryString()).thenReturn("\n");
32+
new DefaultExceptionHandler().handleException(mockRequest, new Exception("Just testing"));
33+
// Just to include a test.
34+
verify(mockRequest).getQueryString();
35+
verifyNoMoreInteractions(mockRequest);
36+
}
37+
38+
@Test
39+
void testHandleExceptionWithNoQueryString() {
40+
final HttpServletRequest mockRequest = mock(HttpServletRequest.class);
41+
when(mockRequest.getQueryString()).thenReturn(null);
42+
new DefaultExceptionHandler().handleException(mockRequest, new Exception("Just testing"));
43+
// Just to include a test.
44+
verify(mockRequest).getQueryString();
45+
}
46+
}

0 commit comments

Comments
 (0)