test_security.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from django.http import HttpResponse
  2. from django.test import RequestFactory, SimpleTestCase
  3. from django.test.utils import override_settings
  4. class SecurityMiddlewareTest(SimpleTestCase):
  5. @property
  6. def middleware(self):
  7. from django.middleware.security import SecurityMiddleware
  8. return SecurityMiddleware()
  9. @property
  10. def secure_request_kwargs(self):
  11. return {"wsgi.url_scheme": "https"}
  12. def response(self, *args, **kwargs):
  13. headers = kwargs.pop("headers", {})
  14. response = HttpResponse(*args, **kwargs)
  15. for k, v in headers.items():
  16. response[k] = v
  17. return response
  18. def process_response(self, *args, **kwargs):
  19. request_kwargs = {}
  20. if kwargs.pop("secure", False):
  21. request_kwargs.update(self.secure_request_kwargs)
  22. request = (kwargs.pop("request", None) or
  23. self.request.get("/some/url", **request_kwargs))
  24. ret = self.middleware.process_request(request)
  25. if ret:
  26. return ret
  27. return self.middleware.process_response(
  28. request, self.response(*args, **kwargs))
  29. request = RequestFactory()
  30. def process_request(self, method, *args, **kwargs):
  31. if kwargs.pop("secure", False):
  32. kwargs.update(self.secure_request_kwargs)
  33. req = getattr(self.request, method.lower())(*args, **kwargs)
  34. return self.middleware.process_request(req)
  35. @override_settings(SECURE_HSTS_SECONDS=3600)
  36. def test_sts_on(self):
  37. """
  38. With HSTS_SECONDS=3600, the middleware adds
  39. "strict-transport-security: max-age=3600" to the response.
  40. """
  41. self.assertEqual(
  42. self.process_response(secure=True)["strict-transport-security"],
  43. "max-age=3600")
  44. @override_settings(SECURE_HSTS_SECONDS=3600)
  45. def test_sts_already_present(self):
  46. """
  47. The middleware will not override a "strict-transport-security" header
  48. already present in the response.
  49. """
  50. response = self.process_response(
  51. secure=True,
  52. headers={"strict-transport-security": "max-age=7200"})
  53. self.assertEqual(response["strict-transport-security"], "max-age=7200")
  54. @override_settings(HSTS_SECONDS=3600)
  55. def test_sts_only_if_secure(self):
  56. """
  57. The "strict-transport-security" header is not added to responses going
  58. over an insecure connection.
  59. """
  60. self.assertNotIn("strict-transport-security", self.process_response(secure=False))
  61. @override_settings(HSTS_SECONDS=0)
  62. def test_sts_off(self):
  63. """
  64. With HSTS_SECONDS of 0, the middleware does not add a
  65. "strict-transport-security" header to the response.
  66. """
  67. self.assertNotIn("strict-transport-security", self.process_response(secure=True))
  68. @override_settings(
  69. SECURE_HSTS_SECONDS=600, SECURE_HSTS_INCLUDE_SUBDOMAINS=True)
  70. def test_sts_include_subdomains(self):
  71. """
  72. With HSTS_SECONDS non-zero and HSTS_INCLUDE_SUBDOMAINS
  73. True, the middleware adds a "strict-transport-security" header with the
  74. "includeSubDomains" tag to the response.
  75. """
  76. response = self.process_response(secure=True)
  77. self.assertEqual(response["strict-transport-security"], "max-age=600; includeSubDomains")
  78. @override_settings(
  79. SECURE_HSTS_SECONDS=600, SECURE_HSTS_INCLUDE_SUBDOMAINS=False)
  80. def test_sts_no_include_subdomains(self):
  81. """
  82. With HSTS_SECONDS non-zero and HSTS_INCLUDE_SUBDOMAINS
  83. False, the middleware adds a "strict-transport-security" header without
  84. the "includeSubDomains" tag to the response.
  85. """
  86. response = self.process_response(secure=True)
  87. self.assertEqual(response["strict-transport-security"], "max-age=600")
  88. @override_settings(SECURE_CONTENT_TYPE_NOSNIFF=True)
  89. def test_content_type_on(self):
  90. """
  91. With CONTENT_TYPE_NOSNIFF set to True, the middleware adds
  92. "x-content-type-options: nosniff" header to the response.
  93. """
  94. self.assertEqual(self.process_response()["x-content-type-options"], "nosniff")
  95. @override_settings(SECURE_CONTENT_TYPE_NO_SNIFF=True)
  96. def test_content_type_already_present(self):
  97. """
  98. The middleware will not override an "x-content-type-options" header
  99. already present in the response.
  100. """
  101. response = self.process_response(secure=True, headers={"x-content-type-options": "foo"})
  102. self.assertEqual(response["x-content-type-options"], "foo")
  103. @override_settings(SECURE_CONTENT_TYPE_NOSNIFF=False)
  104. def test_content_type_off(self):
  105. """
  106. With CONTENT_TYPE_NOSNIFF False, the middleware does not add an
  107. "x-content-type-options" header to the response.
  108. """
  109. self.assertNotIn("x-content-type-options", self.process_response())
  110. @override_settings(SECURE_BROWSER_XSS_FILTER=True)
  111. def test_xss_filter_on(self):
  112. """
  113. With BROWSER_XSS_FILTER set to True, the middleware adds
  114. "s-xss-protection: 1; mode=block" header to the response.
  115. """
  116. self.assertEqual(
  117. self.process_response()["x-xss-protection"],
  118. "1; mode=block")
  119. @override_settings(SECURE_BROWSER_XSS_FILTER=True)
  120. def test_xss_filter_already_present(self):
  121. """
  122. The middleware will not override an "x-xss-protection" header
  123. already present in the response.
  124. """
  125. response = self.process_response(secure=True, headers={"x-xss-protection": "foo"})
  126. self.assertEqual(response["x-xss-protection"], "foo")
  127. @override_settings(BROWSER_XSS_FILTER=False)
  128. def test_xss_filter_off(self):
  129. """
  130. With BROWSER_XSS_FILTER set to False, the middleware does not add an
  131. "x-xss-protection" header to the response.
  132. """
  133. self.assertNotIn("x-xss-protection", self.process_response())
  134. @override_settings(SECURE_SSL_REDIRECT=True)
  135. def test_ssl_redirect_on(self):
  136. """
  137. With SSL_REDIRECT True, the middleware redirects any non-secure
  138. requests to the https:// version of the same URL.
  139. """
  140. ret = self.process_request("get", "/some/url?query=string")
  141. self.assertEqual(ret.status_code, 301)
  142. self.assertEqual(
  143. ret["Location"], "https://testserver/some/url?query=string")
  144. @override_settings(SECURE_SSL_REDIRECT=True)
  145. def test_no_redirect_ssl(self):
  146. """
  147. The middleware does not redirect secure requests.
  148. """
  149. ret = self.process_request("get", "/some/url", secure=True)
  150. self.assertIsNone(ret)
  151. @override_settings(
  152. SECURE_SSL_REDIRECT=True, SECURE_REDIRECT_EXEMPT=["^insecure/"])
  153. def test_redirect_exempt(self):
  154. """
  155. The middleware does not redirect requests with URL path matching an
  156. exempt pattern.
  157. """
  158. ret = self.process_request("get", "/insecure/page")
  159. self.assertIsNone(ret)
  160. @override_settings(
  161. SECURE_SSL_REDIRECT=True, SECURE_SSL_HOST="secure.example.com")
  162. def test_redirect_ssl_host(self):
  163. """
  164. The middleware redirects to SSL_HOST if given.
  165. """
  166. ret = self.process_request("get", "/some/url")
  167. self.assertEqual(ret.status_code, 301)
  168. self.assertEqual(ret["Location"], "https://secure.example.com/some/url")
  169. @override_settings(SECURE_SSL_REDIRECT=False)
  170. def test_ssl_redirect_off(self):
  171. """
  172. With SSL_REDIRECT False, the middleware does no redirect.
  173. """
  174. ret = self.process_request("get", "/some/url")
  175. self.assertIsNone(ret)