Skip to content

Commit 36875f6

Browse files
committed
intermeidta revision - added sanitizing endpoint and associated test (including relatively tricky case
1 parent a229f97 commit 36875f6

2 files changed

Lines changed: 104 additions & 4 deletions

File tree

basic-tests/src/main/java/example/controller/ExampleController.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,19 @@ public String getResourceURI(String resourceFileName) {
286286
}
287287
}
288288

289-
@RequestMapping(method = RequestMethod.GET, value = "/sanitize/{file}", produces = {
289+
@RequestMapping(method = RequestMethod.GET, value = "/sanitize/{filename}/data", produces = {
290290
MediaType.APPLICATION_OCTET_STREAM_VALUE })
291-
public ResponseEntity<?> sanitizeFileName(@PathVariable("file") String file) {
292-
final String special = "^[[\\|$<>&!,`]]";
291+
public ResponseEntity<?> sanitizeFileName(
292+
@PathVariable("filename") String fileName) {
293+
final String special = "^[[\\|$<>&!,{}`]]";
293294
final Pattern regex = Pattern.compile(special);
294-
return ResponseEntity.status(HttpStatus.OK).body(service.handleData(file));
295+
296+
return regex.matcher(fileName).find()
297+
? ResponseEntity.status(HttpStatus.OK)
298+
.body(service.handleData("valid filename: " + fileName))
299+
: ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
300+
.body("invalid filename");
301+
295302
}
296303

297304
@GetMapping(value = "/servererror", produces = MediaType.APPLICATION_JSON_VALUE)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package example.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
5+
import org.springframework.boot.test.mock.mockito.MockBean;
6+
import org.springframework.context.annotation.PropertySource;
7+
8+
import static org.hamcrest.Matchers.containsString;
9+
import static org.hamcrest.CoreMatchers.notNullValue;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
12+
import static org.hamcrest.Matchers.hasSize;
13+
import static org.hamcrest.Matchers.is;
14+
import static org.hamcrest.Matchers.equalTo;
15+
import static org.hamcrest.collection.IsArrayWithSize.*;
16+
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Disabled;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.Assumptions;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import org.springframework.http.MediaType;
25+
import org.springframework.test.web.servlet.MockMvc;
26+
import org.springframework.test.web.servlet.ResultActions;
27+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
28+
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
32+
// https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/result/JsonPathResultMatchers.html
33+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
34+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35+
// requires a later version ?
36+
// import static org.springframework.test.web.servlet.result.JsonPathResultMatchers.isArray;
37+
import java.net.InetSocketAddress;
38+
import java.net.Socket;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
import java.util.stream.Collectors;
42+
43+
import example.controller.ExampleController;
44+
import example.service.ExampleService;
45+
import example.Application;
46+
47+
@WebMvcTest
48+
public class MVCSanitizePathVariableTest {
49+
50+
static String route = "/basic/sanitize/";
51+
// http://www.w3schools.com/tags/ref_urlencode.ASP
52+
static final List<String> badFilenames = Arrays.asList(
53+
new String[] { "%03Ctest", "test%24", "test$$", "test!", "test&" });
54+
static final List<String> goodFilenames = Arrays.asList(
55+
new String[] { "alphanumric123", "white space is OK", "_-123", "test@" });
56+
static final List<String> invalidFilenames = Arrays
57+
.asList(new String[] { "test?" });
58+
@Autowired
59+
private MockMvc mvc;
60+
61+
@MockBean
62+
private ExampleService mockService;
63+
private ResultActions resultActions;
64+
65+
// examine HTTP status and body - missing request params
66+
@Test
67+
public void test1() throws Exception {
68+
for (String filename : badFilenames) {
69+
resultActions = mvc.perform(get(route + "/" + filename + "/data"));
70+
resultActions.andExpect(status().isMethodNotAllowed())
71+
.andExpect(content().string("invalid filename"));
72+
}
73+
}
74+
75+
@Test
76+
public void test2() throws Exception {
77+
for (String filename : goodFilenames) {
78+
resultActions = mvc.perform(get(route + "/" + filename + "/data"));
79+
resultActions.andExpect(status().isMethodNotAllowed())
80+
.andExpect(content().string("invalid filename"));
81+
}
82+
}
83+
84+
@Test
85+
public void test3() throws Exception {
86+
for (String filename : invalidFilenames) {
87+
resultActions = mvc.perform(get(route + "/" + filename + "/data"));
88+
resultActions.andExpect(status().isNotFound())
89+
.andExpect(content().string(""));
90+
}
91+
}
92+
93+
}

0 commit comments

Comments
 (0)