Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/files/utils.py: 53%
51 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import os
2import pathlib
4from django.core.exceptions import SuspiciousFileOperation
7def validate_file_name(name, allow_relative_path=False):
8 # Remove potentially dangerous names
9 if os.path.basename(name) in {"", ".", ".."}: 9 ↛ 10line 9 didn't jump to line 10, because the condition on line 9 was never true
10 raise SuspiciousFileOperation("Could not derive file name from '%s'" % name)
12 if allow_relative_path:
13 # Use PurePosixPath() because this branch is checked only in
14 # FileField.generate_filename() where all file paths are expected to be
15 # Unix style (with forward slashes).
16 path = pathlib.PurePosixPath(name)
17 if path.is_absolute() or ".." in path.parts: 17 ↛ 18line 17 didn't jump to line 18, because the condition on line 17 was never true
18 raise SuspiciousFileOperation(
19 "Detected path traversal attempt in '%s'" % name
20 )
21 elif name != os.path.basename(name): 21 ↛ 22line 21 didn't jump to line 22, because the condition on line 21 was never true
22 raise SuspiciousFileOperation("File name '%s' includes path elements" % name)
24 return name
27class FileProxyMixin:
28 """
29 A mixin class used to forward file methods to an underlaying file
30 object. The internal file object has to be called "file"::
32 class FileProxy(FileProxyMixin):
33 def __init__(self, file):
34 self.file = file
35 """
37 encoding = property(lambda self: self.file.encoding) 37 ↛ exitline 37 didn't run the lambda on line 37
38 fileno = property(lambda self: self.file.fileno) 38 ↛ exitline 38 didn't run the lambda on line 38
39 flush = property(lambda self: self.file.flush) 39 ↛ exitline 39 didn't run the lambda on line 39
40 isatty = property(lambda self: self.file.isatty) 40 ↛ exitline 40 didn't run the lambda on line 40
41 newlines = property(lambda self: self.file.newlines) 41 ↛ exitline 41 didn't run the lambda on line 41
42 read = property(lambda self: self.file.read)
43 readinto = property(lambda self: self.file.readinto) 43 ↛ exitline 43 didn't run the lambda on line 43
44 readline = property(lambda self: self.file.readline) 44 ↛ exitline 44 didn't run the lambda on line 44
45 readlines = property(lambda self: self.file.readlines) 45 ↛ exitline 45 didn't run the lambda on line 45
46 seek = property(lambda self: self.file.seek)
47 tell = property(lambda self: self.file.tell) 47 ↛ exitline 47 didn't run the lambda on line 47
48 truncate = property(lambda self: self.file.truncate) 48 ↛ exitline 48 didn't run the lambda on line 48
49 write = property(lambda self: self.file.write) 49 ↛ exitline 49 didn't run the lambda on line 49
50 writelines = property(lambda self: self.file.writelines) 50 ↛ exitline 50 didn't run the lambda on line 50
52 @property
53 def closed(self):
54 return not self.file or self.file.closed
56 def readable(self):
57 if self.closed:
58 return False
59 if hasattr(self.file, "readable"):
60 return self.file.readable()
61 return True
63 def writable(self):
64 if self.closed:
65 return False
66 if hasattr(self.file, "writable"):
67 return self.file.writable()
68 return "w" in getattr(self.file, "mode", "")
70 def seekable(self):
71 if self.closed:
72 return False
73 if hasattr(self.file, "seekable"):
74 return self.file.seekable()
75 return True
77 def __iter__(self):
78 return iter(self.file)