|
| 1 | +/** |
| 2 | + * Classes for modelling Json.NET. |
| 3 | + */ |
| 4 | + |
| 5 | +import csharp |
| 6 | +import semmle.code.csharp.dataflow.LibraryTypeDataFlow |
| 7 | + |
| 8 | +/** Definitions relating to the `Json.NET` package. */ |
| 9 | +module JsonNET { |
| 10 | + /** The namespace `Newtonsoft.Json`. */ |
| 11 | + class JsonNETNamespace extends Namespace { |
| 12 | + JsonNETNamespace() { this.hasQualifiedName("Newtonsoft.Json") } |
| 13 | + } |
| 14 | + |
| 15 | + /** A class in `Newtonsoft.Json`. */ |
| 16 | + class JsonClass extends Class { JsonClass() { this.getParent() instanceof JsonNETNamespace } } |
| 17 | + |
| 18 | + /** The class `Newtonsoft.Json.JsonConvert`. */ |
| 19 | + class JsonConvertClass extends JsonClass, LibraryTypeDataFlow { |
| 20 | + JsonConvertClass() { this.hasName("JsonConvert") } |
| 21 | + |
| 22 | + /** Gets a `ToString` method. */ |
| 23 | + private Method getAToStringMethod() { |
| 24 | + result = this.getAMethod("ToString") and |
| 25 | + result.isStatic() |
| 26 | + } |
| 27 | + |
| 28 | + /** Gets a `Deserialize` method. */ |
| 29 | + Method getADeserializeMethod() { |
| 30 | + result = this.getAMethod() and |
| 31 | + result.getName().matches("Deserialize%") |
| 32 | + } |
| 33 | + |
| 34 | + /** Gets a `Serialize` method. */ |
| 35 | + Method getASerializeMethod() { |
| 36 | + result = this.getAMethod() and |
| 37 | + result.getName().matches("Serialize%") |
| 38 | + } |
| 39 | + |
| 40 | + private Method getAPopulateMethod() { |
| 41 | + result = this.getAMethod() and |
| 42 | + result.getName().matches("Populate%") |
| 43 | + } |
| 44 | + |
| 45 | + override predicate callableFlow( |
| 46 | + CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable c, |
| 47 | + boolean preservesValue |
| 48 | + ) { |
| 49 | + // ToString methods |
| 50 | + c = getAToStringMethod() and |
| 51 | + preservesValue = true and |
| 52 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 53 | + sink instanceof CallableFlowSinkReturn |
| 54 | + or |
| 55 | + // Deserialize methods |
| 56 | + c = getADeserializeMethod() and |
| 57 | + preservesValue = false and |
| 58 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 59 | + sink instanceof CallableFlowSinkReturn |
| 60 | + or |
| 61 | + // Serialize methods |
| 62 | + c = getASerializeMethod() and |
| 63 | + preservesValue = false and |
| 64 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 65 | + sink instanceof CallableFlowSinkReturn |
| 66 | + or |
| 67 | + // Populate methods |
| 68 | + c = getAPopulateMethod() and |
| 69 | + preservesValue = false and |
| 70 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 71 | + sink = any(CallableFlowSinkArg arg | arg.getArgumentIndex() = 1) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** A type that is serialized. */ |
| 76 | + private class SerializedType extends ValueOrRefType { |
| 77 | + SerializedType() { |
| 78 | + // Supplied as a parameter to a serialize or deserialize method |
| 79 | + exists(TypeParameter tp, JsonConvertClass jc, UnboundGenericMethod serializeMethod | |
| 80 | + this = tp.getAnUltimatelySuppliedType() and |
| 81 | + tp = serializeMethod.getATypeParameter() |
| 82 | + | |
| 83 | + serializeMethod = jc.getASerializeMethod() |
| 84 | + or |
| 85 | + serializeMethod = jc.getADeserializeMethod() |
| 86 | + ) |
| 87 | + or |
| 88 | + exists(Class attribute | attribute = this.getAnAttribute().getType() | |
| 89 | + attribute instanceof JsonObjectAttributeClass |
| 90 | + or |
| 91 | + attribute.hasName("JsonConverterAttribute") |
| 92 | + ) |
| 93 | + or |
| 94 | + this.getAConstructor().getAnAttribute().getType().hasName("JsonConstructorAttribute") |
| 95 | + } |
| 96 | + |
| 97 | + predicate isOptIn() { this.getAnAttribute().(JsonObjectAttribute).isOptIn() } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A field/property that can be serialized, either explicitly |
| 102 | + * or as a member of a serialized type. |
| 103 | + */ |
| 104 | + private class SerializedMember extends TaintTracking::TaintedMember { |
| 105 | + SerializedMember() { |
| 106 | + // This member has a Json attribute |
| 107 | + exists(Class attribute | attribute = this.(Attributable).getAnAttribute().getType() | |
| 108 | + attribute.hasName("JsonPropertyAttribute") |
| 109 | + or |
| 110 | + attribute.hasName("JsonDictionaryAttribute") |
| 111 | + or |
| 112 | + attribute.hasName("JsonRequiredAttribute") |
| 113 | + or |
| 114 | + attribute.hasName("JsonArrayAttribute") |
| 115 | + or |
| 116 | + attribute.hasName("JsonConverterAttribute") |
| 117 | + or |
| 118 | + attribute.hasName("JsonExtensionDataAttribute") |
| 119 | + or |
| 120 | + attribute.hasName("SerializableAttribute") // System.SerializableAttribute |
| 121 | + or |
| 122 | + attribute.hasName("DataMemberAttribute") // System.DataMemberAttribute |
| 123 | + ) |
| 124 | + or |
| 125 | + // This field is a member of an explicitly serialized type |
| 126 | + this.getDeclaringType() instanceof SerializedType and |
| 127 | + not this.getDeclaringType().(SerializedType).isOptIn() and |
| 128 | + not this.(Attributable).getAnAttribute().getType() instanceof NotSerializedAttributeClass |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** The class `NewtonSoft.Json.JsonSerializer`. */ |
| 133 | + class JsonSerializerClass extends JsonClass, LibraryTypeDataFlow { |
| 134 | + JsonSerializerClass() { this.hasName("JsonSerializer") } |
| 135 | + |
| 136 | + Method getSerializeMethod() { result = this.getAMethod("Serialize") } |
| 137 | + |
| 138 | + Method getDeserializeMethod() { result = this.getAMethod("Deserialize") } |
| 139 | + |
| 140 | + override predicate callableFlow( |
| 141 | + CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable c, |
| 142 | + boolean preservesValue |
| 143 | + ) { |
| 144 | + // Serialize |
| 145 | + c = this.getSerializeMethod() and |
| 146 | + preservesValue = false and |
| 147 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 148 | + sink = any(CallableFlowSinkArg arg | arg.getArgumentIndex() = 1) |
| 149 | + or |
| 150 | + // Deserialize |
| 151 | + c = this.getDeserializeMethod() and |
| 152 | + preservesValue = false and |
| 153 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 154 | + sink = any(CallableFlowSinkArg arg | arg.getArgumentIndex() = 1) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** Any attribute class that marks a member to not be serialized. */ |
| 159 | + private class NotSerializedAttributeClass extends JsonClass { |
| 160 | + NotSerializedAttributeClass() { |
| 161 | + this.hasName("JsonIgnoreAttribute") or this.hasName("NonSerializedAttribute") |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** The class `Newtonsoft.Json.ObjectAttribute`. */ |
| 166 | + class JsonObjectAttributeClass extends JsonClass { |
| 167 | + JsonObjectAttributeClass() { this.hasName("JsonObjectAttribute") } |
| 168 | + } |
| 169 | + |
| 170 | + /** An attribute of type `Newtonsoft.Json.ObjectAttribute`. */ |
| 171 | + class JsonObjectAttribute extends Attribute { |
| 172 | + JsonObjectAttribute() { this.getType() instanceof JsonObjectAttributeClass } |
| 173 | + |
| 174 | + /** Holds if the `OptIn` argument has been supplied to this attribute. */ |
| 175 | + predicate isOptIn() { this.getArgument(_).(FieldAccess).getTarget().hasName("OptIn") } |
| 176 | + } |
| 177 | + |
| 178 | + /** The namespace `Newtonsoft.Json.Linq`. */ |
| 179 | + class LinqNamespace extends Namespace { |
| 180 | + LinqNamespace() { |
| 181 | + this.getParentNamespace() instanceof JsonNETNamespace and this.hasName("Linq") |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** A class in `Newtonsoft.Json.Linq`. */ |
| 186 | + class LinqClass extends Class { |
| 187 | + LinqClass() { this.getDeclaringNamespace() instanceof LinqNamespace } |
| 188 | + } |
| 189 | + |
| 190 | + /** The `NewtonSoft.Json.Linq.JObject` class. */ |
| 191 | + class JObjectClass extends LinqClass, LibraryTypeDataFlow { |
| 192 | + JObjectClass() { this.hasName("JObject") } |
| 193 | + |
| 194 | + override predicate callableFlow( |
| 195 | + CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable c, |
| 196 | + boolean preservesValue |
| 197 | + ) { |
| 198 | + // ToString method |
| 199 | + c = this.getAMethod("ToString") and |
| 200 | + source instanceof CallableFlowSourceQualifier and |
| 201 | + sink instanceof CallableFlowSinkReturn and |
| 202 | + preservesValue = false |
| 203 | + or |
| 204 | + // Parse method |
| 205 | + c = this.getParseMethod() and |
| 206 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 207 | + sink instanceof CallableFlowSinkReturn and |
| 208 | + preservesValue = false |
| 209 | + or |
| 210 | + // operator string |
| 211 | + c = any(Operator op | |
| 212 | + op.getDeclaringType() = this.getABaseType*() and op.getReturnType() instanceof StringType |
| 213 | + ) and |
| 214 | + source = any(CallableFlowSourceArg arg | arg.getArgumentIndex() = 0) and |
| 215 | + sink instanceof CallableFlowSinkReturn and |
| 216 | + preservesValue = false |
| 217 | + or |
| 218 | + // SelectToken method |
| 219 | + c = this.getSelectTokenMethod() and |
| 220 | + source instanceof CallableFlowSourceQualifier and |
| 221 | + sink instanceof CallableFlowSinkReturn and |
| 222 | + preservesValue = false |
| 223 | + } |
| 224 | + |
| 225 | + /** Gets the `Parse` method. */ |
| 226 | + Method getParseMethod() { result = this.getAMethod("Parse") } |
| 227 | + |
| 228 | + /** Gets the `SelectToken` method. */ |
| 229 | + Method getSelectTokenMethod() { result = this.getABaseType*().getAMethod("SelectToken") } |
| 230 | + } |
| 231 | +} |
0 commit comments