From 48f6e1b369ceb75e502478fcfeb59ff8df17032f Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 5 Mar 2026 13:35:21 +0000 Subject: [PATCH] Allow python and python3 as stitching commands. They are allowed. Move stitching tests into one file --- .../stitching.py | 2 - tests/unit_tests/test_stitching.py | 23 +++++++++ tests/unit_tests/test_stitching_validation.py | 49 ------------------- 3 files changed, 23 insertions(+), 51 deletions(-) delete mode 100644 tests/unit_tests/test_stitching_validation.py diff --git a/src/openflexure_microscope_server/stitching.py b/src/openflexure_microscope_server/stitching.py index 0f6eb5e7..bf5da43c 100644 --- a/src/openflexure_microscope_server/stitching.py +++ b/src/openflexure_microscope_server/stitching.py @@ -55,8 +55,6 @@ _FORBIDDEN_COMMANDS = { "sudo", "sh", "bash", - "python", - "python3", "perl", "ruby", "php", diff --git a/tests/unit_tests/test_stitching.py b/tests/unit_tests/test_stitching.py index 349d975c..5ae8842e 100644 --- a/tests/unit_tests/test_stitching.py +++ b/tests/unit_tests/test_stitching.py @@ -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. diff --git a/tests/unit_tests/test_stitching_validation.py b/tests/unit_tests/test_stitching_validation.py deleted file mode 100644 index 0223847d..00000000 --- a/tests/unit_tests/test_stitching_validation.py +++ /dev/null @@ -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"])