Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(agent): rate-limit the poll /connect auth path

Juan Denis5598a1c6e230
diff --git a/backend/app/api/agent_poll.py b/backend/app/api/agent_poll.py
index 243b4608..bd3a687b 100644
--- a/backend/app/api/agent_poll.py
+++ b/backend/app/api/agent_poll.py
@@ -16,14 +16,18 @@ Auth model:
which is what authenticates the request — no per-call HMAC.
"""
+import logging
from datetime import datetime
from flask import Blueprint, jsonify, request
from app.services.agent_registry import agent_registry
+from app.agent_gateway import _check_auth_rate_limit
from app.utils.ip_utils import is_ip_allowed
from app.services.anomaly_detection_service import anomaly_detection_service
+logger = logging.getLogger(__name__)
+
agent_poll_bp = Blueprint('agent_poll', __name__)
@@ -47,6 +51,15 @@ def connect():
return jsonify({'success': False, 'error': 'Missing required fields'}), 400
ip = _client_ip()
+
+ # Apply the SAME per-IP auth throttle that guards the WebSocket auth path
+ # (agent_gateway.on_auth). /connect is the REST-transport equivalent of
+ # on_auth, so without this it was an unthrottled bypass an attacker could
+ # use for credential-stuffing while the WS path stayed rate-limited.
+ if not _check_auth_rate_limit(ip):
+ logger.warning("Auth rate limit exceeded for IP: %s (poll/connect)", ip)
+ return jsonify({'success': False, 'error': 'Rate limit exceeded. Try again later.'}), 429
+
server = agent_registry.verify_agent_auth(
agent_id, api_key_prefix, signature, timestamp,
nonce=nonce, ip_address=ip,
diff --git a/backend/tests/test_agent_poll_ratelimit.py b/backend/tests/test_agent_poll_ratelimit.py
new file mode 100644
index 00000000..da3f157e
--- /dev/null
+++ b/backend/tests/test_agent_poll_ratelimit.py
@@ -0,0 +1,43 @@
+"""
+Regression test for the poll-transport auth rate-limit bypass (bug #3).
+
+The WebSocket auth path (agent_gateway.on_auth) is throttled per-IP via
+_check_auth_rate_limit, but the REST long-poll equivalent
+(POST /api/v1/agent/connect) did full HMAC auth WITHOUT the throttle — an
+unthrottled credential-stuffing surface. The fix applies the same shared
+limiter to /connect.
+"""
+import pytest
+
+import app.agent_gateway as gw
+
+
[email protected](autouse=True)
+def _reset_rate_limiter():
+ gw._auth_attempts.clear()
+ yield
+ gw._auth_attempts.clear()
+
+
+def _bogus_auth():
+ # All required fields present so we pass the 400 "missing fields" check and
+ # reach the rate limiter; creds are invalid so auth itself would 401.
+ return {
+ 'agent_id': 'nope',
+ 'api_key_prefix': 'sk_nope12345',
+ 'signature': 'x' * 16,
+ 'timestamp': 1,
+ }
+
+
+def test_poll_connect_is_rate_limited(client):
+ url = '/api/v1/agent/connect'
+
+ # The first N attempts clear the throttle (and fail auth with 401).
+ for _ in range(gw._AUTH_RATE_LIMIT):
+ r = client.post(url, json=_bogus_auth())
+ assert r.status_code == 401, r.get_data(as_text=True)
+
+ # The next attempt from the same IP is throttled, not auth-checked.
+ r = client.post(url, json=_bogus_auth())
+ assert r.status_code == 429