maint: Dependency upgrades and High backlog items (v2.11.0)
- All dependencies to current releases (97 known vulnerabilities -> 0), Python 3.14, uv pinned, requirements-test.txt, lxml/dateutil pinned - CSRF tokens on admin forms, logout and IndieAuth consent - IndieAuth client and crosspost link-card fetches via safe_get - FLASK_DEBUG/FLASK_ENV default off/production; image sets them - Foreign keys enforced on pooled connections; migration 018 removes orphans - Rate limits shared across processes (rate_limits, migration 019) - datetime.utcnow() and sqlite3 default adapters replaced - compose.yaml fixed; duplicate dev_auth.py and test.ini removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f17e145ff
commit
75946be7b2
67 files changed
+885
-498
No files matched your search
@@ -138,6 +138,79 @@ _ip_limiter: RateLimiter | None = None
|
||||
_domain_limiter: RateLimiter | None = None
|
||||
|
||||
|
||||
class SharedRateLimiter:
|
||||
"""
|
||||
Rate limiter whose counts live in SQLite, shared by every gunicorn
|
||||
process (v2.11.0). In-memory counters let each of the 4 workers allow the
|
||||
full limit, so the real limit was four times the setting.
|
||||
|
||||
Sliding-window counter: the current window's count plus the previous
|
||||
window's, weighted by how much of it still overlaps the last
|
||||
`window_seconds`. Same interface as RateLimiter; needs an app context.
|
||||
"""
|
||||
|
||||
CLEANUP_EVERY = 200 # calls between removals of old windows
|
||||
|
||||
def __init__(self, limit: int, window_seconds: int, prefix: str):
|
||||
self.limit = limit
|
||||
self.window_seconds = window_seconds
|
||||
self.prefix = prefix
|
||||
self._calls = 0
|
||||
|
||||
def _db(self):
|
||||
from flask import current_app
|
||||
from starpunk.database import get_db
|
||||
return get_db(current_app)
|
||||
|
||||
def _windows(self):
|
||||
now = time.time()
|
||||
current = int(now // self.window_seconds)
|
||||
overlap = 1 - (now % self.window_seconds) / self.window_seconds
|
||||
return now, current, overlap
|
||||
|
||||
def _estimate(self, db, key: str) -> float:
|
||||
_, current, overlap = self._windows()
|
||||
counts = dict(db.execute(
|
||||
"SELECT window, count FROM rate_limits WHERE key = ? AND window IN (?, ?)",
|
||||
(f"{self.prefix}:{key}", current - 1, current),
|
||||
).fetchall())
|
||||
return counts.get(current - 1, 0) * overlap + counts.get(current, 0)
|
||||
|
||||
def is_allowed(self, key: str) -> bool:
|
||||
db = self._db()
|
||||
_, current, _ = self._windows()
|
||||
if self._estimate(db, key) >= self.limit:
|
||||
return False
|
||||
db.execute(
|
||||
"INSERT INTO rate_limits (key, window, count) VALUES (?, ?, 1) "
|
||||
"ON CONFLICT(key, window) DO UPDATE SET count = count + 1",
|
||||
(f"{self.prefix}:{key}", current),
|
||||
)
|
||||
db.commit()
|
||||
self._calls += 1
|
||||
if self._calls % self.CLEANUP_EVERY == 0:
|
||||
self.cleanup()
|
||||
return True
|
||||
|
||||
def get_retry_after(self, key: str) -> int:
|
||||
now, current, _ = self._windows()
|
||||
return max(1, int((current + 1) * self.window_seconds - now) + 1)
|
||||
|
||||
def get_request_count(self, key: str) -> int:
|
||||
return int(self._estimate(self._db(), key))
|
||||
|
||||
def cleanup(self, max_idle_seconds: int = 300) -> int:
|
||||
"""Remove windows too old to matter (this limiter's keys only)"""
|
||||
_, current, _ = self._windows()
|
||||
db = self._db()
|
||||
cursor = db.execute(
|
||||
"DELETE FROM rate_limits WHERE key LIKE ? AND window < ?",
|
||||
(f"{self.prefix}:%", current - 1),
|
||||
)
|
||||
db.commit()
|
||||
return cursor.rowcount
|
||||
|
||||
|
||||
def init_rate_limiters(app) -> None:
|
||||
"""
|
||||
Initialize rate limiters with Flask app configuration.
|
||||
@@ -157,8 +230,9 @@ def init_rate_limiters(app) -> None:
|
||||
domain_limit = app.config.get('WEBMENTION_RATE_LIMIT_DOMAIN', 10)
|
||||
window = app.config.get('WEBMENTION_RATE_LIMIT_WINDOW', 60)
|
||||
|
||||
_ip_limiter = RateLimiter(ip_limit, window)
|
||||
_domain_limiter = RateLimiter(domain_limit, window)
|
||||
# Shared by all server processes (v2.11.0)
|
||||
_ip_limiter = SharedRateLimiter(ip_limit, window, "webmention-ip")
|
||||
_domain_limiter = SharedRateLimiter(domain_limit, window, "webmention-domain")
|
||||
|
||||
app.logger.info(
|
||||
f"Webmention rate limiter initialized: {ip_limit}/min IP, "
|
||||
|
||||
Reference in new issue
Block a user