Skip to content

Commit 2f2d515

Browse files
yueyueLwulan17
authored andcommitted
pyrofork: fix(security): sanitize file names to prevent CWE-22 path traversal
Signed-off-by: wulan17 <[email protected]>
1 parent e9c4067 commit 2f2d515

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

‎pyrogram/methods/messages/download_media.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ async def progress(current, total):
150150
directory, file_name=os.path.split(file_name)
151151
file_name=file_nameormedia_file_nameor""
152152

153+
# Sanitize file name
154+
# CWE-22: Path Traversal
155+
iffile_name:
156+
# Remove any path components, keeping only the basename
157+
file_name=os.path.basename(file_name)
158+
# Remove null bytes which could cause issues
159+
file_name=file_name.replace('\x00', '')
160+
# Handle edge cases
161+
ifnotfile_nameorfile_namein ('.', '..'):
162+
file_name=""
163+
153164
ifnotos.path.isabs(file_name):
154165
directory=self.PARENT_DIR/ (directoryorDEFAULT_DOWNLOAD_DIR)
155166

0 commit comments

Comments
(0)