CVE-2021-21249 (published 2021-01-15T21:15Z)

OneDev is an all-in-one devops platform. In OneDev before version 4.0.3, there is an issue involving YAML parsing which can lead to post-auth remote code execution. In order to parse and process YAML files, OneDev uses SnakeYaml which by default (when not using `SafeConstructor`) allows the instantiation of arbitrary classes. We can leverage that to run arbitrary code by instantiating classes such as `javax.script.ScriptEngineManager` and using `URLClassLoader` to load the script engine provider, resulting in the instantiation of a user controlled class. For a full example refer to the referenced GHSA. This issue was addressed in 4.0.3 by only allowing certain known classes to be deserialized

Severity: MEDIUM
CVSS v2 base score: 6.5 (AV:N/AC:L/Au:S/C:P/I:P/A:P)
CVSS v3 base score: 8.8, HIGH (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-502
cwe_name: Deserialization of Untrusted Data
description: The product deserializes untrusted data without sufficiently verifying that the resulting data will be valid.
extended_description: 
            It is often convenient to serialize objects for communication or to save them for later use. However, deserialized data or code can often be modified without using the provided accessor functions if it does not use cryptography to protect itself. Furthermore, any cryptography would still be client-side security -- which is a dangerous security assumption.
            Data that is untrusted can not be trusted to be well-formed.
            When developers place no restrictions on "gadget chains," or series of instances and method invocations that can self-execute during the deserialization process (i.e., before the object is returned to the caller), it is sometimes possible for attackers to leverage them to perform unauthorized actions, like generating a shell.
         
         

Fix information:
repo_url: https://github.com/theonedev/onedev
commit_details: repo_url: https://github.com/theonedev/onedev
author: robin
author_date: 2020-11-21 20:16:58+08:00
msg: Fix security vulnerability issue caused by SnakeYaml deserialization
repository: repo_url: https://github.com/theonedev/onedev
repo_name: theonedev/onedev
description: Git Server with CI/CD, Kanban, and Packages. Seamless integration. Unparalleled experience.
repo_language: Java
owner: theonedev
file_changes: filename: VersionedYamlDoc.java
change_type: ModificationType.MODIFY
diff: @@ -38,8 +38,8 @@ import org.yaml.snakeyaml.serializer.Serializer;
 import edu.emory.mathcs.backport.java.util.Collections;
 import io.onedev.commons.launcher.loader.ImplementationRegistry;
 import io.onedev.commons.utils.ClassUtils;
-import io.onedev.server.OneDev;
 import io.onedev.server.GeneralException;
+import io.onedev.server.OneDev;
 import io.onedev.server.util.BeanUtils;
 import io.onedev.server.web.editable.annotation.Editable;
 
@@ -56,7 +56,6 @@ public class VersionedYamlDoc extends MappingNode {
 	@SuppressWarnings("unchecked")
 	public <T> T toBean(Class<T> beanClass) {
         setTag(new Tag(beanClass));
-        
 		if (getVersion() != null) {
 			try {
 				MigrationHelper.migrate(getVersion(), beanClass.newInstance(), this);
@@ -131,17 +130,26 @@ public class VersionedYamlDoc extends MappingNode {
 
 		@Override
 		protected Class<?> getClassForNode(Node node) {
-			Class<?> type = node.getType();
-			if (type.getAnnotation(Editable.class) != null && !ClassUtils.isConcrete(type)) {
-				ImplementationRegistry registry = OneDev.getInstance(ImplementationRegistry.class);
-				for (Class<?> implementationClass: registry.getImplementations(node.getType())) {
-					String implementationTag = new Tag("!" + implementationClass.getSimpleName()).getValue();
-					if (implementationTag.equals(node.getTag().getValue()))
-						return implementationClass;
+			if (node instanceof VersionedYamlDoc) {
+				return super.getClassForNode(node);
+			} else {
+				Class<?> type = node.getType();
+				if (type.getAnnotation(Editable.class) == null) {
+					// Do not deserialize unknown classes to avoid security vulnerabilities
+					throw new IllegalStateException(String.format("Unexpected yaml node (type: %s, tag: %s)", 
+							type, node.getTag()));
+				} else {
+					if (!ClassUtils.isConcrete(type)) {
+						ImplementationRegistry registry = OneDev.getInstance(ImplementationRegistry.class);
+						for (Class<?> implementationClass: registry.getImplementations(node.getType())) {
+							String implementationTag = new Tag("!" + implementationClass.getSimpleName()).getValue();
+							if (implementationTag.equals(node.getTag().getValue()))
+								return implementationClass;
+						}
+					}
+					return super.getClassForNode(node);
 				}
 			}
-			
-			return super.getClassForNode(node);
 		}
 		
 	}

programming_language: Java