Skip to content

Commit ab23a15

Browse files
committed
Python: Move taint-tracking library to new location and extend configuration to match API of other languages.
1 parent 7b8ca30 commit ab23a15

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* For compatibility with other language implementations */
2+
3+
import semmle.python.security.TaintTracking

python/ql/src/semmle/python/security/TaintTracking.qll

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ abstract class TaintKind extends string {
125125
*/
126126
predicate additionalFlowStepVar(EssaVariable fromvar, EssaVariable tovar) { none() }
127127

128-
/** Holds if this kind of taint can start from `expr`.
129-
* In other words, is `expr` a source of this kind of taint.
130-
*/
131-
final predicate startsFrom(ControlFlowNode expr) {
132-
expr.(TaintSource).isSourceOf(this, _)
133-
}
134-
135128
/** Holds if this kind of taint "taints" `expr`.
136129
*/
137130
final predicate taints(ControlFlowNode expr) {
@@ -325,6 +318,8 @@ abstract class Sanitizer extends string {
325318
private predicate valid_sanitizer(Sanitizer sanitizer) {
326319
not exists(TaintTracking::Configuration c)
327320
or
321+
exists(DataFlow::Configuration c | c.isSanitizer(sanitizer))
322+
or
328323
exists(TaintTracking::Configuration c | c.isSanitizer(sanitizer))
329324
}
330325

@@ -594,6 +589,12 @@ private newtype TTaintedNode =
594589
n.(TaintSource).isSourceOf(kind, context)
595590
)
596591
or
592+
exists(DataFlow::Configuration config, TaintKind kind |
593+
taint = TaintFlowImplementation::TTrackedTaint(kind) and
594+
config.isSource(n) and context.getDepth() = 0 and
595+
kind instanceof GenericFlowType
596+
)
597+
or
597598
TaintFlowImplementation::step(_, taint, context, n) and
598599
exists(TaintKind kind |
599600
kind = taint.(TaintFlowImplementation::TrackedTaint).getKind()
@@ -855,6 +856,8 @@ library module TaintFlowImplementation {
855856
(
856857
not exists(TaintTracking::Configuration c)
857858
or
859+
exists(DataFlow::Configuration c | c.isExtension(fromnodenode))
860+
or
858861
exists(TaintTracking::Configuration c | c.isExtension(fromnodenode))
859862
)
860863
|
@@ -1060,7 +1063,13 @@ library module TaintFlowImplementation {
10601063
or
10611064
exists(DataFlowNode originnode |
10621065
originnode = origin.getNode() and
1063-
forall(TaintTracking::Configuration c | c.isExtension(originnode)) and
1066+
(
1067+
not exists(TaintTracking::Configuration c)
1068+
or
1069+
exists(DataFlow::Configuration c | c.isExtension(originnode))
1070+
or
1071+
exists(TaintTracking::Configuration c | c.isExtension(originnode))
1072+
) and
10641073
originnode.getASuccessorVariable() = var and
10651074
context = origin.getContext()
10661075
)
@@ -1467,6 +1476,66 @@ class CallContext extends TCallContext {
14671476
}
14681477

14691478

1479+
/** Data flow module providing an interface compatible with
1480+
* the other language implementations.
1481+
*/
1482+
module DataFlow {
1483+
1484+
class FlowType = TaintKind;
1485+
1486+
/** Generic taint kind, source and sink classes for convenience and
1487+
* compatibility with other language libraries
1488+
*/
1489+
1490+
class Node = ControlFlowNode;
1491+
1492+
class PathNode = TaintedNode;
1493+
1494+
class Extension = DataFlowExtension::DataFlowNode;
1495+
1496+
abstract class Configuration extends string {
1497+
1498+
bindingset[this]
1499+
Configuration() { this = this }
1500+
1501+
abstract predicate isSource(Node source);
1502+
1503+
abstract predicate isSink(Node sink);
1504+
1505+
predicate isSanitizer(Sanitizer sanitizer) { none() }
1506+
1507+
predicate isExtension(Extension extension) { none() }
1508+
1509+
predicate hasFlowPath(PathNode source, PathNode sink) {
1510+
this.isSource(source.getNode()) and
1511+
this.isSink(sink.getNode()) and
1512+
source.getTaintKind() instanceof GenericFlowType and
1513+
sink.getTaintKind() instanceof GenericFlowType
1514+
}
1515+
1516+
predicate hasFlow(Node source, Node sink) {
1517+
exists(PathNode psource, PathNode psink |
1518+
psource.getNode() = source and
1519+
psink.getNode() = sink and
1520+
this.isSource(source) and
1521+
this.isSink(sink) and
1522+
this.hasFlowPath(psource, psink)
1523+
)
1524+
}
1525+
1526+
}
1527+
1528+
}
1529+
1530+
private class GenericFlowType extends DataFlow::FlowType {
1531+
1532+
GenericFlowType() {
1533+
this = "Generic taint kind" and
1534+
exists(DataFlow::Configuration c)
1535+
}
1536+
1537+
}
1538+
14701539
module TaintTracking {
14711540

14721541
class Source = TaintSource;

0 commit comments

Comments
 (0)