Skip to content

Commit 66bd56f

Browse files
committed
Don't use any() as sink
1 parent c0f195b commit 66bd56f

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

ql/src/experimental/CWE-942/CorsMisconfiguration.ql

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -118,63 +118,58 @@ class FlowsFromUntrusted extends TaintTracking::Configuration {
118118

119119
override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource }
120120

121-
override predicate isSink(DataFlow::Node sink) { any() }
122-
}
121+
override predicate isSink(DataFlow::Node sink) { isSink(sink, _) }
123122

124-
/**
125-
* Holds if the provided `dst` is also destination of a `UntrustedFlowSource`.
126-
*/
127-
predicate untrustedFlowsToExpr(Expr dst) {
128-
exists(FlowsFromUntrusted cfg, DataFlow::PathNode source, DataFlow::PathNode sink |
129-
cfg.hasFlowPath(source, sink) and
130-
sink.getNode().asExpr() = dst
131-
)
123+
predicate isSink(DataFlow::Node sink, ControlFlow::ConditionGuardNode cgn) {
124+
exists(IfStmt ifs |
125+
exists(Expr child, Expr operand |
126+
child = ifs.getCond().getAChildExpr*() and
127+
(
128+
operand = child or
129+
operand = child.(LorExpr).getAnOperand() or
130+
operand = child.(LandExpr).getAnOperand()
131+
) and
132+
(
133+
//
134+
exists(DataFlow::CallExpr call | call = operand |
135+
call.getTarget().hasQualifiedName("strings", "HasSuffix") and
136+
sink.asExpr() = call.getArgument(0)
137+
)
138+
or
139+
exists(MapRead mapRead |
140+
operand = mapRead.asExpr() and
141+
sink = mapRead.getIndex().getAPredecessor*()
142+
// TODO: add _, ok : map[untrusted]; ok
143+
)
144+
or
145+
exists(EqlExpr comp |
146+
operand = comp and
147+
(
148+
sink.asExpr() = comp.getLeftOperand() and
149+
not comp.getRightOperand().(StringLit).getStringValue() = ""
150+
or
151+
sink.asExpr() = comp.getRightOperand() and
152+
not comp.getLeftOperand().(StringLit).getStringValue() = ""
153+
)
154+
)
155+
)
156+
)
157+
|
158+
cgn.getCondition() = ifs.getCond()
159+
)
160+
}
132161
}
133162

134163
/**
135164
* Holds if the provided `dst` is also destination of a `UntrustedFlowSource`.
136165
*/
137-
predicate untrustedFlowsTo(DataFlow::Node dst) { untrustedFlowsToExpr(dst.asExpr()) }
138-
139-
/**
140-
* Holds if the provided `allowOriginHW` is guarded by a check on an `UntrustedFlowSource`
141-
* which (supposedly) is an `Origin` header.
142-
*/
143-
predicate isGuardedByCheckOnUntrusted(HTTP::HeaderWrite allowOriginHW) {
144-
exists(ControlFlow::ConditionGuardNode cgn, IfStmt ifs |
145-
exists(Expr child, Expr operand |
146-
child = ifs.getCond().getAChildExpr*() and
147-
(
148-
operand = child or
149-
operand = child.(LorExpr).getAnOperand() or
150-
operand = child.(LandExpr).getAnOperand()
151-
) and
152-
(
153-
exists(DataFlow::CallExpr call | call = operand |
154-
call.getTarget().hasQualifiedName("strings", "HasSuffix") and
155-
untrustedFlowsToExpr(call.getArgument(0))
156-
)
157-
or
158-
exists(MapRead mapRead |
159-
operand = mapRead.asExpr() and
160-
untrustedFlowsTo(mapRead.getIndex().getAPredecessor*())
161-
// TODO: add _, ok : map[untrusted]; ok
162-
)
163-
or
164-
exists(EqlExpr comp |
165-
operand = comp and
166-
(
167-
untrustedFlowsToExpr(comp.getLeftOperand()) and
168-
not comp.getRightOperand().(StringLit).getStringValue() = ""
169-
or
170-
untrustedFlowsToExpr(comp.getRightOperand()) and
171-
not comp.getLeftOperand().(StringLit).getStringValue() = ""
172-
)
173-
)
174-
)
175-
)
166+
predicate flowsToGuardedByCheckOnUntrusted(HTTP::HeaderWrite allowOriginHW) {
167+
exists(
168+
FlowsFromUntrusted cfg, DataFlow::PathNode source, DataFlow::PathNode sink,
169+
ControlFlow::ConditionGuardNode cgn
170+
|
171+
cfg.hasFlowPath(source, sink) and cfg.isSink(sink.getNode(), cgn)
176172
|
177-
cgn.getCondition() = ifs.getCond() and
178173
cgn.dominates(allowOriginHW.getBasicBlock())
179174
)
180175
}
@@ -187,7 +182,7 @@ where
187182
or
188183
allowOriginIsNull(allowOriginHW, message)
189184
) and
190-
not isGuardedByCheckOnUntrusted(allowOriginHW) and
185+
not flowsToGuardedByCheckOnUntrusted(allowOriginHW) and
191186
not exists(ControlFlow::ConditionGuardNode cgn |
192187
cgn.ensures(any(AllowedFlag f).getAFlag().getANode(), _)
193188
|

0 commit comments

Comments
 (0)