Skip to content

Commit 10a6089

Browse files
authored
Merge pull request github#6148 from tamasvajk/feature/try-csv-source-models
C#: Start using CSV based flow models
2 parents 374859e + 0946ae2 commit 10a6089

File tree

24 files changed

+912
-790
lines changed

24 files changed

+912
-790
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package,sink,source,summary,sink:html,sink:xss,source:local,summary:taint
2+
System,5,3,6,4,1,3,6
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
C# framework & library support
2+
================================
3+
4+
.. csv-table::
5+
:header-rows: 1
6+
:class: fullWidthTable
7+
:widths: auto
8+
9+
Framework / library,Package,Flow sources,Taint & value steps,Sinks (total),`CWE-079` :sub:`Cross-site scripting`
10+
System,"``System.*``, ``System``",3,6,5,5
11+
Totals,,3,6,5,5
12+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CWE,Sink identifier,Label
2+
CWE-079,html xss,Cross-site scripting
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Framework name,URL,Namespace prefixes
2+
System,,System.* System
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Framework coverage
3+
* @description The number of API endpoints covered by CSV models sorted by
4+
* package and source-, sink-, and summary-kind.
5+
* @kind table
6+
* @id cs/meta/framework-coverage
7+
*/
8+
9+
import csharp
10+
import semmle.code.csharp.dataflow.ExternalFlow
11+
12+
from string namespace, int pkgs, string kind, string part, int n
13+
where modelCoverage(namespace, pkgs, kind, part, n)
14+
select namespace, pkgs, kind, part, n

csharp/ql/src/semmle/code/csharp/dataflow/ExternalFlow.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ private import internal.FlowSummaryImplSpecific
8484
* ensuring that they are visible to the taint tracking / data flow library.
8585
*/
8686
private module Frameworks {
87-
// TODO
87+
private import semmle.code.csharp.security.dataflow.flowsources.Local
88+
private import semmle.code.csharp.security.dataflow.flowsinks.Html
89+
private import semmle.code.csharp.frameworks.System
90+
private import semmle.code.csharp.security.dataflow.XSS
8891
}
8992

