From 9bf38975daa65566a1078b3f1d5b33ab067f9ae9 Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Fri, 6 Nov 2026 17:07:26 +0100 Subject: [PATCH 1/6] feat(csharp): taint more members of ASP.NET remote flow source types Also taint properties whose accessors wrap a backing field, get-only properties of collection type, and members inherited from base types. Skip constants. Co-Authored-By: Claude Sonnet 5.5 --- .../2026-11-06-taint-plain-data-members.md | 4 ++ .../security/dataflow/flowsources/Remote.qll | 61 +++++++++++++++--- .../aspremote/AspRemoteFlowSource.cs | 46 ++++++++++++++ .../aspremote/aspRemoteFlowSource.expected | 62 +++++++++++-------- .../flowsources/remote/RemoteFlowSource.cs | 42 +++++++++++++ .../remote/remoteFlowSource.expected | 15 ++++- 6 files changed, 196 insertions(+), 34 deletions(-) create mode 100644 csharp/ql/lib/change-notes/2026-11-06-taint-plain-data-members.md 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..85565d16154e 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,54 @@ 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. */ +/** + * 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. + */ +private predicate isPlainDataProperty(Property p) { + p.isAutoImplemented() + or + p.isAutoImplementedReadOnly() + or + exists(Field f | f.getDeclaringType() = p.getDeclaringType() | + exists(FieldRead fr, Expr ret | + fr.getTarget() = f and + fr.getParent*() = ret and + ( + ret = any(ReturnStmt r | r.getEnclosingCallable() = p.getGetter()).getExpr() + or + ret = p.getGetter().getExpressionBody() + ) + ) and + ( + not exists(p.getSetter()) + or + exists(AssignExpr ae, ParameterAccess v | + ae.getLValue().(FieldWrite).getTarget() = f and + ae.getEnclosingCallable() = p.getSetter() and + v.getTarget().hasName("value") and + v.getParent*() = ae.getRValue() + ) + ) + ) +} + +/** + * 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) ) or this = any(Field f | f.isPublic()) @@ -135,6 +172,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 +193,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 +314,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..c8842a43b227 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,53 @@ 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 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 TestController : Controller { + public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp) + { + 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..3aa471a7f992 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,39 @@ 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:41:29:41:37 | ItemsCopy | +| AspRemoteFlowSource.cs:42:23:42:30 | Computed | +| AspRemoteFlowSource.cs:48:22:48:27 | Number | +| AspRemoteFlowSource.cs:55:23:55:30 | Coalesce | +| AspRemoteFlowSource.cs:56:23:56:26 | Cast | +| AspRemoteFlowSource.cs:96:23:96: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:62:47:62:51 | plain | +| AspRemoteFlowSource.cs:62:67:62:73 | derived | +| AspRemoteFlowSource.cs:62:86:62:87 | fp | +| AspRemoteFlowSource.cs:67:42:67:50 | viewModel | +| AspRemoteFlowSource.cs:73:20:73:27 | ordinary | +| AspRemoteFlowSource.cs:74:32:74:40 | fromQuery | +| AspRemoteFlowSource.cs:75:31:75:38 | fromBody | +| AspRemoteFlowSource.cs:76:32:76:40 | fromRoute | +| AspRemoteFlowSource.cs:77:33:77:42 | fromHeader | +| AspRemoteFlowSource.cs:103:42:103:46 | param | +| AspRemoteFlowSource.cs:111:58:111:63 | newUrl | +| AspRemoteFlowSource.cs:112:61:112:65 | myApi | +| AspRemoteFlowSource.cs:112:75:112:79 | myUrl | +| AspRemoteFlowSource.cs:114:46:114:56 | lambdaParam | +| AspRemoteFlowSource.cs:120:65:120:76 | mapPostParam | +| AspRemoteFlowSource.cs:121:63:121:73 | mapPutParam | +| AspRemoteFlowSource.cs:122:69:122:82 | mapDeleteParam | +| AspRemoteFlowSource.cs:124:41:124:44 | item | +| AspRemoteFlowSource.cs:139:34:139:35 | id | +| AspRemoteFlowSource.cs:141:35:141:41 | command | +| AspRemoteFlowSource.cs:141:48:141:52 | count | +| AspRemoteFlowSource.cs:143:40:143:43 | data | +| AspRemoteFlowSource.cs:145:34:145:38 | value | +| AspRemoteFlowSource.cs:147:37:147:42 | itemId | +| AspRemoteFlowSource.cs:160:35:160:46 | derivedParam | 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..f5c08f5dbc55 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,14 @@ 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:113:29:113:37 | ItemsCopy | +| RemoteFlowSource.cs:114:23:114:30 | Computed | +| 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 +30,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 | From 4afcdc3f9357eb2db50b706c8e9939d1c403bbad Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Tue, 10 Nov 2026 11:53:59 +0100 Subject: [PATCH 2/6] fix: fix copilot comments --- .../security/dataflow/flowsources/Remote.qll | 17 ++++-- .../aspremote/AspRemoteFlowSource.cs | 13 +++- .../aspremote/aspRemoteFlowSource.expected | 60 +++++++++---------- .../remote/remoteFlowSource.expected | 2 - 4 files changed, 55 insertions(+), 37 deletions(-) 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 85565d16154e..6dce9b25fc9e 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,6 +117,15 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa override string getSourceType() { result = "ASP.NET web service input" } } +/** Gets `e`, or the expression it wraps in value-preserving casts / null-coalescing. */ +private Expr getAValuePreservingSource(Expr e) { + result = e + or + result = getAValuePreservingSource(e.(CastExpr).getExpr()) + or + result = getAValuePreservingSource(e.(NullCoalescingExpr).getLeftOperand()) +} + /** * 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. @@ -127,10 +136,10 @@ private predicate isPlainDataProperty(Property p) { or p.isAutoImplementedReadOnly() or - exists(Field f | f.getDeclaringType() = p.getDeclaringType() | + exists(Field f | p.getDeclaringType().getABaseType*() = f.getDeclaringType() | exists(FieldRead fr, Expr ret | fr.getTarget() = f and - fr.getParent*() = ret and + fr = getAValuePreservingSource(ret) and ( ret = any(ReturnStmt r | r.getEnclosingCallable() = p.getGetter()).getExpr() or @@ -144,7 +153,7 @@ private predicate isPlainDataProperty(Property p) { ae.getLValue().(FieldWrite).getTarget() = f and ae.getEnclosingCallable() = p.getSetter() and v.getTarget().hasName("value") and - v.getParent*() = ae.getRValue() + v = getAValuePreservingSource(ae.getRValue()) ) ) ) @@ -164,7 +173,7 @@ class CandidateMemberToTaint extends Member { any(Property p | isPlainDataProperty(p) and p.getGetter().isPublic() and - (p.getSetter().isPublic() or p.getType() instanceof CollectionType) + (p.getSetter().isPublic() or (p.getType() instanceof CollectionType and not p.getType() instanceof ArrayType)) ) or this = any(Field f | f.isPublic()) 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 c8842a43b227..be58ca292aaf 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs @@ -48,6 +48,17 @@ public string Computed // Not considered tainted as the getter computes a value. 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; @@ -59,7 +70,7 @@ public class FlowProps public class TestController : Controller { - public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp) + public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp, InheritedField inh) { 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 3aa471a7f992..eff398114ee3 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected @@ -5,35 +5,35 @@ remoteFlowSourceMembers | AspRemoteFlowSource.cs:32:23:32:28 | Backed | | AspRemoteFlowSource.cs:38:29:38:40 | ItemsOnlyGet | | AspRemoteFlowSource.cs:39:29:39:44 | ItemsAutoOnlyGet | -| AspRemoteFlowSource.cs:41:29:41:37 | ItemsCopy | -| AspRemoteFlowSource.cs:42:23:42:30 | Computed | | AspRemoteFlowSource.cs:48:22:48:27 | Number | -| AspRemoteFlowSource.cs:55:23:55:30 | Coalesce | -| AspRemoteFlowSource.cs:56:23:56:26 | Cast | -| AspRemoteFlowSource.cs:96:23:96:29 | Tainted | +| AspRemoteFlowSource.cs:58:23:58:26 | Prop | +| AspRemoteFlowSource.cs:66:23:66:30 | Coalesce | +| AspRemoteFlowSource.cs:67:23:67:26 | Cast | +| AspRemoteFlowSource.cs:107:23:107:29 | Tainted | remoteFlowSources -| AspRemoteFlowSource.cs:62:47:62:51 | plain | -| AspRemoteFlowSource.cs:62:67:62:73 | derived | -| AspRemoteFlowSource.cs:62:86:62:87 | fp | -| AspRemoteFlowSource.cs:67:42:67:50 | viewModel | -| AspRemoteFlowSource.cs:73:20:73:27 | ordinary | -| AspRemoteFlowSource.cs:74:32:74:40 | fromQuery | -| AspRemoteFlowSource.cs:75:31:75:38 | fromBody | -| AspRemoteFlowSource.cs:76:32:76:40 | fromRoute | -| AspRemoteFlowSource.cs:77:33:77:42 | fromHeader | -| AspRemoteFlowSource.cs:103:42:103:46 | param | -| AspRemoteFlowSource.cs:111:58:111:63 | newUrl | -| AspRemoteFlowSource.cs:112:61:112:65 | myApi | -| AspRemoteFlowSource.cs:112:75:112:79 | myUrl | -| AspRemoteFlowSource.cs:114:46:114:56 | lambdaParam | -| AspRemoteFlowSource.cs:120:65:120:76 | mapPostParam | -| AspRemoteFlowSource.cs:121:63:121:73 | mapPutParam | -| AspRemoteFlowSource.cs:122:69:122:82 | mapDeleteParam | -| AspRemoteFlowSource.cs:124:41:124:44 | item | -| AspRemoteFlowSource.cs:139:34:139:35 | id | -| AspRemoteFlowSource.cs:141:35:141:41 | command | -| AspRemoteFlowSource.cs:141:48:141:52 | count | -| AspRemoteFlowSource.cs:143:40:143:43 | data | -| AspRemoteFlowSource.cs:145:34:145:38 | value | -| AspRemoteFlowSource.cs:147:37:147:42 | itemId | -| AspRemoteFlowSource.cs:160:35:160:46 | derivedParam | +| AspRemoteFlowSource.cs:73:47:73:51 | plain | +| AspRemoteFlowSource.cs:73:67:73:73 | derived | +| AspRemoteFlowSource.cs:73:86:73:87 | fp | +| AspRemoteFlowSource.cs:73:105:73:107 | inh | +| AspRemoteFlowSource.cs:78:42:78:50 | viewModel | +| AspRemoteFlowSource.cs:84:20:84:27 | ordinary | +| AspRemoteFlowSource.cs:85:32:85:40 | fromQuery | +| AspRemoteFlowSource.cs:86:31:86:38 | fromBody | +| AspRemoteFlowSource.cs:87:32:87:40 | fromRoute | +| AspRemoteFlowSource.cs:88:33:88:42 | fromHeader | +| AspRemoteFlowSource.cs:114:42:114:46 | param | +| AspRemoteFlowSource.cs:122:58:122:63 | newUrl | +| AspRemoteFlowSource.cs:123:61:123:65 | myApi | +| AspRemoteFlowSource.cs:123:75:123:79 | myUrl | +| AspRemoteFlowSource.cs:125:46:125:56 | lambdaParam | +| AspRemoteFlowSource.cs:131:65:131:76 | mapPostParam | +| AspRemoteFlowSource.cs:132:63:132:73 | mapPutParam | +| AspRemoteFlowSource.cs:133:69:133:82 | mapDeleteParam | +| AspRemoteFlowSource.cs:135:41:135:44 | item | +| AspRemoteFlowSource.cs:150:34:150:35 | id | +| AspRemoteFlowSource.cs:152:35:152:41 | command | +| AspRemoteFlowSource.cs:152:48:152:52 | count | +| AspRemoteFlowSource.cs:154:40:154:43 | data | +| AspRemoteFlowSource.cs:156:34:156:38 | value | +| AspRemoteFlowSource.cs:158:37:158:42 | itemId | +| AspRemoteFlowSource.cs:171:35:171:46 | derivedParam | 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 f5c08f5dbc55..70dd0115b7d7 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected @@ -12,8 +12,6 @@ remoteFlowSourceMembers | RemoteFlowSource.cs:95:23:95:30 | BaseProp | | RemoteFlowSource.cs:105:23:105:28 | Backed | | RemoteFlowSource.cs:111:29:111:40 | ItemsOnlyGet | -| RemoteFlowSource.cs:113:29:113:37 | ItemsCopy | -| RemoteFlowSource.cs:114:23:114:30 | Computed | | RemoteFlowSource.cs:120:22:120:27 | Number | | RemoteFlowSource.cs:127:23:127:30 | Coalesce | | RemoteFlowSource.cs:128:23:128:26 | Cast | From 1f2846a2e46accacdc4048fafbbad80d41ad81d3 Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Mon, 16 Nov 2026 17:32:21 +0100 Subject: [PATCH 3/6] fix: fix copilot review --- .../security/dataflow/flowsources/Remote.qll | 64 ++++++++++++------- .../aspremote/AspRemoteFlowSource.cs | 15 +++++ .../aspremote/aspRemoteFlowSource.expected | 58 +++++++++-------- .../aspremote/aspRemoteFlowSource.ql | 5 ++ 4 files changed, 93 insertions(+), 49 deletions(-) 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 6dce9b25fc9e..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 @@ -121,39 +121,59 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa private Expr getAValuePreservingSource(Expr e) { result = e or - result = getAValuePreservingSource(e.(CastExpr).getExpr()) + result = getAValuePreservingSource(e.(Cast).getExpr()) or - result = getAValuePreservingSource(e.(NullCoalescingExpr).getLeftOperand()) + 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) { - p.isAutoImplemented() - or - p.isAutoImplementedReadOnly() - or - exists(Field f | p.getDeclaringType().getABaseType*() = f.getDeclaringType() | - exists(FieldRead fr, Expr ret | - fr.getTarget() = f and - fr = getAValuePreservingSource(ret) and + 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 ( - ret = any(ReturnStmt r | r.getEnclosingCallable() = p.getGetter()).getExpr() + not exists(u.getSetter()) or - ret = p.getGetter().getExpressionBody() - ) - ) and - ( - not exists(p.getSetter()) - or - exists(AssignExpr ae, ParameterAccess v | - ae.getLValue().(FieldWrite).getTarget() = f and - ae.getEnclosingCallable() = p.getSetter() and - v.getTarget().hasName("value") and - v = getAValuePreservingSource(ae.getRValue()) + 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()) + ) ) ) ) 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 be58ca292aaf..36a441a9f863 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs @@ -66,6 +66,21 @@ public class FlowProps 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 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 eff398114ee3..04431b00ecb1 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected @@ -9,31 +9,35 @@ remoteFlowSourceMembers | AspRemoteFlowSource.cs:58:23:58:26 | Prop | | AspRemoteFlowSource.cs:66:23:66:30 | Coalesce | | AspRemoteFlowSource.cs:67:23:67:26 | Cast | -| AspRemoteFlowSource.cs:107:23:107:29 | Tainted | +| AspRemoteFlowSource.cs:70:23:70:28 | AsCast | +| AspRemoteFlowSource.cs:72:29:72:37 | LazyItems | +| AspRemoteFlowSource.cs:122:23:122:29 | Tainted | remoteFlowSources -| AspRemoteFlowSource.cs:73:47:73:51 | plain | -| AspRemoteFlowSource.cs:73:67:73:73 | derived | -| AspRemoteFlowSource.cs:73:86:73:87 | fp | -| AspRemoteFlowSource.cs:73:105:73:107 | inh | -| AspRemoteFlowSource.cs:78:42:78:50 | viewModel | -| AspRemoteFlowSource.cs:84:20:84:27 | ordinary | -| AspRemoteFlowSource.cs:85:32:85:40 | fromQuery | -| AspRemoteFlowSource.cs:86:31:86:38 | fromBody | -| AspRemoteFlowSource.cs:87:32:87:40 | fromRoute | -| AspRemoteFlowSource.cs:88:33:88:42 | fromHeader | -| AspRemoteFlowSource.cs:114:42:114:46 | param | -| AspRemoteFlowSource.cs:122:58:122:63 | newUrl | -| AspRemoteFlowSource.cs:123:61:123:65 | myApi | -| AspRemoteFlowSource.cs:123:75:123:79 | myUrl | -| AspRemoteFlowSource.cs:125:46:125:56 | lambdaParam | -| AspRemoteFlowSource.cs:131:65:131:76 | mapPostParam | -| AspRemoteFlowSource.cs:132:63:132:73 | mapPutParam | -| AspRemoteFlowSource.cs:133:69:133:82 | mapDeleteParam | -| AspRemoteFlowSource.cs:135:41:135:44 | item | -| AspRemoteFlowSource.cs:150:34:150:35 | id | -| AspRemoteFlowSource.cs:152:35:152:41 | command | -| AspRemoteFlowSource.cs:152:48:152:52 | count | -| AspRemoteFlowSource.cs:154:40:154:43 | data | -| AspRemoteFlowSource.cs:156:34:156:38 | value | -| AspRemoteFlowSource.cs:158:37:158:42 | itemId | -| AspRemoteFlowSource.cs:171:35:171: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%") +} From 5ce5d17d2657ba0ab4a26a64a08ef28c94a37cd1 Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Thu, 26 Nov 2026 10:20:39 +0100 Subject: [PATCH 4/6] fix: review changes from michael comment --- .../2026-11-06-taint-plain-data-members.md | 2 +- .../semmle/code/csharp/frameworks/JsonNET.qll | 10 ++ .../security/dataflow/flowsources/Remote.qll | 119 ++++++++++-------- .../aspremote/AspRemoteFlowSource.cs | 19 ++- .../aspremote/aspRemoteFlowSource.expected | 73 +++++------ .../remote/remoteFlowSource.expected | 7 +- 6 files changed, 138 insertions(+), 92 deletions(-) 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 index 17f806bc70e5..c7db628ad918 100644 --- 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 @@ -1,4 +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. +* Tainted members of ASP.NET and ASP.NET Core remote flow source types now also include private fields that data binders populate (fields marked `[DataMember]` or `[JsonProperty]`, fields 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/frameworks/JsonNET.qll b/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll index 69ae1ec292b5..bb82f79ae1ce 100644 --- a/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll +++ b/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll @@ -126,6 +126,16 @@ module JsonNET { predicate isOptIn() { this.getArgument(_).(FieldAccess).getTarget().hasName("OptIn") } } + /** The class `Newtonsoft.Json.JsonPropertyAttribute`. */ + class JsonPropertyAttributeClass extends JsonClass { + JsonPropertyAttributeClass() { this.hasName("JsonPropertyAttribute") } + } + + /** An attribute of type `Newtonsoft.Json.JsonPropertyAttribute`. */ + class JsonPropertyAttribute extends Attribute { + JsonPropertyAttribute() { this.getType() instanceof JsonPropertyAttributeClass } + } + /** The namespace `Newtonsoft.Json.Linq`. */ class LinqNamespace extends Namespace { LinqNamespace() { 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 c0461636a670..7c0623ff3641 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 @@ -11,6 +11,7 @@ private import semmle.code.csharp.frameworks.system.web.Mvc private import semmle.code.csharp.frameworks.system.web.Services private import semmle.code.csharp.frameworks.system.web.ui.WebControls private import semmle.code.csharp.frameworks.WCF +private import semmle.code.csharp.frameworks.JsonNET private import semmle.code.csharp.frameworks.microsoft.Owin private import semmle.code.csharp.frameworks.microsoft.AspNetCore private import semmle.code.csharp.frameworks.Razor @@ -131,72 +132,88 @@ 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 +/** Holds if the public setter `s` assigns `value` to the own field `f`. */ +private predicate setterAssignsValueToField(Setter s, Field f) { + s.isPublic() and + exists(AssignExpr ae, ParameterAccess v | + ae.getEnclosingCallable() = s and + ae.getLeftOperand().(FieldWrite).getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and + isOwnFieldAccess(ae.getLeftOperand()) and + v = getAValuePreservingSource(ae.getRightOperand()) and + v.getTarget().hasName("value") + ) +} + +/** 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 ( - not exists(u.getSetter()) + ret = getter.getExpressionBody() 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()) - ) + 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 + ( + // populated by data contract / Json.NET deserialization + m.(Attributable).getAnAttribute() instanceof DataMemberAttribute + or + m.(Attributable).getAnAttribute() instanceof JsonNET::JsonPropertyAttribute + or + exists(Property p | + m.(Field).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 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. + * 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 | - isPlainDataProperty(p) and - p.getGetter().isPublic() and - (p.getSetter().isPublic() or (p.getType() instanceof CollectionType and not p.getType() instanceof ArrayType)) - ) + 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) ) } } 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 36a441a9f863..0c26c47a1969 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs @@ -83,9 +83,18 @@ public class GenericModel public List ItemsPlain => items; // Considered tainted. Get-only collection. } + public class AttributedModel + { + [System.Runtime.Serialization.DataMember] private string dataMember; // Considered tainted. Populated by data contract deserialization. + [Newtonsoft.Json.JsonProperty("n")] private string jsonProp; // Considered tainted. Populated by JSON deserialization. + 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) + public object MyAction(PlainDataModel plain, DerivedModel derived, FlowProps fp, InheritedField inh, AttributedModel attributed) { throw null; } @@ -186,3 +195,11 @@ public class DerivedPageModel : MyPageModel public void OnPost(string derivedParam) { } } } + +namespace Newtonsoft.Json +{ + public class JsonPropertyAttribute : System.Attribute + { + public JsonPropertyAttribute(string name) { } + } +} 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 04431b00ecb1..fba4210897d5 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected @@ -2,42 +2,45 @@ remoteFlowSourceMembers | 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: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: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 | +| 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:66:88:75 | dataMember | +| AspRemoteFlowSource.cs:89:60:89:67 | jsonProp | +| AspRemoteFlowSource.cs:91:24:91:30 | guarded | +| AspRemoteFlowSource.cs:131:23:131:29 | Tainted | remoteFlowSources -| 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 | +| 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:83:24:83:33 | ItemsPlain | +| AspRemoteFlowSource.cs:81:25:81:29 | items | 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 70dd0115b7d7..a6301932aaa0 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/remote/remoteFlowSource.expected @@ -10,11 +10,10 @@ remoteFlowSourceMembers | 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:102:24:102:30 | backing | +| RemoteFlowSource.cs:103:30:103:34 | items | | RemoteFlowSource.cs:120:22:120:27 | Number | -| RemoteFlowSource.cs:127:23:127:30 | Coalesce | -| RemoteFlowSource.cs:128:23:128:26 | Cast | +| 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 | From 6ede56898f882560990d5723eb5c637e5fd97d0e Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Thu, 26 Nov 2026 15:04:48 +0100 Subject: [PATCH 5/6] fix: review changes from michael comment --- .../2026-11-06-taint-plain-data-members.md | 2 +- .../semmle/code/csharp/frameworks/JsonNET.qll | 10 ------- .../security/dataflow/flowsources/Remote.qll | 27 +++++++++---------- .../aspremote/AspRemoteFlowSource.cs | 12 ++------- .../aspremote/aspRemoteFlowSource.expected | 3 +-- 5 files changed, 17 insertions(+), 37 deletions(-) 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 index c7db628ad918..86335f33430e 100644 --- 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 @@ -1,4 +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 marked `[DataMember]` or `[JsonProperty]`, fields 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. +* 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/frameworks/JsonNET.qll b/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll index bb82f79ae1ce..69ae1ec292b5 100644 --- a/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll +++ b/csharp/ql/lib/semmle/code/csharp/frameworks/JsonNET.qll @@ -126,16 +126,6 @@ module JsonNET { predicate isOptIn() { this.getArgument(_).(FieldAccess).getTarget().hasName("OptIn") } } - /** The class `Newtonsoft.Json.JsonPropertyAttribute`. */ - class JsonPropertyAttributeClass extends JsonClass { - JsonPropertyAttributeClass() { this.hasName("JsonPropertyAttribute") } - } - - /** An attribute of type `Newtonsoft.Json.JsonPropertyAttribute`. */ - class JsonPropertyAttribute extends Attribute { - JsonPropertyAttribute() { this.getType() instanceof JsonPropertyAttributeClass } - } - /** The namespace `Newtonsoft.Json.Linq`. */ class LinqNamespace extends Namespace { LinqNamespace() { 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 7c0623ff3641..23d2069b815a 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 @@ -11,7 +11,6 @@ private import semmle.code.csharp.frameworks.system.web.Mvc private import semmle.code.csharp.frameworks.system.web.Services private import semmle.code.csharp.frameworks.system.web.ui.WebControls private import semmle.code.csharp.frameworks.WCF -private import semmle.code.csharp.frameworks.JsonNET private import semmle.code.csharp.frameworks.microsoft.Owin private import semmle.code.csharp.frameworks.microsoft.AspNetCore private import semmle.code.csharp.frameworks.Razor @@ -127,17 +126,22 @@ private Expr getAValuePreservingSource(Expr e) { 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 `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 `f`. */ -private predicate setterAssignsValueToField(Setter s, Field f) { +/** + * 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(AssignExpr ae, ParameterAccess v | ae.getEnclosingCallable() = s and - ae.getLeftOperand().(FieldWrite).getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and + ae.getLeftOperand().(AssignableWrite).getTarget().getUnboundDeclaration() = + f.getUnboundDeclaration() and isOwnFieldAccess(ae.getLeftOperand()) and v = getAValuePreservingSource(ae.getRightOperand()) and v.getTarget().hasName("value") @@ -165,14 +169,9 @@ private predicate isPrivateBoundMember(Member m) { m.isPrivate() and not m.isStatic() and ( - // populated by data contract / Json.NET deserialization - m.(Attributable).getAnAttribute() instanceof DataMemberAttribute - or - m.(Attributable).getAnAttribute() instanceof JsonNET::JsonPropertyAttribute - or + // `[DataMember]` / `[JsonProperty]` members are already tainted by `JsonNET.qll` exists(Property p | - m.(Field).getDeclaringType().getUnboundDeclaration() = - p.getDeclaringType().getUnboundDeclaration() and + m.getDeclaringType().getUnboundDeclaration() = p.getDeclaringType().getUnboundDeclaration() and setterAssignsValueToField(p.getUnboundDeclaration().getSetter(), m) ) 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 0c26c47a1969..97e48900c29d 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs @@ -85,8 +85,8 @@ public class GenericModel public class AttributedModel { - [System.Runtime.Serialization.DataMember] private string dataMember; // Considered tainted. Populated by data contract deserialization. - [Newtonsoft.Json.JsonProperty("n")] private string jsonProp; // Considered tainted. Populated by JSON deserialization. + 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. @@ -195,11 +195,3 @@ public class DerivedPageModel : MyPageModel public void OnPost(string derivedParam) { } } } - -namespace Newtonsoft.Json -{ - public class JsonPropertyAttribute : System.Attribute - { - public JsonPropertyAttribute(string name) { } - } -} 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 fba4210897d5..3d078b7c98b1 100644 --- a/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected +++ b/csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected @@ -10,8 +10,7 @@ remoteFlowSourceMembers | AspRemoteFlowSource.cs:69:24:69:24 | o | | AspRemoteFlowSource.cs:71:30:71:33 | lazy | | AspRemoteFlowSource.cs:73:24:73:25 | ow | -| AspRemoteFlowSource.cs:88:66:88:75 | dataMember | -| AspRemoteFlowSource.cs:89:60:89:67 | jsonProp | +| AspRemoteFlowSource.cs:88:24:88:34 | AutoBacking | | AspRemoteFlowSource.cs:91:24:91:30 | guarded | | AspRemoteFlowSource.cs:131:23:131:29 | Tainted | remoteFlowSources From f0edd6893fec1497cf9be5a84c3c581442c01621 Mon Sep 17 00:00:00 2001 From: hugo-syn Date: Thu, 26 Nov 2026 18:08:30 +0100 Subject: [PATCH 6/6] fix: review changes from michael comment --- csharp/ql/lib/semmle/code/csharp/Property.qll | 2 +- .../csharp/security/dataflow/flowsources/Remote.qll | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) 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 23d2069b815a..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 @@ -138,13 +138,11 @@ private predicate isOwnFieldAccess(MemberAccess ma) { private predicate setterAssignsValueToField(Setter s, Member f) { (f instanceof Field or f.(Property).isAutoImplemented()) and s.isPublic() and - exists(AssignExpr ae, ParameterAccess v | - ae.getEnclosingCallable() = s and - ae.getLeftOperand().(AssignableWrite).getTarget().getUnboundDeclaration() = - f.getUnboundDeclaration() and - isOwnFieldAccess(ae.getLeftOperand()) and - v = getAValuePreservingSource(ae.getRightOperand()) and - v.getTarget().hasName("value") + exists(AssignableDefinition def | + def.getEnclosingCallable() = s and + def.getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and + isOwnFieldAccess(def.getTargetAccess()) and + accessToValue() = getAValuePreservingSource(def.getSource()) ) }