visproj commited on
Commit
bcb8efe
·
verified ·
1 Parent(s): 1ba2764

fix: identify visitors by the proxy-added X-Forwarded-For entry (spoofing confirmed)

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -181,8 +181,11 @@ class VisitorQuota:
181
  self._counts: dict[str, int] = {}
182
 
183
  def visitor(self, request: Request) -> str:
184
- forwarded = request.headers.get("x-forwarded-for", "")
185
- ip = forwarded.split(",")[0].strip() or (request.client.host if request.client else "unknown")
 
 
 
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: