Skip to content

Commit 72d7c28

Browse files
author
Dave Syer
committed
Add Spring MVC-generated path suffixes to endpoint paths
Spring Security doesn't know that Spring MVC maps /foo, /foo.json and /foo/ all to the same handler. This change explicitly adds suffixes to the actuator endpoint matchers so they are properly protected.
1 parent 97adb5c commit 72d7c28

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementSecurityAutoConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ private static String[] getEndpointPaths(
221221
List<String> paths = new ArrayList<String>(endpoints.size());
222222
for (MvcEndpoint endpoint : endpoints) {
223223
if (endpoint.isSensitive() == secure) {
224-
paths.add(endpointHandlerMapping.getPrefix() + endpoint.getPath());
224+
String path = endpointHandlerMapping.getPrefix() + endpoint.getPath();
225+
paths.add(path);
226+
if (secure) {
227+
// Add Spring MVC-generated additional paths
228+
paths.add(path + "/");
229+
paths.add(path + ".*");
230+
}
225231
}
226232
}
227233
return paths.toArray(new String[paths.size()]);

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ public void testHomeIsSecure() throws Exception {
7070
.containsKey("Set-Cookie"));
7171
}
7272

73+
@Test
74+
public void testMetricsIsSecure() throws Exception {
75+
@SuppressWarnings("rawtypes")
76+
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
77+
"http://localhost:8080/metrics", Map.class);
78+
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
79+
entity = new TestRestTemplate().getForEntity(
80+
"http://localhost:8080/metrics/", Map.class);
81+
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
82+
entity = new TestRestTemplate().getForEntity(
83+
"http://localhost:8080/metrics/foo", Map.class);
84+
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
85+
entity = new TestRestTemplate().getForEntity(
86+
"http://localhost:8080/metrics.json", Map.class);
87+
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
88+
}
89+
7390
@Test
7491
public void testHome() throws Exception {
7592
@SuppressWarnings("rawtypes")

0 commit comments

Comments
 (0)