From 33ca29a4f822a8d09ac83bea7e458e182b5ec573 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Wed, 10 Jul 2024 17:33:15 +0200 Subject: [PATCH] Fix MessageException handling in get_edit Fixes https://sentry.galaxyproject.org/share/issue/18f12e228c8d4cf0ac62001b5f5ad938/: ``` Message Uncaught exception in exposed API method: Stack Trace Newest Error: Odd-length string File "encodings/hex_codec.py", line 19, in hex_decode return (binascii.a2b_hex(input), len(input)) Error: decoding with 'hex' codec failed (Error: Odd-length string) File "galaxy/security/idencoding.py", line 94, in decode_id return int(unicodify(id_cipher.decrypt(codecs.decode(obj_id, "hex"))).lstrip("!")) MalformedId: Wrong id ( undefined ) specified, unable to decode. File "galaxy/web/framework/decorators.py", line 206, in decorator rval = func(self, trans, *args, **kwargs) File "galaxy/webapps/galaxy/controllers/dataset.py", line 165, in get_edit data, message = self._get_dataset_for_edit(trans, dataset_id) File "galaxy/webapps/galaxy/controllers/dataset.py", line 433, in _get_dataset_for_edit id = self.decode_id(dataset_id) File "galaxy/webapps/base/controller.py", line 114, in decode_id return managers_base.decode_id(self.app, id) File "galaxy/managers/base.py", line 156, in decode_id return decode_with_security(app.security, id, kind=kind) File "galaxy/managers/base.py", line 160, in decode_with_security return security.decode_id(str(id), kind=kind) File "galaxy/security/idencoding.py", line 100, in decode_id raise galaxy.exceptions.MalformedId( ``` --- .../webapps/galaxy/controllers/dataset.py | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/galaxy/webapps/galaxy/controllers/dataset.py b/lib/galaxy/webapps/galaxy/controllers/dataset.py index e1c2f5b79a9a..d3e770fae928 100644 --- a/lib/galaxy/webapps/galaxy/controllers/dataset.py +++ b/lib/galaxy/webapps/galaxy/controllers/dataset.py @@ -19,7 +19,10 @@ encode_dataset_user, ) from galaxy.datatypes.sniff import guess_ext -from galaxy.exceptions import RequestParameterInvalidException +from galaxy.exceptions import ( + MessageException, + RequestParameterInvalidException, +) from galaxy.managers.hdas import ( HDADeserializer, HDAManager, @@ -158,7 +161,7 @@ def display( trans.response.headers.update(headers) return display_data - @web.legacy_expose_api_anonymous + @web.expose_api_anonymous def get_edit(self, trans, dataset_id=None, **kwd): """Produces the input definitions available to modify dataset attributes""" status = None @@ -168,8 +171,8 @@ def get_edit(self, trans, dataset_id=None, **kwd): if self._can_access_dataset(trans, data): if data.state == trans.model.Dataset.states.UPLOAD: - return self.message_exception( - trans, "Please wait until this dataset finishes uploading before attempting to edit its metadata." + raise MessageException( + "Please wait until this dataset finishes uploading before attempting to edit its metadata." ) # let's not overwrite the imported datatypes module with the variable datatypes? # the built-in 'id' is overwritten in lots of places as well @@ -331,8 +334,8 @@ def get_edit(self, trans, dataset_id=None, **kwd): "permission_disable": permission_disable, } else: - return self.message_exception( - trans, f"You do not have permission to edit this dataset's ( id: {dataset_id} ) information." + raise MessageException( + "You do not have permission to edit this dataset's ( id: {dataset_id} ) information." ) @web.expose_api_anonymous @@ -378,9 +381,8 @@ def set_edit(self, trans, payload=None, **kwd): if data.datatype.is_datatype_change_allowed(): # prevent modifying datatype when dataset is queued or running as input/output if not data.ok_to_edit_metadata(): - return self.message_exception( - trans, - "This dataset is currently being used as input or output. You cannot change datatype until the jobs have completed or you have canceled them.", + raise MessageException( + "This dataset is currently being used as input or output. You cannot change datatype until the jobs have completed or you have canceled them." ) else: path = data.dataset.get_file_name() @@ -397,7 +399,7 @@ def set_edit(self, trans, payload=None, **kwd): trans.app.job_manager.enqueue(job, tool=trans.app.datatypes_registry.set_external_metadata_tool) message = f"Detection was finished and changed the datatype to {datatype}." else: - return self.message_exception(trans, f'Changing datatype "{data.extension}" is not allowed.') + raise MessageException(f'Changing datatype "{data.extension}" is not allowed.') elif operation == "autodetect": # The user clicked the Auto-detect button on the 'Edit Attributes' form self.hda_manager.set_metadata(trans, data, overwrite=True) @@ -408,7 +410,7 @@ def set_edit(self, trans, payload=None, **kwd): try: message = data.datatype.convert_dataset(trans, data, target_type) except DatatypeConverterNotFoundException as e: - return self.message_exception(trans, str(e)) + raise MessageException(str(e)) elif operation == "permission": # Adapt form request to API - style. payload_permissions = {} @@ -425,7 +427,7 @@ def set_edit(self, trans, payload=None, **kwd): ) message = "Your changes completed successfully." else: - return self.message_exception(trans, f"Invalid operation identifier ({operation}).") + raise MessageException(f"Invalid operation identifier ({operation}).") return {"status": status, "message": sanitize_text(message)} def _get_dataset_for_edit(self, trans, dataset_id):