Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37d3dbb

Browse filesBrowse files
authored
Improves our solution by providing an extra example
1 parent a453a9a commit 37d3dbb
Copy full SHA for 37d3dbb

File tree

Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed

‎Season-1/Level-3/solution.py

Copy file name to clipboardExpand all lines: Season-1/Level-3/solution.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import os
22

3+
# Example of a secure function that doesn't suffer from path traversal
34
def safe_path(path):
45
base_dir = os.path.dirname(os.path.abspath(__file__))
56
filepath = os.path.normpath(os.path.join(base_dir, path))
67
if base_dir != os.path.commonpath([base_dir, filepath]):
78
return None
89
return filepath
910

11+
# Following the above, this is the secure version of the respective function on code.py
12+
def get_prof_picture(self, path=None):
13+
# setting a profile picture is optional
14+
if not path:
15+
pass
16+
17+
# defends against path traversal attacks
18+
if path.startswith('/') or path.startswith('..'):
19+
return None
20+
21+
# builds path
22+
base_dir = os.path.dirname(os.path.abspath(__file__))
23+
prof_picture_path = os.path.normpath(os.path.join(base_dir, path))
24+
if base_dir != os.path.commonpath([base_dir, prof_picture_path]):
25+
return None
26+
27+
with open(prof_picture_path, 'rb') as pic:
28+
picture = bytearray(pic.read())
29+
30+
# assume that image is returned on screen after this
31+
return prof_picture_path
32+
33+
1034
# Solution explanation
1135

1236
# Path Traversal vulnerability

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.