fix: rate-limit and audit by real client address
uvicorn does not trust forwarded headers from a peer outside forwarded_allow_ips, so request.client.host was the web gateway for every request. The auth-source bucket therefore counted all customers together: 60 failed logins from one attacker locked out everyone. Security events recorded the gateway address, which made the audit trail useless for attribution. The gateway now overwrites X-Forwarded-For with the peer address it observed instead of appending to whatever the client sent, so the header carries one value the client cannot choose, and client_ip() resolves it with a fallback to the connection peer. The guest-session limiter was keyed on the environment name, making it one global bucket of 120 per 15 minutes: roughly eight new visitors a minute for the whole site before legitimate traffic started receiving 429. It is now per source, and the ceiling is deliberately generous because offices and mobile carriers put many real customers behind a single address. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
16
local/app.py
16
local/app.py
@@ -17,7 +17,7 @@ from . import db
|
||||
from .adapters import FakeFreight, FakePayment, LocalS3Storage, require_runtime
|
||||
from .models import Freight, Move, Pay, QuoteRequest, Review, UploadStart, OperatorLogin
|
||||
from .pricing import price
|
||||
from .auth import COOKIE_SECURE, owner, session_row, new_session, operator, throttle, audit, rate_limit
|
||||
from .auth import COOKIE_SECURE, client_ip, owner, session_row, new_session, operator, throttle, audit, rate_limit
|
||||
from .scanning import require_clean
|
||||
|
||||
require_runtime()
|
||||
@@ -28,6 +28,10 @@ ENVIRONMENT = os.environ.get('APP_ENV', 'local')
|
||||
PUBLIC_ORIGIN = os.environ.get('PUBLIC_ORIGIN', 'http://localhost')
|
||||
ALLOWED_HOSTS = [host for host in os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') if host]
|
||||
ALLOWED_ORIGINS = [origin for origin in os.environ.get('ALLOWED_ORIGINS', PUBLIC_ORIGIN).split(',') if origin]
|
||||
# Per source and generous: a browser needs one session and keeps the cookie, but
|
||||
# offices and mobile carriers put many real customers behind one address, so a
|
||||
# tight per-IP ceiling would lock out the same people the old global one did.
|
||||
GUEST_SESSION_LIMIT = int(os.environ.get('GUEST_SESSION_LIMIT', '240'))
|
||||
PART_BYTES = int(os.environ.get('UPLOAD_PART_BYTES', '8388608'))
|
||||
if not 5242880 <= PART_BYTES <= 67108864:
|
||||
raise RuntimeError('UPLOAD_PART_BYTES must be between 5 and 64 MiB')
|
||||
@@ -56,7 +60,7 @@ def operator_login(body: OperatorLogin, request: Request, response: Response):
|
||||
valid_user = secrets.compare_digest(email.encode(), configured_email.encode())
|
||||
valid_password = secrets.compare_digest(body.password.encode(), os.environ['OPERATOR_PASSWORD'].encode())
|
||||
if not (valid_user and valid_password):
|
||||
audit('operator_login_failed')
|
||||
audit('operator_login_failed', ip=client_ip(request))
|
||||
raise HTTPException(401, 'Invalid operator login')
|
||||
token = secrets.token_urlsafe(32)
|
||||
with db.connect() as c:
|
||||
@@ -84,11 +88,11 @@ async def safe_headers(request, call_next):
|
||||
if request.method not in ('GET','HEAD','OPTIONS'):
|
||||
origin = request.headers.get('origin')
|
||||
if request.headers.get('sec-fetch-site') == 'cross-site' or (origin and origin not in ALLOWED_ORIGINS):
|
||||
audit('cross_origin_rejected')
|
||||
audit('cross_origin_rejected', ip=client_ip(request))
|
||||
return JSONResponse({'detail':'Cross-origin request rejected'}, status_code=403)
|
||||
response = await call_next(request)
|
||||
if response.status_code in (401,403,429) or response.status_code>=500:
|
||||
audit('http_security_event', method=request.method, status=response.status_code)
|
||||
audit('http_security_event', method=request.method, status=response.status_code, ip=client_ip(request))
|
||||
response.headers['Cache-Control'] = 'no-store'
|
||||
response.headers['X-Content-Type-Options'] = 'nosniff'
|
||||
response.headers['Referrer-Policy'] = 'no-referrer'
|
||||
@@ -111,7 +115,9 @@ def session(request: Request, response: Response):
|
||||
try:
|
||||
session_id = owner(request)
|
||||
except HTTPException:
|
||||
rate_limit('guest-sessions', ENVIRONMENT, 120, 900)
|
||||
# Per source, not per deployment: keyed on the environment name this was a
|
||||
# single global bucket, so ~8 new visitors a minute exhausted it site-wide.
|
||||
rate_limit('guest-sessions', client_ip(request), GUEST_SESSION_LIMIT, 900)
|
||||
with db.connect() as c:
|
||||
session_id = new_session(c, response)
|
||||
return {'environment': ENVIRONMENT, 'cart_scope': str(session_id), 'part_bytes': PART_BYTES,
|
||||
|
||||
Reference in New Issue
Block a user