From e6149fda1c1b1b0cfa824a294391d68bf219d1fb Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 15 Mar 2018 11:33:59 -0700 Subject: [PATCH] Assert endpoints basePath starts with '/' or is empty Fixes gh-12489 --- .../endpoint/web/WebEndpointProperties.java | 3 +++ .../web/WebEndpointPropertiesTests.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java index 13b03037b1..ee20af6e20 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Set; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -56,6 +57,8 @@ public class WebEndpointProperties { } public void setBasePath(String basePath) { + Assert.isTrue(basePath.isEmpty() || basePath.startsWith("/"), + "Base path must start with '/' or be empty"); this.basePath = cleanBasePath(basePath); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java index bcd824eca8..103e5275f9 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java @@ -16,7 +16,9 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; @@ -27,6 +29,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class WebEndpointPropertiesTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void defaultBasePathShouldBeApplication() { WebEndpointProperties properties = new WebEndpointProperties(); @@ -38,6 +43,23 @@ public class WebEndpointPropertiesTests { WebEndpointProperties properties = new WebEndpointProperties(); properties.setBasePath("/"); assertThat(properties.getBasePath()).isEqualTo(""); + properties.setBasePath("/actuator/"); + assertThat(properties.getBasePath()).isEqualTo("/actuator"); + } + + @Test + public void basePathMustStartWithSlash() { + WebEndpointProperties properties = new WebEndpointProperties(); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Base path must start with '/' or be empty"); + properties.setBasePath("admin"); + } + + @Test + public void basePathCanBeEmpty() { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(""); + assertThat(properties.getBasePath()).isEqualTo(""); } }