test_password_policy.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. import json
  15. from synapse.api.constants import LoginType
  16. from synapse.api.errors import Codes
  17. from synapse.rest import admin
  18. from synapse.rest.client import account, login, password_policy, register
  19. from tests import unittest
  20. class PasswordPolicyTestCase(unittest.HomeserverTestCase):
  21. """Tests the password policy feature and its compliance with MSC2000.
  22. When validating a password, Synapse does the necessary checks in this order:
  23. 1. Password is long enough
  24. 2. Password contains digit(s)
  25. 3. Password contains symbol(s)
  26. 4. Password contains uppercase letter(s)
  27. 5. Password contains lowercase letter(s)
  28. For each test below that checks whether a password triggers the right error code,
  29. that test provides a password good enough to pass the previous tests, but not the
  30. one it is currently testing (nor any test that comes afterward).
  31. """
  32. servlets = [
  33. admin.register_servlets_for_client_rest_resource,
  34. login.register_servlets,
  35. register.register_servlets,
  36. password_policy.register_servlets,
  37. account.register_servlets,
  38. ]
  39. def make_homeserver(self, reactor, clock):
  40. self.register_url = "/_matrix/client/r0/register"
  41. self.policy = {
  42. "enabled": True,
  43. "minimum_length": 10,
  44. "require_digit": True,
  45. "require_symbol": True,
  46. "require_lowercase": True,
  47. "require_uppercase": True,
  48. }
  49. config = self.default_config()
  50. config["password_config"] = {
  51. "policy": self.policy,
  52. }
  53. hs = self.setup_test_homeserver(config=config)
  54. return hs
  55. def test_get_policy(self):
  56. """Tests if the /password_policy endpoint returns the configured policy."""
  57. channel = self.make_request("GET", "/_matrix/client/r0/password_policy")
  58. self.assertEqual(channel.code, 200, channel.result)
  59. self.assertEqual(
  60. channel.json_body,
  61. {
  62. "m.minimum_length": 10,
  63. "m.require_digit": True,
  64. "m.require_symbol": True,
  65. "m.require_lowercase": True,
  66. "m.require_uppercase": True,
  67. },
  68. channel.result,
  69. )
  70. def test_password_too_short(self):
  71. request_data = json.dumps({"username": "kermit", "password": "shorty"})
  72. channel = self.make_request("POST", self.register_url, request_data)
  73. self.assertEqual(channel.code, 400, channel.result)
  74. self.assertEqual(
  75. channel.json_body["errcode"],
  76. Codes.PASSWORD_TOO_SHORT,
  77. channel.result,
  78. )
  79. def test_password_no_digit(self):
  80. request_data = json.dumps({"username": "kermit", "password": "longerpassword"})
  81. channel = self.make_request("POST", self.register_url, request_data)
  82. self.assertEqual(channel.code, 400, channel.result)
  83. self.assertEqual(
  84. channel.json_body["errcode"],
  85. Codes.PASSWORD_NO_DIGIT,
  86. channel.result,
  87. )
  88. def test_password_no_symbol(self):
  89. request_data = json.dumps({"username": "kermit", "password": "l0ngerpassword"})
  90. channel = self.make_request("POST", self.register_url, request_data)
  91. self.assertEqual(channel.code, 400, channel.result)
  92. self.assertEqual(
  93. channel.json_body["errcode"],
  94. Codes.PASSWORD_NO_SYMBOL,
  95. channel.result,
  96. )
  97. def test_password_no_uppercase(self):
  98. request_data = json.dumps({"username": "kermit", "password": "l0ngerpassword!"})
  99. channel = self.make_request("POST", self.register_url, request_data)
  100. self.assertEqual(channel.code, 400, channel.result)
  101. self.assertEqual(
  102. channel.json_body["errcode"],
  103. Codes.PASSWORD_NO_UPPERCASE,
  104. channel.result,
  105. )
  106. def test_password_no_lowercase(self):
  107. request_data = json.dumps({"username": "kermit", "password": "L0NGERPASSWORD!"})
  108. channel = self.make_request("POST", self.register_url, request_data)
  109. self.assertEqual(channel.code, 400, channel.result)
  110. self.assertEqual(
  111. channel.json_body["errcode"],
  112. Codes.PASSWORD_NO_LOWERCASE,
  113. channel.result,
  114. )
  115. def test_password_compliant(self):
  116. request_data = json.dumps({"username": "kermit", "password": "L0ngerpassword!"})
  117. channel = self.make_request("POST", self.register_url, request_data)
  118. # Getting a 401 here means the password has passed validation and the server has
  119. # responded with a list of registration flows.
  120. self.assertEqual(channel.code, 401, channel.result)
  121. def test_password_change(self):
  122. """This doesn't test every possible use case, only that hitting /account/password
  123. triggers the password validation code.
  124. """
  125. compliant_password = "C0mpl!antpassword"
  126. not_compliant_password = "notcompliantpassword"
  127. user_id = self.register_user("kermit", compliant_password)
  128. tok = self.login("kermit", compliant_password)
  129. request_data = json.dumps(
  130. {
  131. "new_password": not_compliant_password,
  132. "auth": {
  133. "password": compliant_password,
  134. "type": LoginType.PASSWORD,
  135. "user": user_id,
  136. },
  137. }
  138. )
  139. channel = self.make_request(
  140. "POST",
  141. "/_matrix/client/r0/account/password",
  142. request_data,
  143. access_token=tok,
  144. )
  145. self.assertEqual(channel.code, 400, channel.result)
  146. self.assertEqual(channel.json_body["errcode"], Codes.PASSWORD_NO_DIGIT)