Skip to content

Commit 45ad183

Browse files
committed
Consider security in ClassUtils#getMostSpecificMethod
Recent changes in ExtendedBeanInfo involve invoking ClassUtils#getMostSpecificMethod when determining JavaBeans get/set pairs; if Java security settings control disallow reflective access, this results in an AccessControlException. This change defends against this (comparatively rare) scenario by catching the exception and falling back to returning the method originally supplied by the user. This change was a result of noticing CallbacksSecurityTests failing following the ExtendedBeanInfo modifications mentioned above Issue: SPR-8949
1 parent 21aed04 commit 45ad183

File tree

1 file changed

+10
-1
lines changed
  • org.springframework.core/src/main/java/org/springframework/util

1 file changed

+10
-1
lines changed

org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
2424
import java.lang.reflect.Proxy;
25+
import java.security.AccessControlException;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.Collections;
@@ -712,6 +713,9 @@ public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodN
712713
* Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
713714
* if bridge method resolution is desirable (e.g. for obtaining metadata from
714715
* the original method definition).
716+
* <p><b>NOTE:</b>Since Spring 3.1.1, if java security settings disallow reflective
717+
* access (e.g. calls to {@code Class#getDeclaredMethods} etc, this implementation
718+
* will fall back to returning the originally provided method.
715719
* @param method the method to be invoked, which may come from an interface
716720
* @param targetClass the target class for the current invocation.
717721
* May be <code>null</code> or may not even implement the method.
@@ -722,7 +726,12 @@ public static Method getMostSpecificMethod(Method method, Class<?> targetClass)
722726
Method specificMethod = null;
723727
if (method != null && isOverridable(method, targetClass) &&
724728
targetClass != null && !targetClass.equals(method.getDeclaringClass())) {
725-
specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(), method.getParameterTypes());
729+
try {
730+
specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(), method.getParameterTypes());
731+
} catch (AccessControlException ex) {
732+
// security settings are disallowing reflective access; leave
733+
// 'specificMethod' null and fall back to 'method' below
734+
}
726735
}
727736
return (specificMethod != null ? specificMethod : method);
728737
}

0 commit comments

Comments
 (0)