Skip to content

Commit 44b70ee

Browse files
committed
Resolved CodeQL warnings about log injection and path traversal
1 parent 97e9757 commit 44b70ee

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/murfey/server/demo_api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Settings(BaseSettings):
110110

111111
settings = Settings()
112112

113-
machine_config: dict = {}
113+
machine_config: dict[str, MachineConfig] = {}
114114
if settings.murfey_machine_configuration:
115115
microscope = get_microscope()
116116
machine_config = from_file(Path(settings.murfey_machine_configuration), microscope)
@@ -1297,6 +1297,12 @@ def suggest_path(
12971297
)
12981298
check_path = check_path.parent / f"{check_path.stem}{count}{check_path.suffix}"
12991299

1300+
# Check for path traversal attempt
1301+
if not str(check_path.resolve()).startswith(
1302+
str(machine_config[instrument_name].rsync_basepath)
1303+
):
1304+
raise Exception(f"Path traversal attempt detected: {str(check_path)!r}")
1305+
13001306
# Check previous year to account for the year rolling over during data collection
13011307
if not sanitise_path(check_path).exists():
13021308
base_path_parts = list(params.base_path.parts)
@@ -1313,10 +1319,15 @@ def suggest_path(
13131319
else Path(f"/dls/{get_microscope()}") / base_path
13141320
)
13151321
check_path = check_path.parent / f"{check_path.stem}{count}{check_path.suffix}"
1322+
# Check for path traversal attempt
1323+
if not str(check_path.resolve()).startswith(
1324+
str(machine_config[instrument_name].rsync_basepath)
1325+
):
1326+
raise Exception(f"Path traversal attempt detected: {str(check_path)!r}")
13161327

13171328
# If visit is not in the previous year either, it's a genuine error
13181329
if not check_path.exists():
1319-
log_message = (
1330+
log_message = sanitise(
13201331
"Unable to find current visit folder under "
13211332
f"{str(check_path_prev)!r} or {str(check_path)!r}"
13221333
)

0 commit comments

Comments
 (0)