Skip to content

Commit c82cfbd

Browse files
committed
Should secure the path not the name
1 parent 6dfe847 commit c82cfbd

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from PIL import Image
66
from sqlalchemy.exc import NoResultFound
77
from sqlmodel import Session, select
8-
from werkzeug.utils import secure_filename
98

109
from murfey.server import _murfey_id, _transport_object, sanitise
1110
from murfey.server.api.auth import MurfeySessionID
11+
from murfey.util import secure_path
1212
from murfey.util.config import get_machine_config, get_microscope
1313
from murfey.util.db import (
1414
AutoProcProgram,
@@ -72,11 +72,8 @@ def register_grid_square(
7272
else:
7373
# mock up response so that below still works
7474
gs_ispyb_response = {"success": False, "return_value": None}
75-
secured_grid_square_image_path = secure_filename(grid_square_params.image)
76-
if (
77-
secured_grid_square_image_path
78-
and Path(secured_grid_square_image_path).is_file()
79-
):
75+
secured_grid_square_image_path = secure_path(Path(grid_square_params.image))
76+
if secured_grid_square_image_path and secured_grid_square_image_path.is_file():
8077
jpeg_size = Image.open(secured_grid_square_image_path).size
8178
else:
8279
jpeg_size = (0, 0)
@@ -98,7 +95,7 @@ def register_grid_square(
9895
thumbnail_size_x=grid_square_params.thumbnail_size_x or jpeg_size[0],
9996
thumbnail_size_y=grid_square_params.thumbnail_size_y or jpeg_size[1],
10097
pixel_size=grid_square_params.pixel_size,
101-
image=secured_grid_square_image_path,
98+
image=str(secured_grid_square_image_path),
10299
)
103100
murfey_db.add(grid_square)
104101
murfey_db.commit()
@@ -124,8 +121,8 @@ def register_foil_hole(
124121
f"Foil hole {sanitise(str(foil_hole_params.name))} could not be registered as grid square {sanitise(str(gs_name))} was not found"
125122
)
126123
return
127-
secured_foil_hole_image_path = secure_filename(foil_hole_params.image)
128-
if foil_hole_params.image and Path(secured_foil_hole_image_path).is_file():
124+
secured_foil_hole_image_path = secure_path(Path(foil_hole_params.image))
125+
if foil_hole_params.image and secured_foil_hole_image_path.is_file():
129126
jpeg_size = Image.open(secured_foil_hole_image_path).size
130127
else:
131128
jpeg_size = (0, 0)
@@ -188,7 +185,7 @@ def register_foil_hole(
188185
thumbnail_size_x=foil_hole_params.thumbnail_size_x or jpeg_size[0],
189186
thumbnail_size_y=foil_hole_params.thumbnail_size_y or jpeg_size[1],
190187
pixel_size=foil_hole_params.pixel_size,
191-
image=secured_foil_hole_image_path,
188+
image=str(secured_foil_hole_image_path),
192189
)
193190
murfey_db.add(foil_hole)
194191
murfey_db.commit()

tests/workflows/spa/test_flush_spa_preprocess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_register_grid_square_update_add_locations(mock_transport, start_postgre
3636
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db)
3737

3838
# Check this would have updated ispyb
39-
mock_transport.do_update_grid_square.assert_called_with(101, new_parameters)
39+
mock_transport.do_update_grid_square.assert_called_with(1, new_parameters)
4040

4141
# Confirm the database was updated
4242
with Session(engine) as murfey_db:
@@ -77,7 +77,7 @@ def test_register_grid_square_update_add_nothing(mock_transport, start_postgres)
7777
flush_spa_preprocess.register_grid_square(2, 101, new_parameters, murfey_db)
7878

7979
# Check this would have updated ispyb
80-
mock_transport.do_update_grid_square.assert_called_with(101, new_parameters)
80+
mock_transport.do_update_grid_square.assert_called_with(1, new_parameters)
8181

8282
# Confirm the database was not updated
8383
with Session(engine) as murfey_db:

0 commit comments

Comments
 (0)