This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathStringBreakCustomizations.qll
More file actions
171 lines (144 loc) · 5.34 KB
/
StringBreakCustomizations.qll
File metadata and controls
171 lines (144 loc) · 5.34 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Provides default sources, sinks and sanitizers for reasoning about unsafe-quoting
* vulnerabilities, as well as extension points for adding your own.
*/
import go
/**
* Provides extension points for customizing the taint tracking configuration for reasoning about
* unsafe-quoting vulnerabilities.
*/
module StringBreak {
/** A (single or double) quote. */
class Quote extends string {
Quote() { this = "'" or this = "\"" }
/** Gets the type of this quote, either single or double. */
string getType() { if this = "'" then result = "single" else result = "double" }
}
/**
* A data flow source for unsafe-quoting vulnerabilities.
*/
abstract class Source extends DataFlow::Node { }
/**
* A data flow sink for unsafe-quoting vulnerabilities.
*/
abstract class Sink extends DataFlow::Node {
/** Gets the quote character for which this is a sink. */
abstract Quote getQuote();
}
/**
* A sanitizer for unsafe-quoting vulnerabilities.
*/
abstract class Sanitizer extends DataFlow::Node {
/** Gets the quote character for which this is a sanitizer. */
Quote getQuote() { any() }
}
/**
* A sanitizer guard for unsafe-quoting vulnerabilities.
*/
abstract class SanitizerGuard extends DataFlow::BarrierGuard { }
/** Holds if `l` contains a `quote` (either single or double). */
private predicate containsQuote(StringOps::ConcatenationLeaf l, Quote quote) {
quote = l.getStringValue().regexpFind("['\"]", _, _)
}
/** A call to `json.Marshal`, considered as a taint source for unsafe quoting. */
class JsonMarshalAsSource extends Source {
JsonMarshalAsSource() {
exists(MarshalingFunction jsonMarshal | jsonMarshal.getFormat() = "JSON" |
this = jsonMarshal.getOutput().getNode(jsonMarshal.getACall())
)
}
}
/** A string concatenation with quotes, considered as a taint sink for unsafe quoting. */
class StringConcatenationAsSink extends Sink {
Quote quote;
StringConcatenationAsSink() {
exists(StringOps::ConcatenationLeaf lf | lf.asNode() = this |
containsQuote(lf.getPreviousLeaf(), quote) and
containsQuote(lf.getNextLeaf(), quote)
)
}
override Quote getQuote() { result = quote }
}
/** A call to `json.Unmarshal`, considered as a sanitizer for unsafe quoting. */
class UnmarshalSanitizer extends Sanitizer {
UnmarshalSanitizer() {
exists(UnmarshalingFunction jsonUnmarshal | jsonUnmarshal.getFormat() = "JSON" |
this = jsonUnmarshal.getOutput().getNode(jsonUnmarshal.getACall())
)
}
}
/**
* A call to `strings.Replace` or `strings.ReplaceAll`, considered as a sanitizer
* for unsafe quoting.
*/
class ReplaceSanitizer extends Sanitizer {
Quote quote;
ReplaceSanitizer() {
exists(string name, DataFlow::CallNode call |
this = call and
call.getTarget().hasQualifiedName("strings", name) and
call.getArgument(1).getStringValue().matches("%" + quote + "%")
|
name = "Replace" and
call.getArgument(3).getNumericValue() < 0
or
name = "ReplaceAll"
)
}
override Quote getQuote() { result = quote }
}
class StringsNewReplacerCall extends DataFlow::CallNode {
StringsNewReplacerCall() { this.getTarget().hasQualifiedName("strings", "NewReplacer") }
DataFlow::Node getAReplacedArgument() { exists(int n | n % 2 = 0 and result = getArgument(n)) }
}
class StringsNewReplacerConfiguration extends DataFlow2::Configuration {
StringsNewReplacerConfiguration() { this = "StringsNewReplacerConfiguration" }
override predicate isSource(DataFlow::Node source) { source instanceof StringsNewReplacerCall }
override predicate isSink(DataFlow::Node sink) {
exists(DataFlow::MethodCallNode call |
sink = call.getReceiver() and
call.getTarget().hasQualifiedName("strings", "Replacer", ["Replace", "WriteString"])
)
}
}
/**
* A call to `strings.Replacer.Replace`, considered as a sanitizer for unsafe
* quoting.
*/
class ReplacerReplaceSanitizer extends DataFlow::MethodCallNode, Sanitizer {
Quote quote;
ReplacerReplaceSanitizer() {
exists(
StringsNewReplacerConfiguration config, DataFlow::Node source, DataFlow::Node sink,
DataFlow::MethodCallNode call
|
config.hasFlow(source, sink) and
call.getTarget().hasQualifiedName("strings", "Replacer", "Replace") and
sink = call.getReceiver() and
this = call.getResult() and
quote = source.(StringsNewReplacerCall).getAReplacedArgument().getStringValue()
)
}
override Quote getQuote() { result = quote }
}
/**
* A call to `strings.Replacer.WriteString`, considered as a sanitizer for
* unsafe quoting.
*/
class ReplacerWriteStringSanitizer extends Sanitizer {
Quote quote;
ReplacerWriteStringSanitizer() {
exists(
StringsNewReplacerConfiguration config, DataFlow::Node source, DataFlow::Node sink,
DataFlow::MethodCallNode call
|
config.hasFlow(source, sink) and
call.getTarget().hasQualifiedName("strings", "Replacer", "WriteString") and
sink = call.getReceiver() and
this = call.getArgument(1) and
quote = source.(StringsNewReplacerCall).getAReplacedArgument().getStringValue()
)
}
override Quote getQuote() { result = quote }
}
}