diff --git a/csharp/ql/lib/change-notes/2026-11-06-taint-plain-data-members.md b/csharp/ql/lib/change-notes/2026-11-06-taint-plain-data-members.md new file mode 100644 index 000000000000..86335f33430e --- /dev/null +++ b/csharp/ql/lib/change-notes/2026-11-06-taint-plain-data-members.md @@ -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. diff --git a/csharp/ql/lib/semmle/code/csharp/Property.qll b/csharp/ql/lib/semmle/code/csharp/Property.qll index 7c048528aaae..70cf57d93ee5 100644 --- a/csharp/ql/lib/semmle/code/csharp/Property.qll +++ b/csharp/ql/lib/semmle/code/csharp/Property.qll @@ -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 } diff --git a/csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/Remote.qll b/csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/Remote.qll index 2a04d9c24c3d..f0d9c90587a5 100644 --- a/csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/Remote.qll +++ b/csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/Remote.qll @@ -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. @@ -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 @@ -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 diff --git a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs index d7b085ef61fd..97e48900c29d 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using System; +using System.Collections.Generic; namespace Testing { @@ -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 items = new List(); + + 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 ItemsOnlyGet => items; // Considered tainted. Get-only collection. + public List ItemsAutoOnlyGet { get; } = new List(); // Considered tainted. Get-only collection. + public string BackedOnlyGet => backing; // Not considered tainted as there is no setter. + public List ItemsCopy => new List(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 lazy; + public List LazyItems => lazy ??= new List(); // 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 + { + private List items = new List(); + public List ItemsCopy => new List(items); // Not considered tainted as the getter computes a value. + public List 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; diff --git a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected index 6ba2741857b5..3d078b7c98b1 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected @@ -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 | diff --git a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.ql b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.ql index 17bceb3f9330..d08600ce15e5 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.ql +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.ql @@ -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%") +} diff --git a/csharp/ql/test/library-tests/dataflow/flowsources/remote/RemoteFlowSource.cs b/csharp/ql/test/library-tests/dataflow/flowsources/remote/RemoteFlowSource.cs index d54a359aca0c..85b92d44bb67 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/remote/RemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/remote/RemoteFlowSource.cs @@ -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 items = new List(); + + public string Backed + { + get { if (backing == null) backing = ""; return backing; } + set { backing = value ?? throw new ArgumentNullException(); } + } + + public List ItemsOnlyGet => items; + public string BackedOnlyGet => backing; + public List ItemsCopy => new List(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) { diff --git a/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected b/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected index 242080e6bda2..a6301932aaa0 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected @@ -9,6 +9,11 @@ remoteFlowSourceMembers | RemoteFlowSource.cs:83:35:83:42 | Elements | | RemoteFlowSource.cs:84:38:84:41 | List | | RemoteFlowSource.cs:89:23:89:26 | Prop | +| RemoteFlowSource.cs:95:23:95:30 | BaseProp | +| RemoteFlowSource.cs:102:24:102:30 | backing | +| RemoteFlowSource.cs:103:30:103:34 | items | +| RemoteFlowSource.cs:120:22:120:27 | Number | +| RemoteFlowSource.cs:125:24:125:24 | f | remoteFlowSources | Controller.cs:11:43:11:52 | sampleData | ASP.NET MVC action method parameter | | Controller.cs:11:62:11:66 | taint | ASP.NET MVC action method parameter | @@ -22,5 +27,8 @@ remoteFlowSources | RemoteFlowSource.cs:45:17:45:23 | access to parameter request | ASP.NET query string | | RemoteFlowSource.cs:45:17:45:42 | access to property RawUrl | ASP.NET unvalidated request data | | RemoteFlowSource.cs:52:55:52:61 | [post] access to local variable segment | external | -| RemoteFlowSource.cs:96:37:96:40 | data | ASP.NET web service input | -| RemoteFlowSource.cs:105:47:105:50 | data | ASP.NET web service input | +| RemoteFlowSource.cs:135:41:135:45 | plain | ASP.NET web service input | +| RemoteFlowSource.cs:135:60:135:66 | derived | ASP.NET web service input | +| RemoteFlowSource.cs:135:79:135:80 | fp | ASP.NET web service input | +| RemoteFlowSource.cs:138:37:138:40 | data | ASP.NET web service input | +| RemoteFlowSource.cs:147:47:147:50 | data | ASP.NET web service input |