Skip to content

Commit 587a816

Browse files
committed
SystemEnvironmentPropertySource uses actual SecurityManager check and direct keySet access
Issue: SPR-12224
1 parent 0934751 commit 587a816

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020

2121
import org.springframework.util.Assert;
22-
import org.springframework.util.ObjectUtils;
2322

2423
/**
2524
* Specialization of {@link MapPropertySource} designed for use with
@@ -56,16 +55,14 @@
5655
* and all its subclasses.
5756
*
5857
* @author Chris Beams
58+
* @author Juergen Hoeller
5959
* @since 3.1
6060
* @see StandardEnvironment
6161
* @see AbstractEnvironment#getSystemEnvironment()
6262
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
6363
*/
6464
public class SystemEnvironmentPropertySource extends MapPropertySource {
6565

66-
/** if SecurityManager scenarios mean that property access should be via getPropertyNames() */
67-
private boolean usePropertyNames;
68-
6966
/**
7067
* Create a new {@code SystemEnvironmentPropertySource} with the given name and
7168
* delegating to the given {@code MapPropertySource}.
@@ -105,48 +102,37 @@ public Object getProperty(String name) {
105102
*/
106103
private String resolvePropertyName(String name) {
107104
Assert.notNull(name, "Property name must not be null");
108-
try {
109-
String[] propertyNames = (this.usePropertyNames ? getPropertyNames() : null);
110-
if (containsProperty(propertyNames, name)) {
111-
return name;
112-
}
113-
114-
String usName = name.replace('.', '_');
115-
if (!name.equals(usName) && containsProperty(propertyNames, usName)) {
116-
return usName;
117-
}
118-
119-
String ucName = name.toUpperCase();
120-
if (!name.equals(ucName)) {
121-
if (containsProperty(propertyNames, ucName)) {
122-
return ucName;
123-
}
124-
else {
125-
String usUcName = ucName.replace('.', '_');
126-
if (!ucName.equals(usUcName) && containsProperty(propertyNames, usUcName)) {
127-
return usUcName;
128-
}
129-
}
130-
}
131-
105+
if (containsKey(name)) {
132106
return name;
133107
}
134-
catch (RuntimeException ex) {
135-
if (this.usePropertyNames) {
136-
throw ex;
108+
109+
String usName = name.replace('.', '_');
110+
if (!name.equals(usName) && containsKey(usName)) {
111+
return usName;
112+
}
113+
114+
String ucName = name.toUpperCase();
115+
if (!name.equals(ucName)) {
116+
if (containsKey(ucName)) {
117+
return ucName;
137118
}
138119
else {
139-
this.usePropertyNames = true;
140-
return resolvePropertyName(name);
120+
String usUcName = ucName.replace('.', '_');
121+
if (!ucName.equals(usUcName) && containsKey(usUcName)) {
122+
return usUcName;
123+
}
141124
}
142125
}
126+
127+
return name;
143128
}
144129

145-
private boolean containsProperty(String[] propertyNames, String name) {
146-
if (propertyNames == null) {
147-
return super.containsProperty(name);
148-
}
149-
return ObjectUtils.containsElement(propertyNames, name);
130+
private boolean containsKey(String name) {
131+
return (isSecurityManagerPresent() ? this.source.keySet().contains(name) : this.source.containsKey(name));
132+
}
133+
134+
protected boolean isSecurityManagerPresent() {
135+
return (System.getSecurityManager() != null);
150136
}
151137

152138
}

spring-core/src/test/java/org/springframework/core/env/SystemEnvironmentPropertySourceTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.springframework.core.env;
1818

1919
import java.util.HashMap;
20+
import java.util.HashSet;
2021
import java.util.Map;
22+
import java.util.Set;
2123

2224
import org.junit.Before;
2325
import org.junit.Test;
@@ -29,19 +31,23 @@
2931
* Unit tests for {@link SystemEnvironmentPropertySource}.
3032
*
3133
* @author Chris Beams
34+
* @author Juergen Hoeller
3235
* @since 3.1
3336
*/
3437
public class SystemEnvironmentPropertySourceTests {
3538

3639
private Map<String, Object> envMap;
40+
3741
private PropertySource<?> ps;
3842

43+
3944
@Before
4045
public void setUp() {
4146
envMap = new HashMap<String, Object>();
4247
ps = new SystemEnvironmentPropertySource("sysEnv", envMap);
4348
}
4449

50+
4551
@Test
4652
public void none() {
4753
assertThat(ps.containsProperty("a.key"), equalTo(false));
@@ -107,9 +113,20 @@ public void withSecurityConstraints() throws Exception {
107113
public boolean containsKey(Object key) {
108114
throw new UnsupportedOperationException();
109115
}
116+
@Override
117+
public Set<String> keySet() {
118+
return new HashSet<String>(super.keySet());
119+
}
110120
};
111-
ps = new SystemEnvironmentPropertySource("sysEnv", envMap);
112121
envMap.put("A_KEY", "a_value");
122+
123+
ps = new SystemEnvironmentPropertySource("sysEnv", envMap) {
124+
@Override
125+
protected boolean isSecurityManagerPresent() {
126+
return true;
127+
}
128+
};
129+
113130
assertThat(ps.containsProperty("A_KEY"), equalTo(true));
114131
assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value"));
115132
}

0 commit comments

Comments
 (0)