test_csp.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. from mock import patch
  4. from exam import fixture
  5. from sentry.interfaces.base import InterfaceValidationError
  6. from sentry.interfaces.csp import Csp
  7. from sentry.testutils import TestCase
  8. class CspTest(TestCase):
  9. @fixture
  10. def interface(self):
  11. return Csp.to_python(dict(
  12. document_uri='http://example.com',
  13. violated_directive='style-src cdn.example.com',
  14. blocked_uri='http://example.com/lol.css',
  15. effective_directive='style-src',
  16. ))
  17. def test_path(self):
  18. assert self.interface.get_path() == 'sentry.interfaces.Csp'
  19. def test_serialize_unserialize_behavior(self):
  20. result = type(self.interface).to_python(self.interface.to_json())
  21. assert result.to_json() == self.interface.to_json()
  22. def test_basic(self):
  23. result = self.interface
  24. assert result.document_uri == 'http://example.com'
  25. assert result.violated_directive == 'style-src cdn.example.com'
  26. assert result.blocked_uri == 'http://example.com/lol.css'
  27. def test_to_python_validation_errors(self):
  28. with self.assertRaises(InterfaceValidationError):
  29. Csp.to_python(dict(blocked_uri='about'))
  30. def test_coerce_blocked_uri_if_script_src(self):
  31. result = Csp.to_python(dict(
  32. effective_directive='script-src'
  33. ))
  34. assert result.blocked_uri == 'self'
  35. def test_violated_directive(self):
  36. result = Csp.to_python(dict(
  37. document_uri='http://example.com/foo',
  38. violated_directive='style-src http://cdn.example.com',
  39. effective_directive='style-src',
  40. ))
  41. assert result.get_violated_directive() == ('violated-directive', 'style-src http://cdn.example.com')
  42. result = Csp.to_python(dict(
  43. document_uri='http://example.com/foo',
  44. violated_directive='style-src cdn.example.com',
  45. effective_directive='style-src',
  46. ))
  47. assert result.get_violated_directive() == ('violated-directive', 'style-src http://cdn.example.com')
  48. result = Csp.to_python(dict(
  49. document_uri='https://example.com/foo',
  50. violated_directive='style-src cdn.example.com',
  51. effective_directive='style-src',
  52. ))
  53. assert result.get_violated_directive() == ('violated-directive', 'style-src https://cdn.example.com')
  54. result = Csp.to_python(dict(
  55. document_uri='http://example.com/foo',
  56. violated_directive='style-src https://cdn.example.com',
  57. effective_directive='style-src',
  58. ))
  59. assert result.get_violated_directive() == ('violated-directive', 'style-src https://cdn.example.com')
  60. result = Csp.to_python(dict(
  61. document_uri='blob:example.com/foo',
  62. violated_directive='style-src cdn.example.com',
  63. effective_directive='style-src',
  64. ))
  65. assert result.get_violated_directive() == ('violated-directive', 'style-src blob:cdn.example.com')
  66. def test_get_culprit_directive(self):
  67. result = Csp.to_python(dict(
  68. document_uri='http://example.com/foo',
  69. blocked_uri='http://example.com/lol.css',
  70. effective_directive='style-src',
  71. ))
  72. assert result.get_culprit_directive() == ('blocked-uri', 'http://example.com/lol.css')
  73. result = Csp.to_python(dict(
  74. document_uri='http://example.com/foo',
  75. blocked_uri='',
  76. effective_directive='style-src',
  77. ))
  78. assert result.get_culprit_directive() == ('effective-directive', 'style-src')
  79. result = Csp.to_python(dict(
  80. document_uri='http://example.com/foo',
  81. effective_directive='script-src',
  82. blocked_uri='',
  83. ))
  84. assert result.get_culprit_directive() == ('blocked-uri', 'self')
  85. @patch('sentry.interfaces.csp.Csp.get_culprit_directive')
  86. @patch('sentry.interfaces.csp.Csp.get_violated_directive')
  87. def test_get_hash(self, get_culprit, get_violated):
  88. get_culprit.return_value = ('a', 'b')
  89. get_violated.return_value = ('c', 'd')
  90. assert self.interface.get_hash() == ['a:b', 'c:d']