|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with the Jackson serialization framework. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +private import semmle.code.java.Reflection |
| 7 | +private import semmle.code.java.dataflow.DataFlow |
| 8 | + |
| 9 | +private class ObjectMapper extends RefType { |
| 10 | + ObjectMapper() { |
| 11 | + getASupertype*().hasQualifiedName("com.fasterxml.jackson.databind", "ObjectMapper") |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +/** A builder for building Jackson's `JsonMapper`. */ |
| 16 | +class MapperBuilder extends RefType { |
| 17 | + MapperBuilder() { |
| 18 | + hasQualifiedName("com.fasterxml.jackson.databind.cfg", "MapperBuilder<JsonMapper,Builder>") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +private class JsonFactory extends RefType { |
| 23 | + JsonFactory() { hasQualifiedName("com.fasterxml.jackson.core", "JsonFactory") } |
| 24 | +} |
| 25 | + |
| 26 | +private class JsonParser extends RefType { |
| 27 | + JsonParser() { hasQualifiedName("com.fasterxml.jackson.core", "JsonParser") } |
| 28 | +} |
| 29 | + |
| 30 | +/** A type descriptor in Jackson libraries. For example, `java.lang.Class`. */ |
| 31 | +class JacksonTypeDescriptorType extends RefType { |
| 32 | + JacksonTypeDescriptorType() { |
| 33 | + this instanceof TypeClass or |
| 34 | + hasQualifiedName("com.fasterxml.jackson.databind", "JavaType") or |
| 35 | + hasQualifiedName("com.fasterxml.jackson.core.type", "TypeReference") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** A method in `ObjectMapper` that deserialize data. */ |
| 40 | +class ObjectMapperReadMethod extends Method { |
| 41 | + ObjectMapperReadMethod() { |
| 42 | + this.getDeclaringType() instanceof ObjectMapper and |
| 43 | + this.hasName(["readValue", "readValues", "treeToValue"]) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** A call that enables the default typing in `ObjectMapper`. */ |
| 48 | +class EnableJacksonDefaultTyping extends MethodAccess { |
| 49 | + EnableJacksonDefaultTyping() { |
| 50 | + this.getMethod().getDeclaringType() instanceof ObjectMapper and |
| 51 | + this.getMethod().hasName("enableDefaultTyping") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** A qualifier of a call to one of the methods in `ObjectMapper` that deserialize data. */ |
| 56 | +class ObjectMapperReadQualifier extends DataFlow::ExprNode { |
| 57 | + ObjectMapperReadQualifier() { |
| 58 | + exists(MethodAccess ma | ma.getQualifier() = this.asExpr() | |
| 59 | + ma.getMethod() instanceof ObjectMapperReadMethod |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** A source that sets a type validator. */ |
| 65 | +class SetPolymorphicTypeValidatorSource extends DataFlow::ExprNode { |
| 66 | + SetPolymorphicTypeValidatorSource() { |
| 67 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 68 | + ( |
| 69 | + m.getDeclaringType() instanceof ObjectMapper and |
| 70 | + m.hasName("setPolymorphicTypeValidator") |
| 71 | + or |
| 72 | + m.getDeclaringType() instanceof MapperBuilder and |
| 73 | + m.hasName("polymorphicTypeValidator") |
| 74 | + ) and |
| 75 | + this.asExpr() = ma.getQualifier() |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** Holds if `fromNode` to `toNode` is a dataflow step that resolves a class. */ |
| 81 | +predicate resolveClassStep(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 82 | + exists(ReflectiveClassIdentifierMethodAccess ma | |
| 83 | + ma.getArgument(0) = fromNode.asExpr() and |
| 84 | + ma = toNode.asExpr() |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Holds if `fromNode` to `toNode` is a dataflow step that creates a Jackson parser. |
| 90 | + * |
| 91 | + * For example, a `createParser(userString)` call yields a `JsonParser`, which becomes dangerous |
| 92 | + * if passed to an unsafely-configured `ObjectMapper`'s `readValue` method. |
| 93 | + */ |
| 94 | +predicate createJacksonJsonParserStep(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 95 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 96 | + (m.getDeclaringType() instanceof ObjectMapper or m.getDeclaringType() instanceof JsonFactory) and |
| 97 | + m.hasName("createParser") and |
| 98 | + ma.getArgument(0) = fromNode.asExpr() and |
| 99 | + ma = toNode.asExpr() |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Holds if `fromNode` to `toNode` is a dataflow step that creates a Jackson `TreeNode`. |
| 105 | + * |
| 106 | + * These are parse trees of user-supplied JSON, which may lead to arbitrary code execution |
| 107 | + * if passed to an unsafely-configured `ObjectMapper`'s `treeToValue` method. |
| 108 | + */ |
| 109 | +predicate createJacksonTreeNodeStep(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 110 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 111 | + m.getDeclaringType() instanceof ObjectMapper and |
| 112 | + m.hasName("readTree") and |
| 113 | + ma.getArgument(0) = fromNode.asExpr() and |
| 114 | + ma = toNode.asExpr() |
| 115 | + ) |
| 116 | + or |
| 117 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 118 | + m.getDeclaringType() instanceof JsonParser and |
| 119 | + m.hasName("readValueAsTree") and |
| 120 | + ma.getQualifier() = fromNode.asExpr() and |
| 121 | + ma = toNode.asExpr() |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Holds if `type` or one of its supertypes has a field with `JsonTypeInfo` annotation |
| 127 | + * that enables polymorphic type handling. |
| 128 | + */ |
| 129 | +private predicate hasJsonTypeInfoAnnotation(RefType type) { |
| 130 | + hasFieldWithJsonTypeAnnotation(type.getASupertype*()) or |
| 131 | + hasJsonTypeInfoAnnotation(type.getAField().getType()) |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Holds if `type` has a field with `JsonTypeInfo` annotation |
| 136 | + * that enables polymorphic type handling. |
| 137 | + */ |
| 138 | +private predicate hasFieldWithJsonTypeAnnotation(RefType type) { |
| 139 | + exists(Annotation a | |
| 140 | + type.getAField().getAnAnnotation() = a and |
| 141 | + a.getType().hasQualifiedName("com.fasterxml.jackson.annotation", "JsonTypeInfo") and |
| 142 | + a.getValue("use").(VarAccess).getVariable().hasName(["CLASS", "MINIMAL_CLASS"]) |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Holds if `call` is a method call to a Jackson deserialization method such as `ObjectMapper.readValue(String, Class)`, |
| 148 | + * and the target deserialized class has a field with a `JsonTypeInfo` annotation that enables polymorphic typing. |
| 149 | + */ |
| 150 | +predicate hasArgumentWithUnsafeJacksonAnnotation(MethodAccess call) { |
| 151 | + call.getMethod() instanceof ObjectMapperReadMethod and |
| 152 | + exists(RefType argType, int i | i > 0 and argType = call.getArgument(i).getType() | |
| 153 | + hasJsonTypeInfoAnnotation(argType.(ParameterizedType).getATypeArgument()) |
| 154 | + ) |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Holds if `fromNode` to `toNode` is a dataflow step that looks like resolving a class. |
| 159 | + * A method probably resolves a class if it takes a string, returns a type descriptor, |
| 160 | + * and its name contains "resolve", "load", etc. |
| 161 | + * |
| 162 | + * Any method call that satisfies the rule above is assumed to propagate taint from its string arguments, |
| 163 | + * so methods that accept user-controlled data but sanitize it or use it for some |
| 164 | + * completely different purpose before returning a type descriptor could result in false positives. |
| 165 | + */ |
| 166 | +predicate looksLikeResolveClassStep(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 167 | + exists(MethodAccess ma, Method m, Expr arg | m = ma.getMethod() and arg = ma.getAnArgument() | |
| 168 | + m.getReturnType() instanceof JacksonTypeDescriptorType and |
| 169 | + m.getName().toLowerCase().regexpMatch("(.*)(resolve|load|class|type)(.*)") and |
| 170 | + arg.getType() instanceof TypeString and |
| 171 | + arg = fromNode.asExpr() and |
| 172 | + ma = toNode.asExpr() |
| 173 | + ) |
| 174 | +} |
0 commit comments