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:
Phillip Webb 2018-10-04 14:06:58 -07:00
parent 6baaa3df77
commit 8b59503291
4 changed files with 18 additions and 8 deletions

View File

@ -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 + "*";
}

View File

@ -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));
}

View File

@ -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 {

View File

@ -1 +1 @@
spring.mvc.servlet.path=/home/*
spring.mvc.servlet.path=/home/