Skip to content

Commit 39c00c4

Browse files
committed
Avoid UnsupportedOperationEx. with active SecurityManager
Issue: SPR-9970
1 parent 078a1c5 commit 39c00c4

File tree

4 files changed

+137
-15
lines changed

4 files changed

+137
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.support;
18+
19+
import static java.lang.String.format;
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.junit.Assert.assertThat;
22+
23+
import java.security.AccessControlException;
24+
import java.security.Permission;
25+
import java.util.Map;
26+
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
32+
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
33+
import org.springframework.context.annotation.Profile;
34+
import org.springframework.core.env.AbstractEnvironment;
35+
import org.springframework.core.env.StandardEnvironmentTests;
36+
import org.springframework.stereotype.Component;
37+
38+
39+
/**
40+
* Tests integration between Environment and SecurityManagers. See SPR-9970.
41+
*
42+
* @author Chris Beams
43+
*/
44+
public class EnvironmentSecurityManagerIntegrationTests {
45+
46+
private SecurityManager originalSecurityManager;
47+
private Map<String, String> env;
48+
49+
@Before
50+
public void setUp() {
51+
originalSecurityManager = System.getSecurityManager();
52+
env = StandardEnvironmentTests.getModifiableSystemEnvironment();
53+
env.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "p1");
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
env.remove(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
59+
System.setSecurityManager(originalSecurityManager);
60+
}
61+
62+
@Test
63+
public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
64+
SecurityManager securityManager = new SecurityManager() {
65+
@Override
66+
public void checkPermission(Permission perm) {
67+
// disallowing access to System#getenv means that our
68+
// ReadOnlySystemAttributesMap will come into play.
69+
if ("getenv.*".equals(perm.getName())) {
70+
throw new AccessControlException(
71+
"Accessing the system environment is disallowed");
72+
}
73+
}
74+
};
75+
System.setSecurityManager(securityManager);
76+
77+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
78+
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf);
79+
reader.register(C1.class);
80+
assertThat(bf.containsBean("c1"), is(true));
81+
}
82+
83+
@Test
84+
public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessToIndividualKey() {
85+
SecurityManager securityManager = new SecurityManager() {
86+
@Override
87+
public void checkPermission(Permission perm) {
88+
// disallowing access to System#getenv means that our
89+
// ReadOnlySystemAttributesMap will come into play.
90+
if ("getenv.*".equals(perm.getName())) {
91+
throw new AccessControlException(
92+
"Accessing the system environment is disallowed");
93+
}
94+
// disallowing access to the spring.profiles.active property means that
95+
// the BeanDefinitionReader won't be able to determine which profiles are
96+
// active. We should see an INFO-level message in the console about this
97+
// and as a result, any components marked with a non-default profile will
98+
// be ignored.
99+
if (("getenv."+AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
100+
throw new AccessControlException(
101+
format("Accessing system environment variable [%s] is disallowed",
102+
AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
103+
}
104+
}
105+
};
106+
System.setSecurityManager(securityManager);
107+
108+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
109+
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf);
110+
reader.register(C1.class);
111+
assertThat(bf.containsBean("c1"), is(false));
112+
}
113+
114+
@Component("c1")
115+
@Profile("p1")
116+
static class C1 {
117+
}
118+
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,21 @@
1717
package org.springframework.core.env;
1818

1919
import java.util.Collection;
20+
import java.util.Collections;
2021
import java.util.Map;
2122
import java.util.Set;
2223

2324
import org.springframework.util.Assert;
2425

2526
/**
26-
* Read-only {@code Map<String, String>} implementation that is backed by system properties or environment
27-
* variables.
27+
* Read-only {@code Map<String, String>} implementation that is backed by system
28+
* properties or environment variables.
2829
*
29-
* <p>Used by {@link AbstractApplicationContext} when a {@link SecurityManager} prohibits access to {@link
30-
* System#getProperties()} or {@link System#getenv()}.
30+
* <p>Used by {@link AbstractApplicationContext} when a {@link SecurityManager} prohibits
31+
* access to {@link System#getProperties()} or {@link System#getenv()}. It is for this
32+
* reason that the implementations of {@link #keySet()}, {@link #entrySet()}, and
33+
* {@link #values()} always return empty even though {@link #get(Object)} may in fact
34+
* return non-null if the current security manager allows access to individual keys.
3135
*
3236
* @author Arjen Poutsma
3337
* @author Chris Beams
@@ -85,19 +89,19 @@ public void clear() {
8589
}
8690

8791
public Set<String> keySet() {
88-
throw new UnsupportedOperationException();
92+
return Collections.emptySet();
8993
}
9094

9195
public void putAll(Map<? extends String, ? extends String> m) {
9296
throw new UnsupportedOperationException();
9397
}
9498

9599
public Collection<String> values() {
96-
throw new UnsupportedOperationException();
100+
return Collections.emptySet();
97101
}
98102

99103
public Set<Entry<String, String>> entrySet() {
100-
throw new UnsupportedOperationException();
104+
return Collections.emptySet();
101105
}
102106

103107
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ public SystemEnvironmentPropertySource(String name, Map<String, Object> source)
7676
*/
7777
@Override
7878
public boolean containsProperty(String name) {
79-
return resolvePropertyName(name) != null;
79+
return getProperty(name) != null;
8080
}
8181

8282
/**
@@ -102,8 +102,8 @@ public Object getProperty(String name) {
102102

103103
/**
104104
* Check to see if this property source contains a property with the given name, or
105-
* any underscore / uppercase variation thereof. Return the resolved name or
106-
* {@code null} if none found.
105+
* any underscore / uppercase variation thereof. Return the resolved name if one is
106+
* found or otherwise the original name. Never returns {@code null}.
107107
*/
108108
private String resolvePropertyName(String name) {
109109
if (super.containsProperty(name)) {
@@ -127,6 +127,6 @@ private String resolvePropertyName(String name) {
127127
}
128128
}
129129

130-
return null;
130+
return name;
131131
}
132132
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -456,7 +456,7 @@ public void checkPermission(Permission perm) {
456456
}
457457

458458
@SuppressWarnings("unchecked")
459-
private static Map<String, String> getModifiableSystemEnvironment() {
459+
public static Map<String, String> getModifiableSystemEnvironment() {
460460
// for os x / linux
461461
Class<?>[] classes = Collections.class.getDeclaredClasses();
462462
Map<String, String> env = System.getenv();

0 commit comments

Comments
 (0)