Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e447c5

Browse files
committed
Python: Handle taint for % formatting
1 parent 80745e8 commit 1e447c5

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

python/ql/src/experimental/dataflow/internal/TaintTrackingPrivate.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ predicate stringMethods(DataFlow::CfgNode nodeFrom, DataFlow::CfgNode nodeTo) {
105105
method_name = "format_map" and
106106
nodeFrom.getNode() = call.getArg(0)
107107
)
108+
or
109+
// % formatting
110+
exists(BinaryExprNode fmt | fmt = nodeTo.getNode() |
111+
fmt.getOp() instanceof Mod and
112+
(
113+
fmt.getLeft() = nodeFrom.getNode()
114+
or
115+
fmt.getRight() = nodeFrom.getNode()
116+
)
117+
)
108118
}

python/ql/test/experimental/dataflow/tainttracking/string/TestTaint.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@
4343
| test.py:89 | fail | str_methods | ts.join(..) |
4444
| test.py:99 | fail | non_syntactic | meth() |
4545
| test.py:100 | fail | non_syntactic | _str(..) |
46+
| test.py:109 | ok | percent_fmt | BinaryExpr |
47+
| test.py:110 | ok | percent_fmt | BinaryExpr |
48+
| test.py:111 | fail | percent_fmt | BinaryExpr |

python/ql/test/experimental/dataflow/tainttracking/string/test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,21 @@ def non_syntactic():
100100
_str(ts),
101101
)
102102

103+
104+
def percent_fmt():
105+
print("\n#percent_fmt")
106+
ts = TAINTED_STRING
107+
tainted_fmt = ts + " %s %s"
108+
ensure_tainted(
109+
tainted_fmt % (1, 2),
110+
"%s foo bar" % ts,
111+
"%s %s %s" % (1, 2, ts),
112+
)
113+
114+
103115
# Make tests runable
104116

105117
str_operations()
106118
str_methods()
107119
non_syntactic()
120+
percent_fmt()

0 commit comments

Comments
 (0)