Skip to content

Commit 24f6214

Browse files
dlwldnjs1009wilkinsona
authored andcommitted
Restrict EndpointRequest links to the specified HTTP method
EndpointRequest.toAnyEndpoint().withHttpMethod(...) restricted endpoint paths but still allowed any HTTP method for the links path, yet the mappings for the links path are only registered for GET requests. Restrict the links path using the configured HttpMethod in both servlet and reactive matchers. Signed-off-by: Lee JiWon <dlwldnjs1009@gmail.com> See gh-50095
1 parent 1b114ae commit 24f6214

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java

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

125125
/**
@@ -335,7 +335,7 @@ protected ServerWebExchangeMatcher createDelegate(PathMappedEndpoints endpoints)
335335
List<ServerWebExchangeMatcher> delegateMatchers = getDelegateMatchers(paths, this.httpMethod);
336336
String linksPath = getLinksPath(endpoints.getBasePath());
337337
if (this.includeLinks && linksPath != null) {
338-
delegateMatchers.add(new LinksServerWebExchangeMatcher());
338+
delegateMatchers.add(new LinksServerWebExchangeMatcher(this.httpMethod));
339339
}
340340
if (delegateMatchers.isEmpty()) {
341341
return EMPTY_MATCHER;
@@ -364,18 +364,21 @@ public String toString() {
364364
*/
365365
public static final class LinksServerWebExchangeMatcher extends AbstractWebExchangeMatcher<WebEndpointProperties> {
366366

367-
private LinksServerWebExchangeMatcher() {
367+
private final HttpMethod httpMethod;
368+
369+
private LinksServerWebExchangeMatcher(HttpMethod httpMethod) {
368370
super(WebEndpointProperties.class);
371+
this.httpMethod = httpMethod;
369372
}
370373

371374
@Override
372375
protected ServerWebExchangeMatcher createDelegate(WebEndpointProperties properties) {
373376
String linksPath = getLinksPath(properties.getBasePath());
374377
if (linksPath != null) {
375378
List<ServerWebExchangeMatcher> linksMatchers = new ArrayList<>();
376-
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath));
379+
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath, this.httpMethod));
377380
if (!linksPath.endsWith("/")) {
378-
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath + "/"));
381+
linksMatchers.add(new PathPatternParserServerWebExchangeMatcher(linksPath + "/", this.httpMethod));
379382
}
380383
return new OrServerWebExchangeMatcher(linksMatchers);
381384
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java

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

227227
protected List<RequestMatcher> getLinksMatchers(RequestMatcherFactory requestMatcherFactory,
228-
RequestMatcherProvider matcherProvider, String linksPath) {
228+
RequestMatcherProvider matcherProvider, HttpMethod httpMethod, String linksPath) {
229229
List<RequestMatcher> linksMatchers = new ArrayList<>();
230-
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, null, linksPath));
230+
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, httpMethod, linksPath));
231231
if (!linksPath.endsWith("/")) {
232-
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, null, linksPath, "/"));
232+
linksMatchers.add(requestMatcherFactory.antPath(matcherProvider, httpMethod, linksPath, "/"));
233233
}
234234
return linksMatchers;
235235
}
@@ -375,7 +375,8 @@ protected RequestMatcher createDelegate(WebApplicationContext context,
375375
String basePath = endpoints.getBasePath();
376376
String linksPath = getLinksPath(context, basePath);
377377
if (this.includeLinks && linksPath != null) {
378-
delegateMatchers.addAll(getLinksMatchers(requestMatcherFactory, matcherProvider, linksPath));
378+
delegateMatchers
379+
.addAll(getLinksMatchers(requestMatcherFactory, matcherProvider, this.httpMethod, linksPath));
379380
}
380381
if (delegateMatchers.isEmpty()) {
381382
return EMPTY_MATCHER;
@@ -411,7 +412,7 @@ protected RequestMatcher createDelegate(WebApplicationContext context,
411412
String linksPath = getLinksPath(context, properties.getBasePath());
412413
if (linksPath != null) {
413414
return new OrRequestMatcher(
414-
getLinksMatchers(requestMatcherFactory, getRequestMatcherProvider(context), linksPath));
415+
getLinksMatchers(requestMatcherFactory, getRequestMatcherProvider(context), null, linksPath));
415416
}
416417
return EMPTY_MATCHER;
417418
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequestTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ void toAnyEndpointWithHttpMethodShouldRespectRequestMethod() {
7373
ServerWebExchangeMatcher matcher = EndpointRequest.toAnyEndpoint().withHttpMethod(HttpMethod.POST);
7474
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/foo");
7575
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/foo");
76+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator");
77+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator");
78+
assertMatcher(matcher, "/actuator").matches(HttpMethod.POST, "/actuator/");
79+
assertMatcher(matcher, "/actuator").doesNotMatch(HttpMethod.GET, "/actuator/");
7680
}
7781

7882
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java

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

7882
@Test

0 commit comments

Comments
 (0)