Skip to content

Commit 6619be3

Browse files
authored
Merge pull request #15940 from michaelnebel/csharp/sourcesinktests
C#: Source- and sink tests.
2 parents ec071cd + 9696769 commit 6619be3

File tree

15 files changed

+750
-80
lines changed

15 files changed

+750
-80
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ Declaration interpretElement(
395395
* A callable where there exists a MaD sink model that applies to it.
396396
*/
397397
class SinkCallable extends Callable {
398-
SinkCallable() { SourceSinkInterpretationInput::sinkElement(this, _, _) }
398+
SinkCallable() { SourceSinkInterpretationInput::sinkElement(this, _, _, _) }
399399
}
400400

401401
/**
402402
* A callable where there exists a MaD source model that applies to it.
403403
*/
404404
class SourceCallable extends Callable {
405-
SourceCallable() { SourceSinkInterpretationInput::sourceElement(this, _, _) }
405+
SourceCallable() { SourceSinkInterpretationInput::sourceElement(this, _, _, _) }
406406
}
407407

408408
cached

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,20 @@ module SourceSinkInterpretationInput implements
160160

161161
class Element = Cs::Element;
162162

163-
predicate sourceElement(Element e, string output, string kind) {
163+
predicate sourceElement(Element e, string output, string kind, Public::Provenance provenance) {
164164
exists(
165165
string namespace, string type, boolean subtypes, string name, string signature, string ext
166166
|
167-
sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, _) and
167+
sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance) and
168168
e = interpretElement(namespace, type, subtypes, name, signature, ext)
169169
)
170170
}
171171

172-
predicate sinkElement(Element e, string input, string kind) {
172+
predicate sinkElement(Element e, string input, string kind, Public::Provenance provenance) {
173173
exists(
174174
string namespace, string type, boolean subtypes, string name, string signature, string ext
175175
|
176-
sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, _) and
176+
sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance) and
177177
e = interpretElement(namespace, type, subtypes, name, signature, ext)
178178
)
179179
}

csharp/ql/test/library-tests/dataflow/library/FlowSummaries.expected

Lines changed: 610 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import shared.FlowSummaries
22
import semmle.code.csharp.dataflow.internal.ExternalFlow
33

