Skip to content

Commit de233e8

Browse files
security: use realpath to prevent symlink-based traversal bypass (fixes #262)
1 parent 0fc83d0 commit de233e8

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

fri/server/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,18 @@ def download(dir):
346346
abort(400, description="Directory traversal attempt detected")
347347

348348
dirname = secure_filename(dir) + "/" + secure_filename(sub_folder)
349-
directory_name = os.path.abspath(os.path.join(concore_path, dirname))
349+
concore_real = os.path.realpath(concore_path)
350+
directory_name = os.path.realpath(os.path.join(concore_real, dirname))
351+
if not directory_name.startswith(concore_real + os.sep):
352+
abort(403, description="Access denied")
350353
if not os.path.exists(directory_name):
351354
resp = jsonify({'message': 'Directory not found'})
352355
resp.status_code = 400
353356
return resp
354357

355-
# Ensure final resolved path is within the intended directory
356-
full_path = os.path.abspath(os.path.join(directory_name, safe_path))
357-
if not full_path.startswith(os.path.abspath(directory_name) + os.sep):
358+
# Ensure final resolved path is within the intended directory, resolving symlinks
359+
full_path = os.path.realpath(os.path.join(directory_name, safe_path))
360+
if not full_path.startswith(directory_name + os.sep):
358361
abort(403, description="Access denied")
359362

360363
try:

0 commit comments

Comments
 (0)