|
1 | 1 | /** |
2 | | - * @name Invalid format string |
3 | | - * @description Using a format string with an incorrect format causes a 'System.FormatException'. |
| 2 | + * @name Invalid string formatting |
| 3 | + * @description Calling 'string.Format()' with either an invalid format string or incorrect |
| 4 | + * number of arguments may result in dropped arguments or a 'System.FormatException'. |
4 | 5 | * @kind path-problem |
5 | 6 | * @problem.severity error |
6 | 7 | * @precision high |
7 | | - * @id cs/invalid-format-string |
| 8 | + * @id cs/invalid-string-formatting |
8 | 9 | * @tags reliability |
9 | 10 | * maintainability |
10 | 11 | */ |
11 | 12 |
|
12 | 13 | import csharp |
13 | 14 | import semmle.code.csharp.frameworks.Format |
14 | | -import FormatFlow |
| 15 | +import DataFlow::PathGraph |
15 | 16 |
|
16 | | -from FormatCall s, InvalidFormatString src, PathNode source, PathNode sink |
| 17 | +private class FormatConfiguration extends DataFlow::Configuration { |
| 18 | + FormatConfiguration() { this = "format" } |
| 19 | + |
| 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 | + } |
| 25 | +} |
| 26 | + |
| 27 | +private predicate invalidFormatString( |
| 28 | + InvalidFormatString src, DataFlow::PathNode source, DataFlow::PathNode sink, string msg, |
| 29 | + FormatCall call, string callString |
| 30 | +) { |
| 31 | + source.getNode().asExpr() = src and |
| 32 | + sink.getNode().asExpr() = call.getFormatExpr() and |
| 33 | + any(FormatConfiguration conf).hasFlowPath(source, sink) and |
| 34 | + call.hasInsertions() and |
| 35 | + msg = "Invalid format string used in $@ formatting call." and |
| 36 | + callString = "this" |
| 37 | +} |
| 38 | + |
| 39 | +private predicate unusedArgument( |
| 40 | + FormatCall call, DataFlow::PathNode source, DataFlow::PathNode sink, string msg, |
| 41 | + ValidFormatString src, string srcString, Expr unusedExpr, string unusedString |
| 42 | +) { |
| 43 | + exists(int unused | |
| 44 | + source.getNode().asExpr() = src and |
| 45 | + sink.getNode().asExpr() = call.getFormatExpr() and |
| 46 | + any(FormatConfiguration conf).hasFlowPath(source, sink) and |
| 47 | + unused = call.getASuppliedArgument() and |
| 48 | + not unused = src.getAnInsert() and |
| 49 | + not src.getValue() = "" and |
| 50 | + msg = "The $@ ignores $@." and |
| 51 | + srcString = "format string" and |
| 52 | + unusedExpr = call.getSuppliedExpr(unused) and |
| 53 | + unusedString = "this supplied value" |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +private predicate missingArgument( |
| 58 | + FormatCall call, DataFlow::PathNode source, DataFlow::PathNode sink, string msg, |
| 59 | + ValidFormatString src, string srcString |
| 60 | +) { |
| 61 | + exists(int used, int supplied | |
| 62 | + source.getNode().asExpr() = src and |
| 63 | + sink.getNode().asExpr() = call.getFormatExpr() and |
| 64 | + any(FormatConfiguration conf).hasFlowPath(source, sink) and |
| 65 | + used = src.getAnInsert() and |
| 66 | + supplied = call.getSuppliedArguments() and |
| 67 | + used >= supplied and |
| 68 | + msg = "Argument '{" + used + "}' has not been supplied to $@ format string." and |
| 69 | + srcString = "this" |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +from |
| 74 | + Element alert, DataFlow::PathNode source, DataFlow::PathNode sink, string msg, Element extra1, |
| 75 | + string extra1String, Element extra2, string extra2String |
17 | 76 | where |
18 | | - hasFlowPath(src, source, s, sink) and |
19 | | - s.hasInsertions() |
20 | | -select src, source, sink, "Invalid format string used in $@ formatting call.", s, "this" |
| 77 | + invalidFormatString(alert, source, sink, msg, extra1, extra1String) and |
| 78 | + extra2 = extra1 and |
| 79 | + extra2String = extra1String |
| 80 | + or |
| 81 | + unusedArgument(alert, source, sink, msg, extra1, extra1String, extra2, extra2String) |
| 82 | + or |
| 83 | + missingArgument(alert, source, sink, msg, extra1, extra1String) and |
| 84 | + extra2 = extra1 and |
| 85 | + extra2String = extra1String |
| 86 | +select alert, source, sink, msg, extra1, extra1String, extra2, extra2String |
0 commit comments