4-
private class IncludeAllSummarizedCallable extends IncludeSummarizedCallable {
5-
IncludeAllSummarizedCallable() { exists(this) }
4+
final private class NeutralCallableFinal = NeutralCallable;
5+
6+
class RelevantNeutralCallable extends NeutralCallableFinal {
7+
final string getCallableCsv() { result = asPartialNeutralModel(this) }
8+
}
9+
10+
class RelevantSourceCallable extends SourceCallable {
11+
string getCallableCsv() { result = asPartialModel(this) }
612
}
713

8-
private class IncludeNeutralSummarizedCallable extends RelevantNeutralCallable {
9-
/** Gets a string representing the callable in semi-colon separated format for use in flow summaries. */
10-
final override string getCallableCsv() { result = asPartialNeutralModel(this) }
14+
class RelevantSinkCallable extends SinkCallable {
15+
string getCallableCsv() { result = asPartialModel(this) }
1116
}
17+
18+
import TestSummaryOutput<IncludeSummarizedCallable>
19+
import TestNeutralOutput<RelevantNeutralCallable>
20+
import External::TestSourceSinkOutput<RelevantSourceCallable, RelevantSinkCallable>

csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
summary
21
| Dapper;CustomPropertyTypeMap;false;CustomPropertyTypeMap;(System.Type,System.Func<System.Type,System.String,System.Reflection.PropertyInfo>);;Argument[1];Argument[1].Parameter[delegate-self];value;hq-generated |
32
| Dapper;DynamicParameters;false;Output<T>;(T,System.Linq.Expressions.Expression<System.Func<T,System.Object>>,System.Nullable<System.Data.DbType>,System.Nullable<System.Int32>);;Argument[1];Argument[1].Parameter[delegate-self];value;hq-generated |
43
| Dapper;SqlMapper+GridReader;false;GridReader;(System.Data.IDbCommand,System.Data.Common.DbDataReader,Dapper.SqlMapper+Identity,System.Action<System.Object>,System.Object,System.Boolean,System.Threading.CancellationToken);;Argument[3];Argument[3].Parameter[delegate-self];value;hq-generated |
@@ -16125,4 +16124,3 @@ summary
1612516124
| System;ValueTuple<T1>;false;ToString;();;Argument[this];ReturnValue;taint;df-generated |
1612616125
| System;ValueTuple<T1>;false;ValueTuple;(T1);;Argument[0];Argument[this].Field[System.ValueTuple`1.Item1];value;manual |
1612716126
| System;ValueTuple<T1>;false;get_Item;(System.Int32);;Argument[this].Field[System.ValueTuple`1.Item1];ReturnValue;value;manual |
16128-
neutral

csharp/ql/test/library-tests/dataflow/library/FlowSummariesFiltered.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import shared.FlowSummaries
22
private import semmle.code.csharp.dataflow.internal.ExternalFlow
33

44
class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable {
5-
IncludeFilteredSummarizedCallable() { exists(this) }
6-
75
/**
86
* Holds if flow is propagated between `input` and `output` and
97
* if there is no summary for a callable in a `base` class or interface
@@ -12,11 +10,13 @@ class IncludeFilteredSummarizedCallable extends IncludeSummarizedCallable {
1210
override predicate relevantSummary(
1311
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
1412
) {
15-
super.propagatesFlow(input, output, preservesValue) and
13+
this.propagatesFlow(input, output, preservesValue) and
1614
not exists(IncludeSummarizedCallable rsc |
1715
isBaseCallableOrPrototype(rsc) and
1816
rsc.propagatesFlow(input, output, preservesValue) and
1917
this.(UnboundCallable).overridesOrImplementsUnbound(rsc)
2018
)
2119
}
2220
}
21+
22+
import TestSummaryOutput<IncludeFilteredSummarizedCallable>

csharp/ql/test/library-tests/frameworks/EntityFramework/FlowSummaries.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ summary
170170
| System.Data.Entity;DbContext;false;SaveChangesAsync;(System.Threading.CancellationToken);;Argument[this].Property[EFTests.MyContext.Persons].Element.Property[EFTests.Person.Id];SyntheticGlobal[EFTests.MyContext.Persons#ReturnValue.Element.Property[EFTests.Person.Id]];value;manual |
171171
| System.Data.Entity;DbContext;false;SaveChangesAsync;(System.Threading.CancellationToken);;Argument[this].Property[EFTests.MyContext.Persons].Element.Property[EFTests.Person.Name];SyntheticGlobal[EFTests.MyContext.PersonAddresses#ReturnValue.Element.Property[EFTests.PersonAddressMap.Person].Property[EFTests.Person.Name]];value;manual |
172172
| System.Data.Entity;DbContext;false;SaveChangesAsync;(System.Threading.CancellationToken);;Argument[this].Property[EFTests.MyContext.Persons].Element.Property[EFTests.Person.Name];SyntheticGlobal[EFTests.MyContext.Persons#ReturnValue.Element.Property[EFTests.Person.Name]];value;manual |
173-
neutral
174173
sourceNode
175174
sinkNode
176175
| EntityFrameworkCore.cs:72:36:72:40 | "sql" | sql-injection |

csharp/ql/test/library-tests/frameworks/EntityFramework/FlowSummaries.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import shared.FlowSummaries
33
import semmle.code.csharp.frameworks.EntityFramework::EntityFramework
44
import semmle.code.csharp.dataflow.internal.ExternalFlow as ExternalFlow
55

6-
private class IncludeEFSummarizedCallable extends IncludeSummarizedCallable instanceof EFSummarizedCallable
7-
{ }
6+
class RelevantSummarizedCallable extends IncludeSummarizedCallable instanceof EFSummarizedCallable {
7+
}
8+
9+
import TestSummaryOutput<RelevantSummarizedCallable>
810

911
query predicate sourceNode(DataFlow::Node node, string kind) {
1012
ExternalFlow::sourceNode(node, kind)
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private
22
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Public
3-
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl::Private::TestOutput
43
private import semmle.code.csharp.dataflow.internal.ExternalFlow
54

6-
abstract class IncludeSummarizedCallable extends RelevantSummarizedCallable {
5+
final private class SummarizedCallableImplFinal = SummarizedCallableImpl;
6+
7+
class IncludeSummarizedCallable extends SummarizedCallableImplFinal {
78
IncludeSummarizedCallable() {
89
[this.(Modifiable), this.(Accessor).getDeclaration()].isEffectivelyPublic()
910
}
1011

1112
/** Gets a string representing the callable in semi-colon separated format for use in flow summaries. */
12-
final override string getCallableCsv() { result = asPartialModel(this) }
13+
final string getCallableCsv() { result = asPartialModel(this) }
14+
15+
predicate relevantSummary(
16+
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
17+
) {
18+
this.propagatesFlow(input, output, preservesValue)
19+
}
1320
}

go/ql/lib/semmle/go/dataflow/ExternalFlow.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ predicate hasExternalSpecification(Function f) {
299299
f = any(SummarizedCallable sc).asFunction()
300300
or
301301
exists(SourceSinkInterpretationInput::SourceOrSinkElement e | f = e.asEntity() |
302-
SourceSinkInterpretationInput::sourceElement(e, _, _) or
303-
SourceSinkInterpretationInput::sinkElement(e, _, _)
302+
SourceSinkInterpretationInput::sourceElement(e, _, _, _) or
303+
SourceSinkInterpretationInput::sinkElement(e, _, _, _)
304304
)
305305
}
306306

go/ql/lib/semmle/go/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@ module SourceSinkInterpretationInput implements
103103
* Holds if an external source specification exists for `e` with output specification
104104
* `output`, kind `kind`, and provenance `provenance`.
105105
*/
106-
predicate sourceElement(SourceOrSinkElement e, string output, string kind) {
106+
predicate sourceElement(
107+
SourceOrSinkElement e, string output, string kind, Public::Provenance provenance
108+
) {
107109
exists(
108110
string package, string type, boolean subtypes, string name, string signature, string ext
109111
|
110-
sourceModel(package, type, subtypes, name, signature, ext, output, kind, _) and
112+
sourceModel(package, type, subtypes, name, signature, ext, output, kind, provenance) and
111113
e = interpretElement(package, type, subtypes, name, signature, ext)
112114
)
113115
}
@@ -116,11 +118,13 @@ module SourceSinkInterpretationInput implements
116118
* Holds if an external sink specification exists for `e` with input specification
117119
* `input`, kind `kind` and provenance `provenance`.
118120
*/
119-
predicate sinkElement(SourceOrSinkElement e, string input, string kind) {
121+
predicate sinkElement(
122+
SourceOrSinkElement e, string input, string kind, Public::Provenance provenance
123+
) {
120124
exists(
121125
string package, string type, boolean subtypes, string name, string signature, string ext
122126
|
123-
sinkModel(package, type, subtypes, name, signature, ext, input, kind, _) and
127+
sinkModel(package, type, subtypes, name, signature, ext, input, kind, provenance) and
124128
e = interpretElement(package, type, subtypes, name, signature, ext)
125129
)
126130
}

java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ module SourceSinkInterpretationInput implements
192192

193193
class Element = J::Element;
194194

195-
predicate sourceElement(Element e, string output, string kind) {
195+
predicate sourceElement(Element e, string output, string kind, Public::Provenance provenance) {
196196
exists(
197197
string namespace, string type, boolean subtypes, string name, string signature, string ext,
198198
SourceOrSinkElement baseSource, string originalOutput
199199
|
200-
sourceModel(namespace, type, subtypes, name, signature, ext, originalOutput, kind, _) and
200+
sourceModel(namespace, type, subtypes, name, signature, ext, originalOutput, kind, provenance) and
201201
baseSource = interpretElement(namespace, type, subtypes, name, signature, ext) and
202202
(
203203
e = baseSource and output = originalOutput
@@ -207,12 +207,12 @@ module SourceSinkInterpretationInput implements
207207
)
208208
}
209209

210-
predicate sinkElement(Element e, string input, string kind) {
210+
predicate sinkElement(Element e, string input, string kind, Public::Provenance provenance) {
211211
exists(
212212
string namespace, string type, boolean subtypes, string name, string signature, string ext,
213213
SourceOrSinkElement baseSink, string originalInput
214214
|
215-
sinkModel(namespace, type, subtypes, name, signature, ext, originalInput, kind, _) and
215+
sinkModel(namespace, type, subtypes, name, signature, ext, originalInput, kind, provenance) and
216216
baseSink = interpretElement(namespace, type, subtypes, name, signature, ext) and
217217
(
218218
e = baseSink and originalInput = input

java/ql/src/utils/modeleditor/FrameworkModeEndpointsQuery.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private import ModelEditor
88
* A class of effectively public callables from source code.
99
*/
1010
class PublicEndpointFromSource extends Endpoint, ModelApi {
11-
override predicate isSource() { SourceSinkInterpretationInput::sourceElement(this, _, _) }
11+
override predicate isSource() { SourceSinkInterpretationInput::sourceElement(this, _, _, _) }
1212

13-
override predicate isSink() { SourceSinkInterpretationInput::sinkElement(this, _, _) }
13+
override predicate isSink() { SourceSinkInterpretationInput::sinkElement(this, _, _, _) }
1414
}

0 commit comments

Comments
 (0)