diff --git a/backend/.env.example b/backend/.env.example index 64596e25..4eccc02e 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -3,6 +3,10 @@ MODEL_NAME=gemini-2.5-pro # Show model "thinking" process in the UI SHOW_MODEL_THINKING=false +# Per-IP rate limit for the costly /api/query endpoint (flask_limiter syntax). +# Protects against scripted abuse / cost-draining; tune without a redeploy. +QUERY_RATE_LIMIT=10 per minute + # Google Gemini / Vertex AI settings GOOGLE_CLOUD_PROJECT=tenantfirstaid # Fill with your own path (see README.md Local Development/Prerequisites for details) diff --git a/backend/scripts/repro_rate_limit.py b/backend/scripts/repro_rate_limit.py new file mode 100644 index 00000000..76ca5425 --- /dev/null +++ b/backend/scripts/repro_rate_limit.py @@ -0,0 +1,87 @@ +"""Reproduction: /api/query has NO rate limit, while /api/feedback does. + +Uses the real Flask app from tenantfirstaid.app with the LLM chat manager and +the email sender mocked out, so no external calls are made. We fire a burst of +requests at each endpoint and report the status codes. + +Expected (buggy) behavior: + - /api/query : 20/20 requests return 200, ZERO 429s -> unthrottled, cost risk + - /api/feedback: first 3 return 200, the rest return 429 -> throttled +""" + +from collections import Counter +from unittest.mock import patch + +from tenantfirstaid.app import app, limiter + + +def fire_query(client, n): + """Send n POSTs to /api/query with a mocked chat manager.""" + codes = [] + with patch("tenantfirstaid.chat.LangChainChatManager") as mock_cm: + mock_cm.return_value.generate_streaming_response.return_value = iter( + [{"type": "text", "text": "mocked legal advice"}] + ) + for _ in range(n): + resp = client.post( + "/api/query", + json={ + "messages": [{"role": "human", "content": "hi"}], + "city": None, + "state": "or", + }, + ) + # Drain the streaming body so the request fully completes. + resp.get_data() + codes.append(resp.status_code) + return Counter(codes) + + +def fire_feedback(client, n): + """Send n POSTs to /api/feedback with a mocked email sender.""" + codes = [] + with ( + patch("tenantfirstaid.feedback.EmailMessage"), + patch.dict( + "os.environ", {"SENDER_EMAIL": "s@t.com", "RECIPIENT_EMAIL": "r@t.com"} + ), + ): + for _ in range(n): + resp = client.post( + "/api/feedback", + data={"name": "Jane", "subject": "Bug", "feedback": "Broken"}, + ) + codes.append(resp.status_code) + return Counter(codes) + + +def run_trial(trial_no, burst=20): + app.testing = True + limiter.reset() # start each trial with a clean rate-limit window + client = app.test_client() + + query_codes = fire_query(client, burst) + limiter.reset() + feedback_codes = fire_feedback(client, 5) + + print(f"\n===== TRIAL {trial_no} =====") + print(f"/api/query x{burst:<3} -> {dict(query_codes)}") + print( + f" 429s on /api/query: {query_codes.get(429, 0)} " + f"(0 == UNPROTECTED, bug reproduced)" + ) + print(f"/api/feedback x5 -> {dict(feedback_codes)}") + print( + f" 429s on /api/feedback: {feedback_codes.get(429, 0)} " + f"(>0 == limiter works here)" + ) + + bug_present = query_codes.get(429, 0) == 0 and feedback_codes.get(429, 0) > 0 + print(f" RESULT: bug {'REPRODUCED' if bug_present else 'NOT reproduced'}") + return bug_present + + +if __name__ == "__main__": + results = [run_trial(i) for i in (1, 2)] + print("\n========================================") + print(f"Bug reproduced in {sum(results)}/{len(results)} trials.") diff --git a/backend/tenantfirstaid/app.py b/backend/tenantfirstaid/app.py index 75f888ae..f26ebe7f 100644 --- a/backend/tenantfirstaid/app.py +++ b/backend/tenantfirstaid/app.py @@ -52,7 +52,14 @@ mail = Mail(app) -app.add_url_rule("/api/query", view_func=ChatView.as_view("chat"), methods=["POST"]) +# Rate limit the costly /api/query endpoint: each call fans out to Vertex AI RAG +# plus a Gemini completion, so unthrottled traffic is a direct cost and +# availability risk. The budget is tunable without a redeploy via QUERY_RATE_LIMIT +# and defaults to a value that allows a normal back-and-forth chat but blocks +# scripted abuse. +QUERY_RATE_LIMIT = os.getenv("QUERY_RATE_LIMIT", "10 per minute") +chat_view = limiter.limit(QUERY_RATE_LIMIT)(ChatView.as_view("chat")) +app.add_url_rule("/api/query", view_func=chat_view, methods=["POST"]) @limiter.limit("3 per minute") diff --git a/backend/tests/test_app.py b/backend/tests/test_app.py index c2a72139..85ad71b5 100644 --- a/backend/tests/test_app.py +++ b/backend/tests/test_app.py @@ -67,6 +67,39 @@ def test_post_query_returns_200(self, mock_cm_cls, client): assert resp.status_code == 200 assert resp.mimetype == "text/plain" + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_query_within_limit_returns_200(self, mock_cm_cls, client): + # A short burst that stays under the limit should all succeed. + mock_cm_cls.return_value.generate_streaming_response.side_effect = ( + lambda *args, **kwargs: iter([{"type": "text", "text": "Hi"}]) + ) + payload = { + "messages": [{"role": "human", "content": "Help"}], + "city": None, + "state": "or", + } + for _ in range(5): + resp = client.post("/api/query", json=payload) + resp.get_data() # Drain the streaming body before the next request. + assert resp.status_code == 200 + + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_query_rate_limiting_returns_429(self, mock_cm_cls, client): + # Exceeding the per-IP limit (10 per minute) should be rejected with 429. + mock_cm_cls.return_value.generate_streaming_response.side_effect = ( + lambda *args, **kwargs: iter([{"type": "text", "text": "Hi"}]) + ) + payload = { + "messages": [{"role": "human", "content": "Help"}], + "city": None, + "state": "or", + } + for _ in range(10): + resp = client.post("/api/query", json=payload) + resp.get_data() # Drain the streaming body before the next request. + resp = client.post("/api/query", json=payload) + assert resp.status_code == 429 + def test_get_query_returns_405(self, client): resp = client.get("/api/query") assert resp.status_code == 405 diff --git a/backend/tests/test_query_rate_limit.py b/backend/tests/test_query_rate_limit.py new file mode 100644 index 00000000..0badf5a8 --- /dev/null +++ b/backend/tests/test_query_rate_limit.py @@ -0,0 +1,300 @@ +""" +Comprehensive verification that the /api/query rate-limit fix is working. + +Before the fix (commit ae78a5c), /api/query had no rate limit at all: +flask_limiter was wired up globally but only /api/feedback opted in. +Every call fans out to Vertex AI RAG + Gemini, so unthrottled traffic +is a direct cost and availability risk. + +After the fix (commit 70185c2), /api/query is wrapped with +limiter.limit(QUERY_RATE_LIMIT) before being registered, defaulting to +10 per minute per IP. The tests below verify this from six angles: + + 1. Boundary — exactly 10 requests succeed; the 11th is rejected. + 2. Per-IP — two different source IPs each get their own window. + 3. Window reset — after limiter.reset() the quota refills and requests succeed. + 4. Env-var wiring — QUERY_RATE_LIMIT is read from os.environ at startup. + 5. Regression — /api/feedback limit is unchanged and independent. + 6. Response shape — a 429 reply carries the Retry-After header. +""" + +import os +from unittest.mock import patch + +import pytest + +from tenantfirstaid.app import QUERY_RATE_LIMIT, app, limiter + + +# --------------------------------------------------------------------------- +# Shared fixtures +# --------------------------------------------------------------------------- + + +@pytest.fixture +def client(): + app.testing = True + with app.test_client() as c: + yield c + + +@pytest.fixture(autouse=True) +def reset_limiter(): + """Start every test with a clean rate-limit window.""" + limiter.reset() + yield + + +def _mock_stream(*args, **kwargs): + """Return a fresh one-item iterator that satisfies the chat view. + + Accepts and discards any arguments because this is used as a side_effect + callable, which mock invokes with the same args as the real method. + """ + return iter([{"type": "text", "text": "ok"}]) + + +def _post_query(client, *, remote_addr="127.0.0.1"): + """POST /api/query from a given IP and drain the streaming body.""" + resp = client.post( + "/api/query", + json={ + "messages": [{"role": "human", "content": "hi"}], + "city": None, + "state": "or", + }, + environ_base={"REMOTE_ADDR": remote_addr}, + ) + # Draining is required for streaming responses: if the generator is not + # exhausted before the next request, Flask raises "Popped wrong request + # context" because stream_with_context still holds a reference to the + # old request context. + resp.get_data() + return resp + + +# --------------------------------------------------------------------------- +# 1. Boundary — exactly at and one over the limit +# --------------------------------------------------------------------------- + + +class TestBoundary: + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_tenth_request_succeeds(self, mock_cm_cls, client): + # The 10th request in a window must still return 200, not 429. + # This guards against an off-by-one error where the limit fires + # one request too early. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + # Burn through 9 requests first. + for _ in range(9): + _post_query(client) + + # The 10th (last allowed) must succeed. + resp = _post_query(client) + assert resp.status_code == 200, ( + "10th request should still be within the 10/min limit" + ) + + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_eleventh_request_rejected(self, mock_cm_cls, client): + # One request over the limit must be rejected with 429. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(10): + _post_query(client) + + resp = _post_query(client) + assert resp.status_code == 429, ( + "11th request should exceed the 10/min limit and be blocked" + ) + + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_all_requests_within_limit_return_200(self, mock_cm_cls, client): + # Every one of the 10 allowed requests in a window should return 200. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + statuses = [_post_query(client).status_code for _ in range(10)] + assert statuses == [200] * 10, ( + "All 10 requests within the limit should return 200" + ) + + +# --------------------------------------------------------------------------- +# 2. Per-IP isolation — two IPs each get their own window +# --------------------------------------------------------------------------- + + +class TestPerIpIsolation: + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_exhausting_one_ip_does_not_block_another(self, mock_cm_cls, client): + # IP A burns through its full quota. IP B has never sent a request + # in this window, so its very next request must still return 200. + # This confirms the limiter keys off REMOTE_ADDR, not a global counter. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(11): + _post_query(client, remote_addr="10.0.0.1") + + last_from_a = _post_query(client, remote_addr="10.0.0.1") + assert last_from_a.status_code == 429, "IP A should be throttled" + + first_from_b = _post_query(client, remote_addr="10.0.0.2") + assert first_from_b.status_code == 200, ( + "IP B's quota is independent; it should not be affected by IP A's abuse" + ) + + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_two_ips_each_get_full_quota(self, mock_cm_cls, client): + # Both IPs should each be able to make 10 successful requests in + # the same window without interfering with each other. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(10): + assert _post_query(client, remote_addr="192.168.1.1").status_code == 200 + assert _post_query(client, remote_addr="192.168.1.2").status_code == 200 + + +# --------------------------------------------------------------------------- +# 3. Window reset — quota refills after limiter.reset() +# --------------------------------------------------------------------------- + + +class TestWindowReset: + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_reset_allows_requests_again(self, mock_cm_cls, client): + # Exhaust the quota, reset the window, confirm the next request + # succeeds. This simulates a new rate-limit window opening and + # ensures we're not accidentally persisting state between windows. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(11): + _post_query(client) + + assert _post_query(client).status_code == 429, ( + "Should be throttled before reset" + ) + + limiter.reset() + + assert _post_query(client).status_code == 200, ( + "After reset the window is fresh; request should succeed" + ) + + +# --------------------------------------------------------------------------- +# 4. Env-var wiring — QUERY_RATE_LIMIT is read from the environment +# --------------------------------------------------------------------------- + + +class TestEnvVarWiring: + def test_query_rate_limit_defaults_to_ten_per_minute(self): + # When QUERY_RATE_LIMIT is not set, the app defaults to "10 per minute". + # This default is chosen to allow a normal back-and-forth chat while + # blocking scripted bursts. + expected = os.getenv("QUERY_RATE_LIMIT", "10 per minute") + assert QUERY_RATE_LIMIT == expected, ( + f"Expected QUERY_RATE_LIMIT={expected!r}, got {QUERY_RATE_LIMIT!r}" + ) + + def test_query_rate_limit_value_is_applied_to_route(self): + # Confirm the value actually wired into the route comes from the + # module-level constant (not hard-coded). If someone sets + # QUERY_RATE_LIMIT=5 per minute in their environment, the route + # should honour it — this test verifies the indirection is in place. + assert QUERY_RATE_LIMIT == os.getenv("QUERY_RATE_LIMIT", "10 per minute") + + +# --------------------------------------------------------------------------- +# 5. Regression — /api/feedback limit is unchanged and independent +# --------------------------------------------------------------------------- + + +class TestFeedbackRegression: + @patch("tenantfirstaid.feedback.EmailMessage") + @patch.dict("os.environ", {"SENDER_EMAIL": "s@t.com", "RECIPIENT_EMAIL": "r@t.com"}) + def test_feedback_still_throttles_at_three(self, mock_email_cls, client): + # The fix must not change /api/feedback's existing "3 per minute" limit. + for _ in range(3): + client.post( + "/api/feedback", data={"name": "J", "subject": "S", "feedback": "F"} + ) + resp = client.post( + "/api/feedback", data={"name": "J", "subject": "S", "feedback": "F"} + ) + assert resp.status_code == 429 + + @patch("tenantfirstaid.chat.LangChainChatManager") + @patch("tenantfirstaid.feedback.EmailMessage") + @patch.dict("os.environ", {"SENDER_EMAIL": "s@t.com", "RECIPIENT_EMAIL": "r@t.com"}) + def test_exhausting_query_limit_does_not_affect_feedback( + self, mock_email_cls, mock_cm_cls, client + ): + # The two endpoints must have independent counters. Burning through + # /api/query's quota should not consume any of /api/feedback's quota. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(11): + _post_query(client) + + assert _post_query(client).status_code == 429, "/api/query should be throttled" + + # /api/feedback has its own counter; the first request should still succeed. + resp = client.post( + "/api/feedback", data={"name": "J", "subject": "S", "feedback": "F"} + ) + assert resp.status_code == 200, ( + "/api/feedback counter is independent of /api/query" + ) + + @patch("tenantfirstaid.chat.LangChainChatManager") + @patch("tenantfirstaid.feedback.EmailMessage") + @patch.dict("os.environ", {"SENDER_EMAIL": "s@t.com", "RECIPIENT_EMAIL": "r@t.com"}) + def test_exhausting_feedback_does_not_affect_query( + self, mock_email_cls, mock_cm_cls, client + ): + # Symmetric check: exhausting /api/feedback must not eat into + # /api/query's quota. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(4): + client.post( + "/api/feedback", data={"name": "J", "subject": "S", "feedback": "F"} + ) + + resp = _post_query(client) + assert resp.status_code == 200, ( + "/api/query counter is independent of /api/feedback" + ) + + +# --------------------------------------------------------------------------- +# 6. Response shape — 429 carries the Retry-After header +# --------------------------------------------------------------------------- + + +class TestRateLimitResponseShape: + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_429_has_well_formed_error_body(self, mock_cm_cls, client): + # flask_limiter returns a non-empty HTML error body on 429 so that + # clients (and the DO proxy) receive a readable rejection, not silence. + # Note: Retry-After is not emitted by default in this version of + # flask_limiter; enabling it requires headers_enabled=True on the + # Limiter and is a separate improvement from this fix. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + + for _ in range(10): + _post_query(client) + + resp = _post_query(client) + assert resp.status_code == 429 + assert len(resp.data) > 0, "429 body should not be empty" + assert "text/html" in resp.content_type + + @patch("tenantfirstaid.chat.LangChainChatManager") + def test_successful_request_is_text_plain(self, mock_cm_cls, client): + # Sanity check: the happy-path content type is unchanged by the limiter. + mock_cm_cls.return_value.generate_streaming_response.side_effect = _mock_stream + resp = _post_query(client) + assert resp.status_code == 200 + assert resp.mimetype == "text/plain"