test_password_policy.py 6.5 KB

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