Skip to content

Commit c432284

Browse files
committed
Polish qhelp
1 parent 12ccd7e commit c432284

File tree

3 files changed

+72
-47
lines changed

3 files changed

+72
-47
lines changed
Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
1-
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" qhelp.dtd">
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
24
<qhelp>
3-
<overview>
4-
<p>
5-
Constructing a regular expression with unsanitized user input is dangerous as a malicious user may
6-
be able to modify the meaning of the expression. In particular, such a user may be able to provide
7-
a regular expression fragment that takes exponential time in the worst case, and use that to
8-
perform a Denial of Service attack.
9-
</p>
10-
</overview>
5+
<overview>
6+
<p>
7+
Constructing a regular expression with unsanitized user input is dangerous as a malicious user may
8+
be able to modify the meaning of the expression. In particular, such a user may be able to provide
9+
a regular expression fragment that takes exponential time in the worst case, and use that to
10+
perform a Denial of Service attack.
11+
</p>
12+
</overview>
1113

12-
<recommendation>
13-
<p>
14-
Before embedding user input into a regular expression, use a sanitization function such as
15-
<code>re.escape</code> to escape meta-characters that have a special meaning regarding
16-
regular expressions' syntax.
17-
</p>
18-
</recommendation>
14+
<recommendation>
15+
<p>
16+
Before embedding user input into a regular expression, use a sanitization function such as
17+
<code>re.escape</code> to escape meta-characters that have a special meaning regarding
18+
regular expressions' syntax.
19+
</p>
20+
</recommendation>
1921

20-
<example>
21-
<p>
22-
The following examples are based on a simple Flask web server environment.
23-
</p>
24-
<p>
25-
The following example shows a HTTP request parameter that is used to construct a regular expression
26-
without sanitizing it first:
27-
</p>
28-
<sample src="unit_tests/re_bad.py" />
29-
<p>
30-
Instead, the request parameter should be sanitized first, for example using the function
31-
<code>re.escape</code>. This ensures that the user cannot insert characters which have a
32-
special meaning in regular expressions.
33-
</p>
34-
<sample src="unit_tests/re_good.py" />
35-
</example>
22+
<example>
23+
<p>
24+
The following examples are based on a simple Flask web server environment.
25+
</p>
26+
<p>
27+
The following example shows a HTTP request parameter that is used to construct a regular expression
28+
without sanitizing it first:
29+
</p>
30+
<sample src="re_bad.py" />
31+
<p>
32+
Instead, the request parameter should be sanitized first, for example using the function
33+
<code>re.escape</code>. This ensures that the user cannot insert characters which have a
34+
special meaning in regular expressions.
35+
</p>
36+
<sample src="re_good.py" />
37+
</example>
3638

37-
<references>
38-
<li>
39-
OWASP:
40-
<a href="https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS">Regular expression Denial of Service - ReDoS</a>.
41-
</li>
42-
<li>
43-
Wikipedia: <a href="https://en.wikipedia.org/wiki/ReDoS">ReDoS</a>.
44-
</li>
45-
<li>
46-
Python docs: <a href="https://docs.python.org/3/library/re.html">re</a>.
47-
</li>
48-
<li>
49-
SonarSource: <a href="https://rules.sonarsource.com/python/type/Vulnerability/RSPEC-2631">RSPEC-2631</a>
50-
</li>
51-
</references>
39+
<references>
40+
<li>OWASP: <a href="https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS">Regular expression Denial of Service - ReDoS</a>.</li>
41+
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/ReDoS">ReDoS</a>.</li>
42+
<li>Python docs: <a href="https://docs.python.org/3/library/re.html">re</a>.</li>
43+
<li>SonarSource: <a href="https://rules.sonarsource.com/python/type/Vulnerability/RSPEC-2631">RSPEC-2631</a>.</li>
44+
</references>
5245
</qhelp>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import request, Flask
2+
import re
3+
4+
5+
@app.route("/direct")
6+
def direct():
7+
unsafe_pattern = request.args["pattern"]
8+
re.search(unsafe_pattern, "")
9+
10+
11+
@app.route("/compile")
12+
def compile():
13+
unsafe_pattern = request.args["pattern"]
14+
compiled_pattern = re.compile(unsafe_pattern)
15+
compiled_pattern.search("")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from flask import request, Flask
2+
import re
3+
4+
5+
@app.route("/direct")
6+
def direct():
7+
unsafe_pattern = request.args['pattern']
8+
safe_pattern = re.escape(unsafe_pattern)
9+
re.search(safe_pattern, "")
10+
11+
12+
@app.route("/compile")
13+
def compile():
14+
unsafe_pattern = request.args['pattern']
15+
safe_pattern = re.escape(unsafe_pattern)
16+
compiled_pattern = re.compile(safe_pattern)
17+
compiled_pattern.search("")

0 commit comments

Comments
 (0)