From 76859d063245fd1ce202362a9301c386f4fd9752 Mon Sep 17 00:00:00 2001 From: ashasomayajula Date: Mon, 22 May 2023 18:48:39 -0500 Subject: [PATCH 1/2] Fix for https://github.com/spring-projects/spring-boot/issues/34834 --- .../endpoint/web/WebEndpointProperties.java | 2 +- .../web/WebEndpointPropertiesTests.java | 68 ++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) 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 06a9223eaf62..2ecd45da2f2a 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 @@ -67,7 +67,7 @@ public void setBasePath(String basePath) { } private String cleanBasePath(String basePath) { - if (StringUtils.hasText(basePath) && basePath.endsWith("/")) { + if (StringUtils.hasText(basePath) && basePath.endsWith("/") && StringUtils.cleanPath(basePath).length() != 1) { return basePath.substring(0, basePath.length() - 1); } return 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 441d15bf6645..8a7cad0b46fc 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,10 +16,18 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; /** * Tests for {@link WebEndpointProperties}. @@ -28,6 +36,8 @@ */ class WebEndpointPropertiesTests { + private static final String BLANK_ERR_MSG = "Base path must start with '/' or be empty"; + @Test void defaultBasePathShouldBeApplication() { WebEndpointProperties properties = new WebEndpointProperties(); @@ -38,7 +48,7 @@ void defaultBasePathShouldBeApplication() { void basePathShouldBeCleaned() { WebEndpointProperties properties = new WebEndpointProperties(); properties.setBasePath("/"); - assertThat(properties.getBasePath()).isEmpty(); + assertThat(properties.getBasePath()).isNotEmpty(); properties.setBasePath("/actuator/"); assertThat(properties.getBasePath()).isEqualTo("/actuator"); } @@ -57,4 +67,60 @@ void basePathCanBeEmpty() { assertThat(properties.getBasePath()).isEmpty(); } + @ParameterizedTest + @MethodSource("notToBeCleanedArguments") + void basePathShouldNotBeCleaned(String path, String expected) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isNotEmpty(); + assertEquals(properties.getBasePath(), expected); + } + + private static Stream notToBeCleanedArguments() { + return Stream.of( + Arguments.of("/path", "/path"), + Arguments.of("/path/", "/path"), + Arguments.of("/", "/"), + Arguments.of("/path/path2", "/path/path2"), + Arguments.of("/path/path2/", "/path/path2")); + } + + @ParameterizedTest + @MethodSource("notEmptyPaths") + void basePathShouldNotBeEmpty(String path) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isNotEmpty(); + } + + private static Stream notEmptyPaths() { + return Stream.of(Arguments.of("/path"), Arguments.of("/path/")); + } + + @ParameterizedTest + @ValueSource(strings = {""}) + void basePathShouldBeEmpty(String path) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isEmpty(); + } + + @ParameterizedTest + @MethodSource("argumentsForExceptions") + void foo(String input, Class expectedEx, String expectedExMsg) { + Assertions.assertThrows( + expectedEx, () -> new WebEndpointProperties().setBasePath(input), expectedExMsg); + } + + private static Stream argumentsForExceptions() { + return Stream.of( + Arguments.of( + "invalidpath", + IllegalArgumentException.class, + BLANK_ERR_MSG), + Arguments.of( + " ", IllegalArgumentException.class, BLANK_ERR_MSG), + Arguments.of( + "null", IllegalArgumentException.class, BLANK_ERR_MSG)); + } } From 4e3b47c18bd96dbb4890f2015ccb79ac75edef09 Mon Sep 17 00:00:00 2001 From: ashasomayajula Date: Tue, 23 May 2023 18:28:15 -0500 Subject: [PATCH 2/2] Fix for https://github.com/spring-projects/spring-boot/issues/34834 --- .../autoconfigure/endpoint/web/WebEndpointPropertiesTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8a7cad0b46fc..1e0e607175ec 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 @@ -107,7 +107,7 @@ void basePathShouldBeEmpty(String path) { @ParameterizedTest @MethodSource("argumentsForExceptions") - void foo(String input, Class expectedEx, String expectedExMsg) { + void basePathShouldThrowException(String input, Class expectedEx, String expectedExMsg) { Assertions.assertThrows( expectedEx, () -> new WebEndpointProperties().setBasePath(input), expectedExMsg); }