Skip to content

Commit 76aed36

Browse files
committed
C#: Re-factor the Api SourceNode implementation to be based on abstract classes and only use it where relevant sources are added.
1 parent 1c56865 commit 76aed36

File tree

4 files changed

+23
-71
lines changed

4 files changed

+23
-71
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/ConditionalBypassQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private import semmle.code.csharp.security.SensitiveActions
1515
/**
1616
* A data flow source for user-controlled bypass of sensitive method.
1717
*/
18-
abstract class Source extends DataFlow::Node { }
18+
abstract class Source extends ApiSourceNode { }
1919

2020
/**
2121
* A data flow sink for user-controlled bypass of sensitive method.
Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,14 @@
11
/** Provides classes representing various flow sources for data flow / taint tracking. */
22

3-
private import semmle.code.csharp.dataflow.internal.ExternalFlow
3+
private import FlowSources as FlowSources
44

5-
/**
6-
* A data flow source node.
7-
*/
8-
abstract class SourceNode extends DataFlow::Node { }
5+
final class SourceNode = FlowSources::SourceNode;
96

107
/**
11-
* Module that adds all sources to `SourceNode`, excluding source for cryptography based
12-
* queries, and queries where sources are not succifiently explicit or mainly hardcoded constants.
8+
* Module that adds all API like sources to `SourceNode`, excluding some sources for cryptography based
9+
* queries, and queries where sources are not succifiently defined (eg. using broad method name matching).
1310
*/
14-
private module AllSources {
15-
private import FlowSources as FlowSources
16-
private import semmle.code.csharp.security.cryptography.HardcodedSymmetricEncryptionKey
17-
private import semmle.code.csharp.security.dataflow.CleartextStorageQuery as CleartextStorageQuery
18-
private import semmle.code.csharp.security.dataflow.CodeInjectionQuery as CodeInjectionQuery
11+
private module AllApiSources {
1912
private import semmle.code.csharp.security.dataflow.ConditionalBypassQuery as ConditionalBypassQuery
20-
private import semmle.code.csharp.security.dataflow.ExposureOfPrivateInformationQuery as ExposureOfPrivateInformationQuery
21-
private import semmle.code.csharp.security.dataflow.HardcodedCredentialsQuery as HardcodedCredentialsQuery
22-
private import semmle.code.csharp.security.dataflow.LDAPInjectionQuery as LdapInjectionQuery
23-
private import semmle.code.csharp.security.dataflow.LogForgingQuery as LogForgingQuery
24-
private import semmle.code.csharp.security.dataflow.MissingXMLValidationQuery as MissingXmlValidationQuery
25-
private import semmle.code.csharp.security.dataflow.ReDoSQuery as ReDosQuery
26-
private import semmle.code.csharp.security.dataflow.RegexInjectionQuery as RegexInjectionQuery
27-
private import semmle.code.csharp.security.dataflow.ResourceInjectionQuery as ResourceInjectionQuery
28-
private import semmle.code.csharp.security.dataflow.SqlInjectionQuery as SqlInjectionQuery
29-
private import semmle.code.csharp.security.dataflow.TaintedPathQuery as TaintedPathQuery
30-
private import semmle.code.csharp.security.dataflow.UnsafeDeserializationQuery as UnsafeDeserializationQuery
31-
private import semmle.code.csharp.security.dataflow.UrlRedirectQuery as UrlRedirectQuery
32-
private import semmle.code.csharp.security.dataflow.XMLEntityInjectionQuery as XmlEntityInjectionQuery
33-
private import semmle.code.csharp.security.dataflow.XPathInjectionQuery as XpathInjectionQuery
3413
private import semmle.code.csharp.security.dataflow.ZipSlipQuery as ZipSlipQuery
35-
36-
private class FlowSourcesSources extends SourceNode instanceof FlowSources::SourceNode { }
37-
38-
private class CodeInjectionSource extends SourceNode instanceof CodeInjectionQuery::Source { }
39-
40-
private class ConditionalBypassSource extends SourceNode instanceof ConditionalBypassQuery::Source
41-
{ }
42-
43-
private class LdapInjectionSource extends SourceNode instanceof LdapInjectionQuery::Source { }
44-
45-
private class LogForgingSource extends SourceNode instanceof LogForgingQuery::Source { }
46-
47-
private class MissingXmlValidationSource extends SourceNode instanceof MissingXmlValidationQuery::Source
48-
{ }
49-
50-
private class ReDosSource extends SourceNode instanceof ReDosQuery::Source { }
51-
52-
private class RegexInjectionSource extends SourceNode instanceof RegexInjectionQuery::Source { }
53-
54-
private class ResourceInjectionSource extends SourceNode instanceof ResourceInjectionQuery::Source
55-
{ }
56-
57-
private class SqlInjectionSource extends SourceNode instanceof SqlInjectionQuery::Source { }
58-
59-
private class TaintedPathSource extends SourceNode instanceof TaintedPathQuery::Source { }
60-
61-
private class UnsafeDeserializationSource extends SourceNode instanceof UnsafeDeserializationQuery::Source
62-
{ }
63-
64-
private class UrlRedirectSource extends SourceNode instanceof UrlRedirectQuery::Source { }
65-
66-
private class XmlEntityInjectionSource extends SourceNode instanceof XmlEntityInjectionQuery::Source
67-
{ }
68-
69-
private class XpathInjectionSource extends SourceNode instanceof XpathInjectionQuery::Source { }
70-
71-
/**
72-
* Add all models as data sources.
73-
*/
74-
private class SourceNodeExternal extends SourceNode {
75-
SourceNodeExternal() { sourceNode(this, _) }
76-
}
7714
}

csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/FlowSources.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ class ThreatModelFlowSource extends DataFlow::Node {
3232
)
3333
}
3434
}
35+
36+
/**
37+
* A data flow source node for an API, which should be considered
38+
* supported from a modeling perspective.
39+
*/
40+
abstract class ApiSourceNode extends DataFlow::Node { }
41+
42+
private class AddSourceNodes extends ApiSourceNode instanceof SourceNode { }
43+
44+
/**
45+
* Add all models as data sources.
46+
*/
47+
private class ApiSourceNodeExternal extends ApiSourceNode {
48+
ApiSourceNodeExternal() { sourceNode(this, _) }
49+
}

csharp/ql/src/Telemetry/ExternalApi.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private import semmle.code.csharp.dataflow.internal.DataFlowDispatch as DataFlow
88
private import semmle.code.csharp.dataflow.internal.ExternalFlow
99
private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
1010
private import semmle.code.csharp.dataflow.internal.TaintTrackingPrivate
11-
private import semmle.code.csharp.security.dataflow.flowsources.ApiSources
11+
private import semmle.code.csharp.security.dataflow.flowsources.ApiSources as ApiSources
1212
private import semmle.code.csharp.security.dataflow.flowsinks.ApiSinks as ApiSinks
1313
private import TestLibrary
1414

@@ -85,7 +85,7 @@ class ExternalApi extends Callable {
8585

8686
/** Holds if this API is a known source. */
8787
pragma[nomagic]
88-
predicate isSource() { this.getAnOutput() instanceof SourceNode }
88+
predicate isSource() { this.getAnOutput() instanceof ApiSources::SourceNode }
8989

9090
/** Holds if this API is a known sink. */
9191
pragma[nomagic]

0 commit comments

Comments
 (0)