JndiPropertySource defensively skips invalid JNDI lookup for property name with colon in resource-ref mode

Issue: SPR-14518
This commit is contained in:
Juergen Hoeller
2016-07-27 22:37:25 +02:00
parent 3ab21741f9
commit 328e04f167
2 changed files with 43 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -78,6 +78,15 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
*/ */
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
if (getSource().isResourceRef() && name.indexOf(':') != -1) {
// We're in resource-ref (prefixing with "java:comp/env") mode. Let's not bother
// with property names with a colon it since they're probably just containing a
// default value clause, very unlikely to match including the colon part even in
// a textual property source, and effectively never meant to match that way in
// JNDI where a colon indicates a separator between JNDI scheme and actual name.
return null;
}
try { try {
Object value = this.source.lookup(name); Object value = this.source.lookup(name);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import static org.junit.Assert.*;
* Unit tests for {@link JndiPropertySource}. * Unit tests for {@link JndiPropertySource}.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1 * @since 3.1
*/ */
public class JndiPropertySourceTests { public class JndiPropertySourceTests {
@@ -56,7 +57,7 @@ public class JndiPropertySourceTests {
jndiLocator.setJndiTemplate(jndiTemplate); jndiLocator.setJndiTemplate(jndiTemplate);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat((String)ps.getProperty("p1"), equalTo("v1")); assertThat(ps.getProperty("p1"), equalTo("v1"));
} }
@Test @Test
@@ -75,7 +76,36 @@ public class JndiPropertySourceTests {
jndiLocator.setJndiTemplate(jndiTemplate); jndiLocator.setJndiTemplate(jndiTemplate);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat((String)ps.getProperty("p1"), equalTo("v1")); assertThat(ps.getProperty("p1"), equalTo("v1"));
}
@Test
public void propertyWithDefaultClauseInResourceRefMode() {
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
@Override
public Object lookup(String jndiName) throws NamingException {
throw new IllegalStateException("Should not get called");
}
};
jndiLocator.setResourceRef(true);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat(ps.getProperty("propertyKey:defaultValue"), nullValue());
}
@Test
public void propertyWithColonInNonResourceRefMode() {
JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate() {
@Override
public Object lookup(String jndiName) throws NamingException {
assertEquals("my:key", jndiName);
return "my:value";
}
};
jndiLocator.setResourceRef(false);
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
assertThat(ps.getProperty("my:key"), equalTo("my:value"));
} }
} }