|
| 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()` (`GetEntity()` in |
| 13 | + * the older `System.Web.Http.OData`) or copied onto an existing entity via |
| 14 | + * `Patch`/`Put`/`CopyChangedValues`/`CopyUnchangedValues`. |
| 15 | + * |
| 16 | + * In both cases the type that ends up holding the client-controlled data has |
| 17 | + * no static relationship to the action method's parameter types, so its |
| 18 | + * members need to be taint-tracked explicitly. |
| 19 | + */ |
| 20 | + |
| 21 | +import csharp |
| 22 | +private import semmle.code.csharp.commons.Collections |
| 23 | +private import semmle.code.csharp.security.dataflow.flowsources.Remote |
| 24 | + |
| 25 | +/** The `ODataActionParameters` dictionary type, across OData library versions. */ |
| 26 | +class ODataActionParametersClass extends Class { |
| 27 | + ODataActionParametersClass() { |
| 28 | + this.hasFullyQualifiedName("Microsoft.AspNet.OData", "ODataActionParameters") or |
| 29 | + this.hasFullyQualifiedName("Microsoft.AspNetCore.OData.Formatter", "ODataActionParameters") or |
| 30 | + this.hasFullyQualifiedName("System.Web.Http.OData", "ODataActionParameters") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Holds if `e` is (or, via local flow -- e.g. an upcast to `IDictionary<string, object>` |
| 36 | + * -- may hold the value of) an `ODataActionParameters` dictionary. |
| 37 | + */ |
| 38 | +private predicate isODataActionParametersValue(Expr e) { |
| 39 | + exists(ParameterAccess e0 | e0.getType() instanceof ODataActionParametersClass | |
| 40 | + e0 = e or DataFlow::localExprFlow(e0, e) |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * An indexer read on an `ODataActionParameters` dictionary, e.g. `parameters["Foo"]` |
| 46 | + * (including through an upcast to a base dictionary type/interface). |
| 47 | + */ |
| 48 | +class ODataActionParameterRead extends ElementAccess { |
| 49 | + ODataActionParameterRead() { isODataActionParametersValue(this.getQualifier()) } |
| 50 | +} |
| 51 | + |
| 52 | +/** Holds if `e` may (locally) hold the value of an `ODataActionParameters` entry. */ |
| 53 | +private predicate isODataParameterValue(Expr e) { |
| 54 | + DataFlow::localExprFlow(any(ODataActionParameterRead r), e) |
| 55 | +} |
| 56 | + |
| 57 | +/** The generic ``Delta`1`` change-tracking class, across OData library versions. */ |
| 58 | +class DeltaClass extends UnboundGenericClass { |
| 59 | + DeltaClass() { |
| 60 | + this.getNumberOfTypeParameters() = 1 and |
| 61 | + ( |
| 62 | + this.hasFullyQualifiedName("Microsoft.AspNet.OData", "Delta`1") or |
| 63 | + this.hasFullyQualifiedName("Microsoft.AspNetCore.OData.Deltas", "Delta`1") or |
| 64 | + this.hasFullyQualifiedName("System.Web.Http.OData", "Delta`1") |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A type that a value read out of `ODataActionParameters` is cast, `as`-converted, |
| 71 | + * or type-tested to -- directly, or wrapped in a collection (`List<T>`, |
| 72 | + * `IEnumerable<T>`, arrays, ...) -- or a type that is tracked by a `Delta<T>`. |
| 73 | + */ |
| 74 | +class ODataBoundType extends ValueOrRefType { |
| 75 | + ODataBoundType() { |
| 76 | + exists(Cast c | isODataParameterValue(c.getExpr()) | |
| 77 | + this = c.getTargetType() or |
| 78 | + this = c.getTargetType().(CollectionType).getElementType() or |
| 79 | + this = c.getTargetType().(ParamsCollectionType).getElementType() |
| 80 | + ) |
| 81 | + or |
| 82 | + exists(IsExpr ie, Type t | |
| 83 | + isODataParameterValue(ie.getExpr()) and |
| 84 | + t = ie.getPattern().(TypePatternExpr).getCheckedType() |
| 85 | + | |
| 86 | + this = t or |
| 87 | + this = t.(CollectionType).getElementType() or |
| 88 | + this = t.(ParamsCollectionType).getElementType() |
| 89 | + ) |
| 90 | + or |
| 91 | + this = any(ConstructedClass c | c.getUnboundGeneric() instanceof DeltaClass).getTypeArgument(0) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Taint members (transitively) on types used in |
| 97 | + * 1. Casts, `as`-conversions, or type tests applied to `ODataActionParameters` values. |
| 98 | + * 2. The type argument of a `Delta<T>`. |
| 99 | + * |
| 100 | + * Note that this also impacts uses of such types in other contexts, the same |
| 101 | + * trade-off `AspNetRemoteFlowSourceMember` (`Remote.qll`) makes for ASP.NET |
| 102 | + * action-method parameters. |
| 103 | + */ |
| 104 | +private class ODataBoundMember extends TaintTracking::TaintedMember, CandidateMemberToTaint { |
| 105 | + ODataBoundMember() { |
| 106 | + exists(Type t, Type t0 | t = this.getDeclaringType() | |
| 107 | + (t = t0 or t = t0.(CollectionType).getElementType()) and |
| 108 | + ( |
| 109 | + t0 = any(ODataBoundMember m).getType() |
| 110 | + or |
| 111 | + t0 instanceof ODataBoundType |
| 112 | + ) |
| 113 | + ) |
| 114 | + } |
| 115 | +} |
0 commit comments