Skip to content

Commit 355574e

Browse files
committed
Sanitise and secure incoming file paths
1 parent dddb8f7 commit 355574e

File tree

1 file changed

+15
-7
lines changed
  • src/murfey/instrument_server

1 file changed

+15
-7
lines changed

src/murfey/instrument_server/api.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,19 @@ def gather_upstream_files(
487487
"""
488488
# Check for forbidden characters
489489
if any(c in visit_name for c in ("/", "\\", ":", ";")):
490-
logger.error(f"Forbidden characters are present in the visit name {visit_name}")
490+
logger.error(
491+
f"Forbidden characters are present in visit name {sanitise(visit_name)}"
492+
)
491493
return {
492494
"succss": False,
493495
"detail": "Forbidden characters present in visit name",
494496
}
497+
498+
# Sanitise inputs
499+
download_dir = secure_path(upstream_file_download.download_dir)
500+
upstream_instrument = sanitise(upstream_file_download.upstream_instrument)
501+
upstream_visit_path = secure_path(upstream_file_download.upstream_visit_path)
502+
495503
# Get the list of files to download
496504
murfey_url = urlparse(_get_murfey_url(), allow_fragments=False)
497505
sanitised_visit_name = sanitise_nonpath(visit_name)
@@ -505,13 +513,13 @@ def gather_upstream_files(
505513
f"{murfey_url.geturl()}{url_path}",
506514
headers={"Authorization": f"Bearer {tokens[session_id]}"},
507515
json={
508-
"upstream_instrument": upstream_file_download.upstream_instrument,
509-
"upstream_visit_path": str(upstream_file_download.upstream_visit_path),
516+
"upstream_instrument": upstream_instrument,
517+
"upstream_visit_path": str(upstream_visit_path),
510518
},
511519
).json()
512520

513521
# Make the download directory and download gathered files
514-
upstream_file_download.download_dir.mkdir(exist_ok=True)
522+
download_dir.mkdir(exist_ok=True)
515523
for upstream_file in upstream_files:
516524
url_path = url_path_for(
517525
"session_control.correlative_router",
@@ -525,10 +533,10 @@ def gather_upstream_files(
525533
headers={"Authorization": f"Bearer {tokens[session_id]}"},
526534
stream=True,
527535
)
528-
upstream_file_relative_path = Path(upstream_file).relative_to(
529-
upstream_file_download.upstream_visit_path
536+
upstream_file_relative_path = secure_path(
537+
Path(upstream_file).relative_to(upstream_visit_path)
530538
)
531-
save_file = upstream_file_download.download_dir / upstream_file_relative_path
539+
save_file = download_dir / upstream_file_relative_path
532540
save_file.parent.mkdir(parents=True, exist_ok=True)
533541
with open(save_file, "wb") as f:
534542
for chunk in file_data.iter_content(chunk_size=32 * 1024**2):

0 commit comments

Comments
 (0)