Skip to content

C#: Taint more members of ASP.NET remote flow source types - #23043

Open
hugo-syn wants to merge 6 commits into
github:mainfrom
hugo-syn:csharp/taint-plain-data-members
Open

hugo-syn wants to merge 6 commits into
github:mainfrom
hugo-syn:csharp/taint-plain-data-members

Conversation

@hugo-syn

@hugo-syn hugo-syn commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Hello @michaelnebel

Follow-up to #21567 / #21612. Members of types bound from [WebMethod] or action-method parameters are only tainted when they are public auto-implemented properties or public fields. Real binder-populated types often don't fit that, so taint stops at the first such member and flow is lost. From my security researcher point of view I was not fully satisfied.

This PR relaxes CandidateMemberToTaint and the two transitive classes (AspNetRemoteFlowSourceMember, AspNetCoreRemoteFlowSourceMember):

  • Backing-field properties: a property is accepted if its getter returns an expression reading a field and its setter assigns an expression using value to that field, for example lazy initialization or a null check. Properties that compute a value are still excluded, which keeps the isAutoImplemented guard's intent.
  • Get-only collections: a plain-data property of collection type is accepted without a setter, since binders such as XmlSerializer and Json.NET call the getter and Add to the returned collection. Get-only non-collections stay untainted.
  • Base types: members declared on base types of a bound type are now covered (shared helper isMemberOfBoundType).
  • Constants: const fields are excluded. The extractor does not mark them static, so the transitive rule otherwise taints short.MaxValue and similar.

As noted in the discussion, tainting is type-wide, so these members are tainted wherever the type is used. The change stays narrow for that reason.

Tests extend the existing aspremote and remote flow-source tests. A change note is included.

I'm still not 100% satisfied because for more complex operations in Getter/Setter this would not work. The only way to do it properly would be with TaintTracking I think.

isPlainDataProperty is purely syntactic: the getter must return an expression that reads a field, and the setter must assign an expression using value to that field. It does not follow accessors with several statements:

public class Model
{
    private string f;

    // Tainted: the setter assigns `value` directly and the getter returns the field.
    public string Simple { get { return f; } set { f = value; } }

    // Not tainted: `value` reaches `f` only through a local variable.
    public string ViaLocal
    {
        get { var r = f; return r; }       // the returned expression is `r`, not a read of `f`
        set { var t = value; f = t; }      // the assigned expression is `t`, not `value`
    }

    // Not tainted: same problem with a branch.
    public string Conditional
    {
        get { string r; if (f != null) r = f; else r = ""; return r; }
        set { var t = value; if (t.Length > 0) f = t; }
    }

    // Tainted by a syntactic check, but only because `value` appears in the call.
    public string Wrapped { get { return f; } set { f = Normalize(value); } }
}

Local data flow (DataFlow::localFlow) would handle ViaLocal and Conditional, because it follows locals and branches. I confirmed it also handles f ?? "" and casts, which the syntactic check misses on the getter side. It would miss Wrapped, because it follows value-preserving steps only, so Trim() and Normalize() end the flow. TaintTracking::localTaint would follow those. However, TaintedMember feeds the taint-step relation, so I expect it to be circular and I haven't tried it.

I would like feedback on whether the extra precision is worth a data-flow dependency inside a candidate-member predicate.

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 <noreply@anthropic.com>
@hugo-syn
hugo-syn requested a review from a team as a code owner November 6, 2026 16:51
Copilot AI adaptive review requested due to automatic review settings November 6, 2026 16:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The source modeling incorrectly taints copied/computed properties and non-bindable collections while missing request-data flow through properties backed by inherited fields.

3 open issues
What changed in this PR

This PR broadens CodeQL’s C# ASP.NET remote-source modeling so taint can propagate through more binder-populated model members.

Changes:

  • Expands property eligibility to backing-field properties and get-only collections.
  • Adds inherited-member traversal and excludes constant fields.
  • Extends legacy ASP.NET and ASP.NET Core test fixtures, expected results, and release documentation.
File Description
csharp/​ql/​test/​library-tests/​dataflow/​flowsources/​remote/​remoteFlowSource.expected Updates expected legacy source and member results.
csharp/​ql/​test/​library-tests/​dataflow/​flowsources/​remote/​RemoteFlowSource.cs Adds legacy web-service model cases.
csharp/​ql/​test/​library-tests/​dataflow/​flowsources/​aspremote/​aspRemoteFlowSource.expected Updates expected ASP.NET Core results.
csharp/​ql/​test/​library-tests/​dataflow/​flowsources/​aspremote/​AspRemoteFlowSource.cs Adds ASP.NET Core model cases and behavior annotations.
csharp/​ql/​lib/​semmle/​code/​csharp/​security/​dataflow/​flowsources/​Remote.qll Expands candidate-member rules and shares base-type traversal.
csharp/​ql/​lib/​change-notes/​2026-11-06-taint-plain-data-members.md Documents the analysis changes.

Review effort: Max (auto)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved taint-modeling defects incorrectly taint properties that cannot preserve bound values while missing valid cast and lazy-initialization flows.

3 open issues
3 previously missed issues

In code that hasn't changed since last review

Medium severity Reject getters that overwrite the backing field before returning it

csharp/​ql/​lib/​semmle/​code/​csharp/​security/​dataflow/​flowsources/​Remote.qll:142

Checking only the returned expression still accepts get { backing = "safe"; return backing; } paired with set { backing = value; }. That property always returns a constant, but it now becomes a TaintedMember, introducing qualifier-to-result taint despite the overwrite. Please account for writes that replace the backing field before it is returned, while preserving the supported lazy-initialization case, and add a negative regression case.

Medium severity Require getter and setter to access the same property receiver

