Skip to content

Commit 8cb0f5f

Browse files
authored
Merge pull request #21140 from owen-mc/csharp/mad-barriers
C#: Allow MaD barriers and barrier guards, and convert some existing ones
2 parents 63f78e7 + 130f8f1 commit 8cb0f5f

File tree

12 files changed

+66
-37
lines changed

12 files changed

+66
-37
lines changed

csharp/ql/lib/ext/System.Web.model.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
extensions:
2+
- addsTo:
3+
pack: codeql/csharp-all
4+
extensible: barrierModel
5+
data:
6+
# The RawUrl property is considered to be safe for URL redirects
7+
- ["System.Web", "HttpRequest", False, "get_RawUrl", "()", "", "ReturnValue", "url-redirection", "manual"]
28
- addsTo:
39
pack: codeql/csharp-all
410
extensible: sinkModel

csharp/ql/lib/ext/System.model.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ extensions:
1111
- ["System", "Environment", False, "get_CommandLine", "()", "", "ReturnValue", "commandargs", "manual"]
1212
- ["System", "Environment", False, "GetEnvironmentVariable", "", "", "ReturnValue", "environment", "manual"]
1313
- ["System", "Environment", False, "GetEnvironmentVariables", "", "", "ReturnValue", "environment", "manual"]
14+
- addsTo:
15+
pack: codeql/csharp-all
16+
extensible: barrierGuardModel
17+
data:
18+
- ["System", "Uri", False, "get_IsAbsoluteUri", "()", "", "Argument[this]", "false", "url-redirection", "manual"]
1419
- addsTo:
1520
pack: codeql/csharp-all
1621
extensible: summaryModel

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import csharp
66
private import semmle.code.csharp.security.dataflow.flowsources.Remote
7+
private import semmle.code.csharp.dataflow.internal.ExternalFlow
78
private import semmle.code.csharp.frameworks.system.Web
89
private import semmle.code.csharp.security.SensitiveActions
910
private import semmle.code.csharp.security.dataflow.flowsinks.ExternalLocationSink
@@ -62,3 +63,5 @@ class ProtectSanitizer extends Sanitizer {
6263
* An external location sink.
6364
*/
6465
class ExternalSink extends Sink instanceof ExternalLocationSink { }
66+
67+
private class ExternalSanitizer extends Sanitizer instanceof ExternalLocationSanitizer { }

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ class RoslynCSharpScriptSink extends Sink {
9595
}
9696
}
9797

