|
| 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 | +} |
0 commit comments