Commit 8b595032 authored by Phillip Webb's avatar Phillip Webb

Validate server.servlet.path does not contain '*'

Update `WebMvcProperties` to enforce that `server.servlet.path` never
contains a wildcard ['*'] character.

Closes gh-13292
parent 6baaa3df
...@@ -242,6 +242,7 @@ public class WebMvcProperties { ...@@ -242,6 +242,7 @@ public class WebMvcProperties {
public void setPath(String path) { public void setPath(String path) {
Assert.notNull(path, "Path must not be null"); Assert.notNull(path, "Path must not be null");
Assert.isTrue(!path.contains("*"), "Path must not contain wildcards");
this.path = path; this.path = path;
} }
...@@ -257,9 +258,6 @@ public class WebMvcProperties { ...@@ -257,9 +258,6 @@ public class WebMvcProperties {
if (this.path.equals("") || this.path.equals("/")) { if (this.path.equals("") || this.path.equals("/")) {
return "/"; return "/";
} }
if (this.path.contains("*")) {
return this.path;
}
if (this.path.endsWith("/")) { if (this.path.endsWith("/")) {
return this.path + "*"; return this.path + "*";
} }
......
...@@ -20,13 +20,16 @@ import java.util.Collections; ...@@ -20,13 +20,16 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.junit.Test; 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.Bindable;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/** /**
* Tests for {@link WebMvcProperties}. * Tests for {@link WebMvcProperties}.
...@@ -38,19 +41,28 @@ public class WebMvcPropertiesTests { ...@@ -38,19 +41,28 @@ public class WebMvcPropertiesTests {
private final WebMvcProperties properties = new WebMvcProperties(); private final WebMvcProperties properties = new WebMvcProperties();
@Test @Test
public void testServletPathAsMapping() { public void servletPathWhenEndsWithSlashHasValidMappingAndPrefix() {
bind("spring.mvc.servlet.path", "/foo/*"); bind("spring.mvc.servlet.path", "/foo/");
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo"); assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
} }
@Test @Test
public void testServletPathAsPrefix() { public void servletPathWhenDoesNotEndWithSlashHasValidMappingAndPrefix() {
bind("spring.mvc.servlet.path", "/foo"); bind("spring.mvc.servlet.path", "/foo");
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).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) { private void bind(String name, String value) {
bind(Collections.singletonMap(name, value)); bind(Collections.singletonMap(name, value));
} }
......
...@@ -47,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -47,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer * @author Dave Syer
*/ */
@RunWith(SpringRunner.class) @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 @DirtiesContext
public class RemappedErrorViewIntegrationTests { public class RemappedErrorViewIntegrationTests {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment