Change make_path_safe to is_path_safe and update tests to reflect this

This commit is contained in:
Beth Probert 2026-03-09 15:45:32 +00:00
parent 3776828699
commit 6d66dd0ed8
3 changed files with 38 additions and 62 deletions

View file

@ -18,7 +18,7 @@ from pydantic import BaseModel
import labthings_fastapi as lt
from openflexure_microscope_server.utilities import make_path_safe
from openflexure_microscope_server.utilities import is_path_safe
IS_WINDOWS = os.name == "nt"
@ -68,7 +68,7 @@ def validate_command(cmd: list[str]) -> None:
"""Validate that the command only contains characters that are allowed in a path.
The values in the commands should be numbers, commandline flags, paths, and
executables. All of these should be allowed by ``make_path_safe``.
executables. All of these should be allowed by ``is_path_safe``.
This also checks against a blacklist of forbidden commands for defense-in-depth.
@ -82,8 +82,7 @@ def validate_command(cmd: list[str]) -> None:
)
# Ensure characters are safe for a path component
_, error_raised = make_path_safe(element)
if error_raised:
if not is_path_safe(element):
raise StitcherValidationError(
f"Invalid stitching command: Element '{element}' contains unsafe characters."
)
@ -155,8 +154,7 @@ class BaseStitcher:
:raises RuntimeError: if inputs are unsafe.
"""
_, error_raised = make_path_safe(self.images_dir)
if error_raised:
if not is_path_safe(self.images_dir):
raise StitcherValidationError(
"Invalid directory path: Contains unsafe characters."
)

View file

@ -110,7 +110,7 @@ _WINDOWS_RESERVED_NAMES = {
}
def make_path_safe(unsafe_path_string: str) -> tuple[str, bool]:
def is_path_safe(unsafe_path_string: str) -> bool:
"""Check if a file path has any unsafe elements in it.
The path is not coerced into a safe form because if we ask
@ -119,8 +119,7 @@ def make_path_safe(unsafe_path_string: str) -> tuple[str, bool]:
:param unsafe_path_string: The original path string to sanitise.
:returns: The input string unchanged and a boolean indicating if
any unsafe features were found.
:returns: A boolean indicating if any unsafe features were found.
"""
# Split by separators first to sanitise components independently
@ -195,14 +194,13 @@ def make_path_safe(unsafe_path_string: str) -> tuple[str, bool]:
)
reserved_word = True
warning_raised = (
return not (
unsafe_relative_path
or trailing_dots
or trailing_whitespace
or unsafe_characters
or reserved_word
)
return unsafe_path_string, warning_raised
def make_name_safe(unsafe_name_string: str) -> str: