2021-04-15 00:23:44 +08:00
|
|
|
import os
|
2021-05-13 14:53:44 +08:00
|
|
|
import pathlib
|
2021-04-15 00:23:44 +08:00
|
|
|
|
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
|
|
|
|
|
|
|
|
2021-05-13 14:53:44 +08:00
|
|
|
def validate_file_name(name, allow_relative_path=False):
|
2021-04-15 00:23:44 +08:00
|
|
|
# Remove potentially dangerous names
|
2021-05-13 14:53:44 +08:00
|
|
|
if os.path.basename(name) in {'', '.', '..'}:
|
2021-04-15 00:23:44 +08:00
|
|
|
raise SuspiciousFileOperation("Could not derive file name from '%s'" % name)
|
|
|
|
|
2021-05-13 14:53:44 +08:00
|
|
|
if allow_relative_path:
|
|
|
|
# Use PurePosixPath() because this branch is checked only in
|
|
|
|
# FileField.generate_filename() where all file paths are expected to be
|
|
|
|
# Unix style (with forward slashes).
|
|
|
|
path = pathlib.PurePosixPath(name)
|
|
|
|
if path.is_absolute() or '..' in path.parts:
|
|
|
|
raise SuspiciousFileOperation(
|
|
|
|
"Detected path traversal attempt in '%s'" % name
|
|
|
|
)
|
|
|
|
elif name != os.path.basename(name):
|
|
|
|
raise SuspiciousFileOperation("File name '%s' includes path elements" % name)
|
|
|
|
|
2021-04-15 00:23:44 +08:00
|
|
|
return name
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class FileProxyMixin:
|
2009-05-08 23:08:09 +08:00
|
|
|
"""
|
|
|
|
A mixin class used to forward file methods to an underlaying file
|
|
|
|
object. The internal file object has to be called "file"::
|
|
|
|
|
|
|
|
class FileProxy(FileProxyMixin):
|
|
|
|
def __init__(self, file):
|
|
|
|
self.file = file
|
|
|
|
"""
|
|
|
|
|
|
|
|
encoding = property(lambda self: self.file.encoding)
|
|
|
|
fileno = property(lambda self: self.file.fileno)
|
|
|
|
flush = property(lambda self: self.file.flush)
|
|
|
|
isatty = property(lambda self: self.file.isatty)
|
|
|
|
newlines = property(lambda self: self.file.newlines)
|
|
|
|
read = property(lambda self: self.file.read)
|
|
|
|
readinto = property(lambda self: self.file.readinto)
|
|
|
|
readline = property(lambda self: self.file.readline)
|
|
|
|
readlines = property(lambda self: self.file.readlines)
|
|
|
|
seek = property(lambda self: self.file.seek)
|
|
|
|
tell = property(lambda self: self.file.tell)
|
|
|
|
truncate = property(lambda self: self.file.truncate)
|
|
|
|
write = property(lambda self: self.file.write)
|
|
|
|
writelines = property(lambda self: self.file.writelines)
|
2016-05-23 00:43:56 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def closed(self):
|
|
|
|
return not self.file or self.file.closed
|
|
|
|
|
|
|
|
def readable(self):
|
|
|
|
if self.closed:
|
|
|
|
return False
|
|
|
|
if hasattr(self.file, 'readable'):
|
|
|
|
return self.file.readable()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def writable(self):
|
|
|
|
if self.closed:
|
|
|
|
return False
|
|
|
|
if hasattr(self.file, 'writable'):
|
|
|
|
return self.file.writable()
|
|
|
|
return 'w' in getattr(self.file, 'mode', '')
|
|
|
|
|
|
|
|
def seekable(self):
|
|
|
|
if self.closed:
|
|
|
|
return False
|
|
|
|
if hasattr(self.file, 'seekable'):
|
|
|
|
return self.file.seekable()
|
|
|
|
return True
|
2009-05-08 23:08:09 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.file)
|