diff --git a/application/src/main/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolver.java b/application/src/main/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolver.java index 029abec8f2..52afaaa214 100644 --- a/application/src/main/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolver.java +++ b/application/src/main/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolver.java @@ -17,8 +17,8 @@ import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; import org.springframework.security.oauth2.core.endpoint.PkceParameterNames; import org.springframework.security.oauth2.core.oidc.OidcScopes; import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames; +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; import org.springframework.security.web.util.UrlUtils; -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.web.util.UriComponents; @@ -50,7 +50,7 @@ public class CustomOAuth2AuthorizationRequestResolver implements OAuth2Authoriza private static final String REGISTRATION_ID_URI_VARIABLE_NAME = "registrationId"; private static final char PATH_DELIMITER = '/'; - private final AntPathRequestMatcher authorizationRequestMatcher = new AntPathRequestMatcher( + private final PathPatternRequestMatcher authorizationRequestMatcher = PathPatternRequestMatcher.withDefaults().matcher( DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{" + REGISTRATION_ID_URI_VARIABLE_NAME + "}"); private final StringKeyGenerator stateGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder()); private final StringKeyGenerator secureKeyGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); diff --git a/application/src/main/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcher.java b/application/src/main/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcher.java index 6ff5ef42be..fe4b9fc987 100644 --- a/application/src/main/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcher.java +++ b/application/src/main/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcher.java @@ -4,7 +4,7 @@ package org.thingsboard.server.service.security.auth.jwt; import jakarta.servlet.http.HttpServletRequest; -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; import org.springframework.security.web.util.matcher.OrRequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.util.Assert; @@ -19,8 +19,9 @@ public class SkipPathRequestMatcher implements RequestMatcher { public SkipPathRequestMatcher(List pathsToSkip, List pathsToProcess) { Assert.notNull(pathsToSkip, "List of paths to skip is required."); - List skip = pathsToSkip.stream().map(AntPathRequestMatcher::new).collect(Collectors.toList()); - List process = pathsToProcess.stream().map(AntPathRequestMatcher::new).collect(Collectors.toList()); + PathPatternRequestMatcher.Builder builder = PathPatternRequestMatcher.withDefaults(); + List skip = pathsToSkip.stream().map(builder::matcher).collect(Collectors.toList()); + List process = pathsToProcess.stream().map(builder::matcher).collect(Collectors.toList()); skipMatchers = new OrRequestMatcher(skip); processMatchers = new OrRequestMatcher(process); } diff --git a/application/src/test/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolverMatcherTest.java b/application/src/test/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolverMatcherTest.java new file mode 100644 index 0000000000..108e04cd6f --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolverMatcherTest.java @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors +// SPDX-License-Identifier: Apache-2.0 +package org.thingsboard.server.config; + +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; + +import static org.assertj.core.api.Assertions.assertThat; + +class CustomOAuth2AuthorizationRequestResolverMatcherTest { + + private final PathPatternRequestMatcher matcher = PathPatternRequestMatcher.withDefaults().matcher("/oauth2/authorization/{registrationId}"); + + @Test + void extractsRegistrationId() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/oauth2/authorization/abc-123"); + assertThat(matcher.matches(request)).isTrue(); + assertThat(matcher.matcher(request).getVariables()).containsEntry("registrationId", "abc-123"); + } + + @Test + void rejectsDeeperPaths() { + assertThat(matcher.matches(new MockHttpServletRequest("GET", "/oauth2/authorization/abc/extra"))).isFalse(); + } + +} diff --git a/application/src/test/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcherTest.java b/application/src/test/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcherTest.java new file mode 100644 index 0000000000..f937534cc3 --- /dev/null +++ b/application/src/test/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcherTest.java @@ -0,0 +1,61 @@ +// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors +// SPDX-License-Identifier: Apache-2.0 +package org.thingsboard.server.service.security.auth.jwt; + +import org.junit.jupiter.api.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.DEVICE_API_ENTRY_POINT; +import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.DEVICE_CONNECTIVITY_CERTIFICATE_DOWNLOAD_ENTRY_POINT; +import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.FORM_BASED_LOGIN_ENTRY_POINT; + +class SkipPathRequestMatcherTest { + + private final SkipPathRequestMatcher matcher = new SkipPathRequestMatcher( + List.of(FORM_BASED_LOGIN_ENTRY_POINT, DEVICE_API_ENTRY_POINT, "/api/noauth/setup/**", + DEVICE_CONNECTIVITY_CERTIFICATE_DOWNLOAD_ENTRY_POINT, "/api/noauth/whiteLabel/loginLogo/*/*"), + List.of("/api/**", "/api/user/{userId:[0-9a-fA-F-]{36}}")); + + @Test + void processesProtectedApiPaths() { + assertThat(matcher.matches(request("/api/device/123"))).isTrue(); + assertThat(matcher.matches(request("/api/user/0a1b2c3d-0000-1111-2222-333344445555"))).isTrue(); + } + + @Test + void skipsListedPaths() { + assertThat(matcher.matches(request("/api/auth/login"))).isFalse(); + assertThat(matcher.matches(request("/api/v1/token/telemetry"))).isFalse(); + assertThat(matcher.matches(request("/api/v1"))).isFalse(); + assertThat(matcher.matches(request("/api/noauth/setup/status"))).isFalse(); + assertThat(matcher.matches(request("/api/device-connectivity/mqtt/certificate/download"))).isFalse(); + } + + @Test + void singleSegmentWildcardsDoNotSpanSegments() { + assertThat(matcher.matches(request("/api/noauth/whiteLabel/loginLogo/abc/def"))).isFalse(); + assertThat(matcher.matches(request("/api/noauth/whiteLabel/loginLogo/abc/def/ghi"))).isTrue(); + } + + @Test + void doesNotSkipNearMisses() { + assertThat(matcher.matches(request("/api/auth/login/"))).isTrue(); + assertThat(matcher.matches(request("/api/auth/loginx"))).isTrue(); + assertThat(matcher.matches(request("/API/auth/login"))).isFalse(); + } + + @Test + void ignoresPathsOutsideApi() { + assertThat(matcher.matches(request("/static/index.html"))).isFalse(); + } + + private static MockHttpServletRequest request(String path) { + MockHttpServletRequest request = new MockHttpServletRequest("GET", path); + request.setServletPath(path); + return request; + } + +} diff --git a/common/transport/http/src/main/java/org/thingsboard/server/transport/http/config/TransportSecurityConfiguration.java b/common/transport/http/src/main/java/org/thingsboard/server/transport/http/config/TransportSecurityConfiguration.java index 6b167a6203..7f94755522 100644 --- a/common/transport/http/src/main/java/org/thingsboard/server/transport/http/config/TransportSecurityConfiguration.java +++ b/common/transport/http/src/main/java/org/thingsboard/server/transport/http/config/TransportSecurityConfiguration.java @@ -12,7 +12,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; @Configuration @EnableWebSecurity @@ -32,7 +32,7 @@ public class TransportSecurityConfiguration { @Order(1) SecurityFilterChain httpTransportFilterChain(HttpSecurity http) throws Exception { http - .securityMatcher(AntPathRequestMatcher.antMatcher(DEVICE_API_ENTRY_POINT)) + .securityMatcher(PathPatternRequestMatcher.withDefaults().matcher(DEVICE_API_ENTRY_POINT)) .cors(cors -> { }) .csrf(AbstractHttpConfigurer::disable)