pathutils.py 2.6 KB

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