|
| 1 | +import csharp |
| 2 | +private import semmle.code.csharp.frameworks.System |
| 3 | +private import semmle.code.csharp.frameworks.system.Collections |
| 4 | +private import semmle.code.csharp.frameworks.Sql |
| 5 | + |
| 6 | +module NHibernate { |
| 7 | + /** A class that is mapped to the database. */ |
| 8 | + abstract class MappedClass extends Class { } |
| 9 | + |
| 10 | + /** The interface `NHibernamte.ISession`. */ |
| 11 | + class ISessionInterface extends Interface { |
| 12 | + ISessionInterface() { this.hasQualifiedName("NHibernate.ISession") } |
| 13 | + |
| 14 | + /** Gets a parameter that uses a mapped object. */ |
| 15 | + Parameter getAMappedObjectParameter() { |
| 16 | + exists(Callable c | |
| 17 | + result.getType() instanceof ObjectType and |
| 18 | + c = this.getAMethod() and |
| 19 | + result = c.getAParameter() and |
| 20 | + result.getName() = "obj" |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + /** Gets a type parameter that specifies a mapped class. */ |
| 25 | + TypeParameter getAMappedObjectTp() { |
| 26 | + exists(string methodName | |
| 27 | + methodName = "Load" |
| 28 | + or |
| 29 | + methodName = "Merge" |
| 30 | + or |
| 31 | + methodName = "Get" |
| 32 | + or |
| 33 | + methodName = "Query" |
| 34 | + | |
| 35 | + result = this.getAMethod(methodName).(UnboundGenericMethod).getTypeParameter(0) |
| 36 | + ) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** A mapped class that is mapped because it is used as a type argument. */ |
| 41 | + private class MappedByTypeArgument extends MappedClass { |
| 42 | + MappedByTypeArgument() { |
| 43 | + this = any(ISessionInterface si).getAMappedObjectTp().getASuppliedType() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** A mapped class that is mapped because it is passed as a parameter. */ |
| 48 | + private class MappedByParam extends MappedClass { |
| 49 | + MappedByParam() { |
| 50 | + exists(ISessionInterface si, Expr e, MethodCall c, Parameter p | |
| 51 | + p = si.getAMappedObjectParameter() and |
| 52 | + e = c.getArgumentForParameter(p) and |
| 53 | + this = e.getType() |
| 54 | + ) and |
| 55 | + not this instanceof ObjectType and |
| 56 | + not this.getABaseInterface*() instanceof SystemCollectionsIEnumerableInterface and |
| 57 | + not this instanceof SystemTypeClass |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** A property that is persisted in the database. */ |
| 62 | + class MappedProperty extends Property { |
| 63 | + MappedProperty() { |
| 64 | + this.getDeclaringType() instanceof MappedClass and |
| 65 | + this.isPublic() |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** A parameter that is interpreted as SQL. */ |
| 70 | + class SqlParameter extends Parameter { |
| 71 | + SqlParameter() { |
| 72 | + this.getType() instanceof StringType and |
| 73 | + (this.getName() = "sql" or this.getName() = "sqlString" or this.getName() = "query") and |
| 74 | + this |
| 75 | + .getCallable() |
| 76 | + .getDeclaringType() |
| 77 | + .getDeclaringNamespace() |
| 78 | + .getParent*() |
| 79 | + .hasQualifiedName("", "NHibernate") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** A call to a method in NHibernate that executes SQL. */ |
| 84 | + class NHibernateSqlSink extends SqlExpr, Call { |
| 85 | + SqlParameter sqlParam; |
| 86 | + |
| 87 | + NHibernateSqlSink() { this.getTarget().getAParameter() = sqlParam } |
| 88 | + |
| 89 | + override Expr getSql() { result = this.getArgumentForParameter(sqlParam) } |
| 90 | + } |
| 91 | + |
| 92 | + /** A taint source where the data has come from a mapped property stored in the database. */ |
| 93 | + class StoredFlowSource extends DataFlow::Node { |
| 94 | + StoredFlowSource() { |
| 95 | + this.asExpr() = any(PropertyRead read | read.getTarget() instanceof MappedProperty) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * A dataflow node whereby data flows from a property write to a property read |
| 101 | + * via some database. The assumption is that all writes can flow to all reads. |
| 102 | + */ |
| 103 | + class MappedPropertyJumpNode extends DataFlow::NonLocalJumpNode { |
| 104 | + MappedProperty property; |
| 105 | + |
| 106 | + MappedPropertyJumpNode() { this.asExpr() = property.getAnAssignedValue() } |
| 107 | + |
| 108 | + override DataFlow::Node getAJumpSuccessor(boolean preservesValue) { |
| 109 | + result.asExpr().(PropertyRead).getTarget() = property and |
| 110 | + preservesValue = false |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments