Repository navigation
Expand file tree
/
Copy pathStrFormatTemplateInjection.ql
More file actions
141 lines (129 loc) · 4.67 KB
/
Copy pathStrFormatTemplateInjection.ql
File metadata and controls
141 lines (129 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @name Format string built from user-controlled sources
* @description Using a user-controlled string as the format string of `str.format`,
* `str.format_map` or `string.Formatter` lets the user traverse the
* attributes and items of the format arguments (for example
* `{0.__init__.__globals__}` or `{1.environ}`), which can disclose secrets
* and internal objects.
* @kind path-problem
* @problem.severity error
* @security-severity 6.5
* @precision medium
* @id py/str-format-template-injection
* @tags security
* external/cwe/cwe-1336
* external/cwe/cwe-134
*/
import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.TaintTracking
import semmle.python.Concepts
import semmle.python.ApiGraphs
import semmle.python.dataflow.new.BarrierGuards
import Shared.CapturedUnpackedVariableFlow
/**
* Holds if `arg` is a literal that offers nothing to traverse, such as `1`, `"x"`, `()` or
* `{"name": "x"}`.
*/
private predicate isLiteralArgument(Expr arg) {
arg instanceof StringLiteral
or
arg instanceof Num
or
arg instanceof None
or
arg instanceof True
or
arg instanceof False
or
(arg instanceof Tuple or arg instanceof List or arg instanceof Set) and
forall(Expr elt | elt = arg.getASubExpression() | isLiteralArgument(elt))
or
isLiteralArgument(arg.(Starred).getValue())
or
arg instanceof Dict and
forall(Expr e | e = arg.(Dict).getAKey() or e = arg.(Dict).getAValue() | isLiteralArgument(e))
}
/**
* Holds if `call` passes at least one non-literal value to be formatted, ignoring the
* first `skip` positional arguments and the named argument `ignoredKeyword`.
*/
bindingset[skip]
private predicate hasNonLiteralFormatArgument(DataFlow::CallCfgNode call, int skip) {
hasNonLiteralFormatArgument(call, skip, "")
}
bindingset[skip, ignoredKeyword]
private predicate hasNonLiteralFormatArgument(
DataFlow::CallCfgNode call, int skip, string ignoredKeyword
) {
exists(Expr arg |
exists(int i | i >= skip and arg = call.getNode().getNode().(Call).getPositionalArg(i))
or
exists(Keyword keyword |
keyword = call.getNode().getNode().(Call).getANamedArg().(Keyword) and
keyword.getArg() != ignoredKeyword and
arg = keyword.getValue()
)
or
arg = call.getNode().getNode().(Call).getANamedArg().(DictUnpacking).getValue()
or
arg = call.getNode().getNode().(Call).getStarArg()
or
arg = call.getNode().getNode().(Call).getKwargs()
|
not isLiteralArgument(arg)
)
}
/**
* A format string whose replacement fields are resolved against non-literal arguments.
*/
private class FormatStringSink extends DataFlow::Node {
FormatStringSink() {
// `fmt.format(...)` and `fmt.format_map(...)`
exists(DataFlow::MethodCallNode call |
call.calls(this, ["format", "format_map"]) and
not this.asExpr() instanceof StringLiteral and
hasNonLiteralFormatArgument(call, 0)
)
or
// `string.Formatter().format(fmt, ...)` and `string.Formatter().vformat(fmt, args, kwargs)`
exists(DataFlow::CallCfgNode call |
call =
API::moduleImport("string")
.getMember("Formatter")
.getASubclass*()
.getReturn()
.getMember(["format", "vformat"])
.getACall() and
this = call.getArg(0) and
hasNonLiteralFormatArgument(call, 1)
)
or
// `string.Formatter().vformat(format_string=fmt, ...)`
exists(DataFlow::CallCfgNode call |
call =
API::moduleImport("string")
.getMember("Formatter")
.getASubclass*()
.getReturn()
.getMember("vformat")
.getACall() and
this = call.getArgByName("format_string") and
hasNonLiteralFormatArgument(call, 0, "format_string")
)
}
}
private module StrFormatTemplateInjectionConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }
predicate isSink(DataFlow::Node sink) { sink instanceof FormatStringSink }
predicate isBarrier(DataFlow::Node node) { node instanceof ConstCompareBarrier }
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
capturedUnpackedVariableStep(nodeFrom, nodeTo)
}
}
module StrFormatTemplateInjectionFlow = TaintTracking::Global<StrFormatTemplateInjectionConfig>;
import StrFormatTemplateInjectionFlow::PathGraph
from StrFormatTemplateInjectionFlow::PathNode source, StrFormatTemplateInjectionFlow::PathNode sink
where StrFormatTemplateInjectionFlow::flowPath(source, sink)
select sink.getNode(), source, sink, "This format string depends on a $@.", source.getNode(),
"user-provided value"