Vulnerability: Remote Code Execution in Spring Framework Spring Framework prior to versions 5.2.20 and 5.3.18 contains a remote code execution vulnerability known as `Spring4Shell`. 

## Impact

A Spring MVC or Spring WebFlux application running on JDK 9+ may be vulnerable to remote code execution (RCE) via data binding. The specific exploit requires the application to run on Tomcat as a WAR deployment. If the application is deployed as a Spring Boot executable jar, i.e. the default, it is not vulnerable to the exploit. However, the nature of the vulnerability is more general, and there may be other ways to exploit it.

These are the prerequisites for the exploit:
- JDK 9 or higher
- Apache Tomcat as the Servlet container
- Packaged as WAR
- `spring-webmvc` or `spring-webflux` dependency

## Patches

- Spring Framework [5.3.18](https://github.com/spring-projects/spring-framework/releases/tag/v5.3.18) and [5.2.20](https://github.com/spring-projects/spring-framework/releases/tag/v5.2.20.RELEASE)
- Spring Boot [2.6.6](https://github.com/spring-projects/spring-boot/releases/tag/v2.6.6) and [2.5.12](https://github.com/spring-projects/spring-boot/releases/tag/v2.5.12)

## Workarounds

For those who are unable to upgrade, leaked reports recommend setting `disallowedFields` on `WebDataBinder` through an `@ControllerAdvice`. This works generally, but as a centrally applied workaround fix, may leave some loopholes, in particular if a controller sets `disallowedFields` locally through its own `@InitBinder` method, which overrides the global setting.

To apply the workaround in a more fail-safe way, applications could extend `RequestMappingHandlerAdapter` to update the `WebDataBinder` at the end after all other initialization. In order to do that, a Spring Boot application can declare a `WebMvcRegistrations` bean (Spring MVC) or a `WebFluxRegistrations` bean (Spring WebFlux).
Vulnerability ID: GHSA-36p3-wjmg-h94x
CWE: CWE-94
Severity score: 9.8
Project: https://github.com/spring-projects/spring-framework
Published: 2022-03-31
File: spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java
Commit message: Refine PropertyDescriptor filtering

Restrict property paths under `Class` and properties of types
`ClassLoader` or `ProtectionDomain`.
Commit author: Brian Clozel

Vulnerable version (before fix):
 /*  * Copyright 2002-2020 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. import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; private CachedIntrospectionResults(Class<?> beanClass) throws BeansException { 			// This call is slow so we do it once. 			PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors(); 			for (PropertyDescriptor pd : pds) { 				if (Class.class == beanClass && 						("classLoader".equals(pd.getName()) ||  "protectionDomain".equals(pd.getName()))) { 					// Ignore Class.getClassLoader() and getProtectionDomain() methods - nobody needs to bind to those 					continue; 				} 				if (logger.isTraceEnabled()) { private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St 						// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method 						// against a declared read method, so we prefer read method descriptors here. 						pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); 						this.propertyDescriptors.put(pd.getName(), pd); 						Method readMethod = pd.getReadMethod(); 						if (readMethod != null) {

Fixed version (after fix):
 /*  * Copyright 2002-2022 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. import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.security.ProtectionDomain; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; private CachedIntrospectionResults(Class<?> beanClass) throws BeansException { 			// This call is slow so we do it once. 			PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors(); 			for (PropertyDescriptor pd : pds) { 				if (Class.class == beanClass && (!"name".equals(pd.getName()) && !pd.getName().endsWith("Name"))) { 					// Only allow all name variants of Class properties 					continue; 				} 				if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType()) 						|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) { 					// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those 					continue; 				} 				if (logger.isTraceEnabled()) { private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St 						// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method 						// against a declared read method, so we prefer read method descriptors here. 						pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); 						if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType()) 								|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) { 							// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those 							continue; 						} 						this.propertyDescriptors.put(pd.getName(), pd); 						Method readMethod = pd.getReadMethod(); 						if (readMethod != null) {

Patch:
patch: @@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2022 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.
@@ -22,6 +22,7 @@
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.security.ProtectionDomain;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -286,9 +287,13 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
 			// This call is slow so we do it once.
 			PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors();
 			for (PropertyDescriptor pd : pds) {
-				if (Class.class == beanClass &&
-						("classLoader".equals(pd.getName()) ||  "protectionDomain".equals(pd.getName()))) {
-					// Ignore Class.getClassLoader() and getProtectionDomain() methods - nobody needs to bind to those
+				if (Class.class == beanClass && (!"name".equals(pd.getName()) && !pd.getName().endsWith("Name"))) {
+					// Only allow all name variants of Class properties
+					continue;
+				}
+				if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
+						|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
+					// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
 					continue;
 				}
 				if (logger.isTraceEnabled()) {
@@ -337,6 +342,11 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St
 						// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
 						// against a declared read method, so we prefer read method descriptors here.
 						pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
+						if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
+								|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
+							// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
+							continue;
+						}
 						this.propertyDescriptors.put(pd.getName(), pd);
 						Method readMethod = pd.getReadMethod();
 						if (readMethod != null) {
path: spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java