Sanitize individual env entry that is matched exactly
Closes gh-9918 See gh-8282
This commit is contained in:
parent
362a8ea9bc
commit
5a8a86375d
|
|
@ -93,8 +93,7 @@ public class EnvironmentMvcEndpoint extends EndpointMvcAdapter
|
|||
|
||||
@Override
|
||||
protected Object getOptionalValue(Environment source, String name) {
|
||||
Object result = ((EnvironmentEndpoint) getDelegate()).getResolver()
|
||||
.getProperty(name, Object.class);
|
||||
Object result = getValue(name);
|
||||
if (result != null) {
|
||||
result = ((EnvironmentEndpoint) getDelegate()).sanitize(name, result);
|
||||
}
|
||||
|
|
@ -103,13 +102,18 @@ public class EnvironmentMvcEndpoint extends EndpointMvcAdapter
|
|||
|
||||
@Override
|
||||
protected Object getValue(Environment source, String name) {
|
||||
Object result = source.getProperty(name, Object.class);
|
||||
Object result = getValue(name);
|
||||
if (result == null) {
|
||||
throw new NoSuchPropertyException("No such property: " + name);
|
||||
}
|
||||
return ((EnvironmentEndpoint) getDelegate()).sanitize(name, result);
|
||||
}
|
||||
|
||||
private Object getValue(String name) {
|
||||
return ((EnvironmentEndpoint) getDelegate()).getResolver().getProperty(name,
|
||||
Object.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ public class EnvironmentMvcEndpointTests {
|
|||
map.put("my.foo", "${my.bar}");
|
||||
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
|
||||
.addFirst(new MapPropertySource("unresolved-placeholder", map));
|
||||
this.mvc.perform(get("/env/my.*")).andExpect(status().isOk())
|
||||
this.mvc.perform(get("/env/my.foo")).andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("\"my.foo\":\"${my.bar}\"")));
|
||||
}
|
||||
|
||||
|
|
@ -156,6 +156,29 @@ public class EnvironmentMvcEndpointTests {
|
|||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("my.foo", "${my.password}");
|
||||
map.put("my.password", "hello");
|
||||
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
|
||||
.addFirst(new MapPropertySource("placeholder", map));
|
||||
this.mvc.perform(get("/env/my.foo")).andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("\"my.foo\":\"******\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPathMatchedByRegexWhenPlaceholderCannotBeResolvedShouldReturnUnresolvedProperty()
|
||||
throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("my.foo", "${my.bar}");
|
||||
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
|
||||
.addFirst(new MapPropertySource("unresolved-placeholder", map));
|
||||
this.mvc.perform(get("/env/my.*")).andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("\"my.foo\":\"${my.bar}\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPathMatchedByRegexWithSensitivePlaceholderShouldSanitize()
|
||||
throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("my.foo", "${my.password}");
|
||||
map.put("my.password", "hello");
|
||||
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
|
||||
.addFirst(new MapPropertySource("placeholder", map));
|
||||
this.mvc.perform(get("/env/my.*")).andExpect(status().isOk())
|
||||
|
|
|
|||
Loading…
Reference in New Issue