Skip to content

Commit 0a6a7ec

Browse files
committed
python: centralize external barrier guard definition
1 parent a5213a7 commit 0a6a7ec

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPublic.qll

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,15 +600,52 @@ signature predicate guardChecksSig(GuardNode g, ControlFlowNode node, boolean br
600600
module BarrierGuard<guardChecksSig/3 guardChecks> {
601601
/** Gets a node that is safely guarded by the given guard check. */
602602
ExprNode getABarrierNode() {
603+
result = ParameterizedBarrierGuard<Unit, extendedGuardChecks/4>::getABarrierNode(_)
604+
}
605+
606+
private predicate extendedGuardChecks(GuardNode g, ControlFlowNode node, boolean branch, Unit u) {
607+
guardChecks(g, node, branch)
608+
}
609+
}
610+
611+
bindingset[this]
612+
private signature class ParamSig;
613+
614+
private module WithParam<ParamSig P> {
615+
signature predicate guardChecksSig(GuardNode g, ControlFlowNode node, boolean branch, P param);
616+
}
617+
618+
module ParameterizedBarrierGuard<ParamSig P, WithParam<P>::guardChecksSig/4 guardChecks> {
619+
/** Gets a node that is safely guarded by the given guard check with parameter `param`. */
620+
ExprNode getABarrierNode(P param) {
603621
exists(GuardNode g, EssaDefinition def, ControlFlowNode node, boolean branch |
604622
AdjacentUses::useOfDef(def, node) and
605-
guardChecks(g, node, branch) and
623+
guardChecks(g, node, branch, param) and
606624
AdjacentUses::useOfDef(def, result.asCfgNode()) and
607625
g.controlsBlock(result.asCfgNode().getBasicBlock(), branch)
608626
)
609627
}
610628
}
611629

630+
module ExternalBarrierGuard {
631+
private import semmle.python.ApiGraphs
632+
633+
predicate guardCheck(GuardNode g, ControlFlowNode node, boolean branch, string kind) {
634+
exists(API::CallNode call, API::Node parameter |
635+
parameter = call.getAParameter() and
636+
parameter = ModelOutput::getABarrierGuardNode(kind, branch)
637+
|
638+
g = call.asCfgNode() and
639+
node = parameter.asSink().asCfgNode()
640+
)
641+
}
642+
643+
/** Gets a node that is an external barrier of the given kind. */
644+
ExprNode getAnExternalBarrierNode(string kind) {
645+
result = ParameterizedBarrierGuard<string, guardCheck/4>::getABarrierNode(kind)
646+
}
647+
}
648+
612649
/**
613650
* Algebraic datatype for tracking data content associated with values.
614651
* Content can be collection elements or object attributes.

python/ql/lib/semmle/python/security/dataflow/UrlRedirectCustomizations.qll

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,10 @@ module UrlRedirect {
161161
/** DEPRECATED: Use ConstCompareAsSanitizerGuard instead. */
162162
deprecated class StringConstCompareAsSanitizerGuard = ConstCompareAsSanitizerGuard;
163163

164-
private predicate urlCheck(DataFlow::GuardNode g, ControlFlowNode node, boolean branch) {
165-
exists(API::CallNode call, API::Node parameter |
166-
parameter = call.getAParameter() and
167-
parameter = ModelOutput::getABarrierGuardNode("url-redirection", branch)
168-
|
169-
g = call.asCfgNode() and
170-
node = parameter.asSink().asCfgNode()
171-
)
172-
}
173-
174164
class SanitizerFromModel extends Sanitizer {
175-
SanitizerFromModel() { this = DataFlow::BarrierGuard<urlCheck/3>::getABarrierNode() }
165+
SanitizerFromModel() {
166+
this = DataFlow::ExternalBarrierGuard::getAnExternalBarrierNode("url-redirection")
167+
}
176168

177169
override predicate sanitizes(FlowState state) {
178170
// sanitize all flow states

0 commit comments

Comments
 (0)