1
0

test_stringutils.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 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. from synapse.api.errors import SynapseError
  16. from synapse.util.stringutils import assert_valid_client_secret
  17. from .. import unittest
  18. class StringUtilsTestCase(unittest.TestCase):
  19. def test_client_secret_regex(self):
  20. """Ensure that client_secret does not contain illegal characters"""
  21. good = [
  22. "abcde12345",
  23. "ABCabc123",
  24. "_--something==_",
  25. "...--==-18913",
  26. "8Dj2odd-e9asd.cd==_--ddas-secret-",
  27. # We temporarily allow : characters: https://github.com/matrix-org/synapse/issues/6766
  28. # To be removed in a future release
  29. "SECRET:1234567890",
  30. ]
  31. bad = [
  32. "--+-/secret",
  33. "\\dx--dsa288",
  34. "",
  35. "AAS//",
  36. "asdj**",
  37. ">X><Z<!!-)))",
  38. "a@b.com",
  39. ]
  40. for client_secret in good:
  41. assert_valid_client_secret(client_secret)
  42. for client_secret in bad:
  43. with self.assertRaises(SynapseError):
  44. assert_valid_client_secret(client_secret)