Allow python and python3 as stitching commands. They are allowed. Move stitching tests into one file

This commit is contained in:
Beth Probert 2026-03-05 13:35:21 +00:00
parent 98e71207c5
commit 48f6e1b369
3 changed files with 23 additions and 51 deletions

View file

@ -55,8 +55,6 @@ _FORBIDDEN_COMMANDS = {
"sudo",
"sh",
"bash",
"python",
"python3",
"perl",
"ruby",
"php",

View file

@ -21,6 +21,7 @@ from openflexure_microscope_server.stitching import (
PreviewStitcher,
StitcherValidationError,
StitchingSettings,
validate_command,
)
from ..shared_utils.lt_test_utils import LabThingsTestEnv
@ -32,6 +33,28 @@ THIS_DIR: str = os.path.dirname(os.path.realpath(__file__))
MOCK_STITCHER: str = os.path.join(THIS_DIR, "mock_stitching", "mock-stitch.py")
def test_validate_command_success():
"""Test valid commands pass validation."""
validate_command(["openflexure-stitch", "--stitching_mode", "all", "path/to/scan"])
validate_command(["--resize", "0.5", "8192"])
def test_validate_command_forbidden():
"""Test forbidden commands raise error."""
with pytest.raises(
StitcherValidationError, match="Forbidden element 'sudo' detected"
):
validate_command(["sudo", "rm", "-rf", "/"])
with pytest.raises(StitcherValidationError, match="Forbidden element 'sh' detected"):
validate_command(["sh", "-c", "whoami"])
with pytest.raises(
StitcherValidationError, match="Forbidden element 'SUDO' detected"
):
validate_command(["SUDO", "ls"])
def test_base_stitcher():
"""Test the logic in BaseStitcher.

View file

@ -1,49 +0,0 @@
"""Unit tests for stitching command validation."""
import pytest
from openflexure_microscope_server.stitching import (
StitcherValidationError,
validate_command,
)
def test_validate_command_success():
"""Test valid commands pass validation."""
validate_command(["openflexure-stitch", "--stitching_mode", "all", "path/to/scan"])
validate_command(["--resize", "0.5", "8192"])
def test_validate_command_forbidden():
"""Test forbidden commands raise error."""
with pytest.raises(
StitcherValidationError, match="Forbidden element 'sudo' detected"
):
validate_command(["sudo", "rm", "-rf", "/"])
with pytest.raises(
StitcherValidationError, match="Forbidden element 'python' detected"
):
validate_command(["python", "-c", "print('hello')"])
with pytest.raises(
StitcherValidationError, match="Forbidden element 'sh' detected"
):
validate_command(["sh", "-c", "whoami"])
def test_validate_command_unsafe_chars():
"""Test elements with unsafe characters raise error."""
with pytest.raises(StitcherValidationError, match="contains unsafe characters"):
validate_command(["openflexure-stitch", "path;rm -rf /"])
with pytest.raises(StitcherValidationError, match="contains unsafe characters"):
validate_command(["openflexure-stitch", "path&echo hello"])
def test_validate_command_case_insensitive():
"""Test forbidden command check is case-insensitive."""
with pytest.raises(
StitcherValidationError, match="Forbidden element 'SUDO' detected"
):
validate_command(["SUDO", "ls"])