Skip to content

Commit 35a53fa

Browse files
authored
Merge pull request github#12183 from RasmusWL/example-update
Python: Update a few examples so queries work on them
2 parents d3e7389 + dc5bb4f commit 35a53fa

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
import os.path
2+
from flask import Flask, request, abort
23

4+
app = Flask(__name__)
35

4-
urlpatterns = [
5-
# Route to user_picture
6-
url(r'^user-pic1$', user_picture1, name='user-picture1'),
7-
url(r'^user-pic2$', user_picture2, name='user-picture2'),
8-
url(r'^user-pic3$', user_picture3, name='user-picture3')
9-
]
10-
11-
12-
def user_picture1(request):
13-
"""A view that is vulnerable to malicious file access."""
14-
filename = request.GET.get('p')
6+
@app.route("/user_picture1")
7+
def user_picture1():
8+
filename = request.args.get('p')
159
# BAD: This could read any file on the file system
1610
data = open(filename, 'rb').read()
17-
return HttpResponse(data)
11+
return data
1812

19-
def user_picture2(request):
20-
"""A view that is vulnerable to malicious file access."""
13+
@app.route("/user_picture2")
14+
def user_picture2():
2115
base_path = '/server/static/images'
22-
filename = request.GET.get('p')
16+
filename = request.args.get('p')
2317
# BAD: This could still read any file on the file system
2418
data = open(os.path.join(base_path, filename), 'rb').read()
25-
return HttpResponse(data)
19+
return data
2620

27-
def user_picture3(request):
28-
"""A view that is not vulnerable to malicious file access."""
21+
@app.route("/user_picture3")
22+
def user_picture3():
2923
base_path = '/server/static/images'
30-
filename = request.GET.get('p')
24+
filename = request.args.get('p')
3125
#GOOD -- Verify with normalised version of path
3226
fullpath = os.path.normpath(os.path.join(base_path, filename))
3327
if not fullpath.startswith(base_path):
34-
raise SecurityException()
28+
raise Exception("not allowed")
3529
data = open(fullpath, 'rb').read()
36-
return HttpResponse(data)
30+
return data
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
1+
import sys
22
import tarfile
33

4-
with tarfile.open('archive.zip') as tar:
4+
with tarfile.open(sys.argv[1]) as tar:
55
#BAD : This could write any file on the filesystem.
66
for entry in tar:
77
tar.extract(entry, "/tmp/unpack/")

python/ql/src/Security/CWE-022/examples/tarslip_good.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
1+
import sys
22
import tarfile
33
import os.path
44

5-
with tarfile.open('archive.zip') as tar:
5+
with tarfile.open(sys.argv[1]) as tar:
66
for entry in tar:
77
#GOOD: Check that entry is safe
88
if os.path.isabs(entry.name) or ".." in entry.name:

0 commit comments

Comments
 (0)