Skip to content

Commit 1cb01a2

Browse files
Add tests for jinja
1 parent 71ab82d commit 1cb01a2

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Server Side Template Injection
3+
* @description Using user-controlled data to create a template can lead to remote code execution or cross site scripting.
4+
* @kind path-problem
5+
* @problem.severity error
6+
* @precision high
7+
* @id py/template-injection
8+
* @tags security
9+
* external/cwe/cwe-074
10+
*/
11+
12+
import python
13+
import semmle.python.security.dataflow.TemplateInjectionQuery
14+
import TemplateInjectionFlow::PathGraph
15+
16+
from TemplateInjectionFlow::PathNode source, TemplateInjectionFlow::PathNode sink
17+
where TemplateInjectionFlow::flowPath(source, sink)
18+
select sink.getNode(), source, sink, "This Template construction depends on $@.", source.getNode(),
19+
"user-provided value"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django.urls import path
2+
from django.http import HttpResponse
3+
from jinja2 import Template
4+
from jinja2 import Environment, DictLoader, escape
5+
6+
7+
def a(request):
8+
# Load the template
9+
template = request.GET['template']
10+
t = Template(template) # BAD: Template constructed from user input
11+
name = request.GET['name']
12+
# Render the template with the context data
13+
html = t.render(name=escape(name))
14+
return HttpResponse(html)
15+
16+
def b(request):
17+
import jinja2
18+
# Load the template
19+
template = request.GET['template']
20+
env = Environment()
21+
t = env.from_string(template) # BAD: Template constructed from user input
22+
name = request.GET['name']
23+
# Render the template with the context data
24+
html = t.render(name=escape(name))
25+
return HttpResponse(html)
26+
27+
28+
urlpatterns = [
29+
path('a', a),
30+
path('b', b)
31+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
edges
2+
| JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | provenance | AdditionalTaintStep |
3+
| JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | provenance | |
4+
| JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | provenance | AdditionalTaintStep |
5+
| JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | provenance | |
6+
nodes
7+
| JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | semmle.label | ControlFlowNode for request |
8+
| JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | semmle.label | ControlFlowNode for template |
9+
| JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | semmle.label | ControlFlowNode for template |
10+
| JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | semmle.label | ControlFlowNode for request |
11+
| JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | semmle.label | ControlFlowNode for template |
12+
| JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | semmle.label | ControlFlowNode for template |
13+
subpaths
14+
#select
15+
| JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | This Template construction depends on $@. | JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | user-provided value |
16+
| JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | This Template construction depends on $@. | JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | user-provided value |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE-074/TemplateInjection.ql

0 commit comments

Comments
 (0)