test_security.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Keys and certificates for tests (KEY1 is a private key of CERT1, etc.)
  2. Generated with:
  3. .. code-block:: console
  4. $ openssl genrsa -des3 -passout pass:test -out key1.key 1024
  5. $ openssl req -new -key key1.key -out key1.csr -passin pass:test
  6. $ cp key1.key key1.key.org
  7. $ openssl rsa -in key1.key.org -out key1.key -passin pass:test
  8. $ openssl x509 -req -days 365 -in cert1.csr \
  9. -signkey key1.key -out cert1.crt
  10. $ rm key1.key.org cert1.csr
  11. """
  12. from __future__ import absolute_import, unicode_literals
  13. import pytest
  14. from case import Mock, mock, patch
  15. from kombu.serialization import disable_insecure_serializers
  16. from celery.exceptions import ImproperlyConfigured, SecurityError
  17. from celery.five import builtins
  18. from celery.security import disable_untrusted_serializers, setup_security
  19. from celery.security.utils import reraise_errors
  20. from kombu.serialization import registry
  21. from .case import SecurityCase
  22. class test_security(SecurityCase):
  23. def teardown(self):
  24. registry._disabled_content_types.clear()
  25. def test_disable_insecure_serializers(self):
  26. try:
  27. disabled = registry._disabled_content_types
  28. assert disabled
  29. disable_insecure_serializers(
  30. ['application/json', 'application/x-python-serialize'],
  31. )
  32. assert 'application/x-yaml' in disabled
  33. assert 'application/json' not in disabled
  34. assert 'application/x-python-serialize' not in disabled
  35. disabled.clear()
  36. disable_insecure_serializers(allowed=None)
  37. assert 'application/x-yaml' in disabled
  38. assert 'application/json' in disabled
  39. assert 'application/x-python-serialize' in disabled
  40. finally:
  41. disable_insecure_serializers(allowed=['json'])
  42. @patch('celery.security._disable_insecure_serializers')
  43. def test_disable_untrusted_serializers(self, disable):
  44. disable_untrusted_serializers(['foo'])
  45. disable.assert_called_with(allowed=['foo'])
  46. def test_setup_security(self):
  47. disabled = registry._disabled_content_types
  48. assert len(disabled) == 0
  49. self.app.conf.task_serializer = 'json'
  50. self.app.setup_security()
  51. assert 'application/x-python-serialize' in disabled
  52. disabled.clear()
  53. @patch('celery.current_app')
  54. def test_setup_security__default_app(self, current_app):
  55. setup_security()
  56. @patch('celery.security.register_auth')
  57. @patch('celery.security._disable_insecure_serializers')
  58. def test_setup_registry_complete(self, dis, reg, key='KEY', cert='CERT'):
  59. calls = [0]
  60. def effect(*args):
  61. try:
  62. m = Mock()
  63. m.read.return_value = 'B' if calls[0] else 'A'
  64. return m
  65. finally:
  66. calls[0] += 1
  67. self.app.conf.task_serializer = 'auth'
  68. with mock.open(side_effect=effect):
  69. with patch('celery.security.registry') as registry:
  70. store = Mock()
  71. self.app.setup_security(['json'], key, cert, store)
  72. dis.assert_called_with(['json'])
  73. reg.assert_called_with('A', 'B', store, 'sha1', 'json')
  74. registry._set_default_serializer.assert_called_with('auth')
  75. def test_security_conf(self):
  76. self.app.conf.task_serializer = 'auth'
  77. with pytest.raises(ImproperlyConfigured):
  78. self.app.setup_security()
  79. _import = builtins.__import__
  80. def import_hook(name, *args, **kwargs):
  81. if name == 'OpenSSL':
  82. raise ImportError
  83. return _import(name, *args, **kwargs)
  84. builtins.__import__ = import_hook
  85. with pytest.raises(ImproperlyConfigured):
  86. self.app.setup_security()
  87. builtins.__import__ = _import
  88. def test_reraise_errors(self):
  89. with pytest.raises(SecurityError):
  90. with reraise_errors(errors=(KeyError,)):
  91. raise KeyError('foo')
  92. with pytest.raises(KeyError):
  93. with reraise_errors(errors=(ValueError,)):
  94. raise KeyError('bar')