-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCorsMisconfiguration.ql
More file actions
241 lines (215 loc) · 8.13 KB
/
CorsMisconfiguration.ql
File metadata and controls
241 lines (215 loc) · 8.13 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @name CORS misconfiguration
* @description If a CORS policy is configured to accept an origin value obtained from the request data,
* or is set to `null`, and it allows credential sharing, then the users of the
* application are vulnerable to the same range of attacks as in XSS (credential stealing, etc.).
* @kind problem
* @problem.severity warning
* @id go/cors-misconfiguration
* @tags security
* experimental
* external/cwe/cwe-942
* external/cwe/cwe-346
*/
import go
import semmle.go.security.InsecureFeatureFlag::InsecureFeatureFlag
/**
* A flag indicating a check for satisfied permissions or test configuration.
*/
class AllowedFlag extends FlagKind {
AllowedFlag() { this = "allowed" }
bindingset[result]
override string getAFlagName() {
result.regexpMatch("(?i).*(allow|match|check|debug|devel|insecure).*")
}
}
/**
* Provides the name of the `Access-Control-Allow-Origin` header key.
*/
string headerAllowOrigin() { result = "Access-Control-Allow-Origin".toLowerCase() }
/**
* Provides the name of the `Access-Control-Allow-Credentials` header key.
*/
string headerAllowCredentials() { result = "Access-Control-Allow-Credentials".toLowerCase() }
/**
* An `Access-Control-Allow-Origin` header write.
*/
class AllowOriginHeaderWrite extends Http::HeaderWrite {
AllowOriginHeaderWrite() { this.getHeaderName() = headerAllowOrigin() }
}
/**
* An `Access-Control-Allow-Credentials` header write.
*/
class AllowCredentialsHeaderWrite extends Http::HeaderWrite {
AllowCredentialsHeaderWrite() { this.getHeaderName() = headerAllowCredentials() }
}
module UntrustedToAllowOriginHeaderConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }
additional predicate isSinkHW(DataFlow::Node sink, AllowOriginHeaderWrite hw) {
sink = hw.getValue()
}
predicate isBarrier(DataFlow::Node node) {
exists(ControlFlow::ConditionGuardNode cgn |
cgn.ensures(any(AllowedFlag f).getAFlag().getANode(), _)
|
cgn.dominates(node.getBasicBlock())
)
}
predicate isSink(DataFlow::Node sink) { isSinkHW(sink, _) }
}
module UntrustedToAllowOriginConfigConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }
additional predicate isSinkWrite(DataFlow::Node sink, UniversalOriginWrite w) { sink = w }
predicate isSink(DataFlow::Node sink) { isSinkWrite(sink, _) }
}
/**
* Tracks taint flowfor reasoning about when a `ActiveThreatModelSource` flows to
* a `HeaderWrite` that writes an `Access-Control-Allow-Origin` header's value.
*/
module UntrustedToAllowOriginHeaderFlow = TaintTracking::Global<UntrustedToAllowOriginHeaderConfig>;
/**
* Tracks taint flowfor reasoning about when a `ActiveThreatModelSource` flows to
* a `AllowOriginsWrite` that writes an `Access-Control-Allow-Origin` header's value.
*/
module UntrustedToAllowOriginConfigFlow = TaintTracking::Global<UntrustedToAllowOriginConfigConfig>;
/**
* Holds if the provided `allowOriginHW` HeaderWrite's parent ResponseWriter
* also has another HeaderWrite that sets a `Access-Control-Allow-Credentials`
* header to `true`.
*/
predicate allowCredentialsIsSetToTrue(DataFlow::ExprNode allowOriginHW) {
exists(AllowCredentialsHeaderWrite allowCredentialsHW |
allowCredentialsHW.getHeaderValue().toLowerCase() = "true"
|
allowOriginHW.(AllowOriginHeaderWrite).getResponseWriter() =
allowCredentialsHW.getResponseWriter()
)
or
exists(UniversalAllowCredentialsWrite allowCredentialsGin |
allowCredentialsGin.getExpr().getBoolValue() = true
|
allowCredentialsGin.getConfig() = allowOriginHW.(UniversalOriginWrite).getConfig() and
not exists(UniversalAllowAllOriginsWrite allowAllOrigins |
allowAllOrigins.getExpr().getBoolValue() = true and
allowCredentialsGin.getConfig() = allowAllOrigins.getConfig()
)
or
allowCredentialsGin.getBase() = allowOriginHW.(UniversalOriginWrite).getBase() and
not exists(UniversalAllowAllOriginsWrite allowAllOrigins |
allowAllOrigins.getExpr().getBoolValue() = true and
allowCredentialsGin.getBase() = allowAllOrigins.getBase()
)
)
}
/**
* Holds if the provided `allowOriginHW` HeaderWrite's value is set using an
* ActiveThreatModelSource.
* The `message` parameter is populated with the warning message to be returned by the query.
*/
predicate flowsFromUntrustedToAllowOrigin(DataFlow::ExprNode allowOriginHW, string message) {
exists(DataFlow::Node sink |
UntrustedToAllowOriginHeaderFlow::flowTo(sink) and
UntrustedToAllowOriginHeaderConfig::isSinkHW(sink, allowOriginHW)
or
UntrustedToAllowOriginConfigFlow::flowTo(sink) and
UntrustedToAllowOriginConfigConfig::isSinkWrite(sink, allowOriginHW)
|
message =
headerAllowOrigin() + " header is set to a user-defined value, and " +
headerAllowCredentials() + " is set to `true`"
)
}
/**
* Holds if the provided `allowOriginHW` HeaderWrite is for a `Access-Control-Allow-Origin`
* header and the value is set to `null`.
*/
predicate allowOriginIsNull(DataFlow::ExprNode allowOriginHW, string message) {
allowOriginHW.(AllowOriginHeaderWrite).getHeaderValue().toLowerCase() = "null" and
message =
headerAllowOrigin() + " header is set to `" +
allowOriginHW.(AllowOriginHeaderWrite).getHeaderValue() + "`, and " + headerAllowCredentials()
+ " is set to `true`"
or
allowOriginHW
.(UniversalOriginWrite)
.asExpr()
.(SliceLit)
.getAnElement()
.getStringValue()
.toLowerCase() = "null" and
message =
headerAllowOrigin() + " header is set to `" + "null" + "`, and " + headerAllowCredentials() +
" is set to `true`"
}
/**
* A read on a map type.
*/
class MapRead extends DataFlow::ElementReadNode {
MapRead() { this.getBase().getType() instanceof MapType }
}
module FromUntrustedConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource }
predicate isSink(DataFlow::Node sink) { isSinkCgn(sink, _) }
additional predicate isSinkCgn(DataFlow::Node sink, ControlFlow::ConditionGuardNode cgn) {
exists(IfStmt ifs |
exists(Expr operand |
operand = ifs.getCond().getAChildExpr*() and
(
exists(DataFlow::CallExpr call | call = operand |
call.getTarget().hasQualifiedName("strings", "HasSuffix") and
sink.asExpr() = call.getArgument(0)
)
or
exists(MapRead mapRead |
operand = mapRead.asExpr() and
sink = mapRead.getIndex().getAPredecessor*()
// TODO: add _, ok : map[untrusted]; ok
)
or
exists(EqlExpr comp |
operand = comp and
(
sink.asExpr() = comp.getLeftOperand() and
not comp.getRightOperand().(StringLit).getStringValue() = ""
or
sink.asExpr() = comp.getRightOperand() and
not comp.getLeftOperand().(StringLit).getStringValue() = ""
)
)
)
)
|
cgn.getCondition() = ifs.getCond()
)
}
}
/**
* Tracks taint flow for reasoning about when a `ActiveThreatModelSource` flows
* somewhere.
*/
module FromUntrustedFlow = TaintTracking::Global<FromUntrustedConfig>;
/**
* Holds if the provided `allowOriginHW` is also destination of a `ActiveThreatModelSource`.
*/
predicate flowsToGuardedByCheckOnUntrusted(DataFlow::ExprNode allowOriginHW) {
exists(DataFlow::Node sink, ControlFlow::ConditionGuardNode cgn |
FromUntrustedFlow::flowTo(sink) and FromUntrustedConfig::isSinkCgn(sink, cgn)
|
cgn.dominates(allowOriginHW.getBasicBlock())
)
}
from DataFlow::ExprNode allowOriginHW, string message
where
allowCredentialsIsSetToTrue(allowOriginHW) and
(
flowsFromUntrustedToAllowOrigin(allowOriginHW, message)
or
allowOriginIsNull(allowOriginHW, message)
) and
not flowsToGuardedByCheckOnUntrusted(allowOriginHW) and
not exists(ControlFlow::ConditionGuardNode cgn |
cgn.ensures(any(AllowedFlag f).getAFlag().getANode(), _)
|
cgn.dominates(allowOriginHW.getBasicBlock())
)
select allowOriginHW, message