Skip to content

Commit 4e94e84

Browse files
committed
JndiPropertySource defensively skips invalid JNDI lookup for property name with colon in resource-ref mode
Issue: SPR-14518 (cherry picked from commit 7021a4b)
1 parent f8abba0 commit 4e94e84

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -78,6 +78,15 @@ public JndiPropertySource(String name, JndiLocatorDelegate jndiLocator) {
7878
*/
7979
@Override
8080
public Object getProperty(String name) {
81+
if (getSource().isResourceRef() && name.indexOf(':') != -1) {
82+
// We're in resource-ref (prefixing with "java:comp/env") mode. Let's not bother
83+
// with property names with a colon it since they're probably just containing a
84+
// default value clause, very unlikely to match including the colon part even in
85+
// a textual property source, and effectively never meant to match that way in
86+
// JNDI where a colon indicates a separator between JNDI scheme and actual name.
87+
return null;
88+
}
89+
8190
try {
8291
Object value = this.source.lookup(name);
8392
if (logger.isDebugEnabled()) {

spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java

+33-3
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-2016 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.
@@ -30,6 +30,7 @@
3030
* Unit tests for {@link JndiPropertySource}.
3131
*
3232
* @author Chris Beams
33+
* @author Juergen Hoeller
3334
* @since 3.1
3435
*/
3536
public class JndiPropertySourceTests {
@@ -56,7 +57,7 @@ protected Context createInitialContext() throws NamingException {
5657
jndiLocator.setJndiTemplate(jndiTemplate);
5758

5859
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
59-
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
60+
assertThat(ps.getProperty("p1"), equalTo("v1"));
6061
}
6162

6263
@Test
@@ -75,7 +76,36 @@ protected Context createInitialContext() throws NamingException {
7576
jndiLocator.setJndiTemplate(jndiTemplate);
7677

7778
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
78-
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
79+
assertThat(ps.getProperty("p1"), equalTo("v1"));
80+
}
81+
82+
@Test
83+
public void propertyWithDefaultClauseInResourceRefMode() {
84+
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
85+
@Override
86+
public Object lookup(String jndiName) throws NamingException {
87+
throw new IllegalStateException("Should not get called");
88+
}
89+
};
90+
jndiLocator.setResourceRef(true);
91+
92+
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
93+
assertThat(ps.getProperty("propertyKey:defaultValue"), nullValue());
94+
}
95+
96+
@Test
97+
public void propertyWithColonInNonResourceRefMode() {
98+
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
99+
@Override
100+
public Object lookup(String jndiName) throws NamingException {
101+
assertEquals("my:key", jndiName);
102+
return "my:value";
103+
}
104+
};
105+
jndiLocator.setResourceRef(false);
106+
107+
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
108+
assertThat(ps.getProperty("my:key"), equalTo("my:value"));
79109
}
80110

81111
}

0 commit comments

Comments
 (0)