-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_io.py
More file actions
182 lines (161 loc) · 6.51 KB
/
Copy pathbackend_io.py
File metadata and controls
182 lines (161 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Shared bounded network and filesystem helpers used by backend operations."""
from __future__ import annotations
import hashlib
import os
import tempfile
from pathlib import Path
from urllib.parse import urlparse
from urllib.request import Request, urlopen
DEFAULT_MAX_DOWNLOAD = 64 * 1024 * 1024
CHUNK_SIZE = 1024 * 1024
MAX_RESPONSE_BYTES = 8 * 1024 * 1024
def validate_http_url(url: str) -> str:
parsed = urlparse(str(url).strip())
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
raise ValueError("Only absolute HTTP(S) URLs are supported.")
if parsed.username or parsed.password:
raise ValueError("URLs with embedded credentials are not supported.")
return parsed.geturl()
def read_limited(response, max_bytes=MAX_RESPONSE_BYTES) -> bytes:
"""Read a response without allowing an unbounded API payload into memory."""
declared = 0
headers = getattr(response, "headers", None)
if headers:
try:
declared = int(headers.get("Content-Length", "0"))
except (TypeError, ValueError):
declared = 0
if declared < 0 or declared > max_bytes:
raise ValueError("The remote response is too large.")
chunks = []
total = 0
while True:
chunk = response.read(min(CHUNK_SIZE, max(0, max_bytes - total + 1)))
if not chunk:
break
total += len(chunk)
if total > max_bytes:
raise ValueError("The remote response is too large.")
chunks.append(chunk)
return b"".join(chunks)
def fsync_directory(path: Path) -> None:
try:
directory_fd = os.open(Path(path), os.O_RDONLY)
try:
os.fsync(directory_fd)
finally:
os.close(directory_fd)
except OSError:
pass
def atomic_write_bytes(path: Path, data: bytes, *, mode=0o600) -> Path:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.", suffix=".tmp", dir=path.parent)
temporary = Path(temporary_name)
try:
with os.fdopen(fd, "wb") as output:
output.write(data)
output.flush()
os.fsync(output.fileno())
os.chmod(temporary, mode)
os.replace(temporary, path)
os.chmod(path, mode)
fsync_directory(path.parent)
finally:
temporary.unlink(missing_ok=True)
return path
def atomic_write_text(path: Path, value: str, *, mode=0o600) -> Path:
return atomic_write_bytes(Path(path), str(value).encode("utf-8"), mode=mode)
def atomic_copy_stream(source, path: Path, *, mode=0o600, max_bytes=None) -> Path:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.", suffix=".tmp", dir=path.parent)
temporary = Path(temporary_name)
total = 0
try:
with os.fdopen(fd, "wb") as output:
while True:
chunk = source.read(CHUNK_SIZE)
if not chunk:
break
total += len(chunk)
if max_bytes is not None and total > max_bytes:
raise ValueError("The file is larger than the allowed limit.")
output.write(chunk)
output.flush()
os.fsync(output.fileno())
os.chmod(temporary, mode)
os.replace(temporary, path)
os.chmod(path, mode)
fsync_directory(path.parent)
finally:
temporary.unlink(missing_ok=True)
return path
def contained_path(path: Path, roots, *, must_exist=False) -> Path:
candidate = Path(path).expanduser().resolve(strict=False)
allowed = [Path(root).expanduser().resolve(strict=False) for root in roots]
if not any(candidate == root or root in candidate.parents for root in allowed):
raise ValueError(f"Path is outside an approved OpenBox directory: {candidate}")
if must_exist and not candidate.exists():
raise FileNotFoundError(str(candidate))
return candidate
def download_file(
url: str,
destination: Path,
*,
expected_types=(),
max_bytes=DEFAULT_MAX_DOWNLOAD,
timeout=30,
opener=urlopen,
headers=None,
sha256="",
mode=0o644,
) -> Path:
url = validate_http_url(url)
request = Request(url, headers={"User-Agent": "OpenBox/1", **(headers or {})})
destination = Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True)
temporary_name = None
digest = hashlib.sha256()
try:
with opener(request, timeout=timeout) as response:
status = getattr(response, "status", 200)
if status and int(status) >= 400:
raise ValueError(f"The remote server returned HTTP {status}.")
headers = getattr(response, "headers", {}) or {}
if hasattr(headers, "get_content_type"):
content_type = headers.get_content_type()
else:
content_type = str(headers.get("Content-Type", "")).split(";", 1)[0] if headers else ""
if expected_types and not any(content_type.startswith(item) for item in expected_types):
raise ValueError(f"The remote server returned an unsupported content type: {content_type or 'unknown'}")
try:
declared = int(headers.get("Content-Length", "0"))
except (TypeError, ValueError):
declared = 0
if declared > max_bytes:
raise ValueError("The download is too large.")
fd, temporary_name = tempfile.mkstemp(prefix=f".{destination.name}.", suffix=".tmp", dir=destination.parent)
with os.fdopen(fd, "wb") as output:
total = 0
while True:
chunk = response.read(CHUNK_SIZE)
if not chunk:
break
total += len(chunk)
if total > max_bytes:
raise ValueError("The download is too large.")
digest.update(chunk)
output.write(chunk)
output.flush()
os.fsync(output.fileno())
if sha256 and digest.hexdigest().casefold() != str(sha256).casefold():
raise ValueError("The downloaded file failed checksum verification.")
os.chmod(temporary_name, mode)
os.replace(temporary_name, destination)
fsync_directory(destination.parent)
temporary_name = None
finally:
if temporary_name:
Path(temporary_name).unlink(missing_ok=True)
return destination