_os.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import tempfile
  3. from os.path import abspath, dirname, join, normcase, sep
  4. from pathlib import Path
  5. from django.core.exceptions import SuspiciousFileOperation
  6. def safe_join(base, *paths):
  7. """
  8. Join one or more path components to the base path component intelligently.
  9. Return a normalized, absolute version of the final path.
  10. Raise ValueError if the final path isn't located inside of the base path
  11. component.
  12. """
  13. final_path = abspath(join(base, *paths))
  14. base_path = abspath(base)
  15. # Ensure final_path starts with base_path (using normcase to ensure we
  16. # don't false-negative on case insensitive operating systems like Windows),
  17. # further, one of the following conditions must be true:
  18. # a) The next character is the path separator (to prevent conditions like
  19. # safe_join("/dir", "/../d"))
  20. # b) The final path must be the same as the base path.
  21. # c) The base path must be the most root path (meaning either "/" or "C:\\")
  22. if (not normcase(final_path).startswith(normcase(base_path + sep)) and
  23. normcase(final_path) != normcase(base_path) and
  24. dirname(normcase(base_path)) != normcase(base_path)):
  25. raise SuspiciousFileOperation(
  26. 'The joined path ({}) is located outside of the base path '
  27. 'component ({})'.format(final_path, base_path))
  28. return final_path
  29. def symlinks_supported():
  30. """
  31. Return whether or not creating symlinks are supported in the host platform
  32. and/or if they are allowed to be created (e.g. on Windows it requires admin
  33. permissions).
  34. """
  35. with tempfile.TemporaryDirectory() as temp_dir:
  36. original_path = os.path.join(temp_dir, 'original')
  37. symlink_path = os.path.join(temp_dir, 'symlink')
  38. os.makedirs(original_path)
  39. try:
  40. os.symlink(original_path, symlink_path)
  41. supported = True
  42. except (OSError, NotImplementedError):
  43. supported = False
  44. return supported
  45. def to_path(value):
  46. """Convert value to a pathlib.Path instance, if not already a Path."""
  47. if isinstance(value, Path):
  48. return value
  49. elif not isinstance(value, str):
  50. raise TypeError('Invalid path type: %s' % type(value).__name__)
  51. return Path(value)