Repository navigation
Expand file tree
/
Copy pathrequest_limits.py
More file actions
371 lines (315 loc) · 14.1 KB
/
Copy pathrequest_limits.py
File metadata and controls
371 lines (315 loc) · 14.1 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""Request body and export admission limits for the self-hosted ASGI app.
The middleware reads an HTTP request body into a bounded in-memory buffer before
calling the application. This keeps a chunked multipart upload from reaching
Starlette's multipart parser (and its temporary-file handling) first. A body is
replayed through a small ASGI ``receive`` wrapper after it has passed the limit.
Defaults are deliberately tied to the route's 2 MiB SVG limit plus 64 KiB for
multipart framing and form fields:
``INFINITYGRID_MAX_REQUEST_BYTES``
Positive integer byte limit. Defaults to 2,162,688 bytes (2 MiB + 64 KiB).
``INFINITYGRID_EXPORT_RATE_PER_MINUTE``
Positive decimal token refill rate. Defaults to 12 exports per minute.
``0``, ``off``, ``disabled``, ``none`` or ``false`` explicitly disable the
export admission limiter.
``INFINITYGRID_EXPORT_BURST``
Positive integer number of immediately available export tokens. Defaults
to 8 and is ignored when the rate limiter is disabled.
``INFINITYGRID_EXPORT_RATE_MAX_KEYS``
Positive integer maximum number of peer buckets retained in one process.
Defaults to 1024; the least recently used bucket is evicted at capacity.
The rate limiter is intentionally process-local. A multi-worker deployment
needs a shared proxy or shared store if one global rate budget is required.
Peer identity is taken only from the ASGI ``scope['client']`` tuple. Forwarded
headers are not trusted; a reverse proxy therefore shares one conservative
bucket when it presents one direct peer address.
"""
from __future__ import annotations
import json
import math
import os
import re
import time
from collections import OrderedDict
from dataclasses import dataclass
from typing import Any, Awaitable, Callable, Iterable, Mapping, MutableMapping
DEFAULT_MAX_SVG_BYTES = 2 * 1024 * 1024
DEFAULT_MULTIPART_OVERHEAD_BYTES = 64 * 1024
DEFAULT_MAX_REQUEST_BYTES = DEFAULT_MAX_SVG_BYTES + DEFAULT_MULTIPART_OVERHEAD_BYTES
DEFAULT_EXPORT_RATE_PER_MINUTE = 12.0
DEFAULT_EXPORT_BURST = 8
DEFAULT_EXPORT_RATE_MAX_KEYS = 1024
EXPORT_PATHS = frozenset({"/api/export_step", "/api/export_3mf"})
_DECIMAL_BYTES = re.compile(rb"^[0-9]+$")
_DISABLED_VALUES = frozenset({"0", "off", "disabled", "none", "false"})
Scope = MutableMapping[str, Any]
Message = MutableMapping[str, Any]
Receive = Callable[[], Awaitable[Message]]
Send = Callable[[Message], Awaitable[None]]
ASGIApp = Callable[[Scope, Receive, Send], Awaitable[None]]
class RequestLimitConfigurationError(ValueError):
"""Raised when a limit environment variable has an invalid value."""
def _env(name: str) -> str | None:
value = os.environ.get(name)
return value.strip() if value is not None else None
def _positive_int(name: str, default: int) -> int:
value = _env(name)
if value is None:
return default
if not value.isdecimal():
raise RequestLimitConfigurationError(f"{name} must be a positive integer")
try:
parsed = int(value)
except ValueError:
raise RequestLimitConfigurationError(f"{name} must be a positive integer") from None
if parsed <= 0:
raise RequestLimitConfigurationError(f"{name} must be a positive integer")
return parsed
def _positive_float(name: str, default: float) -> float:
value = _env(name)
if value is None:
return default
try:
parsed = float(value)
except ValueError:
parsed = 0.0
if not math.isfinite(parsed) or parsed <= 0:
raise RequestLimitConfigurationError(f"{name} must be a positive number")
return parsed
def _rate_from_env() -> float | None:
value = _env("INFINITYGRID_EXPORT_RATE_PER_MINUTE")
if value is None:
return DEFAULT_EXPORT_RATE_PER_MINUTE
if value.lower() in _DISABLED_VALUES:
return None
return _positive_float("INFINITYGRID_EXPORT_RATE_PER_MINUTE", DEFAULT_EXPORT_RATE_PER_MINUTE)
@dataclass(frozen=True)
class RequestLimitSettings:
"""Validated settings consumed by :class:`RequestLimitsMiddleware`."""
max_request_bytes: int = DEFAULT_MAX_REQUEST_BYTES
export_rate_per_minute: float | None = DEFAULT_EXPORT_RATE_PER_MINUTE
export_burst: int = DEFAULT_EXPORT_BURST
export_rate_max_keys: int = DEFAULT_EXPORT_RATE_MAX_KEYS
@classmethod
def from_env(cls) -> "RequestLimitSettings":
rate = _rate_from_env()
# A disabled limiter has no bucket, so its burst and key-cap settings
# are intentionally ignored (including malformed values).
burst = (
_positive_int("INFINITYGRID_EXPORT_BURST", DEFAULT_EXPORT_BURST)
if rate is not None
else DEFAULT_EXPORT_BURST
)
return cls(
max_request_bytes=_positive_int("INFINITYGRID_MAX_REQUEST_BYTES", DEFAULT_MAX_REQUEST_BYTES),
export_rate_per_minute=rate,
export_burst=burst,
export_rate_max_keys=_positive_int(
"INFINITYGRID_EXPORT_RATE_MAX_KEYS", DEFAULT_EXPORT_RATE_MAX_KEYS
),
)
def _header_values(headers: Iterable[tuple[bytes, bytes]], name: bytes) -> list[bytes]:
return [value.strip() for key, value in headers if key.lower() == name]
def _declared_content_length(headers: Iterable[tuple[bytes, bytes]]) -> tuple[int | None, str | None]:
"""Return a strict Content-Length value and an optional validation error."""
values = _header_values(headers, b"content-length")
if not values:
return None, None
parsed: list[int] = []
for value in values:
# Comma-separated duplicate values are legal only when every value agrees.
for item in value.split(b","):
item = item.strip()
if not _DECIMAL_BYTES.fullmatch(item):
return None, "Malformed Content-Length header."
try:
parsed.append(int(item))
except ValueError:
return None, "Malformed Content-Length header."
if not parsed or len(set(parsed)) != 1:
return None, "Conflicting Content-Length headers."
return parsed[0], None
def _peer_key(scope: Mapping[str, Any]) -> str:
client = scope.get("client")
if isinstance(client, (tuple, list)) and client:
# Do not stringify the port: all connections from the same direct peer
# share a bucket, including connections forwarded by one proxy.
return str(client[0])
return "<unknown-peer>"
@dataclass
class _Bucket:
tokens: float
updated_at: float
class ExportRateLimiter:
"""A bounded, event-loop-local monotonic token bucket keyed by peer IP."""
def __init__(
self,
rate_per_minute: float | None = DEFAULT_EXPORT_RATE_PER_MINUTE,
burst: int = DEFAULT_EXPORT_BURST,
max_keys: int = DEFAULT_EXPORT_RATE_MAX_KEYS,
*,
clock: Callable[[], float] = time.monotonic,
) -> None:
if rate_per_minute is not None and (not math.isfinite(rate_per_minute) or rate_per_minute <= 0):
raise ValueError("rate_per_minute must be positive or None")
if burst <= 0 or max_keys <= 0:
raise ValueError("burst and max_keys must be positive")
self.rate_per_second = rate_per_minute / 60.0 if rate_per_minute is not None else None
self.burst = burst
self.max_keys = max_keys
self._clock = clock
self._buckets: OrderedDict[str, _Bucket] = OrderedDict()
@property
def enabled(self) -> bool:
return self.rate_per_second is not None
@property
def bucket_count(self) -> int:
return len(self._buckets)
def admit(self, peer: str) -> int | None:
"""Consume one token; return integer Retry-After seconds when denied."""
if self.rate_per_second is None:
return None
now = self._clock()
bucket = self._buckets.get(peer)
if bucket is None:
while len(self._buckets) >= self.max_keys:
self._buckets.popitem(last=False)
bucket = _Bucket(tokens=float(self.burst), updated_at=now)
self._buckets[peer] = bucket
else:
elapsed = max(0.0, now - bucket.updated_at)
bucket.tokens = min(float(self.burst), bucket.tokens + elapsed * self.rate_per_second)
bucket.updated_at = now
self._buckets.move_to_end(peer)
if bucket.tokens >= 1.0:
bucket.tokens -= 1.0
return None
wait_seconds = (1.0 - bucket.tokens) / self.rate_per_second
return max(1, math.ceil(wait_seconds))
def _json_response(status: int, detail: str, *, retry_after: int | None = None) -> list[Message]:
body = json.dumps({"detail": detail}, separators=(",", ":")).encode("utf-8")
headers = [
(b"content-type", b"application/json"),
(b"content-length", str(len(body)).encode("ascii")),
]
if retry_after is not None:
headers.append((b"retry-after", str(retry_after).encode("ascii")))
return [
{"type": "http.response.start", "status": status, "headers": headers},
{"type": "http.response.body", "body": body, "more_body": False},
]
class RequestLimitsMiddleware:
"""Buffer and validate request bodies, then apply export admission limits."""
def __init__(
self,
app: ASGIApp,
*,
max_request_bytes: int = DEFAULT_MAX_REQUEST_BYTES,
export_rate_per_minute: float | None = DEFAULT_EXPORT_RATE_PER_MINUTE,
export_burst: int = DEFAULT_EXPORT_BURST,
export_rate_max_keys: int = DEFAULT_EXPORT_RATE_MAX_KEYS,
limiter: ExportRateLimiter | None = None,
) -> None:
if max_request_bytes <= 0:
raise ValueError("max_request_bytes must be positive")
self.app = app
self.max_request_bytes = max_request_bytes
self.limiter = limiter or ExportRateLimiter(
rate_per_minute=export_rate_per_minute,
burst=export_burst,
max_keys=export_rate_max_keys,
)
@classmethod
def from_env(cls, app: ASGIApp) -> "RequestLimitsMiddleware":
settings = RequestLimitSettings.from_env()
return cls(
app,
max_request_bytes=settings.max_request_bytes,
export_rate_per_minute=settings.export_rate_per_minute,
export_burst=settings.export_burst,
export_rate_max_keys=settings.export_rate_max_keys,
)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope.get("type") != "http":
await self.app(scope, receive, send)
return
method = str(scope.get("method", "")).upper()
path = scope.get("path", "")
declared_length, length_error = _declared_content_length(scope.get("headers", ()))
if length_error is not None:
for message in _json_response(400, length_error):
await send(message)
return
if declared_length is not None and declared_length > self.max_request_bytes:
for message in _json_response(413, "Request body is too large."):
await send(message)
return
if method == "POST" and path in EXPORT_PATHS and self.limiter.enabled:
retry_after = self.limiter.admit(_peer_key(scope))
if retry_after is not None:
for message in _json_response(
429,
"Export rate limit exceeded. Please try again shortly.",
retry_after=retry_after,
):
await send(message)
return
# Static/icon requests are ordinarily bodyless. Keep their original
# receive channel so response/disconnect handling remains native and
# avoid allocating a replay buffer for a normal GET or HEAD. A
# positive declared length is treated as a real body and is buffered;
# malformed and oversized declarations were rejected above.
if method in {"GET", "HEAD"} and declared_length in {None, 0}:
await self.app(scope, receive, send)
return
body = bytearray()
while True:
message = await receive()
message_type = message.get("type")
if message_type == "http.disconnect":
# The peer has gone away; sending a response can itself fail and
# the downstream app must never see a partial request.
return
if message_type != "http.request":
for response in _json_response(400, "Malformed HTTP request body."):
await send(response)
return
chunk = message.get("body", b"")
if not isinstance(chunk, bytes):
for response in _json_response(400, "Malformed HTTP request body."):
await send(response)
return
if len(body) + len(chunk) > self.max_request_bytes:
for response in _json_response(413, "Request body is too large."):
await send(response)
return
body.extend(chunk)
if not message.get("more_body", False):
break
if declared_length is not None and declared_length != len(body):
for message in _json_response(400, "Content-Length does not match the request body."):
await send(message)
return
replayed = False
async def replay_receive() -> Message:
nonlocal replayed
if replayed:
# The middleware consumed the request body before dispatch.
# Preserve the original channel for post-body disconnect
# listeners and cancellation-aware response streaming.
return await receive()
replayed = True
return {"type": "http.request", "body": bytes(body), "more_body": False}
await self.app(scope, replay_receive, send)
__all__ = [
"DEFAULT_MAX_REQUEST_BYTES",
"DEFAULT_MAX_SVG_BYTES",
"DEFAULT_MULTIPART_OVERHEAD_BYTES",
"DEFAULT_EXPORT_RATE_PER_MINUTE",
"DEFAULT_EXPORT_BURST",
"DEFAULT_EXPORT_RATE_MAX_KEYS",
"EXPORT_PATHS",
"ExportRateLimiter",
"RequestLimitConfigurationError",
"RequestLimitSettings",
"RequestLimitsMiddleware",
]