Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Tainted members of ASP.NET and ASP.NET Core remote flow source types now also include private fields that data binders populate (fields and auto-implemented properties assigned from `value` in a public property setter, and fields returned by a public get-only collection property), get-only auto-implemented properties of collection type, and members inherited from base types. Public properties that wrap a backing field are no longer tainted directly; taint reaches them through the private field. Constants are no longer tainted.
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/Property.qll
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class Setter extends Accessor, @setter {
/**
* Gets an access to the special `value` parameter available in setters.
*/
private ParameterAccess accessToValue() {
ParameterAccess accessToValue() {
result.getTarget().hasName("value") and
result.getEnclosingCallable() instanceof Setter
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,114 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa
override string getSourceType() { result = "ASP.NET web service input" }
}

/** A public, non-static, auto-implemented property or field, candidate for taint-tracking. */
/** Gets `e`, or the expression it wraps in value-preserving casts / null-coalescing. */
private Expr getAValuePreservingSource(Expr e) {
result = e
or
result = getAValuePreservingSource(e.(Cast).getExpr())
or
result = getAValuePreservingSource(e.(NullCoalescingOperation).getLeftOperand())
}

/** Holds if `ma` accesses a member on the current instance. */
private predicate isOwnFieldAccess(MemberAccess ma) {
ma.getQualifier() instanceof ThisAccess or ma.getQualifier() instanceof BaseAccess
}

/**
* Holds if the public setter `s` assigns `value` to the own field, or
* auto-implemented property, `f`.
*/
private predicate setterAssignsValueToField(Setter s, Member f) {
(f instanceof Field or f.(Property).isAutoImplemented()) and
s.isPublic() and
exists(AssignableDefinition def |
def.getEnclosingCallable() = s and
def.getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and
isOwnFieldAccess(def.getTargetAccess()) and
accessToValue() = getAValuePreservingSource(def.getSource())
)
}

/** Holds if `getter` returns the own field `f`, directly (get-only property). */
private predicate getterReturnsField(Getter getter, Field f) {
exists(FieldRead fr |
fr.getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and
isOwnFieldAccess(fr) and
exists(Expr ret |
fr = getAValuePreservingSource(ret) and
(
ret = getter.getExpressionBody()
or
ret = any(ReturnStmt r | r.getEnclosingCallable() = getter).getExpr()
)
)
)
}

/** A private field or property that data binders populate. */
private predicate isPrivateBoundMember(Member m) {
m.isPrivate() and
not m.isStatic() and
(
// `[DataMember]` / `[JsonProperty]` members are already tainted by `JsonNET.qll`
exists(Property p |
m.getDeclaringType().getUnboundDeclaration() = p.getDeclaringType().getUnboundDeclaration() and
setterAssignsValueToField(p.getUnboundDeclaration().getSetter(), m)
)
or
// a public get-only collection property exposes this field; binders `Add` to it
exists(Property p |
p.isPublic() and
not exists(p.getSetter()) and
p.getType() instanceof CollectionType and
not p.getType() instanceof ArrayType and
getterReturnsField(p.getUnboundDeclaration().getGetter(), m)
)
)
}

/**
* A public, non-static auto-implemented property or field, or a private member
* that data binders populate (see `isPrivateBoundMember`), candidate for taint-tracking.
*/
class CandidateMemberToTaint extends Member {
CandidateMemberToTaint() {
this.isPublic() and
not this.isStatic() and
not this.(Field).isConst() and
(
this =
any(Property p |
p.isAutoImplemented() and
p.getGetter().isPublic() and
p.getSetter().isPublic()
)
this.isPublic() and
(
this =
any(Property p |
p.getGetter().isPublic() and
(
p.isAutoImplemented() and p.getSetter().isPublic()
or
p.getUnboundDeclaration().isAutoImplementedReadOnly() and
p.getType() instanceof CollectionType and
not p.getType() instanceof ArrayType
)
)
or
this = any(Field f | f.isPublic())
)
or
this = any(Field f | f.isPublic())
isPrivateBoundMember(this)
)
}
}

/**
* Holds if `m` is declared on `t0`, on a base type of `t0`, or on (a base type of)
* the element type of `t0` when `t0` is a collection.
*/
private predicate isMemberOfBoundType(Member m, Type t0) {
exists(Type t | t = t0 or t = t0.(CollectionType).getElementType() |
m.getDeclaringType() = t.(ValueOrRefType).getABaseType*()
)
}

/**
* Taint members (transitively) on types used in
* 1. Action method parameters.
Expand All @@ -146,8 +236,8 @@ private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember,
CandidateMemberToTaint
{
AspNetRemoteFlowSourceMember() {
exists(Type t, Type t0 | t = this.getDeclaringType() |
(t = t0 or t = t0.(CollectionType).getElementType()) and
exists(Type t0 |
isMemberOfBoundType(this, t0) and
(
t0 = any(AspNetRemoteFlowSourceMember m).getType()
or
Expand Down Expand Up @@ -267,8 +357,8 @@ private class AspNetCoreRemoteFlowSourceMember extends TaintTracking::TaintedMem
CandidateMemberToTaint
{
AspNetCoreRemoteFlowSourceMember() {
exists(Type t, Type t0 | t = this.getDeclaringType() |
(t = t0 or t = t0.(CollectionType).getElementType()) and
exists(Type t0 |
isMemberOfBoundType(this, t0) and
(
t0 = any(AspNetCoreRemoteFlowSourceMember m).getType()
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;

namespace Testing
{
Expand All @@ -16,8 +17,88 @@ public class ViewModel
private string RequestIdPrivate { get; set; } // Not considered tainted as it is private.
}

public class BaseModel
{
public string BaseProp { get; set; } // Considered tainted, as it is inherited by `DerivedModel`.
}

public class DerivedModel : BaseModel { }

public class PlainDataModel
{
private string backing;
private List<string> items = new List<string>();

public string Backed // Considered tainted. Accessors wrap a backing field.
{
get { if (backing == null) backing = ""; return backing; }
set { backing = value ?? throw new ArgumentNullException(); }
}

public List<string> ItemsOnlyGet => items; // Considered tainted. Get-only collection.
public List<string> ItemsAutoOnlyGet { get; } = new List<string>(); // Considered tainted. Get-only collection.
public string BackedOnlyGet => backing; // Not considered tainted as there is no setter.
public List<string> ItemsCopy => new List<string>(items); // Not considered tainted as the getter computes a value.
public string Computed // Not considered tainted as the getter computes a value.
{
get { return backing + "x"; }
set { backing = value; }
}
public const string Constant = "x"; // Not considered tainted as it is a constant.
public short Number { get; set; } // Considered tainted. `short.MaxValue` is not.
}

public class FieldBase
{
protected string shared;
}

public class InheritedField : FieldBase
{
public string Prop { get { return shared; } set { shared = value; } } // Considered tainted. Backing field is declared on a base type.
public string[] NamesOnlyGet { get; } = new[] { "fixed" }; // Not considered tainted. Binding cannot modify a get-only array.
}

public class FlowProps
{
private string f;
public string Discard { get { return ""; } set { } } // Not considered tainted. Ignores the value.
public string Coalesce { get { return f ?? ""; } set { f = value; } } // Considered tainted.
public string Cast { get { return f; } set { f = (string)(object)value; } } // Considered tainted.
public string Ignore { get { return f; } set { f = "x"; } } // Not considered tainted. Setter ignores the value.
private object o;
public string AsCast { get { return o as string; } set { o = value; } } // Considered tainted.
private List<string> lazy;
public List<string> LazyItems => lazy ??= new List<string>(); // Considered tainted. Lazily initialized get-only collection.
private string ow;
public string Overwrite { get { ow = "safe"; return ow; } set { ow = value; } } // Not considered tainted. Getter overwrites the field.
private string other;
public string OtherReceiver { get { return this.other; } set { new FlowProps().other = value; } } // Not considered tainted. Setter writes to another instance.
}

public class GenericModel<T>
{
private List<T> items = new List<T>();
public List<T> ItemsCopy => new List<T>(items); // Not considered tainted as the getter computes a value.
public List<T> ItemsPlain => items; // Considered tainted. Get-only collection.
}

public class AttributedModel
{
private string AutoBacking { get; set; }
public string ViaAutoProp { set { AutoBacking = value; } } // `AutoBacking` is considered tainted. Setter assigns a private auto-implemented property.
private string unmarked; // Not considered tainted. No attribute, and not assigned from a setter.
private string guarded;
public string Guarded { get { return guarded; } set { if (value == null) throw new ArgumentNullException(); guarded = value; } } // `guarded` is considered tainted. Guarded setter.
}

public class TestController : Controller
{
public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp, InheritedField inh, AttributedModel attributed)
{
throw null;
}

public object MyAction(ViewModel viewModel)
{
throw null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
remoteFlowSourceMembers
| AspRemoteFlowSource.cs:11:23:11:31 | RequestId |
| AspRemoteFlowSource.cs:12:23:12:36 | RequestIdField |
| AspRemoteFlowSource.cs:50:23:50:29 | Tainted |
| AspRemoteFlowSource.cs:12:23:12:31 | RequestId |
| AspRemoteFlowSource.cs:13:23:13:36 | RequestIdField |
| AspRemoteFlowSource.cs:22:23:22:30 | BaseProp |
| AspRemoteFlowSource.cs:29:24:29:30 | backing |
| AspRemoteFlowSource.cs:30:30:30:34 | items |
| AspRemoteFlowSource.cs:39:29:39:44 | ItemsAutoOnlyGet |
| AspRemoteFlowSource.cs:48:22:48:27 | Number |
| AspRemoteFlowSource.cs:64:24:64:24 | f |
| AspRemoteFlowSource.cs:69:24:69:24 | o |
| AspRemoteFlowSource.cs:71:30:71:33 | lazy |
| AspRemoteFlowSource.cs:73:24:73:25 | ow |
| AspRemoteFlowSource.cs:88:24:88:34 | AutoBacking |
| AspRemoteFlowSource.cs:91:24:91:30 | guarded |
| AspRemoteFlowSource.cs:131:23:131:29 | Tainted |
remoteFlowSources
| AspRemoteFlowSource.cs:21:42:21:50 | viewModel |
| AspRemoteFlowSource.cs:27:20:27:27 | ordinary |
| AspRemoteFlowSource.cs:28:32:28:40 | fromQuery |
| AspRemoteFlowSource.cs:29:31:29:38 | fromBody |
| AspRemoteFlowSource.cs:30:32:30:40 | fromRoute |
| AspRemoteFlowSource.cs:31:33:31:42 | fromHeader |
| AspRemoteFlowSource.cs:57:42:57:46 | param |
| AspRemoteFlowSource.cs:65:58:65:63 | newUrl |
| AspRemoteFlowSource.cs:66:61:66:65 | myApi |
| AspRemoteFlowSource.cs:66:75:66:79 | myUrl |
| AspRemoteFlowSource.cs:68:46:68:56 | lambdaParam |
| AspRemoteFlowSource.cs:74:65:74:76 | mapPostParam |
| AspRemoteFlowSource.cs:75:63:75:73 | mapPutParam |
| AspRemoteFlowSource.cs:76:69:76:82 | mapDeleteParam |
| AspRemoteFlowSource.cs:78:41:78:44 | item |
| AspRemoteFlowSource.cs:93:34:93:35 | id |
| AspRemoteFlowSource.cs:95:35:95:41 | command |
| AspRemoteFlowSource.cs:95:48:95:52 | count |
| AspRemoteFlowSource.cs:97:40:97:43 | data |
| AspRemoteFlowSource.cs:99:34:99:38 | value |
| AspRemoteFlowSource.cs:101:37:101:42 | itemId |
| AspRemoteFlowSource.cs:114:35:114:46 | derivedParam |
| AspRemoteFlowSource.cs:97:47:97:51 | plain |
| AspRemoteFlowSource.cs:97:67:97:73 | derived |
| AspRemoteFlowSource.cs:97:86:97:87 | fp |
| AspRemoteFlowSource.cs:97:105:97:107 | inh |
| AspRemoteFlowSource.cs:97:126:97:135 | attributed |
| AspRemoteFlowSource.cs:102:42:102:50 | viewModel |
| AspRemoteFlowSource.cs:108:20:108:27 | ordinary |
| AspRemoteFlowSource.cs:109:32:109:40 | fromQuery |
| AspRemoteFlowSource.cs:110:31:110:38 | fromBody |
| AspRemoteFlowSource.cs:111:32:111:40 | fromRoute |
| AspRemoteFlowSource.cs:112:33:112:42 | fromHeader |
| AspRemoteFlowSource.cs:138:42:138:46 | param |
| AspRemoteFlowSource.cs:146:58:146:63 | newUrl |
| AspRemoteFlowSource.cs:147:61:147:65 | myApi |
| AspRemoteFlowSource.cs:147:75:147:79 | myUrl |
| AspRemoteFlowSource.cs:149:46:149:56 | lambdaParam |
| AspRemoteFlowSource.cs:155:65:155:76 | mapPostParam |
| AspRemoteFlowSource.cs:156:63:156:73 | mapPutParam |
| AspRemoteFlowSource.cs:157:69:157:82 | mapDeleteParam |
| AspRemoteFlowSource.cs:159:41:159:44 | item |
| AspRemoteFlowSource.cs:174:34:174:35 | id |
| AspRemoteFlowSource.cs:176:35:176:41 | command |
| AspRemoteFlowSource.cs:176:48:176:52 | count |
| AspRemoteFlowSource.cs:178:40:178:43 | data |
| AspRemoteFlowSource.cs:180:34:180:38 | value |
| AspRemoteFlowSource.cs:182:37:182:42 | itemId |
| AspRemoteFlowSource.cs:195:35:195:46 | derivedParam |
genericCandidateMembers
| AspRemoteFlowSource.cs:81:25:81:29 | items |
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ query predicate remoteFlowSourceMembers(TaintTracking::TaintedMember m) { m.from
query predicate remoteFlowSources(AspNetCoreRemoteFlowSource s) {
s.getEnclosingCallable().fromSource()
}

// Accessor bodies of generic types are only extracted for the unbound declaration.
query predicate genericCandidateMembers(CandidateMemberToTaint m) {
m.getDeclaringType().getName().matches("GenericModel%")
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,50 @@ public class MyDataElement
}


public class BaseData
{
public string BaseProp { get; set; }
}

public class DerivedData : BaseData { }

public class PlainData
{
private string backing;
private List<string> items = new List<string>();

public string Backed
{
get { if (backing == null) backing = ""; return backing; }
set { backing = value ?? throw new ArgumentNullException(); }
}

public List<string> ItemsOnlyGet => items;
public string BackedOnlyGet => backing;
public List<string> ItemsCopy => new List<string>(items);
public string Computed
{
get { return backing + "x"; }
set { backing = value; }
}
public const string Constant = "x";
public short Number { get; set; }
}

public class FlowProps
{
private string f;
public string Discard { get { return ""; } set { } } // Not considered tainted. Ignores the value.
public string Coalesce { get { return f ?? ""; } set { f = value; } } // Considered tainted.
public string Cast { get { return f; } set { f = (string)(object)value; } } // Considered tainted.
public string Ignore { get { return f; } set { f = "x"; } } // Not considered tainted. Setter ignores the value.
}

public class MyService
{
[WebMethod]
public void MyMethod3(PlainData plain, DerivedData derived, FlowProps fp) { }

[WebMethod]
public void MyMethod(MyData data)
{
Expand Down
Loading