pathutils.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Helper functions for working with paths.
  19. """
  20. import os
  21. import posixpath
  22. from . import log
  23. def sanitize_path(path):
  24. """Make path absolute with leading slash to prevent access to other data.
  25. Preserve a potential trailing slash.
  26. """
  27. trailing_slash = "/" if path.endswith("/") else ""
  28. path = posixpath.normpath(path)
  29. new_path = "/"
  30. for part in path.split("/"):
  31. if not part or part in (".", ".."):
  32. continue
  33. new_path = posixpath.join(new_path, part)
  34. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  35. return new_path + trailing_slash
  36. def is_safe_path_component(path):
  37. """Check if path is a single component of a POSIX path.
  38. Check that the path is safe to join too.
  39. """
  40. if not path:
  41. return False
  42. head, _ = posixpath.split(path)
  43. if head:
  44. return False
  45. if path in (".", ".."):
  46. return False
  47. return True
  48. def is_safe_filesystem_path_component(path):
  49. """Check if path is a single component of a filesystem path.
  50. Check that the path is safe to join too.
  51. """
  52. if not path:
  53. return False
  54. drive, _ = os.path.splitdrive(path)
  55. if drive:
  56. return False
  57. head, _ = os.path.split(path)
  58. if head:
  59. return False
  60. if path in (os.curdir, os.pardir):
  61. return False
  62. return True
  63. def path_to_filesystem(path, base_folder):
  64. """Convert path to a local filesystem path relative to base_folder.
  65. Conversion is done in a secure manner, or raises ValueError.
  66. """
  67. sane_path = sanitize_path(path).strip("/")
  68. safe_path = base_folder
  69. if not sane_path:
  70. return safe_path
  71. for part in sane_path.split("/"):
  72. if not is_safe_filesystem_path_component(part):
  73. log.LOGGER.debug(
  74. "Can't translate path safely to filesystem: %s", path)
  75. raise ValueError("Unsafe path")
  76. safe_path = os.path.join(safe_path, part)
  77. return safe_path