Skip to content

Commit ada4ad3

Browse files
Copy template injection to standard pack + add jinja sinks
1 parent 6a3e34c commit ada4ad3

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,32 @@ class LdapFilterEscaping extends Escaping {
861861
LdapFilterEscaping() { super.getKind() = Escaping::getLdapFilterKind() }
862862
}
863863

864+
/**
865+
* A data-flow node that constructs a template in a templating engine.
866+
*
867+
* Extend this class to refine existing API models. If you want to model new APIs,
868+
* extend `TemplateConstruction::Range` instead.
869+
*/
870+
class TemplateConstruction extends DataFlow::Node instanceof TemplateConstruction::Range {
871+
/** Gets the argument that specifies the template source. */
872+
DataFlow::Node getSourceArg() { result = super.getSourceArg() }
873+
}
874+
875+
/** Provides classes for modelling template construction APIs. */
876+
module TemplateConstruction {
877+
/**
878+
* A data-flow node that constructs a template in a templating engine.
879+
*
880+
* Extend this class to model new APIs. If you want to refine existing API models,
881+
* extend `TemplateConstruction` instead.
882+
*/
883+
abstract class Range extends DataFlow::Node {
884+
/** Gets the argument that specifies the template source. */
885+
abstract DataFlow::Node getSourceArg();
886+
}
887+
}
888+
889+
864890
/** Provides classes for modeling HTTP-related APIs. */
865891
module Http {
866892
/** Gets an HTTP verb, in upper case */

python/ql/lib/semmle/python/frameworks/Jinja2.qll

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Provides default sources, sinks and sanitizers for detecting
3+
* "template injection"
4+
* vulnerabilities, as well as extension points for adding your own.
5+
*/
6+
7+
private import python
8+
private import semmle.python.dataflow.new.DataFlow
9+
private import semmle.python.Concepts
10+
private import semmle.python.dataflow.new.RemoteFlowSources
11+
private import semmle.python.dataflow.new.BarrierGuards
12+
13+
/**
14+
* Provides default sources, sinks and sanitizers for detecting
15+
* "template injection"
16+
* vulnerabilities, as well as extension points for adding your own.
17+
*/
18+
module TemplateInjection {
19+
/**
20+
* A data flow source for "template injection" vulnerabilities.
21+
*/
22+
abstract class Source extends DataFlow::Node { }
23+
24+
/**
25+
* A data flow sink for "template injection" vulnerabilities.
26+
*/
27+
abstract class Sink extends DataFlow::Node { }
28+
29+
/**
30+
* A sanitizer for "template injection" vulnerabilities.
31+
*/
32+
abstract class Sanitizer extends DataFlow::Node { }
33+
34+
/**
35+
* An active threat-model source, considered as a flow source.
36+
*/
37+
private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { }
38+
39+
/**
40+
* A SQL statement of a SQL construction, considered as a flow sink.
41+
*/
42+
class TemplateConstructionAsSink extends Sink {
43+
TemplateConstructionAsSink() { this = any(TemplateConstruction c).getSourceArg() }
44+
}
45+
46+
/**
47+
* A comparison with a constant, considered as a sanitizer-guard.
48+
*/
49+
class ConstCompareAsSanitizerGuard extends Sanitizer, ConstCompareBarrier { }
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides a taint-tracking configuration for detecting "template injection" vulnerabilities.
3+
*
4+
* Note, for performance reasons: only import this file if
5+
* `TemplateInjectionFlow` is needed, otherwise
6+
* `TemplateInjectionCustomizations` should be imported instead.
7+
*/
8+
9+
private import python
10+
import semmle.python.dataflow.new.DataFlow
11+
import semmle.python.dataflow.new.TaintTracking
12+
import TemplateInjectionCustomizations::TemplateInjection
13+
14+
module TemplateInjectionConfig implements DataFlow::ConfigSig {
15+
predicate isSource(DataFlow::Node node) { node instanceof Source }
16+
17+
predicate isSink(DataFlow::Node node) { node instanceof Sink }
18+
19+
predicate isBarrierIn(DataFlow::Node node) { node instanceof Sanitizer }
20+
}
21+
22+
module TemplateInjectionFlow = TaintTracking::Global<TemplateInjectionConfig>;

python/ql/src/experimental/Security/CWE-074/TemplateConstructionConcept.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ class Jinja2TemplateConstruction extends TemplateConstruction::Range, API::CallN
134134
/** A call to `jinja2.from_string`. */
135135
class Jinja2FromStringConstruction extends TemplateConstruction::Range, API::CallNode {
136136
Jinja2FromStringConstruction() {
137-
this = API::moduleImport("jinja2").getMember("from_string").getACall()
137+
this =
138+
API::moduleImport("jinja2")
139+
.getMember("Environment")
140+
.getReturn()
141+
.getMember("from_string")
142+
.getACall()
138143
}
139144

140145
override DataFlow::Node getSourceArg() { result = this.getArg(0) }

python/ql/src/experimental/Security/CWE-074/TemplateInjectionCustomizations.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
private import python
88
private import semmle.python.dataflow.new.DataFlow
9-
private import semmle.python.Concepts
9+
private import semmle.python.Concepts as C
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
1212
private import TemplateConstructionConcept
@@ -40,7 +40,7 @@ module TemplateInjection {
4040
/**
4141
* An active threat-model source, considered as a flow source.
4242
*/
43-
private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { }
43+
private class ActiveThreatModelSourceAsSource extends Source, C::ActiveThreatModelSource { }
4444

4545
/**
4646
* A SQL statement of a SQL construction, considered as a flow sink.

0 commit comments

Comments
 (0)