Skip to content

Commit 5392132

Browse files
committed
C#: Include CompositeFormat.Parse as Format like method.
1 parent e23a2ea commit 5392132

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

csharp/ql/lib/semmle/code/csharp/frameworks/Format.qll

+28
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,31 @@ class FormatCall extends MethodCall {
289289
result = this.getArgument(this.getFirstArgument() + index)
290290
}
291291
}
292+
293+
/**
294+
* A method call to a method that parses a format string, for example a call
295+
* to `string.Format()`.
296+
*/
297+
abstract private class FormatStringParseCallImpl extends MethodCall {
298+
/**
299+
* Gets the expression used as the format string.
300+
*/
301+
abstract Expr getFormatExpr();
302+
}
303+
304+
final class FormatStringParseCall = FormatStringParseCallImpl;
305+
306+
private class OrdinaryFormatCall extends FormatStringParseCallImpl instanceof FormatCall {
307+
override Expr getFormatExpr() { result = FormatCall.super.getFormatExpr() }
308+
}
309+
310+
/**
311+
* A method call to `System.Text.CompositeFormat.Parse`.
312+
*/
313+
class ParseFormatStringCall extends FormatStringParseCallImpl {
314+
ParseFormatStringCall() {
315+
this.getTarget() = any(SystemTextCompositeFormatClass x).getParseMethod()
316+
}
317+
318+
override Expr getFormatExpr() { result = this.getArgument(0) }
319+
}

csharp/ql/src/API Abuse/FormatInvalid.ql

-16
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ import semmle.code.csharp.frameworks.system.Text
1515
import semmle.code.csharp.frameworks.Format
1616
import FormatFlow::PathGraph
1717

18-
abstract class FormatStringParseCall extends MethodCall {
19-
abstract Expr getFormatExpr();
20-
}
21-
22-
class OrdinaryFormatCall extends FormatStringParseCall instanceof FormatCall {
23-
override Expr getFormatExpr() { result = FormatCall.super.getFormatExpr() }
24-
}
25-
26-
class ParseFormatStringCall extends FormatStringParseCall {
27-
ParseFormatStringCall() {
28-
this.getTarget() = any(SystemTextCompositeFormatClass x).getParseMethod()
29-
}
30-
31-
override Expr getFormatExpr() { result = this.getArgument(0) }
32-
}
33-
3418
module FormatInvalidConfig implements DataFlow::ConfigSig {
3519
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof StringLiteral }
3620

csharp/ql/src/Security Features/CWE-134/UncontrolledFormatString.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module FormatStringConfig implements DataFlow::ConfigSig {
2020
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }
2121

2222
predicate isSink(DataFlow::Node sink) {
23-
sink.asExpr() = any(FormatCall call | call.hasInsertions()).getFormatExpr()
23+
sink.asExpr() = any(FormatStringParseCall call).getFormatExpr()
2424
}
2525
}
2626

0 commit comments

Comments
 (0)