C#: Add ContentDataFlow test

This commit is contained in:
Tom Hvitved
2022-05-13 11:30:28 +02:00
parent 2b2ac06128
commit 0a52420581
3 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
using System;
public class ContentFlow
{
public class A
{
public A FieldA;
public B FieldB;
}
public class B
{
public A FieldA;
public B FieldB;
}
public void M(A a, B b)
{
var a1 = new A();
Sink(a1.FieldA.FieldB);
a.FieldA.FieldB = new B();
Sink(a);
var a2 = new A();
b.FieldB.FieldA = a2.FieldB.FieldA;
Sink(b);
}
public static void Sink<T>(T t) { }
}

View File

@@ -0,0 +1,3 @@
| ContentFlow.cs:18:18:18:24 | object creation of type A | field FieldB.field FieldA. | ContentFlow.cs:19:14:19:29 | access to field FieldB | | true |
| ContentFlow.cs:21:27:21:33 | object creation of type B | | ContentFlow.cs:22:14:22:14 | access to parameter a | field FieldA.field FieldB. | true |
| ContentFlow.cs:24:18:24:24 | object creation of type A | field FieldA.field FieldB. | ContentFlow.cs:26:14:26:14 | access to parameter b | field FieldB.field FieldA. | true |

View File

@@ -0,0 +1,23 @@
import csharp
import semmle.code.csharp.dataflow.internal.ContentDataFlow
class Conf extends ContentDataFlow::Configuration {
Conf() { this = "ContentFlowConf" }
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof ObjectCreation }
override predicate isSink(DataFlow::Node sink) {
exists(MethodCall mc |
mc.getTarget().hasUndecoratedName("Sink") and
mc.getAnArgument() = sink.asExpr()
)
}
override int accessPathLimit() { result = 2 }
}
from
Conf conf, ContentDataFlow::Node source, ContentDataFlow::AccessPath sourceAp,
ContentDataFlow::Node sink, ContentDataFlow::AccessPath sinkAp, boolean preservesValue
where conf.hasFlow(source, sourceAp, sink, sinkAp, preservesValue)
select source, sourceAp, sink, sinkAp, preservesValue