Browse Source

Replace AntPathRequestMatcher with PathPatternRequestMatcher

pull/16341/head
Viacheslav Klimov 1 month ago
parent
commit
22251084eb
Failed to extract signature
  1. 4
      application/src/main/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolver.java
  2. 7
      application/src/main/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcher.java
  3. 27
      application/src/test/java/org/thingsboard/server/config/CustomOAuth2AuthorizationRequestResolverMatcherTest.java
  4. 61
      application/src/test/java/org/thingsboard/server/service/security/auth/jwt/SkipPathRequestMatcherTest.java
  5. 4
      common/transport/http/src/main/java/org/thingsboard/server/transport/http/config/TransportSecurityConfiguration.java

4
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);

7
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<String> pathsToSkip, List<String> pathsToProcess) {
Assert.notNull(pathsToSkip, "List of paths to skip is required.");
List<RequestMatcher> skip = pathsToSkip.stream().map(AntPathRequestMatcher::new).collect(Collectors.toList());
List<RequestMatcher> process = pathsToProcess.stream().map(AntPathRequestMatcher::new).collect(Collectors.toList());
PathPatternRequestMatcher.Builder builder = PathPatternRequestMatcher.withDefaults();
List<RequestMatcher> skip = pathsToSkip.stream().<RequestMatcher>map(builder::matcher).collect(Collectors.toList());
List<RequestMatcher> process = pathsToProcess.stream().<RequestMatcher>map(builder::matcher).collect(Collectors.toList());
skipMatchers = new OrRequestMatcher(skip);
processMatchers = new OrRequestMatcher(process);
}

27
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();
}
}

61
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;
}
}

4
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)

Loading…
Cancel
Save