Skip to content

Commit 65cc57d

Browse files
committed
SystemEnvironmentPropertySource uses regular property names check instead of optimized Map lookup (for defensiveness in SecurityManager scenarios)
Issue: SPR-12224
1 parent 7f8d611 commit 65cc57d

File tree

3 files changed

+26
-46
lines changed

3 files changed

+26
-46
lines changed

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

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package org.springframework.core.env;
1818

19-
import org.apache.commons.logging.Log;
20-
import org.apache.commons.logging.LogFactory;
21-
22-
import org.springframework.util.Assert;
19+
import org.springframework.util.ObjectUtils;
2320

2421
/**
2522
* A {@link PropertySource} implementation capable of interrogating its
@@ -41,24 +38,16 @@
4138
* or not.
4239
*
4340
* @author Chris Beams
41+
* @author Juergen Hoeller
4442
* @since 3.1
4543
*/
4644
public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
4745

48-
protected final Log logger = LogFactory.getLog(getClass());
49-
50-
5146
public EnumerablePropertySource(String name, T source) {
5247
super(name, source);
5348
}
5449

5550

56-
/**
57-
* Return the names of all properties contained by the
58-
* {@linkplain #getSource() source} object (never {@code null}).
59-
*/
60-
public abstract String[] getPropertyNames();
61-
6251
/**
6352
* Return whether this {@code PropertySource} contains a property with the given name.
6453
* <p>This implementation checks for the presence of the given name within the
@@ -67,19 +56,13 @@ public EnumerablePropertySource(String name, T source) {
6756
*/
6857
@Override
6958
public boolean containsProperty(String name) {
70-
Assert.notNull(name, "Property name must not be null");
71-
for (String candidate : getPropertyNames()) {
72-
if (candidate.equals(name)) {
73-
if (logger.isDebugEnabled()) {
74-
logger.debug(String.format("PropertySource [%s] contains '%s'", getName(), name));
75-
}
76-
return true;
77-
}
78-
}
79-
if (logger.isTraceEnabled()) {
80-
logger.trace(String.format("PropertySource [%s] does not contain '%s'", getName(), name));
81-
}
82-
return false;
59+
return ObjectUtils.containsElement(getPropertyNames(), name);
8360
}
8461

62+
/**
63+
* Return the names of all properties contained by the
64+
* {@linkplain #getSource() source} object (never {@code null}).
65+
*/
66+
public abstract String[] getPropertyNames();
67+
8568
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import java.util.Map;
2020

21-
import org.springframework.util.Assert;
2221
import org.springframework.util.StringUtils;
2322

2423
/**
2524
* {@link PropertySource} that reads keys and values from a {@code Map} object.
2625
*
2726
* @author Chris Beams
27+
* @author Juergen Hoeller
2828
* @since 3.1
2929
* @see PropertiesPropertySource
3030
*/
@@ -34,25 +34,20 @@ public MapPropertySource(String name, Map<String, Object> source) {
3434
super(name, source);
3535
}
3636

37+
3738
@Override
3839
public Object getProperty(String name) {
3940
return this.source.get(name);
4041
}
4142

4243
@Override
43-
public String[] getPropertyNames() {
44-
return StringUtils.toStringArray(this.source.keySet());
44+
public boolean containsProperty(String name) {
45+
return this.source.containsKey(name);
4546
}
4647

4748
@Override
48-
public boolean containsProperty(String name) {
49-
Assert.notNull(name, "Property name must not be null");
50-
boolean containsProperty = this.source.containsKey(name);
51-
if (logger.isDebugEnabled()) {
52-
logger.debug(String.format("PropertySource [%s] %s '%s'", getName(),
53-
(containsProperty ? "contains" : "does not contain"), name));
54-
}
55-
return containsProperty;
49+
public String[] getPropertyNames() {
50+
return StringUtils.toStringArray(this.source.keySet());
5651
}
5752

5853
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.springframework.util.Assert;
22+
import org.springframework.util.ObjectUtils;
2223

2324
/**
2425
* Specialization of {@link MapPropertySource} designed for use with
@@ -70,8 +71,9 @@ public SystemEnvironmentPropertySource(String name, Map<String, Object> source)
7071
super(name, source);
7172
}
7273

74+
7375
/**
74-
* Return true if a property with the given name or any underscore/uppercase variant
76+
* Return {@link true} if a property with the given name or any underscore/uppercase variant
7577
* thereof exists in this property source.
7678
*/
7779
@Override
@@ -80,13 +82,11 @@ public boolean containsProperty(String name) {
8082
}
8183

8284
/**
83-
* {@inheritDoc}
84-
* <p>This implementation returns {@code true} if a property with the given name or
85+
* This implementation returns {@code true} if a property with the given name or
8586
* any underscore/uppercase variant thereof exists in this property source.
8687
*/
8788
@Override
8889
public Object getProperty(String name) {
89-
Assert.notNull(name, "property name must not be null");
9090
String actualName = resolvePropertyName(name);
9191
if (logger.isDebugEnabled() && !name.equals(actualName)) {
9292
logger.debug(String.format("PropertySource [%s] does not contain '%s', but found equivalent '%s'",
@@ -101,28 +101,30 @@ public Object getProperty(String name) {
101101
* found or otherwise the original name. Never returns {@code null}.
102102
*/
103103
private String resolvePropertyName(String name) {
104-
if (super.containsProperty(name)) {
104+
Assert.notNull(name, "Property name must not be null");
105+
if (ObjectUtils.containsElement(getPropertyNames(), name)) {
105106
return name;
106107
}
107108

108109
String usName = name.replace('.', '_');
109-
if (!name.equals(usName) && super.containsProperty(usName)) {
110+
if (!name.equals(usName) && ObjectUtils.containsElement(getPropertyNames(), usName)) {
110111
return usName;
111112
}
112113

113114
String ucName = name.toUpperCase();
114115
if (!name.equals(ucName)) {
115-
if (super.containsProperty(ucName)) {
116+
if (ObjectUtils.containsElement(getPropertyNames(), ucName)) {
116117
return ucName;
117118
}
118119
else {
119120
String usUcName = ucName.replace('.', '_');
120-
if (!ucName.equals(usUcName) && super.containsProperty(usUcName)) {
121+
if (!ucName.equals(usUcName) && ObjectUtils.containsElement(getPropertyNames(), usUcName)) {
121122
return usUcName;
122123
}
123124
}
124125
}
125126

126127
return name;
127128
}
129+
128130
}

0 commit comments

Comments
 (0)