Skip to content

Commit 31ac33e

Browse files
authored
Merge pull request #528 from taus-semmle/python-flask-debug
Python: Implement check for flask debug mode.
2 parents 2b340b4 + 7f94c25 commit 31ac33e

File tree

9 files changed

+115
-1
lines changed

9 files changed

+115
-1
lines changed

change-notes/1.19/analysis-python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ A new predicate `Stmt.getAnEntryNode()` has been added to make it easier to writ
5656

5757
| **Query** | **Tags** | **Purpose** |
5858
|-----------------------------|-----------|--------------------------------------------------------------------|
59+
| Flask app is run in debug mode (`py/flask-debug`) | security, external/cwe/cwe-215, external/cwe/cwe-489 | Finds instances where a Flask application is run in debug mode. Enabled on LGTM by default. |
5960
| Information exposure through an exception (`py/stack-trace-exposure`) | security, external/cwe/cwe-209, external/cwe/cwe-497 | Finds instances where information about an exception may be leaked to an external user. Enabled on LGTM by default. |
6061
| Request without certificate validation (`py/request-without-cert-validation`) | security, external/cwe/cwe-295 | Finds requests where certificate verification has been explicitly turned off, possibly allowing man-in-the-middle attacks. Not enabled on LGTM by default. |
6162

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/crash')
6+
def main():
7+
raise Exception()
8+
9+
app.run(debug=True)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Running a Flask application with debug mode enabled may allow an
8+
attacker to gain access through the Werkzeug debugger.
9+
</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>
15+
Ensure that Flask applications that are run in a production
16+
environment have debugging disabled.
17+
</p>
18+
19+
</recommendation>
20+
<example>
21+
22+
<p>
23+
Running the following code starts a Flask webserver that has
24+
debugging enabled. By visiting <code>/crash</code>, it is possible
25+
to gain access to the debugger, and run arbitrary code through the
26+
interactive debugger.
27+
</p>
28+
29+
<sample src="FlaskDebug.py" />
30+
31+
</example>
32+
33+
<references>
34+
<li>Flask Quickstart Documentation: <a href="http://flask.pocoo.org/docs/1.0/quickstart/#debug-mode">Debug Mode</a>.</li>
35+
<li>Werkzeug Documentation: <a href="http://werkzeug.pocoo.org/docs/0.14/debug/">Debugging Applications</a>.</li>
36+
</references>
37+
38+
</qhelp>
39+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @name Flask app is run in debug mode
3+
* @description Running a Flask app in debug mode may allow an attacker to run arbitrary code through the Werkzeug debugger.
4+
* @kind problem
5+
* @problem.severity error
6+
* @precision high
7+
* @id py/flask-debug
8+
* @tags security
9+
* external/cwe/cwe-215
10+
* external/cwe/cwe-489
11+
*/
12+
13+
import python
14+
15+
import semmle.python.web.flask.General
16+
17+
18+
from CallNode call, Object isTrue
19+
where
20+
call = theFlaskClass().declaredAttribute("run").(FunctionObject).getACall() and
21+
call.getArgByName("debug").refersTo(isTrue) and
22+
isTrue.booleanValue() = true
23+
select call, "A Flask app appears to be run in debug mode. This may allow an attacker to run arbitrary code through the debugger."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.py:10:1:10:19 | ControlFlowNode for Attribute() | A Flask app appears to be run in debug mode. This may allow an attacker to run arbitrary code through the debugger. |
2+
| test.py:25:1:25:20 | ControlFlowNode for Attribute() | A Flask app appears to be run in debug mode. This may allow an attacker to run arbitrary code through the debugger. |
3+
| test.py:29:1:29:20 | ControlFlowNode for Attribute() | A Flask app appears to be run in debug mode. This may allow an attacker to run arbitrary code through the debugger. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE-215/FlaskDebug.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=2 -p ../lib
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/crash')
6+
def main():
7+
raise Exception()
8+
9+
# bad
10+
app.run(debug=True)
11+
12+
# okay
13+
app.run()
14+
app.run(debug=False)
15+
16+
# also okay
17+
run(debug=True)
18+
19+
app.notrun(debug=True)
20+
21+
# a slightly more involved example using flow and truthy values
22+
23+
DEBUG = True
24+
25+
app.run(debug=DEBUG)
26+
27+
DEBUG = 1
28+
29+
app.run(debug=DEBUG)
30+
31+
if False:
32+
app.run(debug=True)
33+
34+
# false negative
35+
36+
runapp = app.run
37+
runapp(debug=True)

python/ql/test/query-tests/Security/lib/flask/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
class Flask(object):
4-
pass
4+
def run(self, *args, **kwargs): pass
55

66
from .globals import request
77

0 commit comments

Comments
 (0)