-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d04af8
commit 6a0ddf1
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @name Server Side Template Injection | ||
* @description Using user-controlled data to create a template can lead to remote code execution or cross site scripting. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @precision high | ||
* @id py/template-injection | ||
* @tags security | ||
* external/cwe/cwe-074 | ||
*/ | ||
|
||
import python | ||
import semmle.python.security.dataflow.TemplateInjectionQuery | ||
import TemplateInjectionFlow::PathGraph | ||
|
||
from TemplateInjectionFlow::PathNode source, TemplateInjectionFlow::PathNode sink | ||
where TemplateInjectionFlow::flowPath(source, sink) | ||
select sink.getNode(), source, sink, "This Template construction depends on $@.", source.getNode(), | ||
"user-provided value" | ||
Check warning Code scanning / CodeQL Missing security metadata Warning
This query file is missing a @security-severity tag.
|
31 changes: 31 additions & 0 deletions
31
python/ql/test/query-tests/Security/CWE-074-TemplateInjection/JinjaSsti.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.urls import path | ||
from django.http import HttpResponse | ||
from jinja2 import Template | ||
from jinja2 import Environment, DictLoader, escape | ||
|
||
|
||
def a(request): | ||
# Load the template | ||
template = request.GET['template'] | ||
t = Template(template) # BAD: Template constructed from user input | ||
name = request.GET['name'] | ||
# Render the template with the context data | ||
html = t.render(name=escape(name)) | ||
return HttpResponse(html) | ||
|
||
def b(request): | ||
import jinja2 | ||
# Load the template | ||
template = request.GET['template'] | ||
env = Environment() | ||
t = env.from_string(template) # BAD: Template constructed from user input | ||
name = request.GET['name'] | ||
# Render the template with the context data | ||
html = t.render(name=escape(name)) | ||
return HttpResponse(html) | ||
|
||
|
||
urlpatterns = [ | ||
path('a', a), | ||
path('b', b) | ||
] |
16 changes: 16 additions & 0 deletions
16
python/ql/test/query-tests/Security/CWE-074-TemplateInjection/TemplateInjection.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
edges | ||
| JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | provenance | AdditionalTaintStep | | ||
| JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | provenance | | | ||
| JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | provenance | AdditionalTaintStep | | ||
| JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | provenance | | | ||
nodes | ||
| JinjaSsti.py:7:7:7:13 | ControlFlowNode for request | semmle.label | ControlFlowNode for request | | ||
| JinjaSsti.py:9:5:9:12 | ControlFlowNode for template | semmle.label | ControlFlowNode for template | | ||
| JinjaSsti.py:10:18:10:25 | ControlFlowNode for template | semmle.label | ControlFlowNode for template | | ||
| JinjaSsti.py:16:7:16:13 | ControlFlowNode for request | semmle.label | ControlFlowNode for request | | ||
| JinjaSsti.py:19:5:19:12 | ControlFlowNode for template | semmle.label | ControlFlowNode for template | | ||
| JinjaSsti.py:21:25:21:32 | ControlFlowNode for template | semmle.label | ControlFlowNode for template | | ||
subpaths | ||
#select | ||
| 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 | | ||
| 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 | |
1 change: 1 addition & 0 deletions
1
python/ql/test/query-tests/Security/CWE-074-TemplateInjection/TemplateInjection.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Security/CWE-074/TemplateInjection.ql |