Skip to content

Commit b77fb76

Browse files
committed
Sanitise inputs for 'gather_upstream_files'
1 parent dddb8f7 commit b77fb76

File tree

1 file changed

+16
-6
lines changed
  • src/murfey/instrument_server

1 file changed

+16
-6
lines changed

src/murfey/instrument_server/api.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,21 @@ 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 = Path(sanitise(str(upstream_file_download.download_dir)))
500+
upstream_instrument = sanitise(upstream_file_download.upstream_instrument)
501+
upstream_visit_path = Path(
502+
sanitise(str(upstream_file_download.upstream_visit_path))
503+
)
504+
495505
# Get the list of files to download
496506
murfey_url = urlparse(_get_murfey_url(), allow_fragments=False)
497507
sanitised_visit_name = sanitise_nonpath(visit_name)
@@ -505,13 +515,13 @@ def gather_upstream_files(
505515
f"{murfey_url.geturl()}{url_path}",
506516
headers={"Authorization": f"Bearer {tokens[session_id]}"},
507517
json={
508-
"upstream_instrument": upstream_file_download.upstream_instrument,
509-
"upstream_visit_path": str(upstream_file_download.upstream_visit_path),
518+
"upstream_instrument": upstream_instrument,
519+
"upstream_visit_path": str(upstream_visit_path),
510520
},
511521
).json()
512522

513523
# Make the download directory and download gathered files
514-
upstream_file_download.download_dir.mkdir(exist_ok=True)
524+
download_dir.mkdir(exist_ok=True)
515525
for upstream_file in upstream_files:
516526
url_path = url_path_for(
517527
"session_control.correlative_router",
@@ -526,9 +536,9 @@ def gather_upstream_files(
526536
stream=True,
527537
)
528538
upstream_file_relative_path = Path(upstream_file).relative_to(
529-
upstream_file_download.upstream_visit_path
539+
upstream_visit_path
530540
)
531-
save_file = upstream_file_download.download_dir / upstream_file_relative_path
541+
save_file = download_dir / upstream_file_relative_path
532542
save_file.parent.mkdir(parents=True, exist_ok=True)
533543
with open(save_file, "wb") as f:
534544
for chunk in file_data.iter_content(chunk_size=32 * 1024**2):

0 commit comments

Comments
 (0)