98-
/** Code injection sinks defined through CSV models. */
98+
/** A code injection sink defined through Models as Data. */
9999
private class ExternalCodeInjectionExprSink extends Sink {
100100
ExternalCodeInjectionExprSink() { sinkNode(this, "code-injection") }
101101
}
102+
103+
/** A sanitizer for code injection defined through Models as Data. */
104+
private class ExternalCodeInjectionSanitizer extends Sanitizer {
105+
ExternalCodeInjectionSanitizer() { barrierNode(this, "code-injection") }
106+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ deprecated class RemoteSource extends DataFlow::Node instanceof RemoteFlowSource
6161
/** A source supported by the current threat model. */
6262
class ThreatModelSource extends Source instanceof ActiveThreatModelSource { }
6363

64-
/** Command Injection sinks defined through Models as Data. */
64+
/** A Command Injection sink defined through Models as Data. */
6565
private class ExternalCommandInjectionExprSink extends Sink {
6666
ExternalCommandInjectionExprSink() { sinkNode(this, "command-injection") }
6767
}
6868

69+
/** A sanitizer for command injection defined through Models as Data. */
70+
private class ExternalCommandInjectionSanitizer extends Sanitizer {
71+
ExternalCommandInjectionSanitizer() { barrierNode(this, "command-injection") }
72+
}
73+
6974
/**
7075
* A sink in `System.Diagnostic.Process` or its related classes.
7176
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ private class PrivateDataSource extends Source {
4646
}
4747

4848
private class ExternalLocation extends Sink instanceof ExternalLocationSink { }
49+
50+
private class ExternalSanitizer extends Sanitizer instanceof ExternalLocationSanitizer { }

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ deprecated class RemoteSource extends DataFlow::Node instanceof RemoteFlowSource
6464
/** A source supported by the current threat model. */
6565
class ThreatModelSource extends Source instanceof ActiveThreatModelSource { }
6666

67-
/** LDAP sinks defined through Models as Data. */
67+
/** An LDAP sink defined through Models as Data. */
6868
private class ExternalLdapExprSink extends Sink {
6969
ExternalLdapExprSink() { sinkNode(this, "ldap-injection") }
7070
}
7171

72+
/** A sanitizer for LDAP injection defined through Models as Data. */
73+
private class ExternalLdapInjectionSanitizer extends Sanitizer {
74+
ExternalLdapInjectionSanitizer() { barrierNode(this, "ldap-injection") }
75+
}
76+
7277
/**
7378
* An argument that sets the `Path` property of a `DirectoryEntry` object that is a sink for LDAP
7479
* injection.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ private class LogForgingLogMessageSink extends Sink, LogMessageSink { }
6161
*/
6262
private class LogForgingTraceMessageSink extends Sink, TraceMessageSink { }
6363

64-
/** Log Forging sinks defined through Models as Data. */
64+
/** A Log Forging sink defined through Models as Data. */
6565
private class ExternalLoggingExprSink extends Sink {
6666
ExternalLoggingExprSink() { sinkNode(this, "log-injection") }
6767
}
6868

69+
/** A sanitizer for log forging defined through Models as Data. */
70+
private class ExternalLogForgingSanitizer extends Sanitizer {
71+
ExternalLogForgingSanitizer() { barrierNode(this, "log-injection") }
72+
}
73+
6974
/**
7075
* A call to String replace or remove that is considered to sanitize replaced string.
7176
*/

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ class SqlInjectionExprSink extends Sink {
7474
SqlInjectionExprSink() { exists(SqlExpr s | this.getExpr() = s.getSql()) }
7575
}
7676

77-
/** SQL sinks defined through CSV models. */
77+
/** An SQL sink defined through CSV models. */
7878
private class ExternalSqlInjectionExprSink extends Sink {
7979
ExternalSqlInjectionExprSink() { sinkNode(this, "sql-injection") }
8080
}
8181

82+
/** A sanitizer for SQL injection defined through Models as Data. */
83+
private class ExternalSqlInjectionSanitizer extends Sanitizer {
84+
ExternalSqlInjectionSanitizer() { barrierNode(this, "sql-injection") }
85+
}
86+
8287
private class SimpleTypeSanitizer extends Sanitizer, SimpleTypeSanitizedExpr { }
8388

8489
private class GuidSanitizer extends Sanitizer, GuidSanitizedExpr { }

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ deprecated class RemoteSource extends DataFlow::Node instanceof RemoteFlowSource
5656
/** A source supported by the current threat model. */
5757
class ThreatModelSource extends Source instanceof ActiveThreatModelSource { }
5858

59-
/** URL Redirection sinks defined through Models as Data. */
59+
/** A URL Redirection sink defined through Models as Data. */
6060
private class ExternalUrlRedirectExprSink extends Sink {
6161
ExternalUrlRedirectExprSink() { sinkNode(this, "url-redirection") }
6262
}
6363

64+
/** A sanitizer for URL redirection defined through Models as Data. */
65+
private class ExternalUrlRedirectSanitizer extends Sanitizer {
66+
ExternalUrlRedirectSanitizer() { barrierNode(this, "url-redirection") }
67+
}
68+
6469
/**
6570
* A URL argument to a call to `HttpResponse.Redirect()` or `Controller.Redirect()`, that is a
6671
* sink for URL redirects.
@@ -160,27 +165,6 @@ class ContainsUrlSanitizer extends Sanitizer {
160165
}
161166
}
162167

163-
/**
164-
* A check that the URL is relative, and therefore safe for URL redirects.
165-
*/
166-
private predicate isRelativeUrlSanitizer(Guard guard, Expr e, GuardValue v) {
167-
guard =
168-
any(PropertyAccess access |
169-
access.getProperty().hasFullyQualifiedName("System", "Uri", "IsAbsoluteUri") and
170-
e = access.getQualifier() and
171-
v.asBooleanValue() = false
172-
)
173-
}
174-
175-
/**
176-
* A check that the URL is relative, and therefore safe for URL redirects.
177-
*/
178-
class RelativeUrlSanitizer extends Sanitizer {
179-
RelativeUrlSanitizer() {
180-
this = DataFlow::BarrierGuard<isRelativeUrlSanitizer/3>::getABarrierNode()
181-
}
182-
}
183-
184168
/**
185169
* A comparison on the `Host` property of a url, that is a sanitizer for URL redirects.
186170
* E.g. `url.Host == "example.org"`
@@ -205,16 +189,6 @@ class HostComparisonSanitizer extends Sanitizer {
205189
}
206190
}
207191

208-
/**
209-
* A call to the getter of the RawUrl property, whose value is considered to be safe for URL
210-
* redirects.
211-
*/
212-
class RawUrlSanitizer extends Sanitizer {
213-
RawUrlSanitizer() {
214-
this.getExpr() = any(SystemWebHttpRequestClass r).getRawUrlProperty().getGetter().getACall()
215-
}
216-
}
217-
218192
/**
219193
* A string concatenation expression, where the left hand side contains the character "?".
220194
*

0 commit comments

Comments
 (0)