Skip to content

Commit

Permalink
Implemented sanitisation to satisfy CodeQL warning on uncontrolled co…
Browse files Browse the repository at this point in the history
…mmand line input
  • Loading branch information
tieneupin committed Jan 14, 2025
1 parent 2bb132a commit bb0ec71
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/murfey/instrument_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from murfey.client.multigrid_control import MultigridController
from murfey.client.rsync import RSyncer
from murfey.client.watchdir_multigrid import MultigridDirWatcher
from murfey.util import sanitise_nonpath, secure_path
from murfey.util import sanitise, sanitise_nonpath, secure_path

Check warning on line 24 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L24

Added line #L24 was not covered by tests
from murfey.util.instrument_models import MultigridWatcherSpec
from murfey.util.models import File, Token

Expand Down Expand Up @@ -278,19 +278,16 @@ class GainReference(BaseModel):

@router.post("/sessions/{session_id}/upload_gain_reference")
def upload_gain_reference(session_id: MurfeySessionID, gain_reference: GainReference):
safe_gain_path = sanitise(str(gain_reference.gain_path))
safe_visit_path = sanitise(gain_reference.visit_path)
safe_destination_dir = sanitise(gain_reference.gain_destination_dir)

Check warning on line 283 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L281-L283

Added lines #L281 - L283 were not covered by tests
cmd = [
"rsync",
str(gain_reference.gain_path),
f"{urlparse(_get_murfey_url(), allow_fragments=False).hostname}::{gain_reference.visit_path}/{gain_reference.gain_destination_dir}/{secure_filename(gain_reference.gain_path.name)}",
safe_gain_path,
f"{urlparse(_get_murfey_url(), allow_fragments=False).hostname}::{safe_visit_path}/{safe_destination_dir}/{secure_filename(gain_reference.gain_path.name)}",
]
gain_rsync = subprocess.run(cmd)

Check failure

Code scanning / CodeQL

Uncontrolled command line Critical

This command line depends on a
user-provided value
.
This command line depends on a user-provided value.

Check warning on line 289 in src/murfey/instrument_server/api.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/instrument_server/api.py#L289

Added line #L289 was not covered by tests
if gain_rsync.returncode:
safe_gain_path = (
str(gain_reference.gain_path).replace("\r\n", "").replace("\n", "")
)
safe_visit_path = gain_reference.visit_path.replace("\r\n", "").replace(
"\n", ""
)
logger.warning(
f"Gain reference file {safe_gain_path} was not successfully transferred to {safe_visit_path}/processing"
)
Expand Down

0 comments on commit bb0ec71

Please sign in to comment.