|
| 1 | +/** |
| 2 | + * Provides taint modeling for `Microsoft.AspNet.OData`/`Microsoft.AspNetCore.OData` |
| 3 | + * (and the older `System.Web.Http.OData`) OData action parameter binding. |
| 4 | + * |
| 5 | + * OData actions receive their untrusted payload in one of two shapes that |
| 6 | + * bypass the usual "type used as an action-method parameter" taint modeling: |
| 7 | + * |
| 8 | + * - `ODataActionParameters`, an untyped `Dictionary<string, object>` whose |
| 9 | + * values are cast, `as`-converted, or type-tested to arbitrary model types |
| 10 | + * by the action method body. |
| 11 | + * - `Delta<T>`, a change-tracking wrapper for PATCH/PUT requests, whose |
| 12 | + * tracked property values are exposed via `GetInstance()` or copied onto an |
| 13 | + * existing entity via `Patch`/`Put`/`CopyChangedValues`/`CopyUnchangedValues`. |
| 14 | + * |
| 15 | + * In both cases the type that ends up holding the client-controlled data has |
| 16 | + * no static relationship to the action method's parameter types, so its |
| 17 | + * members need to be taint-tracked explicitly. |
| 18 | + */ |
| 19 | + |
| 20 | +private import csharp |
| 21 | +private import semmle.code.csharp.commons.Collections |
| 22 | +private import semmle.code.csharp.dataflow.FlowSteps |
| 23 | +private import semmle.code.csharp.dataflow.TaintTracking |
| 24 | +private import semmle.code.csharp.dataflow.internal.DataFlowPrivate |
| 25 | + |
| 26 | +/** The `ODataActionParameters` dictionary type, across OData library versions. */ |
| 27 | +private class ODataActionParametersClass extends Class { |
| 28 | + ODataActionParametersClass() { |
| 29 | + this.hasFullyQualifiedName("Microsoft.AspNet.OData", "ODataActionParameters") or |
| 30 | + this.hasFullyQualifiedName("Microsoft.AspNetCore.OData.Formatter", "ODataActionParameters") or |
| 31 | + this.hasFullyQualifiedName("System.Web.Http.OData", "ODataActionParameters") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** An indexer read on an `ODataActionParameters` dictionary, e.g. `parameters["CabFile"]`. */ |
| 36 | +private class ODataActionParameterRead extends ElementAccess { |
| 37 | + ODataActionParameterRead() { this.getQualifier().getType() instanceof ODataActionParametersClass } |
| 38 | +} |
| 39 | + |
| 40 | +/** Holds if `e` may (locally) hold the value of an `ODataActionParameters` entry. */ |
| 41 | +private predicate isODataParameterValue(Expr e) { |
| 42 | + TaintTracking::localExprTaint(any(ODataActionParameterRead r), e) |
| 43 | +} |
| 44 | + |
| 45 | +/** The generic `Delta<TStructuralType>` change-tracking class, across OData library versions. */ |
| 46 | +private class DeltaClass extends UnboundGenericClass { |
| 47 | + DeltaClass() { |
| 48 | + this.getNumberOfTypeParameters() = 1 and |
| 49 | + ( |
| 50 | + this.hasFullyQualifiedName("Microsoft.AspNet.OData", "Delta`1") or |
| 51 | + this.hasFullyQualifiedName("Microsoft.AspNetCore.OData.Deltas", "Delta`1") |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * A type that a value read out of `ODataActionParameters` is cast, `as`-converted, |
| 58 | + * or type-tested to -- directly, or wrapped in a collection (`List<T>`, |
| 59 | + * `IEnumerable<T>`, arrays, ...) -- or a type that is tracked by a `Delta<T>`. |
| 60 | + */ |
| 61 | +private class ODataBoundType extends ValueOrRefType { |
| 62 | + ODataBoundType() { |
| 63 | + exists(Cast c | isODataParameterValue(c.getExpr()) | |
| 64 | + this = c.getTargetType() or |
| 65 | + this = c.getTargetType().(CollectionType).getElementType() or |
| 66 | + this = c.getTargetType().(ParamsCollectionType).getElementType() |
| 67 | + ) |
| 68 | + or |
| 69 | + exists(IsExpr ie, Type t | |
| 70 | + isODataParameterValue(ie.getExpr()) and |
| 71 | + t = ie.getPattern().(TypePatternExpr).getCheckedType() |
| 72 | + | |
| 73 | + this = t or |
| 74 | + this = t.(CollectionType).getElementType() or |
| 75 | + this = t.(ParamsCollectionType).getElementType() |
| 76 | + ) |
| 77 | + or |
| 78 | + this = any(ConstructedClass c | c.getUnboundGeneric() instanceof DeltaClass).getTypeArgument(0) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +private class CandidateODataMember extends Member { |
| 83 | + CandidateODataMember() { |
| 84 | + this.isPublic() and |
| 85 | + not this.isStatic() and |
| 86 | + ( |
| 87 | + this = |
| 88 | + any(Property p | |
| 89 | + p.isAutoImplemented() and |
| 90 | + p.getGetter().isPublic() and |
| 91 | + p.getSetter().isPublic() |
| 92 | + ) |
| 93 | + or |
| 94 | + this = any(Field f | f.isPublic()) |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Taint members (transitively) on types used in |
| 101 | + * 1. Casts, `as`-conversions, or type tests applied to `ODataActionParameters` values. |
| 102 | + * 2. The type argument of a `Delta<T>`. |
| 103 | + * |
| 104 | + * Note that this also impacts uses of such types in other contexts, the same |
| 105 | + * trade-off `AspNetRemoteFlowSourceMember` (`Remote.qll`) makes for ASP.NET |
| 106 | + * action-method parameters. |
| 107 | + */ |
| 108 | +private class ODataBoundMember extends TaintTracking::TaintedMember, CandidateODataMember { |
| 109 | + ODataBoundMember() { |
| 110 | + exists(Type t, Type t0 | t = this.getDeclaringType() | |
| 111 | + (t = t0 or t = t0.(CollectionType).getElementType()) and |
| 112 | + ( |
| 113 | + t0 = any(ODataBoundMember m).getType() |
| 114 | + or |
| 115 | + t0 instanceof ODataBoundType |
| 116 | + ) |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** The `Patch`, `Put`, `CopyChangedValues`, and `CopyUnchangedValues` methods on `Delta<T>`. */ |
| 122 | +private class DeltaMutatingMethod extends Method { |
| 123 | + DeltaMutatingMethod() { |
| 124 | + this.getDeclaringType() instanceof DeltaClass and |
| 125 | + this.hasName(["Patch", "Put", "CopyChangedValues", "CopyUnchangedValues"]) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * A call to `Delta<T>.Patch`/`Put`/`CopyChangedValues`/`CopyUnchangedValues` |
| 131 | + * copies the changes tracked by the `Delta<T>` receiver onto its `original` |
| 132 | + * entity argument. |
| 133 | + */ |
| 134 | +private class DeltaMutatingCallTaintStep extends AdditionalTaintStep { |
| 135 | + override predicate step(DataFlow::Node node1, DataFlow::Node node2) { |
| 136 | + exists(MethodCall mc | |
| 137 | + mc.getTarget().getUnboundDeclaration() instanceof DeltaMutatingMethod and |
| 138 | + node1.asExpr() = mc.getQualifier() and |
| 139 | + node2.(PostUpdateNode).getPreUpdateNode().asExpr() = mc.getArgument(0) |
| 140 | + ) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** The `GetInstance` method on `Delta<T>`. */ |
| 145 | +private class DeltaGetInstanceMethod extends Method { |
| 146 | + DeltaGetInstanceMethod() { |
| 147 | + this.getDeclaringType() instanceof DeltaClass and |
| 148 | + this.hasName("GetInstance") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** `Delta<T>.GetInstance()` returns the tracked entity, carrying the same taint as the `Delta<T>` itself. */ |
| 153 | +private class DeltaGetInstanceTaintStep extends AdditionalTaintStep { |
| 154 | + override predicate step(DataFlow::Node node1, DataFlow::Node node2) { |
| 155 | + exists(MethodCall mc | |
| 156 | + mc.getTarget().getUnboundDeclaration() instanceof DeltaGetInstanceMethod and |
| 157 | + node1.asExpr() = mc.getQualifier() and |
| 158 | + node2.asExpr() = mc |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
0 commit comments