Skip to content

Commit 2ec513c

Browse files
Promote jinja sinks
1 parent fbe93ac commit 2ec513c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Provides classes modeling security-relevant aspects of the `jinja2` PyPI package.
3+
* See https://jinja.palletsprojects.com.
4+
*/
5+
6+
private import python
7+
private import semmle.python.dataflow.new.DataFlow
8+
private import semmle.python.ApiGraphs
9+
private import semmle.python.Concepts
10+
private import semmle.python.frameworks.data.ModelsAsData
11+
12+
module Jinja2 {
13+
/** A call to `jinja2.Template`. */
14+
class Jinja2TemplateConstruction extends TemplateConstruction::Range, API::CallNode {
15+
Jinja2TemplateConstruction() {
16+
this = API::moduleImport("jinja2").getMember("Template").getACall()
17+
}
18+
19+
override DataFlow::Node getSourceArg() { result = this.getArg(0) }
20+
}
21+
22+
module EnvironmentClass {
23+
/** Gets a reference to the `jinja2.Environment` class. */
24+
API::Node classRef() {
25+
result = API::moduleImport("jinja2").getMember("Environment")
26+
or
27+
result = ModelOutput::getATypeNode("jinja.Environment~Subclass").getASubclass*()
28+
}
29+
30+
/** Gets a reference to an instance of `jinja2.Environment`. */
31+
private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) {
32+
t.start() and
33+
result = EnvironmentClass::classRef().getACall()
34+
or
35+
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
36+
}
37+
38+
/** Gets a reference to an instance of `jinja2.Environment`. */
39+
DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) }
40+
41+
/** A call to `jinja2.Environment.from_string`. */
42+
class Jinja2FromStringConstruction extends TemplateConstruction::Range, DataFlow::MethodCallNode
43+
{
44+
Jinja2FromStringConstruction() { this.calls(EnvironmentClass::instance(), "from_string") }
45+
46+
override DataFlow::Node getSourceArg() { result = this.getArg(0) }
47+
}
48+
}
49+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class GenshiMarkupTemplateConstruction extends TemplateConstruction::Range, API:
122122
override DataFlow::Node getSourceArg() { result = this.getArg(0) }
123123
}
124124

125+
//
125126
/** A call to `jinja2.Template`. */
126127
class Jinja2TemplateConstruction extends TemplateConstruction::Range, API::CallNode {
127128
Jinja2TemplateConstruction() {

0 commit comments

Comments
 (0)