9093
/**

csharp/ql/src/semmle/code/csharp/dataflow/LibraryTypeDataFlow.qll

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -499,33 +499,6 @@ private module FrameworkDataFlowAdaptor {
499499
}
500500
}
501501

502-
/** Data flow for `System.Int32`. */
503-
class SystemInt32Flow extends LibraryTypeDataFlow, SystemInt32Struct {
504-
override predicate callableFlow(
505-
CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable c,
506-
boolean preservesValue
507-
) {
508-
methodFlow(source, sink, c) and
509-
preservesValue = false
510-
}
511-
512-
private predicate methodFlow(
513-
CallableFlowSource source, CallableFlowSink sink, SourceDeclarationMethod m
514-
) {
515-
m = getParseMethod() and
516-
source = TCallableFlowSourceArg(0) and
517-
sink = TCallableFlowSinkReturn()
518-
or
519-
m = getTryParseMethod() and
520-
source = TCallableFlowSourceArg(0) and
521-
(
522-
sink = TCallableFlowSinkReturn()
523-
or
524-
sink = TCallableFlowSinkArg(any(int i | m.getParameter(i).isOutOrRef()))
525-
)
526-
}
527-
}
528-
529502
/** Data flow for `System.Boolean`. */
530503
class SystemBooleanFlow extends LibraryTypeDataFlow, SystemBooleanStruct {
531504
override predicate callableFlow(

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import DataFlowPublic
66
private import DataFlowPrivate
77
private import FlowSummaryImpl as FlowSummaryImpl
88
private import semmle.code.csharp.dataflow.FlowSummary
9+
private import semmle.code.csharp.dataflow.ExternalFlow
910
private import semmle.code.csharp.dispatch.Dispatch
1011
private import semmle.code.csharp.frameworks.system.Collections
1112
private import semmle.code.csharp.frameworks.system.collections.Generic
@@ -14,6 +15,8 @@ private predicate summarizedCallable(DataFlowCallable c) {
1415
c instanceof SummarizedCallable
1516
or
1617
FlowSummaryImpl::Private::summaryReturnNode(_, TJumpReturnKind(c, _))
18+
or
19+
c = interpretElement(_, _, _, _, _, _)
1720
}
1821

1922
/**

csharp/ql/src/semmle/code/csharp/frameworks/System.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import csharp
44
private import system.Reflection
5+
private import semmle.code.csharp.dataflow.ExternalFlow
56

67
/** The `System` namespace. */
78
class SystemNamespace extends Namespace {
@@ -200,6 +201,28 @@ class SystemInt32Struct extends IntType {
200201
}
201202
}
202203

204+
/** Data flow for `System.Int32`. */
205+
private class SystemInt32FlowModelCsv extends SummaryModelCsv {
206+
override predicate row(string row) {
207+
row =
208+
[
209+
"System;Int32;false;Parse;(System.String);;Argument[0];ReturnValue;taint",
210+
"System;Int32;false;Parse;(System.String,System.IFormatProvider);;Argument[0];ReturnValue;taint",
211+
"System;Int32;false;Parse;(System.String,System.Globalization.NumberStyles);;Argument[0];ReturnValue;taint",
212+
"System;Int32;false;Parse;(System.String,System.Globalization.NumberStyles,System.IFormatProvider);;Argument[0];ReturnValue;taint",
213+
"System;Int32;false;Parse;(System.ReadOnlySpan<System.Char>,System.Globalization.NumberStyles,System.IFormatProvider);;Element of Argument[0];ReturnValue;taint",
214+
"System;Int32;false;TryParse;(System.String,System.Int32);;Argument[0];ReturnValue;taint",
215+
"System;Int32;false;TryParse;(System.String,System.Int32);;Argument[0];Argument[1];taint",
216+
"System;Int32;false;TryParse;(System.ReadOnlySpan<System.Char>,System.Int32);;Element of Argument[0];ReturnValue;taint",
217+
"System;Int32;false;TryParse;(System.ReadOnlySpan<System.Char>,System.Int32);;Element of Argument[0];Argument[1];taint",
218+
"System;Int32;false;TryParse;(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Int32);;Argument[0];ReturnValue;taint",
219+
"System;Int32;false;TryParse;(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Int32);;Argument[0];Argument[3];taint",
220+
"System;Int32;false;TryParse;(System.ReadOnlySpan<System.Char>,System.Globalization.NumberStyles,System.IFormatProvider,System.Int32);;Element of Argument[0];ReturnValue;taint",
221+
"System;Int32;false;TryParse;(System.ReadOnlySpan<System.Char>,System.Globalization.NumberStyles,System.IFormatProvider,System.Int32);;Element of Argument[0];Argument[3];taint"
222+
]
223+
}
224+
}
225+
203226
/** The `System.InvalidCastException` class. */
204227
class SystemInvalidCastExceptionClass extends SystemClass {
205228
SystemInvalidCastExceptionClass() { this.hasName("InvalidCastException") }

csharp/ql/src/semmle/code/csharp/security/dataflow/XSS.qll

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module XSS {
1616
import semmle.code.csharp.security.dataflow.flowsources.Remote
1717
private import semmle.code.csharp.dataflow.DataFlow2
1818
private import semmle.code.csharp.dataflow.TaintTracking2
19+
private import semmle.code.csharp.dataflow.ExternalFlow
1920

2021
/**
2122
* Holds if there is tainted flow from `source` to `sink` that may lead to a
@@ -119,6 +120,10 @@ module XSS {
119120
string explanation() { none() }
120121
}
121122

123+
private class ExternalXssSink extends Sink {
124+
ExternalXssSink() { sinkNode(this, "xss") }
125+
}
126+
122127
/**
123128
* A data flow source for cross-site scripting (XSS) vulnerabilities.
124129
*/
@@ -406,12 +411,9 @@ module XSS {
406411
/**
407412
* An expression passed as the `content` argument to the constructor of `StringContent`.
408413
*/
409-
private class StringContent extends Sink {
410-
StringContent() {
411-
this.getExpr() =
412-
any(ObjectCreation oc |
413-
oc.getTarget().getDeclaringType().hasQualifiedName("System.Net.Http", "StringContent")
414-
).getArgumentForName("content")
414+
private class StringContentSinkModelCsv extends SinkModelCsv {
415+
override predicate row(string row) {
416+
row = ["System.Net.Http;StringContent;false;StringContent;;;Argument[0];xss"]
415417
}
416418
}
417419
}

0 commit comments

Comments
 (0)