Skip to content

Commit 5fdac98

Browse files
authored
Merge pull request #74 from bnc-projects/use-docker-container-tag
Use docker container tag
2 parents e0be9a5 + 15ef2a0 commit 5fdac98

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

Diff for: build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ allprojects {
6363
toolVersion = "0.8.5"
6464
}
6565

66+
jacocoTestReport {
67+
reports {
68+
xml.enabled true
69+
}
70+
}
71+
6672
checkstyle {
6773
toolVersion '8.28'
6874
tasks.withType(Checkstyle) {

Diff for: service/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM adoptopenjdk:11-jre-hotspot
1+
FROM adoptopenjdk:11.0.7_10-jre-hotspot-bionic
22

33
RUN groupadd -g 999 appuser && useradd -r -u 999 -g appuser appuser
44
USER appuser

Diff for: service/build.gradle

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id "org.springframework.boot" version "2.2.4.RELEASE"
2+
id "org.springframework.boot" version "2.2.7.RELEASE"
33
id "io.spring.dependency-management" version "1.0.9.RELEASE"
44
id "com.palantir.docker" version "0.24.0"
55
id "com.gorylenko.gradle-git-properties" version "2.2.0"
@@ -37,9 +37,11 @@ docker {
3737

3838
dependencies {
3939
implementation project(':client')
40+
4041
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
4142
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
42-
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.2.1.RELEASE'
43-
implementation group: 'io.micrometer', name: 'micrometer-registry-new-relic', version: '1.3.3'
43+
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.2.2.RELEASE'
44+
implementation group: 'io.micrometer', name: 'micrometer-registry-new-relic'
45+
4446
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
4547
}

Diff for: service/src/main/java/com/bnc/sbjb/rest/DefaultExceptionHandler.java

+20-1
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
}
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)