From 328e04f16732cc83be882670fec42421b88aa8e4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 27 Jul 2016 22:37:25 +0200 Subject: [PATCH] JndiPropertySource defensively skips invalid JNDI lookup for property name with colon in resource-ref mode Issue: SPR-14518 --- .../jndi/JndiPropertySource.java | 11 +++++- .../jndi/JndiPropertySourceTests.java | 36 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java b/spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java index b33fc6d885..e2c40f34a8 100644 --- a/spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java +++ b/spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java @@ -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"); * you may not use this file except in compliance with the License. @@ -78,6 +78,15 @@ public class JndiPropertySource extends PropertySource { */ @Override 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 { Object value = this.source.lookup(name); if (logger.isDebugEnabled()) { diff --git a/spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java b/spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java index a8a68cd0eb..cc8423ddb2 100644 --- a/spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java +++ b/spring-context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java @@ -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"); * 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}. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 */ public class JndiPropertySourceTests { @@ -56,7 +57,7 @@ public class JndiPropertySourceTests { jndiLocator.setJndiTemplate(jndiTemplate); JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); - assertThat((String)ps.getProperty("p1"), equalTo("v1")); + assertThat(ps.getProperty("p1"), equalTo("v1")); } @Test @@ -75,7 +76,36 @@ public class JndiPropertySourceTests { jndiLocator.setJndiTemplate(jndiTemplate); 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")); } }