Skip to content

Commit d5120a7

Browse files
committed
Sanitise symlink path and verify that it's relative to 'rsync_basepath'
1 parent 09d3241 commit d5120a7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/murfey/server/api/file_io_frontend.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
process_gain as _process_gain,
1515
)
1616
from murfey.server.murfey_db import murfey_db
17+
from murfey.util import secure_path
1718
from murfey.util.config import get_machine_config
1819
from murfey.util.db import Session
1920

@@ -51,10 +52,22 @@ async def create_symlink(
5152
instrument_name
5253
]
5354
rsync_basepath = (machine_config.rsync_basepath or Path("")).resolve()
54-
symlink_full_path = rsync_basepath / symlink_params.symlink
55+
symlink_full_path = secure_path(
56+
rsync_basepath / symlink_params.symlink, keep_spaces=True
57+
)
58+
# Verify that the symlink provided does not lead elsewhere
59+
if not symlink_full_path.resolve().is_relative_to(rsync_basepath):
60+
logger.warning(
61+
"Symlink rejected because it will be created in a forbidden location"
62+
)
63+
return ""
64+
# Remove and replace symlink if it exists are 'override' is set
5565
if symlink_full_path.is_symlink() and symlink_params.override:
5666
symlink_full_path.unlink()
67+
# If a file/folder already exists using the desired symlink name, return empty string
5768
if symlink_full_path.exists():
5869
return ""
59-
symlink_full_path.symlink_to(rsync_basepath / symlink_params.target)
70+
symlink_full_path.symlink_to(
71+
secure_path(rsync_basepath / symlink_params.target, keep_spaces=True)
72+
)
6073
return str(symlink_params.symlink)

0 commit comments

Comments
 (0)