The personal finance app for everyone (by everyone)
Ruby
Reject the MCP_API_TOKEN fallback for an inactive user (#5693)
David Gil92b0ac312314
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rbindex ee5fb5e918..c3d5ec6210 100644--- a/app/controllers/mcp_controller.rb+++ b/app/controllers/mcp_controller.rb@@ -169,8 +169,8 @@ class McpController < ApplicationControlleruser = User.find_by_email_with_legacy_fallback(ENV["MCP_USER_EMAIL"])- unless user- Rails.logger.warn "[MCP] MCP_USER_EMAIL does not match any user — check environment configuration"+ unless user&.active?+ Rails.logger.warn "[MCP] MCP_USER_EMAIL does not match an active user — check environment configuration"return falseenddiff --git a/docs/hosting/mcp.md b/docs/hosting/mcp.mdindex 966d2f96e2..d4e8503cab 100644--- a/docs/hosting/mcp.md+++ b/docs/hosting/mcp.md@@ -52,7 +52,7 @@ openssl rand -base64 32### Choosing the user for static-token auth-The `MCP_USER_EMAIL` must match an existing Sure user's email address. The AI assistant will have access to all financial data for that user's family.+The `MCP_USER_EMAIL` must match the email address of an existing, active Sure user. The AI assistant will have access to all financial data for that user's family.> [!CAUTION]> The AI assistant can call the MCP tools available to the specified user. This includes reading financial data and write-capable tools such as statement import, goal/category/tag changes, transaction updates, and budget updates. Only set this for users you trust with your AI provider.@@ -443,15 +443,16 @@ MCP does not depend on the in-app assistant. To use only your own agent, set the- The OAuth flow completed successfully and the client is sending the issued bearer token- The static token matches `MCP_API_TOKEN`-- If you are using the static-token flow, `MCP_USER_EMAIL` matches an existing Sure user+- If you are using the static-token flow, `MCP_USER_EMAIL` matches an existing, active Sure user### Static token works, but the user still gets rejected**Symptom:** Requests return HTTP 401 even though the bearer token matches `MCP_API_TOKEN`-**Fix:** The `MCP_USER_EMAIL` probably does not match an existing user. Check that:+**Fix:** The `MCP_USER_EMAIL` probably does not match an existing, active user. Check that:- The email is correct- The user exists in the database+- The user is active (deactivated users are rejected)- There are no typos or extra spaces### Pipelock connection refuseddiff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rbindex be0a9809ee..d92b9036fe 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -183,6 +183,22 @@ class McpControllerTest < ActionDispatch::IntegrationTestendend+ test "env-var token is rejected for a deactivated user" do+ inactive_user = users(:family_member)+ inactive_user.update!(active: false)++ with_env_overrides("MCP_API_TOKEN" => @token, "MCP_USER_EMAIL" => inactive_user.email) do # pipelock:ignore+ Rails.logger.expects(:warn).with(regexp_matches(/MCP_USER_EMAIL/)).once++ post "/mcp", params: jsonrpc_request("initialize").to_json,+ headers: mcp_headers(@token)++ assert_response :unauthorized+ end+ ensure+ inactive_user&.update!(active: true)+ end+test "returns 401 and warns when env-var token matches but MCP_USER_EMAIL finds no user" dowith_env_overrides("MCP_API_TOKEN" => @token, "MCP_USER_EMAIL" => "[email protected]") do # pipelock:ignoreRails.logger.expects(:warn).with(regexp_matches(/MCP_USER_EMAIL/)).once