test_password_policy.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. channel = self.make_request("GET", "/_matrix/client/r0/password_policy")
  60. self.assertEqual(channel.code, 200, channel.result)
  61. self.assertEqual(
  62. channel.json_body,
  63. {
  64. "m.minimum_length": 10,
  65. "m.require_digit": True,
  66. "m.require_symbol": True,
  67. "m.require_lowercase": True,
  68. "m.require_uppercase": True,
  69. },
  70. channel.result,
  71. )
  72. def test_password_too_short(self):
  73. request_data = json.dumps({"username": "kermit", "password": "shorty"})
  74. channel = self.make_request("POST", self.register_url, request_data)
  75. self.assertEqual(channel.code, 400, channel.result)
  76. self.assertEqual(
  77. channel.json_body["errcode"], Codes.PASSWORD_TOO_SHORT, 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"], Codes.PASSWORD_NO_DIGIT, channel.result,
  85. )
  86. def test_password_no_symbol(self):
  87. request_data = json.dumps({"username": "kermit", "password": "l0ngerpassword"})
  88. channel = self.make_request("POST", self.register_url, request_data)
  89. self.assertEqual(channel.code, 400, channel.result)
  90. self.assertEqual(
  91. channel.json_body["errcode"], Codes.PASSWORD_NO_SYMBOL, channel.result,
  92. )
  93. def test_password_no_uppercase(self):
  94. request_data = json.dumps({"username": "kermit", "password": "l0ngerpassword!"})
  95. channel = self.make_request("POST", self.register_url, request_data)
  96. self.assertEqual(channel.code, 400, channel.result)
  97. self.assertEqual(
  98. channel.json_body["errcode"], Codes.PASSWORD_NO_UPPERCASE, channel.result,
  99. )
  100. def test_password_no_lowercase(self):
  101. request_data = json.dumps({"username": "kermit", "password": "L0NGERPASSWORD!"})
  102. channel = self.make_request("POST", self.register_url, request_data)
  103. self.assertEqual(channel.code, 400, channel.result)
  104. self.assertEqual(
  105. channel.json_body["errcode"], Codes.PASSWORD_NO_LOWERCASE, channel.result,
  106. )
  107. def test_password_compliant(self):
  108. request_data = json.dumps({"username": "kermit", "password": "L0ngerpassword!"})
  109. channel = self.make_request("POST", self.register_url, request_data)
  110. # Getting a 401 here means the password has passed validation and the server has
  111. # responded with a list of registration flows.
  112. self.assertEqual(channel.code, 401, channel.result)
  113. def test_password_change(self):
  114. """This doesn't test every possible use case, only that hitting /account/password
  115. triggers the password validation code.
  116. """
  117. compliant_password = "C0mpl!antpassword"
  118. not_compliant_password = "notcompliantpassword"
  119. user_id = self.register_user("kermit", compliant_password)
  120. tok = self.login("kermit", compliant_password)
  121. request_data = json.dumps(
  122. {
  123. "new_password": not_compliant_password,
  124. "auth": {
  125. "password": compliant_password,
  126. "type": LoginType.PASSWORD,
  127. "user": user_id,
  128. },
  129. }
  130. )
  131. channel = self.make_request(
  132. "POST",
  133. "/_matrix/client/r0/account/password",
  134. request_data,
  135. access_token=tok,
  136. )
  137. self.assertEqual(channel.code, 400, channel.result)
  138. self.assertEqual(channel.json_body["errcode"], Codes.PASSWORD_NO_DIGIT)