csharp/​ql/​lib/​semmle/​code/​csharp/​security/​dataflow/​flowsources/​Remote.qll:157

Matching the field declaration does not ensure the getter and setter use the same instance: get { return this.f; } set { other.f = value; } passes this check. Binding that property never changes its returned value, yet the property gains a type-wide taint step. Require both field accesses to target the property receiver (this/base) and add a negative test.

Medium severity Check unbound accessor shape for constructed generic properties

csharp/​ql/​lib/​semmle/​code/​csharp/​security/​dataflow/​flowsources/​Remote.qll:174

Constructed generic accessors have no extracted bodies, so isAutoImplementedReadOnly() treats a computed get-only property as auto-implemented. Binding Model<string> with Model<T>.ItemsCopy => new List<T>(items) therefore taints ItemsCopy, although populating the returned temporary list cannot affect subsequent getter calls. Check p.getUnboundDeclaration() for the accessor shape, keeping p for the constructed collection-type check, and add a generic negative test.

3 resolved since last review

Review effort: Max (dynamic)

private Expr getAValuePreservingSource(Expr e) {
result = e
or
result = getAValuePreservingSource(e.(CastExpr).getExpr())
or
result = getAValuePreservingSource(e.(CastExpr).getExpr())
or
result = getAValuePreservingSource(e.(NullCoalescingExpr).getLeftOperand())
Comment on lines +174 to +176
isPlainDataProperty(p) and
p.getGetter().isPublic() and
p.getSetter().isPublic()
(p.getSetter().isPublic() or (p.getType() instanceof CollectionType and not p.getType() instanceof ArrayType))
Comment on lines +146 to +176
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())
)

@michaelnebel michaelnebel Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugo-syn : Thank you very much for trying to improve this - we really appreciate any increase in the precision of the modelling (and especially source modelling as this improves the quality of the query results for a wide range of queries).

Really great, that you

  • Remove the public constants from the set of tainted members.
  • Consider collections tainted, if they only have a setter (as they can be have their elements tainted)!

Also, really good that you try to be as conservative as possible.

It looks like the general problem is that we can have private members that we should consider tainted.

Perhaps, instead of checking whether a property both reads and writes a "backing" field, we should create a new disjunct in the CandidateMemberToTaint for private members. If we "taint" (some) private members, then I expect that dataflow will do the work for us in the queries, when a property is returning the value of the (tainted) private member.

One initial suggestion for which private members (private fields and private auto-implemented[Readonly] properties) to consider tainted:

  • Private members tagged with the DataMember attribute (isn't that the members that could be tainted using data contract deserialization?).
  • Private members tagged with JsonProperty (json deserialization).
  • Private members that are being set to value in a public property setter and the setter only has one statement. The question (as you mention) is whether we should consider extending using dataflow (such that all private fields that has flow from value is considered tainted). However, we could consider just doing the former, unless you have seen real world examples, where you think we could benefit from using data-flow to taint more private fields?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @michaelnebel, thanks for the detailed review. I reworked the PR along the lines you suggested.

What changed

  • The getter/setter pattern matching is gone, so there is no more check that a property reads and writes the same backing field.
  • A private field is now a candidate if:
    • it is marked [DataMember] or [JsonProperty]. I reused the existing DataMemberAttribute from WCF.qll and added a JsonPropertyAttribute class to JsonNET.qll, next to JsonObjectAttribute;
    • a public setter assigns value to it, possibly through a cast or ??; or
    • a public get-only collection property returns it.
  • Dataflow now carries taint through the public getter, as you described.
  • Constants are still excluded, and members of base types are still covered.
  • The public auto-property rules are unchanged. Get-only auto-implemented collection properties are still tainted.

Where I deviated from your suggestion

  1. Setter rule: I dropped the "setter has only one statement" condition. It now only requires a field = value assignment in the setter. I tried your rules exactly as written against the WSUS reporting service from CVE-2025-59287, which is the chain I was trying to model. They missed it, because ReportingEvent.ExtendedData has a guarded setter:
set { if (value == null) throw new ArgumentNullException("value"); extendedData = value; }
  1. Get-only collections: I added the get-only collection rule because ExtendedData.MiscData is public StringCollection MiscData => miscData;, with the field assigned in the constructor. No attribute or setter rule covers that.

I did not use dataflow to decide which fields are tainted, maybe another time as I have no example where it would be needed.

JsonNET.qll's SerializedMember already taints [JsonProperty] and [DataMember] members, so the attribute disjuncts add no new results in my tests. I kept them so that the intent is explicit in Remote.qll. Do you prefer I drop them and rely on JsonNET.qll?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonNET.qll's SerializedMember already taints [JsonProperty] and [DataMember] members, so the attribute disjuncts add no new results in my tests. I kept them so that the intent is explicit in Remote.qll. Do you prefer I drop them and rely on JsonNET.qll?

Uh, yes you are right - they are already there. I think we should drop them here then!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've removed them

}

/** Holds if the public setter `s` assigns `value` to the own field `f`. */
private predicate setterAssignsValueToField(Setter s, Field f) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting values of auto-implemented (field like) private properties?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +138 to +144
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")
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")
)
exists(AssignableDefinition ad |
ad.getEnclosingCallable() = s and
ad.getTarget().getUnboundDeclaration() = f.getUnboundDeclaration() and
isOwnFieldAccess(ad.getTargetAccess()) and
accessToValue() = getAValuePreservingSource(ad.getSource())
)

Instead of relying on the syntactic construction AssignExpr, perhaps we can use AssignableDefinition. Note that the change also requires making the accessToValue() "public" (feel free to do so).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this one too

@michaelnebel michaelnebel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for incorporating the suggestions!
I have added a couple of follow comments.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants