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..17f806bc70e5 --- /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 public properties whose accessors wrap a backing field, public get-only properties of collection type, and members inherited from base types. Constants are no longer tainted. 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..c0461636a670 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,17 +117,83 @@ 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 `fa` accesses a field on the current instance. */ +private predicate isOwnFieldAccess(FieldAccess fa) { + fa.getQualifier() instanceof ThisAccess or fa.getQualifier() instanceof BaseAccess +} + +/** + * Holds if `p` is auto-implemented, or its getter returns an expression reading a field + * and its setter (if any) assigns an expression using `value` to that field. + * Properties that compute a value do not qualify. + * Accessor bodies are only extracted for the unbound declaration, so shape checks use it. + */ +private predicate isPlainDataProperty(Property p) { + exists(Property u | u = p.getUnboundDeclaration() | + u.isAutoImplemented() + or + u.isAutoImplementedReadOnly() + or + exists(Field f | + u.getDeclaringType().getABaseType*().getUnboundDeclaration() = + f.getDeclaringType().getUnboundDeclaration() + | + exists(FieldRead fr, Expr ret | + fr.getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and + isOwnFieldAccess(fr) and + fr = getAValuePreservingSource(ret) and + ( + ret = any(ReturnStmt r | r.getEnclosingCallable() = u.getGetter()).getExpr() + or + ret = u.getGetter().getExpressionBody() + ) + ) and + // The getter must not unconditionally overwrite the field before returning it. + not exists(AssignExpr ow | + ow.getLeftOperand().(FieldWrite).getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and + isOwnFieldAccess(ow.getLeftOperand()) and + ow.getParent().(ExprStmt).getParent() = u.getGetter().getBody() + ) and + ( + not exists(u.getSetter()) + or + exists(AssignExpr ae, ParameterAccess v | + ae.getLeftOperand().(FieldWrite).getTarget().getUnboundDeclaration() = + f.getUnboundDeclaration() and + isOwnFieldAccess(ae.getLeftOperand()) and + ae.getEnclosingCallable() = u.getSetter() and + v.getTarget().hasName("value") and + v = getAValuePreservingSource(ae.getRightOperand()) + ) + ) + ) + ) +} + +/** + * A public, non-static field, or a public plain-data property that is either settable or + * of collection type. Binders such as `XmlSerializer` populate a get-only collection in place. + */ class CandidateMemberToTaint extends Member { CandidateMemberToTaint() { this.isPublic() and not this.isStatic() and + not this.(Field).isConst() and ( this = any(Property p | - p.isAutoImplemented() and + isPlainDataProperty(p) and p.getGetter().isPublic() and - p.getSetter().isPublic() + (p.getSetter().isPublic() or (p.getType() instanceof CollectionType and not p.getType() instanceof ArrayType)) ) or this = any(Field f | f.isPublic()) @@ -135,6 +201,16 @@ class CandidateMemberToTaint extends Member { } } +/** + * 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 +222,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 +343,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..36a441a9f863 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,79 @@ 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 TestController : Controller { + public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp, InheritedField inh) + { + 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..04431b00ecb1 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,43 @@ 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:32:23:32:28 | Backed | +| AspRemoteFlowSource.cs:38:29:38:40 | ItemsOnlyGet | +| AspRemoteFlowSource.cs:39:29:39:44 | ItemsAutoOnlyGet | +| AspRemoteFlowSource.cs:48:22:48:27 | Number | +| AspRemoteFlowSource.cs:58:23:58:26 | Prop | +| AspRemoteFlowSource.cs:66:23:66:30 | Coalesce | +| AspRemoteFlowSource.cs:67:23:67:26 | Cast | +| AspRemoteFlowSource.cs:70:23:70:28 | AsCast | +| AspRemoteFlowSource.cs:72:29:72:37 | LazyItems | +| AspRemoteFlowSource.cs:122:23:122: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:88:47:88:51 | plain | +| AspRemoteFlowSource.cs:88:67:88:73 | derived | +| AspRemoteFlowSource.cs:88:86:88:87 | fp | +| AspRemoteFlowSource.cs:88:105:88:107 | inh | +| AspRemoteFlowSource.cs:93:42:93:50 | viewModel | +| AspRemoteFlowSource.cs:99:20:99:27 | ordinary | +| AspRemoteFlowSource.cs:100:32:100:40 | fromQuery | +| AspRemoteFlowSource.cs:101:31:101:38 | fromBody | +| AspRemoteFlowSource.cs:102:32:102:40 | fromRoute | +| AspRemoteFlowSource.cs:103:33:103:42 | fromHeader | +| AspRemoteFlowSource.cs:129:42:129:46 | param | +| AspRemoteFlowSource.cs:137:58:137:63 | newUrl | +| AspRemoteFlowSource.cs:138:61:138:65 | myApi | +| AspRemoteFlowSource.cs:138:75:138:79 | myUrl | +| AspRemoteFlowSource.cs:140:46:140:56 | lambdaParam | +| AspRemoteFlowSource.cs:146:65:146:76 | mapPostParam | +| AspRemoteFlowSource.cs:147:63:147:73 | mapPutParam | +| AspRemoteFlowSource.cs:148:69:148:82 | mapDeleteParam | +| AspRemoteFlowSource.cs:150:41:150:44 | item | +| AspRemoteFlowSource.cs:165:34:165:35 | id | +| AspRemoteFlowSource.cs:167:35:167:41 | command | +| AspRemoteFlowSource.cs:167:48:167:52 | count | +| AspRemoteFlowSource.cs:169:40:169:43 | data | +| AspRemoteFlowSource.cs:171:34:171:38 | value | +| AspRemoteFlowSource.cs:173:37:173:42 | itemId | +| AspRemoteFlowSource.cs:186:35:186:46 | derivedParam | +genericCandidateMembers +| AspRemoteFlowSource.cs:83:24:83:33 | ItemsPlain | 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..70dd0115b7d7 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,12 @@ 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:105:23:105:28 | Backed | +| RemoteFlowSource.cs:111:29:111:40 | ItemsOnlyGet | +| RemoteFlowSource.cs:120:22:120:27 | Number | +| RemoteFlowSource.cs:127:23:127:30 | Coalesce | +| RemoteFlowSource.cs:128:23:128:26 | Cast | 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 +28,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 |