test_security.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from django.http import HttpResponse
  2. from django.test import TestCase, RequestFactory
  3. from django.test.utils import override_settings
  4. class SecurityMiddlewareTest(TestCase):
  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(
  78. response["strict-transport-security"],
  79. "max-age=600; includeSubDomains",
  80. )
  81. @override_settings(
  82. SECURE_HSTS_SECONDS=600, SECURE_HSTS_INCLUDE_SUBDOMAINS=False)
  83. def test_sts_no_include_subdomains(self):
  84. """
  85. With HSTS_SECONDS non-zero and HSTS_INCLUDE_SUBDOMAINS
  86. False, the middleware adds a "strict-transport-security" header without
  87. the "includeSubDomains" tag to the response.
  88. """
  89. response = self.process_response(secure=True)
  90. self.assertEqual(response["strict-transport-security"], "max-age=600")
  91. @override_settings(SECURE_CONTENT_TYPE_NOSNIFF=True)
  92. def test_content_type_on(self):
  93. """
  94. With CONTENT_TYPE_NOSNIFF set to True, the middleware adds
  95. "x-content-type-options: nosniff" header to the response.
  96. """
  97. self.assertEqual(self.process_response()["x-content-type-options"], "nosniff")
  98. @override_settings(SECURE_CONTENT_TYPE_NO_SNIFF=True)
  99. def test_content_type_already_present(self):
  100. """
  101. The middleware will not override an "x-content-type-options" header
  102. already present in the response.
  103. """
  104. response = self.process_response(secure=True, headers={"x-content-type-options": "foo"})
  105. self.assertEqual(response["x-content-type-options"], "foo")
  106. @override_settings(SECURE_CONTENT_TYPE_NOSNIFF=False)
  107. def test_content_type_off(self):
  108. """
  109. With CONTENT_TYPE_NOSNIFF False, the middleware does not add an
  110. "x-content-type-options" header to the response.
  111. """
  112. self.assertNotIn("x-content-type-options", self.process_response())
  113. @override_settings(SECURE_BROWSER_XSS_FILTER=True)
  114. def test_xss_filter_on(self):
  115. """
  116. With BROWSER_XSS_FILTER set to True, the middleware adds
  117. "s-xss-protection: 1; mode=block" header to the response.
  118. """
  119. self.assertEqual(
  120. self.process_response()["x-xss-protection"],
  121. "1; mode=block")
  122. @override_settings(SECURE_BROWSER_XSS_FILTER=True)
  123. def test_xss_filter_already_present(self):
  124. """
  125. The middleware will not override an "x-xss-protection" header
  126. already present in the response.
  127. """
  128. response = self.process_response(secure=True, headers={"x-xss-protection": "foo"})
  129. self.assertEqual(response["x-xss-protection"], "foo")
  130. @override_settings(BROWSER_XSS_FILTER=False)
  131. def test_xss_filter_off(self):
  132. """
  133. With BROWSER_XSS_FILTER set to False, the middleware does not add an
  134. "x-xss-protection" header to the response.
  135. """
  136. self.assertFalse("x-xss-protection" in self.process_response())
  137. @override_settings(SECURE_SSL_REDIRECT=True)
  138. def test_ssl_redirect_on(self):
  139. """
  140. With SSL_REDIRECT True, the middleware redirects any non-secure
  141. requests to the https:// version of the same URL.
  142. """
  143. ret = self.process_request("get", "/some/url?query=string")
  144. self.assertEqual(ret.status_code, 301)
  145. self.assertEqual(
  146. ret["Location"], "https://testserver/some/url?query=string")
  147. @override_settings(SECURE_SSL_REDIRECT=True)
  148. def test_no_redirect_ssl(self):
  149. """
  150. The middleware does not redirect secure requests.
  151. """
  152. ret = self.process_request("get", "/some/url", secure=True)
  153. self.assertEqual(ret, None)
  154. @override_settings(
  155. SECURE_SSL_REDIRECT=True, SECURE_REDIRECT_EXEMPT=["^insecure/"])
  156. def test_redirect_exempt(self):
  157. """
  158. The middleware does not redirect requests with URL path matching an
  159. exempt pattern.
  160. """
  161. ret = self.process_request("get", "/insecure/page")
  162. self.assertEqual(ret, None)
  163. @override_settings(
  164. SECURE_SSL_REDIRECT=True, SECURE_SSL_HOST="secure.example.com")
  165. def test_redirect_ssl_host(self):
  166. """
  167. The middleware redirects to SSL_HOST if given.
  168. """
  169. ret = self.process_request("get", "/some/url")
  170. self.assertEqual(ret.status_code, 301)
  171. self.assertEqual(ret["Location"], "https://secure.example.com/some/url")
  172. @override_settings(SECURE_SSL_REDIRECT=False)
  173. def test_ssl_redirect_off(self):
  174. """
  175. With SSL_REDIRECT False, the middleware does no redirect.
  176. """
  177. ret = self.process_request("get", "/some/url")
  178. self.assertEqual(ret, None)