velocity-python 0.9.3__py3-none-any.whl → 0.9.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- velocity/__init__.py +1 -1
- velocity/aws/capabilities/security.py +74 -15
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/METADATA +1 -1
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/RECORD +8 -8
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/WHEEL +0 -0
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/entry_points.txt +0 -0
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/licenses/LICENSE +0 -0
- {velocity_python-0.9.3.dist-info → velocity_python-0.9.4.dist-info}/top_level.txt +0 -0
velocity/__init__.py
CHANGED
|
@@ -40,7 +40,8 @@ import logging
|
|
|
40
40
|
|
|
41
41
|
from velocity.aws import feature_flags
|
|
42
42
|
from velocity.cache import memo, read_cache
|
|
43
|
-
from velocity.db import _pgddl, audit
|
|
43
|
+
from velocity.db import Expr, _pgddl, audit
|
|
44
|
+
from velocity.db.exceptions import DbLockTimeoutError
|
|
44
45
|
|
|
45
46
|
from .base import (
|
|
46
47
|
CIDR_LIST,
|
|
@@ -77,6 +78,17 @@ EVENT_COLUMNS = {
|
|
|
77
78
|
# or enforce) itself, so the rule holds whether or not this module is imported.
|
|
78
79
|
# core infrastructure (velocity.aws.schema "core"): every site has the table
|
|
79
80
|
SCHEMA = {
|
|
81
|
+
# check_rate's upsert conflicts on bucket, so the unique index is what
|
|
82
|
+
# makes the count atomic; sys_created serves the hourly prune.
|
|
83
|
+
RATE_TABLE: {
|
|
84
|
+
"columns": {"bucket": "TEXT", "window_start": "TIMESTAMPTZ", "hits": "BIGINT"},
|
|
85
|
+
"indexes": [
|
|
86
|
+
{"columns": ["sys_created"], "name": "idx__sys_rate_limit__sys_created"}
|
|
87
|
+
],
|
|
88
|
+
"unique_indexes": [
|
|
89
|
+
{"columns": ["bucket"], "name": "uidx__sys_rate_limit__bucket"}
|
|
90
|
+
],
|
|
91
|
+
},
|
|
80
92
|
EVENTS_TABLE: {
|
|
81
93
|
"columns": dict(EVENT_COLUMNS),
|
|
82
94
|
"indexes": [
|
|
@@ -212,17 +224,65 @@ def check_payload(body_size, *, max_bytes, enabled):
|
|
|
212
224
|
|
|
213
225
|
|
|
214
226
|
def check_rate(tx, bucket, *, limit, now, table=RATE_TABLE):
|
|
215
|
-
"""
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
227
|
+
"""One bucket's :func:`check_rates`."""
|
|
228
|
+
return check_rates(tx, [bucket], limit=limit, now=now, table=table)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def check_rates(tx, buckets, *, limit, now, table=RATE_TABLE):
|
|
232
|
+
"""Fixed one-minute window per bucket, kept in a velocity table: one
|
|
233
|
+
atomic upsert per bucket (``ON CONFLICT (bucket) DO UPDATE SET hits =
|
|
234
|
+
hits + 1 RETURNING hits``), so concurrent requests in one window each
|
|
235
|
+
count. The first refusal wins; later buckets are not counted after it.
|
|
236
|
+
|
|
237
|
+
Every bucket counts on ONE side transaction, committed before the
|
|
238
|
+
request goes on, as :func:`record_event` writes: a request that ends in
|
|
239
|
+
an error (a bad token, a refusal — this one included) rolls its own
|
|
240
|
+
transaction back, and the hits that most need counting would be lost
|
|
241
|
+
with it; the row locks are released at once instead of serializing the
|
|
242
|
+
caller's other requests behind a slow one. The lock wait is bounded
|
|
243
|
+
(``EVENT_LOCK_TIMEOUT``): a bucket still contended after it — a flood on
|
|
244
|
+
one address or user — is refused like an exceeded one (a 403 and its
|
|
245
|
+
event), not a 500. Any other failure (no unique index on ``bucket``: run
|
|
246
|
+
the schema lane) raises, so the guard fails closed."""
|
|
247
|
+
keys = [(b, f"{b}:{now.strftime('%Y%m%d%H%M')}") for b in buckets]
|
|
248
|
+
open_transaction = getattr(getattr(tx, "engine", None), "side_transaction", None)
|
|
249
|
+
if not callable(open_transaction):
|
|
250
|
+
return _count_buckets(tx, table, keys, now, limit)
|
|
251
|
+
try:
|
|
252
|
+
with open_transaction() as rate_tx:
|
|
253
|
+
if "postgres" in str(_pgddl.server_name(rate_tx) or "").lower():
|
|
254
|
+
rate_tx.execute(
|
|
255
|
+
"SELECT set_config('lock_timeout', %s, true)",
|
|
256
|
+
(EVENT_LOCK_TIMEOUT,),
|
|
257
|
+
)
|
|
258
|
+
return _count_buckets(rate_tx, table, keys, now, limit)
|
|
259
|
+
except DbLockTimeoutError:
|
|
260
|
+
names = ", ".join(b for b, _ in keys)
|
|
261
|
+
return "rate_limit", f"{names} contended past {EVENT_LOCK_TIMEOUT}"
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _count_buckets(tx, table, keys, now, limit):
|
|
265
|
+
for bucket, key in keys:
|
|
266
|
+
refusal = _rate_refusal(_count_hit(tx, table, key, now), bucket, limit)
|
|
267
|
+
if refusal:
|
|
268
|
+
return refusal
|
|
269
|
+
return None
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _count_hit(tx, table, key, now):
|
|
273
|
+
row = tx.table(table).merge(
|
|
274
|
+
{"bucket": key, "window_start": now, "hits": 1},
|
|
275
|
+
pk={"bucket": key},
|
|
276
|
+
on_conflict={"hits": Expr(f'COALESCE("{table}".hits, 0) + 1')},
|
|
277
|
+
insert_only=["window_start"],
|
|
278
|
+
returning=["hits"],
|
|
279
|
+
)
|
|
280
|
+
if not row or row.get("hits") is None:
|
|
281
|
+
raise RuntimeError(f"security: rate bucket {key!r} returned no count")
|
|
282
|
+
return int(row["hits"])
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _rate_refusal(hits, bucket, limit):
|
|
226
286
|
if hits > limit:
|
|
227
287
|
return "rate_limit", f"{bucket} exceeded {limit} requests/minute ({hits})"
|
|
228
288
|
return None
|
|
@@ -320,9 +380,8 @@ def enforce(tx, aws_event, context, *, now=None):
|
|
|
320
380
|
if not refusal and truthy(config.get("security_rate_limit_enabled")):
|
|
321
381
|
if not ip_in(facts["ip"], parse_cidrs(config.get("rate_limit_exempt"))):
|
|
322
382
|
limit = int(config.get("security_rate_limit_per_minute") or 120)
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
refusal = check_rate(tx, f"user:{user}", limit=limit, now=now)
|
|
383
|
+
buckets = [f"ip:{facts['ip']}"] + ([f"user:{user}"] if user else [])
|
|
384
|
+
refusal = check_rates(tx, buckets, limit=limit, now=now)
|
|
326
385
|
|
|
327
386
|
if refusal:
|
|
328
387
|
guard, reason = refusal
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
velocity/__init__.py,sha256=
|
|
1
|
+
velocity/__init__.py,sha256=LQvWrQ_E0n_N5MpD66cec4EmIkVVG4DomZiIyvZIsXA,362
|
|
2
2
|
velocity/cache.py,sha256=pthvw7gDVEXRzkTcQtA5YVf4Q1IxAamWsba4UReT0RE,8933
|
|
3
3
|
velocity/hooks.py,sha256=jY_qtAapNHvptmryFSD-Zc7RvLhV6e5nJEH3x2xTgCw,20575
|
|
4
4
|
velocity/logging.py,sha256=MAsZap1cDSdOBNRc_9ezQxup5dHOS0ARxU9NI_-UK3E,4037
|
|
@@ -40,7 +40,7 @@ velocity/aws/capabilities/infra.py,sha256=nUorjg7KpGhLW9ShUGdyh6yAwSKVy7xCAKKXwa
|
|
|
40
40
|
velocity/aws/capabilities/jobs.py,sha256=mf6sU7NG_j4Vzth6RARpazbUD7hx79FmTowXD0xunfc,5192
|
|
41
41
|
velocity/aws/capabilities/provisioning.py,sha256=4PF5QzQ_Pa51v5XN1OOn1juDxWqWPO3WI5fupqA9Agw,17844
|
|
42
42
|
velocity/aws/capabilities/retention.py,sha256=V-vSbfPdpm7w67kaXi3OkmqtqEYFDeDn6P8kCs_rEzs,3198
|
|
43
|
-
velocity/aws/capabilities/security.py,sha256=
|
|
43
|
+
velocity/aws/capabilities/security.py,sha256=37mqO5LtSL2gyaVspSh-l-boUJDL23es0s9u4alVoGI,25429
|
|
44
44
|
velocity/aws/capabilities/single_session.py,sha256=UQhBc4hR-qSZfpBM_o8434Xpg6Bt3EbzI7ugZIxJh8Y,3798
|
|
45
45
|
velocity/aws/capabilities/workflow.py,sha256=P7k6n2oZVovToAcHJUQwLdkD7duS4wKwBQKXaAMAnnU,1875
|
|
46
46
|
velocity/aws/deploy/__init__.py,sha256=nOLW_NqxEv7i2xRSoc4wmawfAEo9z7oPrjxJagKZzaM,4832
|
|
@@ -254,9 +254,9 @@ velocity/workflow/points.py,sha256=g_vQ3cdHdx_JloVCS5dVjo_jJoSolO92K5p3suDzxlE,1
|
|
|
254
254
|
velocity/workflow/schema.py,sha256=1ZAkGZ_4YrTAfY6TIPCviWQJGLhhmAJmkxisfLyy2og,21597
|
|
255
255
|
velocity/workflow/traveler.py,sha256=Ik1InoEo69sZB1wVV-UinVo4P0P5QlcV_hh858p3OWE,20562
|
|
256
256
|
velocity/workflow/views.py,sha256=oTPjpuheND8y4VbcTY8c-4fMdSHKb0jgGzXMuT_G7_s,32175
|
|
257
|
-
velocity_python-0.9.
|
|
258
|
-
velocity_python-0.9.
|
|
259
|
-
velocity_python-0.9.
|
|
260
|
-
velocity_python-0.9.
|
|
261
|
-
velocity_python-0.9.
|
|
262
|
-
velocity_python-0.9.
|
|
257
|
+
velocity_python-0.9.4.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
|
|
258
|
+
velocity_python-0.9.4.dist-info/METADATA,sha256=-FK2yC9InT4hH-wiYPk3-FX3JgIK0pN-oIR1H950VZc,17789
|
|
259
|
+
velocity_python-0.9.4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
260
|
+
velocity_python-0.9.4.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
|
|
261
|
+
velocity_python-0.9.4.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
|
|
262
|
+
velocity_python-0.9.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|