test_password_policy.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from http import HTTPStatus
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import LoginType
  17. from synapse.api.errors import Codes
  18. from synapse.rest import admin
  19. from synapse.rest.client import account, login, password_policy, register
  20. from synapse.server import HomeServer
  21. from synapse.util import Clock
  22. from tests import unittest
  23. class PasswordPolicyTestCase(unittest.HomeserverTestCase):
  24. """Tests the password policy feature and its compliance with MSC2000.
  25. When validating a password, Synapse does the necessary checks in this order:
  26. 1. Password is long enough
  27. 2. Password contains digit(s)
  28. 3. Password contains symbol(s)
  29. 4. Password contains uppercase letter(s)
  30. 5. Password contains lowercase letter(s)
  31. For each test below that checks whether a password triggers the right error code,
  32. that test provides a password good enough to pass the previous tests, but not the
  33. one it is currently testing (nor any test that comes afterward).
  34. """
  35. servlets = [
  36. admin.register_servlets_for_client_rest_resource,
  37. login.register_servlets,
  38. register.register_servlets,
  39. password_policy.register_servlets,
  40. account.register_servlets,
  41. ]
  42. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  43. self.register_url = "/_matrix/client/r0/register"
  44. self.policy = {
  45. "enabled": True,
  46. "minimum_length": 10,
  47. "require_digit": True,
  48. "require_symbol": True,
  49. "require_lowercase": True,
  50. "require_uppercase": True,
  51. }
  52. config = self.default_config()
  53. config["password_config"] = {
  54. "policy": self.policy,
  55. }
  56. hs = self.setup_test_homeserver(config=config)
  57. return hs
  58. def test_get_policy(self) -> None:
  59. """Tests if the /password_policy endpoint returns the configured policy."""
  60. channel = self.make_request("GET", "/_matrix/client/r0/password_policy")
  61. self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
  62. self.assertEqual(
  63. channel.json_body,
  64. {
  65. "m.minimum_length": 10,
  66. "m.require_digit": True,
  67. "m.require_symbol": True,
  68. "m.require_lowercase": True,
  69. "m.require_uppercase": True,
  70. },
  71. channel.result,
  72. )
  73. def test_password_too_short(self) -> None:
  74. request_data = {"username": "kermit", "password": "shorty"}
  75. channel = self.make_request("POST", self.register_url, request_data)
  76. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  77. self.assertEqual(
  78. channel.json_body["errcode"],
  79. Codes.PASSWORD_TOO_SHORT,
  80. channel.result,
  81. )
  82. def test_password_no_digit(self) -> None:
  83. request_data = {"username": "kermit", "password": "longerpassword"}
  84. channel = self.make_request("POST", self.register_url, request_data)
  85. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  86. self.assertEqual(
  87. channel.json_body["errcode"],
  88. Codes.PASSWORD_NO_DIGIT,
  89. channel.result,
  90. )
  91. def test_password_no_symbol(self) -> None:
  92. request_data = {"username": "kermit", "password": "l0ngerpassword"}
  93. channel = self.make_request("POST", self.register_url, request_data)
  94. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  95. self.assertEqual(
  96. channel.json_body["errcode"],
  97. Codes.PASSWORD_NO_SYMBOL,
  98. channel.result,
  99. )
  100. def test_password_no_uppercase(self) -> None:
  101. request_data = {"username": "kermit", "password": "l0ngerpassword!"}
  102. channel = self.make_request("POST", self.register_url, request_data)
  103. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  104. self.assertEqual(
  105. channel.json_body["errcode"],
  106. Codes.PASSWORD_NO_UPPERCASE,
  107. channel.result,
  108. )
  109. def test_password_no_lowercase(self) -> None:
  110. request_data = {"username": "kermit", "password": "L0NGERPASSWORD!"}
  111. channel = self.make_request("POST", self.register_url, request_data)
  112. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  113. self.assertEqual(
  114. channel.json_body["errcode"],
  115. Codes.PASSWORD_NO_LOWERCASE,
  116. channel.result,
  117. )
  118. def test_password_compliant(self) -> None:
  119. request_data = {"username": "kermit", "password": "L0ngerpassword!"}
  120. channel = self.make_request("POST", self.register_url, request_data)
  121. # Getting a 401 here means the password has passed validation and the server has
  122. # responded with a list of registration flows.
  123. self.assertEqual(channel.code, HTTPStatus.UNAUTHORIZED, channel.result)
  124. def test_password_change(self) -> None:
  125. """This doesn't test every possible use case, only that hitting /account/password
  126. triggers the password validation code.
  127. """
  128. compliant_password = "C0mpl!antpassword"
  129. not_compliant_password = "notcompliantpassword"
  130. user_id = self.register_user("kermit", compliant_password)
  131. tok = self.login("kermit", compliant_password)
  132. request_data = {
  133. "new_password": not_compliant_password,
  134. "auth": {
  135. "password": compliant_password,
  136. "type": LoginType.PASSWORD,
  137. "user": user_id,
  138. },
  139. }
  140. channel = self.make_request(
  141. "POST",
  142. "/_matrix/client/r0/account/password",
  143. request_data,
  144. access_token=tok,
  145. )
  146. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST, channel.result)
  147. self.assertEqual(channel.json_body["errcode"], Codes.PASSWORD_NO_DIGIT)