test_auth.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2016 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2022 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Radicale tests with simple requests and authentication.
  21. """
  22. import base64
  23. import logging
  24. import os
  25. import sys
  26. from typing import Iterable, Tuple, Union
  27. import pytest
  28. from radicale import utils, xmlutils
  29. from radicale.tests import BaseTest
  30. class TestBaseAuthRequests(BaseTest):
  31. """Tests basic requests with auth.
  32. We should setup auth for each type before creating the Application object.
  33. """
  34. # test for available bcrypt module
  35. try:
  36. import bcrypt
  37. except ImportError:
  38. has_bcrypt = 0
  39. else:
  40. has_bcrypt = 1
  41. # test for available argon2 module
  42. try:
  43. import argon2
  44. from passlib.hash import argon2 # noqa: F811
  45. except ImportError:
  46. has_argon2 = 0
  47. else:
  48. has_argon2 = 1
  49. def _test_htpasswd(self, htpasswd_encryption: str, htpasswd_content: str,
  50. test_matrix: Union[str, Iterable[Tuple[str, str, bool]]]
  51. = "ascii") -> None:
  52. """Test htpasswd authentication with user "tmp" and password "bepo" for
  53. ``test_matrix`` "ascii" or user "😀" and password "🔑" for
  54. ``test_matrix`` "unicode"."""
  55. htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")
  56. encoding: str = self.configuration.get("encoding", "stock")
  57. with open(htpasswd_file_path, "w", encoding=encoding) as f:
  58. f.write(htpasswd_content)
  59. self.configure({"auth": {"type": "htpasswd",
  60. "htpasswd_filename": htpasswd_file_path,
  61. "htpasswd_encryption": htpasswd_encryption}})
  62. if test_matrix == "ascii":
  63. test_matrix = (("tmp", "bepo", True), ("tmp", "tmp", False),
  64. ("tmp", "", False), ("unk", "unk", False),
  65. ("unk", "", False), ("", "", False))
  66. elif test_matrix == "unicode":
  67. test_matrix = (("😀", "🔑", True), ("😀", "🌹", False),
  68. ("😁", "🔑", False), ("😀", "", False),
  69. ("", "🔑", False), ("", "", False))
  70. elif isinstance(test_matrix, str):
  71. raise ValueError("Unknown test matrix %r" % test_matrix)
  72. for user, password, valid in test_matrix:
  73. self.propfind("/", check=207 if valid else 401,
  74. login="%s:%s" % (user, password))
  75. def test_htpasswd_plain(self) -> None:
  76. self._test_htpasswd("plain", "tmp:bepo")
  77. def test_htpasswd_plain_autodetect(self) -> None:
  78. self._test_htpasswd("autodetect", "tmp:bepo")
  79. def test_htpasswd_plain_password_split(self) -> None:
  80. self._test_htpasswd("plain", "tmp:be:po", (
  81. ("tmp", "be:po", True), ("tmp", "bepo", False)))
  82. def test_htpasswd_plain_unicode(self) -> None:
  83. self._test_htpasswd("plain", "😀:🔑", "unicode")
  84. def test_htpasswd_md5(self) -> None:
  85. self._test_htpasswd("md5", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  86. def test_htpasswd_md5_autodetect(self) -> None:
  87. self._test_htpasswd("autodetect", "tmp:$apr1$BI7VKCZh$GKW4vq2hqDINMr8uv7lDY/")
  88. def test_htpasswd_md5_unicode(self):
  89. self._test_htpasswd(
  90. "md5", "😀:$apr1$w4ev89r1$29xO8EvJmS2HEAadQ5qy11", "unicode")
  91. def test_htpasswd_sha256(self) -> None:
  92. self._test_htpasswd("sha256", "tmp:$5$i4Ni4TQq6L5FKss5$ilpTjkmnxkwZeV35GB9cYSsDXTALBn6KtWRJAzNlCL/")
  93. def test_htpasswd_sha256_autodetect(self) -> None:
  94. self._test_htpasswd("autodetect", "tmp:$5$i4Ni4TQq6L5FKss5$ilpTjkmnxkwZeV35GB9cYSsDXTALBn6KtWRJAzNlCL/")
  95. def test_htpasswd_sha512(self) -> None:
  96. self._test_htpasswd("sha512", "tmp:$6$3Qhl8r6FLagYdHYa$UCH9yXCed4A.J9FQsFPYAOXImzZUMfvLa0lwcWOxWYLOF5sE/lF99auQ4jKvHY2vijxmefl7G6kMqZ8JPdhIJ/")
  97. def test_htpasswd_sha512_autodetect(self) -> None:
  98. self._test_htpasswd("autodetect", "tmp:$6$3Qhl8r6FLagYdHYa$UCH9yXCed4A.J9FQsFPYAOXImzZUMfvLa0lwcWOxWYLOF5sE/lF99auQ4jKvHY2vijxmefl7G6kMqZ8JPdhIJ/")
  99. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  100. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  101. def test_htpasswd_bcrypt_2a(self) -> None:
  102. self._test_htpasswd("bcrypt", "tmp:$2a$10$Mj4A9vMecAp/K7.0fMKoVOk1SjgR.RBhl06a52nvzXhxlT3HB7Reu")
  103. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed or incompatibe")
  104. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  105. def test_htpasswd_bcrypt_2a_autodetect(self) -> None:
  106. self._test_htpasswd("autodetect", "tmp:$2a$10$Mj4A9vMecAp/K7.0fMKoVOk1SjgR.RBhl06a52nvzXhxlT3HB7Reu")
  107. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  108. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  109. def test_htpasswd_bcrypt_2b(self) -> None:
  110. self._test_htpasswd("bcrypt", "tmp:$2b$12$7a4z/fdmXlBIfkz0smvzW.1Nds8wpgC/bo2DVOb4OSQKWCDL1A1wu")
  111. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  112. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  113. def test_htpasswd_bcrypt_2b_autodetect(self) -> None:
  114. self._test_htpasswd("autodetect", "tmp:$2b$12$7a4z/fdmXlBIfkz0smvzW.1Nds8wpgC/bo2DVOb4OSQKWCDL1A1wu")
  115. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  116. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  117. def test_htpasswd_bcrypt_2y(self) -> None:
  118. self._test_htpasswd("bcrypt", "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  119. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  120. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  121. def test_htpasswd_bcrypt_2y_autodetect(self) -> None:
  122. self._test_htpasswd("autodetect", "tmp:$2y$05$oD7hbiQFQlvCM7zoalo/T.MssV3VNTRI3w5KDnj8NTUKJNWfVpvRq")
  123. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  124. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  125. def test_htpasswd_bcrypt_C10(self) -> None:
  126. self._test_htpasswd("bcrypt", "tmp:$2y$10$bZsWq06ECzxqi7RmulQvC.T1YHUnLW2E3jn.MU2pvVTGn1dfORt2a")
  127. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  128. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  129. def test_htpasswd_bcrypt_C10_autodetect(self) -> None:
  130. self._test_htpasswd("bcrypt", "tmp:$2y$10$bZsWq06ECzxqi7RmulQvC.T1YHUnLW2E3jn.MU2pvVTGn1dfORt2a")
  131. @pytest.mark.skipif(has_bcrypt == 0, reason="No bcrypt module installed")
  132. @pytest.mark.skipif(not utils.passlib_libpass_supports_bcrypt()[0], reason="bcrypt module incompatible with passlib/libpass module")
  133. def test_htpasswd_bcrypt_unicode(self) -> None:
  134. self._test_htpasswd("bcrypt", "😀:$2y$10$Oyz5aHV4MD9eQJbk6GPemOs4T6edK6U9Sqlzr.W1mMVCS8wJUftnW", "unicode")
  135. @pytest.mark.skipif(has_argon2 == 0, reason="No argon2 module installed")
  136. def test_htpasswd_argon2_i(self) -> None:
  137. self._test_htpasswd("argon2", "tmp:$argon2i$v=19$m=65536,t=3,p=4$NgZg7F1rzRkDoNSaMwag9A$qmsvMKEn5zOXHm8e3O5fKzzcRo0UESwaDr/cETe5YPI")
  138. @pytest.mark.skipif(has_argon2 == 0, reason="No argon2 module installed")
  139. def test_htpasswd_argon2_d(self) -> None:
  140. self._test_htpasswd("argon2", "tmp:$argon2d$v=19$m=65536,t=3,p=4$ufe+txYiJKR0zlkLwVirVQ$MjGqRyVLes38hA6CEOkloMcTYCuLjxCKgIjtfYZ3iSM")
  141. @pytest.mark.skipif(has_argon2 == 0, reason="No argon2 module installed")
  142. def test_htpasswd_argon2_id(self) -> None:
  143. self._test_htpasswd("argon2", "tmp:$argon2id$v=19$m=65536,t=3,p=4$t7bWuneOkdIa45xTqjXGmA$ORnRJyz9kHogJs6bDgZrTBPlzi4+p023PSEABb3xX1g")
  144. def test_htpasswd_multi(self) -> None:
  145. self._test_htpasswd("plain", "ign:ign\ntmp:bepo")
  146. # login cache successful
  147. def test_htpasswd_login_cache_successful_plain(self, caplog) -> None:
  148. caplog.set_level(logging.INFO)
  149. self.configure({"auth": {"cache_logins": "True"}})
  150. self._test_htpasswd("plain", "tmp:bepo", (("tmp", "bepo", True), ("tmp", "bepo", True)))
  151. htpasswd_found = False
  152. htpasswd_cached_found = False
  153. for line in caplog.messages:
  154. if line == "Successful login: 'tmp' (htpasswd)":
  155. htpasswd_found = True
  156. elif line == "Successful login: 'tmp' (htpasswd / cached)":
  157. htpasswd_cached_found = True
  158. if (htpasswd_found is False) or (htpasswd_cached_found is False):
  159. raise ValueError("Logging misses expected log lines")
  160. # login cache failed
  161. def test_htpasswd_login_cache_failed_plain(self, caplog) -> None:
  162. caplog.set_level(logging.INFO)
  163. self.configure({"auth": {"cache_logins": "True"}})
  164. self._test_htpasswd("plain", "tmp:bepo", (("tmp", "bepo1", False), ("tmp", "bepo1", False)))
  165. htpasswd_found = False
  166. htpasswd_cached_found = False
  167. for line in caplog.messages:
  168. if line == "Failed login attempt from unknown: 'tmp' (htpasswd)":
  169. htpasswd_found = True
  170. elif line == "Failed login attempt from unknown: 'tmp' (htpasswd / cached)":
  171. htpasswd_cached_found = True
  172. if (htpasswd_found is False) or (htpasswd_cached_found is False):
  173. raise ValueError("Logging misses expected log lines")
  174. # htpasswd file cache
  175. def test_htpasswd_file_cache(self, caplog) -> None:
  176. self.configure({"auth": {"htpasswd_cache": "True"}})
  177. self._test_htpasswd("plain", "tmp:bepo")
  178. # detection of broken htpasswd file entries
  179. def test_htpasswd_broken(self) -> None:
  180. for userpass in ["tmp:", ":tmp"]:
  181. try:
  182. self._test_htpasswd("plain", userpass)
  183. except RuntimeError:
  184. pass
  185. else:
  186. raise
  187. @pytest.mark.skipif(sys.platform == "win32", reason="leading and trailing "
  188. "whitespaces not allowed in file names")
  189. def test_htpasswd_whitespace_user(self) -> None:
  190. for user in (" tmp", "tmp ", " tmp "):
  191. self._test_htpasswd("plain", "%s:bepo" % user, (
  192. (user, "bepo", True), ("tmp", "bepo", False)))
  193. def test_htpasswd_whitespace_password(self) -> None:
  194. for password in (" bepo", "bepo ", " bepo "):
  195. self._test_htpasswd("plain", "tmp:%s" % password, (
  196. ("tmp", password, True), ("tmp", "bepo", False)))
  197. def test_htpasswd_comment(self) -> None:
  198. self._test_htpasswd("plain", "#comment\n #comment\n \ntmp:bepo\n\n")
  199. def test_htpasswd_lc_username(self) -> None:
  200. self.configure({"auth": {"lc_username": "True"}})
  201. self._test_htpasswd("plain", "tmp:bepo", (
  202. ("tmp", "bepo", True), ("TMP", "bepo", True), ("tmp1", "bepo", False)))
  203. def test_htpasswd_uc_username(self) -> None:
  204. self.configure({"auth": {"uc_username": "True"}})
  205. self._test_htpasswd("plain", "TMP:bepo", (
  206. ("tmp", "bepo", True), ("TMP", "bepo", True), ("TMP1", "bepo", False)))
  207. def test_htpasswd_strip_domain(self) -> None:
  208. self.configure({"auth": {"strip_domain": "True"}})
  209. self._test_htpasswd("plain", "tmp:bepo", (
  210. ("tmp", "bepo", True), ("tmp@domain.example", "bepo", True), ("tmp1", "bepo", False)))
  211. def test_remote_user(self) -> None:
  212. self.configure({"auth": {"type": "remote_user"}})
  213. _, responses = self.propfind("/", """\
  214. <?xml version="1.0" encoding="utf-8"?>
  215. <propfind xmlns="DAV:">
  216. <prop>
  217. <current-user-principal />
  218. </prop>
  219. </propfind>""", REMOTE_USER="test")
  220. assert responses is not None
  221. response = responses["/"]
  222. assert not isinstance(response, int)
  223. status, prop = response["D:current-user-principal"]
  224. assert status == 200
  225. href_element = prop.find(xmlutils.make_clark("D:href"))
  226. assert href_element is not None and href_element.text == "/test/"
  227. def test_http_remote_user(self) -> None:
  228. self.configure({"auth": {"type": "http_remote_user"}})
  229. _, responses = self.propfind("/", """\
  230. <?xml version="1.0" encoding="utf-8"?>
  231. <propfind xmlns="DAV:">
  232. <prop>
  233. <current-user-principal />
  234. </prop>
  235. </propfind>""", HTTP_REMOTE_USER="test")
  236. assert responses is not None
  237. response = responses["/"]
  238. assert not isinstance(response, int)
  239. status, prop = response["D:current-user-principal"]
  240. assert status == 200
  241. href_element = prop.find(xmlutils.make_clark("D:href"))
  242. assert href_element is not None and href_element.text == "/test/"
  243. def test_http_x_remote_user(self) -> None:
  244. self.configure({"auth": {"type": "http_x_remote_user"}})
  245. _, responses = self.propfind("/", """\
  246. <?xml version="1.0" encoding="utf-8"?>
  247. <propfind xmlns="DAV:">
  248. <prop>
  249. <current-user-principal />
  250. </prop>
  251. </propfind>""", HTTP_X_REMOTE_USER="test")
  252. assert responses is not None
  253. response = responses["/"]
  254. assert not isinstance(response, int)
  255. status, prop = response["D:current-user-principal"]
  256. assert status == 200
  257. href_element = prop.find(xmlutils.make_clark("D:href"))
  258. assert href_element is not None and href_element.text == "/test/"
  259. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  260. def _test_dovecot(
  261. self, user, password, expected_status, expected_rip=None,
  262. response=b'FAIL\t1', mech=[b'PLAIN'], broken=None,
  263. extra_config=None, extra_env=None):
  264. import socket
  265. from unittest.mock import DEFAULT, patch
  266. if extra_env is None:
  267. extra_env = {}
  268. if extra_config is None:
  269. extra_config = {}
  270. config = {"auth": {"type": "dovecot",
  271. "dovecot_socket": "./dovecot.sock"}}
  272. for toplvl, entries in extra_config.items():
  273. for key, val in entries.items():
  274. config[toplvl][key] = val
  275. self.configure(config)
  276. if broken is None:
  277. broken = []
  278. handshake = b''
  279. if "version" not in broken:
  280. handshake += b'VERSION\t'
  281. if "incompatible" in broken:
  282. handshake += b'2'
  283. else:
  284. handshake += b'1'
  285. handshake += b'\t2\n'
  286. if "mech" not in broken:
  287. handshake += b'MECH\t%b\n' % b' '.join(mech)
  288. if "duplicate" in broken:
  289. handshake += b'VERSION\t1\t2\n'
  290. if "done" not in broken:
  291. handshake += b'DONE\n'
  292. sent_rip = None
  293. def record_sent_data(s, data, flags=None):
  294. nonlocal sent_rip
  295. if b'\trip=' in data:
  296. sent_rip = data.split(b'\trip=')[1].split(b'\t')[0]
  297. return len(data)
  298. with patch.multiple(
  299. 'socket.socket',
  300. connect=DEFAULT,
  301. send=record_sent_data,
  302. recv=DEFAULT
  303. ) as mock_socket:
  304. if "socket" in broken:
  305. mock_socket["connect"].side_effect = socket.error(
  306. "Testing error with the socket"
  307. )
  308. mock_socket["recv"].side_effect = [handshake, response]
  309. status, _, answer = self.request(
  310. "PROPFIND", "/",
  311. HTTP_AUTHORIZATION="Basic %s" % base64.b64encode(
  312. ("%s:%s" % (user, password)).encode()).decode(),
  313. **extra_env)
  314. assert sent_rip == expected_rip
  315. assert status == expected_status
  316. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  317. def test_dovecot_no_user(self):
  318. self._test_dovecot("", "", 401)
  319. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  320. def test_dovecot_no_password(self):
  321. self._test_dovecot("user", "", 401)
  322. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  323. def test_dovecot_broken_handshake_no_version(self):
  324. self._test_dovecot("user", "password", 401, broken=["version"])
  325. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  326. def test_dovecot_broken_handshake_incompatible(self):
  327. self._test_dovecot("user", "password", 401, broken=["incompatible"])
  328. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  329. def test_dovecot_broken_handshake_duplicate(self):
  330. self._test_dovecot(
  331. "user", "password", 207, response=b'OK\t1',
  332. broken=["duplicate"]
  333. )
  334. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  335. def test_dovecot_broken_handshake_no_mech(self):
  336. self._test_dovecot("user", "password", 401, broken=["mech"])
  337. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  338. def test_dovecot_broken_handshake_unsupported_mech(self):
  339. self._test_dovecot("user", "password", 401, mech=[b'ONE', b'TWO'])
  340. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  341. def test_dovecot_broken_handshake_no_done(self):
  342. self._test_dovecot("user", "password", 401, broken=["done"])
  343. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  344. def test_dovecot_broken_socket(self):
  345. self._test_dovecot("user", "password", 401, broken=["socket"])
  346. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  347. def test_dovecot_auth_good1(self):
  348. self._test_dovecot("user", "password", 207, response=b'OK\t1')
  349. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  350. def test_dovecot_auth_good2(self):
  351. self._test_dovecot(
  352. "user", "password", 207, response=b'OK\t1',
  353. mech=[b'PLAIN\nEXTRA\tTERM']
  354. )
  355. self._test_dovecot("user", "password", 207, response=b'OK\t1')
  356. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  357. def test_dovecot_auth_bad1(self):
  358. self._test_dovecot("user", "password", 401, response=b'FAIL\t1')
  359. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  360. def test_dovecot_auth_bad2(self):
  361. self._test_dovecot("user", "password", 401, response=b'CONT\t1')
  362. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  363. def test_dovecot_auth_id_mismatch(self):
  364. self._test_dovecot("user", "password", 401, response=b'OK\t2')
  365. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  366. def test_dovecot_remote_addr(self):
  367. self._test_dovecot("user", "password", 401, expected_rip=b'172.17.16.15',
  368. extra_env={
  369. 'REMOTE_ADDR': '172.17.16.15',
  370. 'HTTP_X_REMOTE_ADDR': '127.0.0.1',
  371. })
  372. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  373. def test_dovecot_x_remote_addr(self):
  374. self._test_dovecot("user", "password", 401, expected_rip=b'172.17.16.15',
  375. extra_env={
  376. 'REMOTE_ADDR': '127.0.0.1',
  377. 'HTTP_X_REMOTE_ADDR': '172.17.16.15',
  378. },
  379. extra_config={
  380. 'auth': {"remote_ip_source": "X-Remote-Addr"},
  381. })
  382. @pytest.mark.skipif(sys.platform == 'win32', reason="Not supported on Windows")
  383. def test_dovecot_x_remote_addr_whitespace(self):
  384. self._test_dovecot("user", "password", 401, expected_rip=b'172.17.16.15rip=127.0.0.1',
  385. extra_env={
  386. 'REMOTE_ADDR': '127.0.0.1',
  387. 'HTTP_X_REMOTE_ADDR': '172.17.16.15\trip=127.0.0.1',
  388. },
  389. extra_config={
  390. 'auth': {"remote_ip_source": "X-Remote-Addr"},
  391. })
  392. def test_custom(self) -> None:
  393. """Custom authentication."""
  394. self.configure({"auth": {"type": "radicale.tests.custom.auth"}})
  395. self.propfind("/tmp/", login="tmp:")
  396. def test_none(self) -> None:
  397. self.configure({"auth": {"type": "none"}})
  398. self.propfind("/tmp/", login="tmp:")
  399. def test_denyall(self) -> None:
  400. self.configure({"auth": {"type": "denyall"}})
  401. self.propfind("/tmp/", login="tmp:", check=401)