Skip to content

Commit 097cde1

Browse files
committed
Restrict EndpointRequest links to the specified HTTP method
EndpointRequest.toAnyEndpoint().withHttpMethod(...) restricted endpoint paths but still allowed any HTTP method for the links path. Restrict the links path using the configured HttpMethod in both servlet and reactive matchers. See gh-49885 Signed-off-by: Lee JiWon <dlwldnjs1009@gmail.com>
1 parent 6f85469 commit 097cde1

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/actuate/web/reactive/EndpointRequest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static EndpointServerWebExchangeMatcher to(String... endpoints) {
120120
* @return the configured {@link ServerWebExchangeMatcher}
121121
*/
122122
public static LinksServerWebExchangeMatcher toLinks() {
123-
return new LinksServerWebExchangeMatcher();
123+
return new LinksServerWebExchangeMatcher(null);
124124
}
125125

126126
/**
@@ -344,7 +344,7 @@ protected ServerWebExchangeMatcher createDelegate(PathMappedEndpoints endpoints)
344344
List<ServerWebExchangeMatcher> delegateMatchers = getDelegateMatchers(paths, this.httpMethod);
345345
String linksPath = getLinksPath(endpoints.getBasePath());
346346
if (this.includeLinks && linksPath != null) {
347-
delegateMatchers.add(new LinksServerWebExchangeMatcher());
347+
delegateMatchers.add(new LinksServerWebExchangeMatcher(this.httpMethod));
348348
}
349349
if (delegateMatchers.isEmpty()) {
350350
return EMPTY_MATCHER;
@@ -373,18 +373,21 @@ public String toString() {
373373
*/
374374
public static final class LinksServerWebExchangeMatcher extends AbstractWebExchangeMatcher<WebEndpointProperties> {
375375

376-
private LinksServerWebExchangeMatcher() {
376+
private final @Nullable HttpMethod httpMethod;
377+
378+
private LinksServerWebExchangeMatcher(@Nullable HttpMethod httpMethod) {
377379
super(WebEndpointProperties.class);
380+
this.httpMethod = httpMethod;
378381
}
379382

380383
@Override
381384
protected ServerWebExchangeMatcher createDelegate(WebEndpointProperties properties) {
382385
String linksPath = getLinksPath(properties.getBasePath());
383386
if (linksPath != null) {
384387
List<ServerWebExchangeMatcher> linksMatchers = new ArrayList<>();
385-
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath));
388+
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath, this.httpMethod));
386389
if (!linksPath.endsWith("/")) {
387-
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath + "/"));
390+
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath + "/", this.httpMethod));
388391
}
389392
return new OrServerWebExchangeMatcher(linksMatchers);
390393
}

module/spring-boot-security/src/main/java/org/springframework/boot/security/autoconfigure/actuate/web/servlet/EndpointRequest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ protected final List<RequestMatcher> getDelegateMatchers(RequestMatcherFactory r
224224
}
225225

226226
protected List<RequestMatcher> getLinksMatchers(RequestMatcherFactory requestMatcherFactory,
227-
RequestMatcherProvider matcherProvider, String linksPath) {
227+
RequestMatcherProvider matcherProvider, @Nullable HttpMethod httpMethod, String linksPath) {
228228
List<RequestMatcher> linksMatchers = new ArrayList<>();
229-
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, null, linksPath));
229+
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, httpMethod, linksPath));
230230
if (!linksPath.endsWith("/")) {
231-
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, null, linksPath, "/"));
231+
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, httpMethod, linksPath, "/"));
232232
}
233233
return linksMatchers;
234234
}
@@ -357,7 +357,8 @@ protected RequestMatcher createDelegate(WebApplicationContext context,
357357
String basePath = endpoints.getBasePath();
358358
String linksPath = getLinksPath(context, basePath);
359359
if (this.includeLinks && linksPath != null) {
360-
delegateMatchers.addAll(getLinksMatchers(requestMatcherFactory, matcherProvider, linksPath));
360+
delegateMatchers
361+
.addAll(getLinksMatchers(requestMatcherFactory, matcherProvider, this.httpMethod, linksPath));
361362
}
362363
if (delegateMatchers.isEmpty()) {
363364
return EMPTY_MATCHER;
@@ -393,7 +394,7 @@ protected RequestMatcher createDelegate(WebApplicationContext context,
393394
String linksPath = getLinksPath(context, properties.getBasePath());
394395
if (linksPath != null) {
395396
return new OrRequestMatcher(
396-
getLinksMatchers(requestMatcherFactory, getRequestMatcherProvider(context), linksPath));
397+
getLinksMatchers(requestMatcherFactory, getRequestMatcherProvider(context), null, linksPath));
397398
}
398399
return EMPTY_MATCHER;
399400
}

module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/actuate/web/reactive/EndpointRequestTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ void toAnyEndpointWithHttpMethodShouldRespectRequestMethod() {
7575
ServerWebExchangeMatcher matcher = EndpointRequest.toAnyEndpoint().withHttpMethod(HttpMethod.POST);
7676
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/foo");
7777
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/foo");
78+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator");
79+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator");
80+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/");
81+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/");
7882
}
7983

8084
@Test

module/spring-boot-security/src/test/java/org/springframework/boot/security/autoconfigure/actuate/web/servlet/EndpointRequestTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ void toAnyEndpointWithHttpMethodShouldRespectRequestMethod() {
7474
.withHttpMethod(HttpMethod.POST);
7575
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/foo");
7676
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/foo");
77+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator");
78+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator");
79+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/");
80+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/");
7781
}
7882

7983
@Test

0 commit comments

Comments
 (0)