Skip to content

Commit 47292c5

Browse files
fix(smb): add path sanitization helper and apply to download_folder
1 parent 0806923 commit 47292c5

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

nxc/helpers/path.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pathlib import PurePosixPath
2+
3+
4+
def sanitize_filename(name: str) -> str:
5+
"""Strip path traversal components from an SMB filename.
6+
7+
Follows the pattern from spider_plus.py — filters '..' and '.' from
8+
PurePosixPath.parts to prevent directory traversal attacks from
9+
malicious SMB servers.
10+
"""
11+
parts = PurePosixPath(name.replace("\\", "/")).parts
12+
clean = [p for p in parts if p not in ("..", ".", "/")]
13+
return str(PurePosixPath(*clean)) if clean else ""

nxc/protocols/smb.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import re
55
import struct
66
import ipaddress
7+
from pathlib import Path
8+
9+
from nxc.helpers.path import sanitize_filename
710
from Cryptodome.Hash import MD4
811
from textwrap import dedent
912

@@ -2005,7 +2008,10 @@ def download_folder(self, folder, dest, recursive=False, silent=False, base_dir=
20052008
self.logger.display(f"Created empty directory '{local_folder_path}'")
20062009

20072010
for item in filtered_items:
2008-
item_name = item.get_longname()
2011+
item_name = sanitize_filename(item.get_longname())
2012+
if not item_name:
2013+
self.logger.fail(f"Path traversal detected in '{item.get_longname()}', skipping")
2014+
continue
20092015
dir_path = ntpath.normpath(ntpath.join(normalized_folder, item_name))
20102016
self.logger.debug(f"Parsing item: {item_name}, {dir_path}")
20112017

@@ -2015,6 +2021,11 @@ def download_folder(self, folder, dest, recursive=False, silent=False, base_dir=
20152021
elif not item.is_directory():
20162022
remote_file_path = ntpath.join(folder, item_name)
20172023
local_file_path = os.path.join(local_folder_path, item_name)
2024+
# Defense-in-depth: verify path stays under destination
2025+
resolved = Path(local_file_path).resolve()
2026+
if not str(resolved).startswith(str(Path(dest).resolve()) + os.sep):
2027+
self.logger.fail(f"Path traversal detected in '{item_name}', skipping")
2028+
continue
20182029
self.logger.debug(f"{dest=} {remote_file_path=} {relative_path=} {local_folder_path=} {local_file_path=}")
20192030

20202031
try:

0 commit comments

Comments
 (0)