fix: identify visitors by the proxy-added X-Forwarded-For entry (spoofing confirmed)
Browse files
app.py
CHANGED
|
@@ -181,8 +181,11 @@ class VisitorQuota:
|
|
| 181 |
self._counts: dict[str, int] = {}
|
| 182 |
|
| 183 |
def visitor(self, request: Request) -> str:
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
return hmac.new(self._key, ip.encode(), hashlib.sha256).hexdigest()
|
| 187 |
|
| 188 |
def _roll(self) -> None:
|
|
|
|
| 181 |
self._counts: dict[str, int] = {}
|
| 182 |
|
| 183 |
def visitor(self, request: Request) -> str:
|
| 184 |
+
# HF's proxy appends the real client address to any X-Forwarded-For the client sent,
|
| 185 |
+
# so the left-most entry is client-controlled (verified: a fake one dodged the limit).
|
| 186 |
+
# Use the right-most entry, the one the proxy added.
|
| 187 |
+
forwarded = [part.strip() for part in request.headers.get("x-forwarded-for", "").split(",") if part.strip()]
|
| 188 |
+
ip = forwarded[-1] if forwarded else (request.client.host if request.client else "unknown")
|
| 189 |
return hmac.new(self._key, ip.encode(), hashlib.sha256).hexdigest()
|
| 190 |
|
| 191 |
def _roll(self) -> None:
|