Skip to content

Commit 23d64ff

Browse files
authored
Merge pull request github#9135 from tausbn/python-modernise-py-jinja2-autoescape-false
Python: Modernise py/jinja2/autoescape-false
2 parents 4874256 + ea32299 commit 23d64ff

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

python/ql/src/Security/CWE-079/Jinja2WithoutEscaping.ql

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313

1414
import python
15+
import semmle.python.dataflow.new.DataFlow
16+
import semmle.python.ApiGraphs
1517

1618
/*
1719
* Jinja 2 Docs:
@@ -25,25 +27,24 @@ import python
2527
* safe1_tmpl = Template('Hello {{ name }}!', autoescape=True)
2628
*/
2729

28-
ClassValue jinja2EnvironmentOrTemplate() {
29-
result = Value::named("jinja2.Environment")
30+
private API::Node jinja2EnvironmentOrTemplate() {
31+
result = API::moduleImport("jinja2").getMember("Environment")
3032
or
31-
result = Value::named("jinja2.Template")
33+
result = API::moduleImport("jinja2").getMember("Template")
3234
}
3335

34-
ControlFlowNode getAutoEscapeParameter(CallNode call) { result = call.getArgByName("autoescape") }
35-
36-
from CallNode call
36+
from API::CallNode call
3737
where
38-
call.getFunction().pointsTo(jinja2EnvironmentOrTemplate()) and
39-
not exists(call.getNode().getStarargs()) and
40-
not exists(call.getNode().getKwargs()) and
38+
call = jinja2EnvironmentOrTemplate().getACall() and
39+
not exists(call.asCfgNode().(CallNode).getNode().getStarargs()) and
40+
not exists(call.asCfgNode().(CallNode).getNode().getKwargs()) and
4141
(
42-
not exists(getAutoEscapeParameter(call))
42+
not exists(call.getArgByName("autoescape"))
4343
or
44-
exists(Value isFalse |
45-
getAutoEscapeParameter(call).pointsTo(isFalse) and
46-
isFalse.getDefiniteBooleanValue() = false
47-
)
44+
call.getKeywordParameter("autoescape")
45+
.getAValueReachingRhs()
46+
.asExpr()
47+
.(ImmutableLiteral)
48+
.booleanValue() = false
4849
)
4950
select call, "Using jinja2 templates with autoescape=False can potentially allow XSS attacks."

python/ql/test/query-tests/Security/CWE-079-Jinja2WithoutEscaping/Jinja2WithoutEscaping.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
| jinja2_escaping.py:41:5:41:29 | ControlFlowNode for Environment() | Using jinja2 templates with autoescape=False can potentially allow XSS attacks. |
33
| jinja2_escaping.py:43:1:43:3 | ControlFlowNode for E() | Using jinja2 templates with autoescape=False can potentially allow XSS attacks. |
44
| jinja2_escaping.py:44:1:44:15 | ControlFlowNode for E() | Using jinja2 templates with autoescape=False can potentially allow XSS attacks. |
5+
| jinja2_escaping.py:50:13:50:40 | ControlFlowNode for Environment() | Using jinja2 templates with autoescape=False can potentially allow XSS attacks. |
56
| jinja2_escaping.py:53:15:53:43 | ControlFlowNode for Template() | Using jinja2 templates with autoescape=False can potentially allow XSS attacks. |

0 commit comments

Comments
 (0)