1212
1313import csharp
1414import semmle.code.csharp.frameworks.Format
15- import DataFlow :: PathGraph
15+ import FormatInvalid :: PathGraph
1616
17- private class FormatConfiguration extends DataFlow:: Configuration {
18- FormatConfiguration ( ) { this = "format" }
17+ module FormatInvalidConfig implements DataFlow:: ConfigSig {
18+ predicate isSource ( DataFlow :: Node n ) { n . asExpr ( ) instanceof StringLiteral }
1919
20- override predicate isSource ( DataFlow:: Node n ) { n .asExpr ( ) instanceof StringLiteral }
21-
22- override predicate isSink ( DataFlow:: Node n ) {
23- exists ( FormatCall c | n .asExpr ( ) = c .getFormatExpr ( ) )
24- }
20+ predicate isSink ( DataFlow:: Node n ) { exists ( FormatCall c | n .asExpr ( ) = c .getFormatExpr ( ) ) }
2521}
2622
23+ module FormatInvalid = DataFlow:: Global< FormatInvalidConfig > ;
24+
2725private predicate invalidFormatString (
28- InvalidFormatString src , DataFlow :: PathNode source , DataFlow :: PathNode sink , string msg ,
26+ InvalidFormatString src , FormatInvalid :: PathNode source , FormatInvalid :: PathNode sink , string msg ,
2927 FormatCall call , string callString
3028) {
3129 source .getNode ( ) .asExpr ( ) = src and
3230 sink .getNode ( ) .asExpr ( ) = call .getFormatExpr ( ) and
33- any ( FormatConfiguration conf ) . hasFlowPath ( source , sink ) and
31+ FormatInvalid :: flowPath ( source , sink ) and
3432 call .hasInsertions ( ) and
3533 msg = "Invalid format string used in $@ formatting call." and
3634 callString = "this"
3735}
3836
3937private predicate unusedArgument (
40- FormatCall call , DataFlow :: PathNode source , DataFlow :: PathNode sink , string msg ,
38+ FormatCall call , FormatInvalid :: PathNode source , FormatInvalid :: PathNode sink , string msg ,
4139 ValidFormatString src , string srcString , Expr unusedExpr , string unusedString
4240) {
4341 exists ( int unused |
4442 source .getNode ( ) .asExpr ( ) = src and
4543 sink .getNode ( ) .asExpr ( ) = call .getFormatExpr ( ) and
46- any ( FormatConfiguration conf ) . hasFlowPath ( source , sink ) and
44+ FormatInvalid :: flowPath ( source , sink ) and
4745 unused = call .getASuppliedArgument ( ) and
4846 not unused = src .getAnInsert ( ) and
4947 not src .getValue ( ) = "" and
@@ -55,13 +53,13 @@ private predicate unusedArgument(
5553}
5654
5755private predicate missingArgument (
58- FormatCall call , DataFlow :: PathNode source , DataFlow :: PathNode sink , string msg ,
56+ FormatCall call , FormatInvalid :: PathNode source , FormatInvalid :: PathNode sink , string msg ,
5957 ValidFormatString src , string srcString
6058) {
6159 exists ( int used , int supplied |
6260 source .getNode ( ) .asExpr ( ) = src and
6361 sink .getNode ( ) .asExpr ( ) = call .getFormatExpr ( ) and
64- any ( FormatConfiguration conf ) . hasFlowPath ( source , sink ) and
62+ FormatInvalid :: flowPath ( source , sink ) and
6563 used = src .getAnInsert ( ) and
6664 supplied = call .getSuppliedArguments ( ) and
6765 used >= supplied and
@@ -71,8 +69,8 @@ private predicate missingArgument(
7169}
7270
7371from
74- Element alert , DataFlow :: PathNode source , DataFlow :: PathNode sink , string msg , Element extra1 ,
75- string extra1String , Element extra2 , string extra2String
72+ Element alert , FormatInvalid :: PathNode source , FormatInvalid :: PathNode sink , string msg ,
73+ Element extra1 , string extra1String , Element extra2 , string extra2String
7674where
7775 invalidFormatString ( alert , source , sink , msg , extra1 , extra1String ) and
7876 extra2 = extra1 and
0 commit comments