-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtainted_path.py
More file actions
30 lines (26 loc) · 929 Bytes
/
tainted_path.py
File metadata and controls
30 lines (26 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os.path
from flask import Flask, request, abort
app = Flask(__name__)
@app.route("/user_picture1")
def user_picture1():
filename = request.args.get('p')
# BAD: This could read any file on the file system
data = open(filename, 'rb').read()
return data
@app.route("/user_picture2")
def user_picture2():
base_path = '/server/static/images'
filename = request.args.get('p')
# BAD: This could still read any file on the file system
data = open(os.path.join(base_path, filename), 'rb').read()
return data
@app.route("/user_picture3")
def user_picture3():
base_path = '/server/static/images'
filename = request.args.get('p')
#GOOD -- Verify with normalised version of path
fullpath = os.path.normpath(os.path.join(base_path, filename))
if not fullpath.startswith(base_path):
raise Exception("not allowed")
data = open(fullpath, 'rb').read()
return data