|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with the Json-io framework. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +import semmle.code.java.Maps |
| 7 | +import semmle.code.java.dataflow.DataFlow |
| 8 | +import semmle.code.java.dataflow.DataFlow2 |
| 9 | + |
| 10 | +/** |
| 11 | + * The class `com.cedarsoftware.util.io.JsonReader`. |
| 12 | + */ |
| 13 | +class JsonIoJsonReader extends RefType { |
| 14 | + JsonIoJsonReader() { this.hasQualifiedName("com.cedarsoftware.util.io", "JsonReader") } |
| 15 | +} |
| 16 | + |
| 17 | +/** A method with the name `jsonToJava` declared in `com.cedarsoftware.util.io.JsonReader`. */ |
| 18 | +class JsonIoJsonToJavaMethod extends Method { |
| 19 | + JsonIoJsonToJavaMethod() { |
| 20 | + this.getDeclaringType() instanceof JsonIoJsonReader and |
| 21 | + this.getName() = "jsonToJava" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** A method with the name `readObject` declared in `com.cedarsoftware.util.io.JsonReader`. */ |
| 26 | +class JsonIoReadObjectMethod extends Method { |
| 27 | + JsonIoReadObjectMethod() { |
| 28 | + this.getDeclaringType() instanceof JsonIoJsonReader and |
| 29 | + this.getName() = "readObject" |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * A call to `Map.put` method, set the value of the `USE_MAPS` key to `true`. |
| 35 | + */ |
| 36 | +class JsonIoUseMapsSetter extends MethodAccess { |
| 37 | + JsonIoUseMapsSetter() { |
| 38 | + this.getMethod().getDeclaringType().getASourceSupertype*() instanceof MapType and |
| 39 | + this.getMethod().hasName("put") and |
| 40 | + this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "USE_MAPS" and |
| 41 | + this.getArgument(1).(CompileTimeConstantExpr).getBooleanValue() = true |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** A data flow configuration tracing flow from JsonIo safe settings. */ |
| 46 | +class SafeJsonIoConfig extends DataFlow2::Configuration { |
| 47 | + SafeJsonIoConfig() { this = "UnsafeDeserialization::SafeJsonIoConfig" } |
| 48 | + |
| 49 | + override predicate isSource(DataFlow::Node src) { |
| 50 | + exists(MethodAccess ma | |
| 51 | + ma instanceof JsonIoUseMapsSetter and |
| 52 | + src.asExpr() = ma.getQualifier() |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + override predicate isSink(DataFlow::Node sink) { |
| 57 | + exists(MethodAccess ma | |
| 58 | + ma.getMethod() instanceof JsonIoJsonToJavaMethod and |
| 59 | + sink.asExpr() = ma.getArgument(1) |
| 60 | + ) |
| 61 | + or |
| 62 | + exists(ClassInstanceExpr cie | |
| 63 | + cie.getConstructor().getDeclaringType() instanceof JsonIoJsonReader and |
| 64 | + sink.asExpr() = cie.getArgument(1) |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
0 commit comments