Validate server.servlet.path does not contain '*'
Update `WebMvcProperties` to enforce that `server.servlet.path` never contains a wildcard ['*'] character. Closes gh-13292
This commit is contained in:
parent
6baaa3df77
commit
8b59503291
|
@ -242,6 +242,7 @@ public class WebMvcProperties {
|
|||
|
||||
public void setPath(String path) {
|
||||
Assert.notNull(path, "Path must not be null");
|
||||
Assert.isTrue(!path.contains("*"), "Path must not contain wildcards");
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
|
@ -257,9 +258,6 @@ public class WebMvcProperties {
|
|||
if (this.path.equals("") || this.path.equals("/")) {
|
||||
return "/";
|
||||
}
|
||||
if (this.path.contains("*")) {
|
||||
return this.path;
|
||||
}
|
||||
if (this.path.endsWith("/")) {
|
||||
return this.path + "*";
|
||||
}
|
||||
|
|
|
@ -20,13 +20,16 @@ import java.util.Collections;
|
|||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.testcontainers.shaded.com.google.common.base.Throwables;
|
||||
|
||||
import org.springframework.boot.context.properties.bind.BindException;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link WebMvcProperties}.
|
||||
|
@ -38,19 +41,28 @@ public class WebMvcPropertiesTests {
|
|||
private final WebMvcProperties properties = new WebMvcProperties();
|
||||
|
||||
@Test
|
||||
public void testServletPathAsMapping() {
|
||||
bind("spring.mvc.servlet.path", "/foo/*");
|
||||
public void servletPathWhenEndsWithSlashHasValidMappingAndPrefix() {
|
||||
bind("spring.mvc.servlet.path", "/foo/");
|
||||
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
|
||||
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServletPathAsPrefix() {
|
||||
public void servletPathWhenDoesNotEndWithSlashHasValidMappingAndPrefix() {
|
||||
bind("spring.mvc.servlet.path", "/foo");
|
||||
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
|
||||
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletPathWhenHasWildcardThrowsException() {
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> bind("spring.mvc.servlet.path", "/*"))
|
||||
.withRootCauseInstanceOf(IllegalArgumentException.class)
|
||||
.satisfies((ex) -> assertThat(Throwables.getRootCause(ex))
|
||||
.hasMessage("Path must not contain wildcards"));
|
||||
}
|
||||
|
||||
private void bind(String name, String value) {
|
||||
bind(Collections.singletonMap(name, value));
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.mvc.servlet.path:/spring/*")
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.mvc.servlet.path:/spring/")
|
||||
@DirtiesContext
|
||||
public class RemappedErrorViewIntegrationTests {
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
spring.mvc.servlet.path=/home/*
|
||||
spring.mvc.servlet.path=/home/
|
||||
|
|
Loading…
Reference in New Issue