Skip to content

Commit 30e1781

Browse files
DMP-3682 Error messages disclosure (#3083)
1 parent 9b7f079 commit 30e1781

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerSearchPostTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.gov.hmcts.darts.cases.controller;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.junit.jupiter.api.BeforeEach;
45
import org.junit.jupiter.api.Test;
56
import org.skyscreamer.jsonassert.JSONCompareMode;
@@ -48,6 +49,7 @@
4849
import static uk.gov.hmcts.darts.test.common.data.SecurityGroupTestData.createGroupForRole;
4950
import static uk.gov.hmcts.darts.testutils.stubs.UserAccountStub.INTEGRATION_TEST_USER_EMAIL;
5051

52+
@Slf4j
5153
@AutoConfigureMockMvc
5254
class CaseControllerSearchPostTest extends IntegrationBase {
5355

@@ -539,6 +541,37 @@ void casesSearchPost_shouldReturn400_whenCourtroomIsLowercase() throws Exception
539541
.andExpect(status().isBadRequest());
540542
}
541543

544+
@Test
545+
void casesSearchPost_shouldReturn400_whenUnknownField() throws Exception {
546+
setupUserAndSecurityGroupForCourthouses(List.of(swanseaCourthouse));
547+
String requestBody = """
548+
{
549+
"courtroom": "1",
550+
"date_from": "2023-05-19",
551+
"date_to": "2023-05-20",
552+
"test": "test unknown field"
553+
}""";
554+
555+
String expectedResponse = """
556+
{
557+
"status": 400,
558+
"title": "Bad Request",
559+
"detail": "JSON parse error"
560+
}
561+
""";
562+
563+
MockHttpServletRequestBuilder requestBuilder = post("/cases/search")
564+
.contentType(MediaType.APPLICATION_JSON_VALUE)
565+
.content(requestBody);
566+
567+
var result = mockMvc.perform(requestBuilder)
568+
.andExpect(status().isBadRequest())
569+
.andReturn();
570+
571+
String actualResponse = result.getResponse().getContentAsString();
572+
assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);
573+
}
574+
542575
@Test
543576
void casesSearchPost_shouldReturn400_whenEventTextLengthIs2() throws Exception {
544577
user = dartsDatabase.getUserAccountStub().getIntegrationTestUserAccountEntity();

src/integrationTest/java/uk/gov/hmcts/darts/common/exception/ExceptionHandlerTest.java renamed to src/integrationTest/java/uk/gov/hmcts/darts/common/exception/GlobalExceptionHandlerTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
import org.springframework.context.annotation.Import;
1212
import org.springframework.http.HttpStatus;
1313
import org.springframework.http.ResponseEntity;
14+
import org.springframework.http.converter.HttpMessageNotReadableException;
1415
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1516
import org.springframework.test.web.servlet.MockMvc;
1617
import org.springframework.test.web.servlet.MvcResult;
1718
import org.springframework.web.bind.annotation.GetMapping;
1819
import org.springframework.web.bind.annotation.RestController;
19-
import uk.gov.hmcts.darts.common.exception.ExceptionHandlerTest.MockController;
20+
import uk.gov.hmcts.darts.common.exception.GlobalExceptionHandlerTest.MockController;
2021
import uk.gov.hmcts.darts.testutils.IntegrationBase;
2122

2223
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2324
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2425

2526
@AutoConfigureMockMvc
2627
@Import(MockController.class)
27-
class ExceptionHandlerTest extends IntegrationBase {
28+
class GlobalExceptionHandlerTest extends IntegrationBase {
2829

2930
private static final String ENDPOINT = "/test";
3031

@@ -133,4 +134,26 @@ void shouldReturnAGenericRfc7807ResponseWhenARuntimeExceptionIsThrown() throws E
133134
JSONAssert.assertEquals(expectedResponseBody, actualResponseBody, JSONCompareMode.NON_EXTENSIBLE);
134135
}
135136

137+
@Test
138+
void handleMessageNotReadableHandler_shouldReturnBadRequestProblem_whenHttpMessageNotReadableExceptionIsThrown() throws Exception {
139+
Mockito.when(mockController.test())
140+
.thenThrow(new HttpMessageNotReadableException("JSON parse error"));
141+
142+
MvcResult response = mockMvc.perform(get(ENDPOINT))
143+
.andExpect(status().isBadRequest())
144+
.andReturn();
145+
146+
String actualResponseBody = response.getResponse().getContentAsString();
147+
148+
String expectedResponseBody = """
149+
{
150+
"detail":"JSON parse error",
151+
"title":"Bad Request",
152+
"status":400
153+
}
154+
""";
155+
156+
JSONAssert.assertEquals(expectedResponseBody, actualResponseBody, JSONCompareMode.NON_EXTENSIBLE);
157+
}
158+
136159
}

src/main/java/uk/gov/hmcts/darts/common/exception/ExceptionHandler.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package uk.gov.hmcts.darts.common.exception;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.http.converter.HttpMessageNotReadableException;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.context.request.NativeWebRequest;
11+
import org.zalando.problem.Problem;
12+
import org.zalando.problem.spring.common.HttpStatusAdapter;
13+
import org.zalando.problem.spring.web.advice.ProblemHandling;
14+
15+
16+
@ControllerAdvice
17+
@EnableAutoConfiguration(exclude = ErrorMvcAutoConfiguration.class)
18+
public class GlobalExceptionHandler implements ProblemHandling, DartsApiTrait, ErrorResponseAdviceTrait {
19+
20+
// Override the default HttpMessageNotReadableException as this reveals class names in the exception message (DMP-3682)
21+
@Override
22+
@ExceptionHandler(HttpMessageNotReadableException.class)
23+
public ResponseEntity<Problem> handleMessageNotReadableException(HttpMessageNotReadableException exception,
24+
NativeWebRequest request) {
25+
Problem problem = Problem.builder()
26+
.withTitle("Bad Request")
27+
.withStatus(new HttpStatusAdapter(HttpStatus.BAD_REQUEST))
28+
.withDetail("JSON parse error")
29+
.build();
30+
return create(exception, problem, request);
31+
}
32+
}

0 commit comments

Comments
 (0)