test_filepath.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. # Copyright 2021 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 inspect
  15. import os
  16. from typing import Iterable
  17. from synapse.rest.media.v1.filepath import MediaFilePaths, _wrap_with_jail_check
  18. from tests import unittest
  19. class MediaFilePathsTestCase(unittest.TestCase):
  20. def setUp(self) -> None:
  21. super().setUp()
  22. self.filepaths = MediaFilePaths("/media_store")
  23. def test_local_media_filepath(self) -> None:
  24. """Test local media paths"""
  25. self.assertEqual(
  26. self.filepaths.local_media_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"),
  27. "local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  28. )
  29. self.assertEqual(
  30. self.filepaths.local_media_filepath("GerZNDnDZVjsOtardLuwfIBg"),
  31. "/media_store/local_content/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  32. )
  33. def test_local_media_thumbnail(self) -> None:
  34. """Test local media thumbnail paths"""
  35. self.assertEqual(
  36. self.filepaths.local_media_thumbnail_rel(
  37. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  38. ),
  39. "local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  40. )
  41. self.assertEqual(
  42. self.filepaths.local_media_thumbnail(
  43. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  44. ),
  45. "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  46. )
  47. def test_local_media_thumbnail_dir(self) -> None:
  48. """Test local media thumbnail directory paths"""
  49. self.assertEqual(
  50. self.filepaths.local_media_thumbnail_dir("GerZNDnDZVjsOtardLuwfIBg"),
  51. "/media_store/local_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  52. )
  53. def test_remote_media_filepath(self) -> None:
  54. """Test remote media paths"""
  55. self.assertEqual(
  56. self.filepaths.remote_media_filepath_rel(
  57. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  58. ),
  59. "remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  60. )
  61. self.assertEqual(
  62. self.filepaths.remote_media_filepath(
  63. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  64. ),
  65. "/media_store/remote_content/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  66. )
  67. def test_remote_media_thumbnail(self) -> None:
  68. """Test remote media thumbnail paths"""
  69. self.assertEqual(
  70. self.filepaths.remote_media_thumbnail_rel(
  71. "example.com",
  72. "GerZNDnDZVjsOtardLuwfIBg",
  73. 800,
  74. 600,
  75. "image/jpeg",
  76. "scale",
  77. ),
  78. "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  79. )
  80. self.assertEqual(
  81. self.filepaths.remote_media_thumbnail(
  82. "example.com",
  83. "GerZNDnDZVjsOtardLuwfIBg",
  84. 800,
  85. 600,
  86. "image/jpeg",
  87. "scale",
  88. ),
  89. "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  90. )
  91. def test_remote_media_thumbnail_legacy(self) -> None:
  92. """Test old-style remote media thumbnail paths"""
  93. self.assertEqual(
  94. self.filepaths.remote_media_thumbnail_rel_legacy(
  95. "example.com", "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg"
  96. ),
  97. "remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg",
  98. )
  99. def test_remote_media_thumbnail_dir(self) -> None:
  100. """Test remote media thumbnail directory paths"""
  101. self.assertEqual(
  102. self.filepaths.remote_media_thumbnail_dir(
  103. "example.com", "GerZNDnDZVjsOtardLuwfIBg"
  104. ),
  105. "/media_store/remote_thumbnail/example.com/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  106. )
  107. def test_url_cache_filepath(self) -> None:
  108. """Test URL cache paths"""
  109. self.assertEqual(
  110. self.filepaths.url_cache_filepath_rel("2020-01-02_GerZNDnDZVjsOtar"),
  111. "url_cache/2020-01-02/GerZNDnDZVjsOtar",
  112. )
  113. self.assertEqual(
  114. self.filepaths.url_cache_filepath("2020-01-02_GerZNDnDZVjsOtar"),
  115. "/media_store/url_cache/2020-01-02/GerZNDnDZVjsOtar",
  116. )
  117. def test_url_cache_filepath_legacy(self) -> None:
  118. """Test old-style URL cache paths"""
  119. self.assertEqual(
  120. self.filepaths.url_cache_filepath_rel("GerZNDnDZVjsOtardLuwfIBg"),
  121. "url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  122. )
  123. self.assertEqual(
  124. self.filepaths.url_cache_filepath("GerZNDnDZVjsOtardLuwfIBg"),
  125. "/media_store/url_cache/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  126. )
  127. def test_url_cache_filepath_dirs_to_delete(self) -> None:
  128. """Test URL cache cleanup paths"""
  129. self.assertEqual(
  130. self.filepaths.url_cache_filepath_dirs_to_delete(
  131. "2020-01-02_GerZNDnDZVjsOtar"
  132. ),
  133. ["/media_store/url_cache/2020-01-02"],
  134. )
  135. def test_url_cache_filepath_dirs_to_delete_legacy(self) -> None:
  136. """Test old-style URL cache cleanup paths"""
  137. self.assertEqual(
  138. self.filepaths.url_cache_filepath_dirs_to_delete(
  139. "GerZNDnDZVjsOtardLuwfIBg"
  140. ),
  141. [
  142. "/media_store/url_cache/Ge/rZ",
  143. "/media_store/url_cache/Ge",
  144. ],
  145. )
  146. def test_url_cache_thumbnail(self) -> None:
  147. """Test URL cache thumbnail paths"""
  148. self.assertEqual(
  149. self.filepaths.url_cache_thumbnail_rel(
  150. "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale"
  151. ),
  152. "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale",
  153. )
  154. self.assertEqual(
  155. self.filepaths.url_cache_thumbnail(
  156. "2020-01-02_GerZNDnDZVjsOtar", 800, 600, "image/jpeg", "scale"
  157. ),
  158. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar/800-600-image-jpeg-scale",
  159. )
  160. def test_url_cache_thumbnail_legacy(self) -> None:
  161. """Test old-style URL cache thumbnail paths"""
  162. self.assertEqual(
  163. self.filepaths.url_cache_thumbnail_rel(
  164. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  165. ),
  166. "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  167. )
  168. self.assertEqual(
  169. self.filepaths.url_cache_thumbnail(
  170. "GerZNDnDZVjsOtardLuwfIBg", 800, 600, "image/jpeg", "scale"
  171. ),
  172. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg/800-600-image-jpeg-scale",
  173. )
  174. def test_url_cache_thumbnail_directory(self) -> None:
  175. """Test URL cache thumbnail directory paths"""
  176. self.assertEqual(
  177. self.filepaths.url_cache_thumbnail_directory_rel(
  178. "2020-01-02_GerZNDnDZVjsOtar"
  179. ),
  180. "url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  181. )
  182. self.assertEqual(
  183. self.filepaths.url_cache_thumbnail_directory("2020-01-02_GerZNDnDZVjsOtar"),
  184. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  185. )
  186. def test_url_cache_thumbnail_directory_legacy(self) -> None:
  187. """Test old-style URL cache thumbnail directory paths"""
  188. self.assertEqual(
  189. self.filepaths.url_cache_thumbnail_directory_rel(
  190. "GerZNDnDZVjsOtardLuwfIBg"
  191. ),
  192. "url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  193. )
  194. self.assertEqual(
  195. self.filepaths.url_cache_thumbnail_directory("GerZNDnDZVjsOtardLuwfIBg"),
  196. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  197. )
  198. def test_url_cache_thumbnail_dirs_to_delete(self) -> None:
  199. """Test URL cache thumbnail cleanup paths"""
  200. self.assertEqual(
  201. self.filepaths.url_cache_thumbnail_dirs_to_delete(
  202. "2020-01-02_GerZNDnDZVjsOtar"
  203. ),
  204. [
  205. "/media_store/url_cache_thumbnails/2020-01-02/GerZNDnDZVjsOtar",
  206. "/media_store/url_cache_thumbnails/2020-01-02",
  207. ],
  208. )
  209. def test_url_cache_thumbnail_dirs_to_delete_legacy(self) -> None:
  210. """Test old-style URL cache thumbnail cleanup paths"""
  211. self.assertEqual(
  212. self.filepaths.url_cache_thumbnail_dirs_to_delete(
  213. "GerZNDnDZVjsOtardLuwfIBg"
  214. ),
  215. [
  216. "/media_store/url_cache_thumbnails/Ge/rZ/NDnDZVjsOtardLuwfIBg",
  217. "/media_store/url_cache_thumbnails/Ge/rZ",
  218. "/media_store/url_cache_thumbnails/Ge",
  219. ],
  220. )
  221. def test_server_name_validation(self) -> None:
  222. """Test validation of server names"""
  223. self._test_path_validation(
  224. [
  225. "remote_media_filepath_rel",
  226. "remote_media_filepath",
  227. "remote_media_thumbnail_rel",
  228. "remote_media_thumbnail",
  229. "remote_media_thumbnail_rel_legacy",
  230. "remote_media_thumbnail_dir",
  231. ],
  232. parameter="server_name",
  233. valid_values=[
  234. "matrix.org",
  235. "matrix.org:8448",
  236. "matrix-federation.matrix.org",
  237. "matrix-federation.matrix.org:8448",
  238. "10.1.12.123",
  239. "10.1.12.123:8448",
  240. "[fd00:abcd::ffff]",
  241. "[fd00:abcd::ffff]:8448",
  242. ],
  243. invalid_values=[
  244. "/matrix.org",
  245. "matrix.org/..",
  246. "matrix.org\x00",
  247. "",
  248. ".",
  249. "..",
  250. "/",
  251. ],
  252. )
  253. def test_file_id_validation(self) -> None:
  254. """Test validation of local, remote and legacy URL cache file / media IDs"""
  255. # File / media IDs get split into three parts to form paths, consisting of the
  256. # first two characters, next two characters and rest of the ID.
  257. valid_file_ids = [
  258. "GerZNDnDZVjsOtardLuwfIBg",
  259. # Unexpected, but produces an acceptable path:
  260. "GerZN", # "N" becomes the last directory
  261. ]
  262. invalid_file_ids = [
  263. "/erZNDnDZVjsOtardLuwfIBg",
  264. "Ge/ZNDnDZVjsOtardLuwfIBg",
  265. "GerZ/DnDZVjsOtardLuwfIBg",
  266. "GerZ/..",
  267. "G\x00rZNDnDZVjsOtardLuwfIBg",
  268. "Ger\x00NDnDZVjsOtardLuwfIBg",
  269. "GerZNDnDZVjsOtardLuwfIBg\x00",
  270. "",
  271. "Ge",
  272. "GerZ",
  273. "GerZ.",
  274. "..rZNDnDZVjsOtardLuwfIBg",
  275. "Ge..NDnDZVjsOtardLuwfIBg",
  276. "GerZ..",
  277. "GerZ/",
  278. ]
  279. self._test_path_validation(
  280. [
  281. "local_media_filepath_rel",
  282. "local_media_filepath",
  283. "local_media_thumbnail_rel",
  284. "local_media_thumbnail",
  285. "local_media_thumbnail_dir",
  286. # Legacy URL cache media IDs
  287. "url_cache_filepath_rel",
  288. "url_cache_filepath",
  289. # `url_cache_filepath_dirs_to_delete` is tested below.
  290. "url_cache_thumbnail_rel",
  291. "url_cache_thumbnail",
  292. "url_cache_thumbnail_directory_rel",
  293. "url_cache_thumbnail_directory",
  294. "url_cache_thumbnail_dirs_to_delete",
  295. ],
  296. parameter="media_id",
  297. valid_values=valid_file_ids,
  298. invalid_values=invalid_file_ids,
  299. )
  300. # `url_cache_filepath_dirs_to_delete` ignores what would be the last path
  301. # component, so only the first 4 characters matter.
  302. self._test_path_validation(
  303. [
  304. "url_cache_filepath_dirs_to_delete",
  305. ],
  306. parameter="media_id",
  307. valid_values=valid_file_ids,
  308. invalid_values=[
  309. "/erZNDnDZVjsOtardLuwfIBg",
  310. "Ge/ZNDnDZVjsOtardLuwfIBg",
  311. "G\x00rZNDnDZVjsOtardLuwfIBg",
  312. "Ger\x00NDnDZVjsOtardLuwfIBg",
  313. "",
  314. "Ge",
  315. "..rZNDnDZVjsOtardLuwfIBg",
  316. "Ge..NDnDZVjsOtardLuwfIBg",
  317. ],
  318. )
  319. self._test_path_validation(
  320. [
  321. "remote_media_filepath_rel",
  322. "remote_media_filepath",
  323. "remote_media_thumbnail_rel",
  324. "remote_media_thumbnail",
  325. "remote_media_thumbnail_rel_legacy",
  326. "remote_media_thumbnail_dir",
  327. ],
  328. parameter="file_id",
  329. valid_values=valid_file_ids,
  330. invalid_values=invalid_file_ids,
  331. )
  332. def test_url_cache_media_id_validation(self) -> None:
  333. """Test validation of URL cache media IDs"""
  334. self._test_path_validation(
  335. [
  336. "url_cache_filepath_rel",
  337. "url_cache_filepath",
  338. # `url_cache_filepath_dirs_to_delete` only cares about the date prefix
  339. "url_cache_thumbnail_rel",
  340. "url_cache_thumbnail",
  341. "url_cache_thumbnail_directory_rel",
  342. "url_cache_thumbnail_directory",
  343. "url_cache_thumbnail_dirs_to_delete",
  344. ],
  345. parameter="media_id",
  346. valid_values=[
  347. "2020-01-02_GerZNDnDZVjsOtar",
  348. "2020-01-02_G", # Unexpected, but produces an acceptable path
  349. ],
  350. invalid_values=[
  351. "2020-01-02",
  352. "2020-01-02-",
  353. "2020-01-02-.",
  354. "2020-01-02-..",
  355. "2020-01-02-/",
  356. "2020-01-02-/GerZNDnDZVjsOtar",
  357. "2020-01-02-GerZNDnDZVjsOtar/..",
  358. "2020-01-02-GerZNDnDZVjsOtar\x00",
  359. ],
  360. )
  361. def test_content_type_validation(self) -> None:
  362. """Test validation of thumbnail content types"""
  363. self._test_path_validation(
  364. [
  365. "local_media_thumbnail_rel",
  366. "local_media_thumbnail",
  367. "remote_media_thumbnail_rel",
  368. "remote_media_thumbnail",
  369. "remote_media_thumbnail_rel_legacy",
  370. "url_cache_thumbnail_rel",
  371. "url_cache_thumbnail",
  372. ],
  373. parameter="content_type",
  374. valid_values=[
  375. "image/jpeg",
  376. ],
  377. invalid_values=[
  378. "", # ValueError: not enough values to unpack
  379. "image/jpeg/abc", # ValueError: too many values to unpack
  380. "image/jpeg\x00",
  381. ],
  382. )
  383. def test_thumbnail_method_validation(self) -> None:
  384. """Test validation of thumbnail methods"""
  385. self._test_path_validation(
  386. [
  387. "local_media_thumbnail_rel",
  388. "local_media_thumbnail",
  389. "remote_media_thumbnail_rel",
  390. "remote_media_thumbnail",
  391. "url_cache_thumbnail_rel",
  392. "url_cache_thumbnail",
  393. ],
  394. parameter="method",
  395. valid_values=[
  396. "crop",
  397. "scale",
  398. ],
  399. invalid_values=[
  400. "/scale",
  401. "scale/..",
  402. "scale\x00",
  403. "/",
  404. ],
  405. )
  406. def _test_path_validation(
  407. self,
  408. methods: Iterable[str],
  409. parameter: str,
  410. valid_values: Iterable[str],
  411. invalid_values: Iterable[str],
  412. ) -> None:
  413. """Test that the specified methods validate the named parameter as expected
  414. Args:
  415. methods: The names of `MediaFilePaths` methods to test
  416. parameter: The name of the parameter to test
  417. valid_values: A list of parameter values that are expected to be accepted
  418. invalid_values: A list of parameter values that are expected to be rejected
  419. Raises:
  420. AssertionError: If a value was accepted when it should have failed
  421. validation.
  422. ValueError: If a value failed validation when it should have been accepted.
  423. """
  424. for method in methods:
  425. get_path = getattr(self.filepaths, method)
  426. parameters = inspect.signature(get_path).parameters
  427. kwargs = {
  428. "server_name": "matrix.org",
  429. "media_id": "GerZNDnDZVjsOtardLuwfIBg",
  430. "file_id": "GerZNDnDZVjsOtardLuwfIBg",
  431. "width": 800,
  432. "height": 600,
  433. "content_type": "image/jpeg",
  434. "method": "scale",
  435. }
  436. if get_path.__name__.startswith("url_"):
  437. kwargs["media_id"] = "2020-01-02_GerZNDnDZVjsOtar"
  438. kwargs = {k: v for k, v in kwargs.items() if k in parameters}
  439. kwargs.pop(parameter)
  440. for value in valid_values:
  441. kwargs[parameter] = value
  442. get_path(**kwargs)
  443. # No exception should be raised
  444. for value in invalid_values:
  445. with self.assertRaises(ValueError):
  446. kwargs[parameter] = value
  447. path_or_list = get_path(**kwargs)
  448. self.fail(
  449. f"{value!r} unexpectedly passed validation: "
  450. f"{method} returned {path_or_list!r}"
  451. )
  452. class MediaFilePathsJailTestCase(unittest.TestCase):
  453. def _check_relative_path(self, filepaths: MediaFilePaths, path: str) -> None:
  454. """Passes a relative path through the jail check.
  455. Args:
  456. filepaths: The `MediaFilePaths` instance.
  457. path: A path relative to the media store directory.
  458. Raises:
  459. ValueError: If the jail check fails.
  460. """
  461. @_wrap_with_jail_check(relative=True)
  462. def _make_relative_path(self: MediaFilePaths, path: str) -> str:
  463. return path
  464. _make_relative_path(filepaths, path)
  465. def _check_absolute_path(self, filepaths: MediaFilePaths, path: str) -> None:
  466. """Passes an absolute path through the jail check.
  467. Args:
  468. filepaths: The `MediaFilePaths` instance.
  469. path: A path relative to the media store directory.
  470. Raises:
  471. ValueError: If the jail check fails.
  472. """
  473. @_wrap_with_jail_check(relative=False)
  474. def _make_absolute_path(self: MediaFilePaths, path: str) -> str:
  475. return os.path.join(self.base_path, path)
  476. _make_absolute_path(filepaths, path)
  477. def test_traversal_inside(self) -> None:
  478. """Test the jail check for paths that stay within the media directory."""
  479. # Despite the `../`s, these paths still lie within the media directory and it's
  480. # expected for the jail check to allow them through.
  481. # These paths ought to trip the other checks in place and should never be
  482. # returned.
  483. filepaths = MediaFilePaths("/media_store")
  484. path = "url_cache/2020-01-02/../../GerZNDnDZVjsOtar"
  485. self._check_relative_path(filepaths, path)
  486. self._check_absolute_path(filepaths, path)
  487. def test_traversal_outside(self) -> None:
  488. """Test that the jail check fails for paths that escape the media directory."""
  489. filepaths = MediaFilePaths("/media_store")
  490. path = "url_cache/2020-01-02/../../../GerZNDnDZVjsOtar"
  491. with self.assertRaises(ValueError):
  492. self._check_relative_path(filepaths, path)
  493. with self.assertRaises(ValueError):
  494. self._check_absolute_path(filepaths, path)
  495. def test_traversal_reentry(self) -> None:
  496. """Test the jail check for paths that exit and re-enter the media directory."""
  497. # These paths lie outside the media directory if it is a symlink, and inside
  498. # otherwise. Ideally the check should fail, but this proves difficult.
  499. # This test documents the behaviour for this edge case.
  500. # These paths ought to trip the other checks in place and should never be
  501. # returned.
  502. filepaths = MediaFilePaths("/media_store")
  503. path = "url_cache/2020-01-02/../../../media_store/GerZNDnDZVjsOtar"
  504. self._check_relative_path(filepaths, path)
  505. self._check_absolute_path(filepaths, path)
  506. def test_symlink(self) -> None:
  507. """Test that a symlink does not cause the jail check to fail."""
  508. media_store_path = self.mktemp()
  509. # symlink the media store directory
  510. os.symlink("/mnt/synapse/media_store", media_store_path)
  511. # Test that relative and absolute paths don't trip the check
  512. # NB: `media_store_path` is a relative path
  513. filepaths = MediaFilePaths(media_store_path)
  514. self._check_relative_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  515. self._check_absolute_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  516. filepaths = MediaFilePaths(os.path.abspath(media_store_path))
  517. self._check_relative_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  518. self._check_absolute_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  519. def test_symlink_subdirectory(self) -> None:
  520. """Test that a symlinked subdirectory does not cause the jail check to fail."""
  521. media_store_path = self.mktemp()
  522. os.mkdir(media_store_path)
  523. # symlink `url_cache/`
  524. os.symlink(
  525. "/mnt/synapse/media_store_url_cache",
  526. os.path.join(media_store_path, "url_cache"),
  527. )
  528. # Test that relative and absolute paths don't trip the check
  529. # NB: `media_store_path` is a relative path
  530. filepaths = MediaFilePaths(media_store_path)
  531. self._check_relative_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  532. self._check_absolute_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  533. filepaths = MediaFilePaths(os.path.abspath(media_store_path))
  534. self._check_relative_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")
  535. self._check_absolute_path(filepaths, "url_cache/2020-01-02/GerZNDnDZVjsOtar")