Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep guests from changing rules through MCP (#5208)

jaysbeekay28f80e1aaeb9
diff --git a/app/models/assistant.rb b/app/models/assistant.rb
index b28b331dad..111ac7484d 100644
--- a/app/models/assistant.rb
+++ b/app/models/assistant.rb
@@ -35,14 +35,20 @@ module Assistant
Function::RecordBillPayment
].freeze
- # Guests may view family configuration but not change it, so these tools are
- # left out of a guest's registry (chat and MCP alike).
+ # Guests may view family configuration (tags, categories, rules) but not
+ # change it, so these tools are left out of a guest's registry (chat and MCP
+ # alike). PreviewRule writes nothing, but it issues the token ApplyRule needs,
+ # and the web blocks guests from a rule's confirmation page too.
FAMILY_CONFIG_FUNCTION_CLASSES = [
Function::CreateTag,
Function::UpdateTag,
Function::CreateCategory,
Function::UpdateCategory,
- Function::DeleteCategory
+ Function::DeleteCategory,
+ Function::CreateRule,
+ Function::UpdateRule,
+ Function::ApplyRule,
+ Function::PreviewRule
].freeze
class << self
diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rb
index 71db85ecaa..6df8b0ae1c 100644
--- a/test/controllers/mcp_controller_test.rb
+++ b/test/controllers/mcp_controller_test.rb
@@ -1,6 +1,8 @@
require "test_helper"
class McpControllerTest < ActionDispatch::IntegrationTest
+ include EntriesTestHelper
+
MCP_PROTOCOL_VERSION = "2025-06-18"
setup do
@@ -547,6 +549,21 @@ class McpControllerTest < ActionDispatch::IntegrationTest
end
end
+ test "tools/list omits rule write tools for a guest" do
+ with_guest_mcp_env do
+ post "/mcp", params: jsonrpc_request("tools/list").to_json,
+ headers: mcp_headers(@token)
+
+ assert_response :ok
+ tool_names = JSON.parse(response.body)["result"]["tools"].map { |t| t["name"] }
+
+ assert_includes tool_names, "get_rules"
+ %w[create_rule update_rule apply_rule preview_rule].each do |name|
+ assert_not_includes tool_names, name
+ end
+ end
+ end
+
# -- tools/call --
test "tools/call rejects a preview tool for a user without preview features" do
@@ -564,6 +581,66 @@ class McpControllerTest < ActionDispatch::IntegrationTest
end
end
+ test "tools/call creates a rule for an admin" do
+ with_mcp_env do
+ assert_difference "Rule.count", 1 do
+ post "/mcp", params: jsonrpc_request("tools/call", { name: "create_rule", arguments: rule_arguments }, id: 7).to_json,
+ headers: mcp_headers(@token)
+ end
+
+ assert_response :ok
+ assert_nil JSON.parse(response.body)["error"]
+ end
+ end
+
+ test "tools/call rejects create_rule for a guest" do
+ with_guest_mcp_env do
+ assert_no_difference "Rule.count" do
+ post "/mcp", params: jsonrpc_request("tools/call", { name: "create_rule", arguments: rule_arguments }, id: 7).to_json,
+ headers: mcp_headers(@token)
+ end
+
+ assert_response :ok
+ assert_equal(-32602, JSON.parse(response.body)["error"]["code"])
+ end
+ end
+
+ test "tools/call rejects apply_rule for a guest" do
+ # The rule matches a transaction the guest can see, so the preview lists
+ # it and issues a token, and one in an account the guest cannot reach,
+ # whose category would change if the apply ran.
+ shared = accounts(:credit_card)
+ shared.account_shares.create!(user: family_guest, permission: "read_only")
+ hidden = accounts(:depository)
+ assert_not Account.accessible_by(family_guest).exists?(hidden.id)
+ create_transaction(account: shared, name: "Guest reach coffee")
+ hidden_entry = create_transaction(account: hidden, name: "Guest reach coffee", category: categories(:food_and_drink))
+ target = @user.family.categories.create!(name: "Guest rule target")
+
+ rule = @user.family.rules.create!(
+ resource_type: "transaction",
+ conditions_attributes: [ { condition_type: "transaction_name", operator: "like", value: "Guest reach coffee" } ],
+ actions_attributes: [ { action_type: "set_transaction_category", value: target.id } ]
+ )
+ # Preview tokens are bound to the user who previewed, so the guest needs its
+ # own. It is issued directly because MCP does not offer preview_rule to a
+ # guest.
+ token = Assistant::Function::PreviewRule.new(family_guest).call("rule_id" => rule.id)[:preview][:preview_token]
+ assert token, "a preview of a saved rule should issue a token"
+
+ with_guest_mcp_env do
+ perform_enqueued_jobs(only: RuleJob) do
+ post "/mcp", params: jsonrpc_request("tools/call", { name: "apply_rule", arguments: { rule_id: rule.id, preview_token: token } }, id: 8).to_json,
+ headers: mcp_headers(@token)
+ end
+
+ assert_response :ok
+ assert_equal categories(:food_and_drink).id, hidden_entry.transaction.reload.category_id, "apply_rule recategorised a transaction the guest cannot reach"
+ assert_not rule.reload.active, "apply_rule activated the rule for a guest"
+ assert_equal(-32602, JSON.parse(response.body).dig("error", "code"))
+ end
+ end
+
test "tools/call returns error for unknown tool with request id preserved" do
Usage.expects(:capture).with("mcp_call_served_daily").never
with_mcp_env do
@@ -821,6 +898,17 @@ class McpControllerTest < ActionDispatch::IntegrationTest
with_env_overrides("MCP_API_TOKEN" => @token, "MCP_USER_EMAIL" => @user.email, &block) # pipelock:ignore
end
+ def with_guest_mcp_env(&block)
+ with_env_overrides("MCP_API_TOKEN" => @token, "MCP_USER_EMAIL" => family_guest.email, &block) # pipelock:ignore
+ end
+
+ def rule_arguments
+ {
+ conditions: [ { condition_type: "transaction_name", operator: "like", value: "zxq" } ],
+ actions: [ { action_type: "set_transaction_category", value: categories(:food_and_drink).id } ]
+ }
+ end
+
def with_mcp_cache
original_cache = Rails.cache
Rails.cache = ActiveSupport::Cache::MemoryStore.new
diff --git a/test/models/assistant_test.rb b/test/models/assistant_test.rb
index 973abfbd8e..268c2ba700 100644
--- a/test/models/assistant_test.rb
+++ b/test/models/assistant_test.rb
@@ -29,6 +29,28 @@ class AssistantTest < ActiveSupport::TestCase
assert_includes guest_classes, Assistant::Function::GetCategories
end
+ test "guest registry leaves out the rule write tools" do
+ guest_classes = Assistant.function_classes(family_guest)
+ admin_classes = Assistant.function_classes(users(:family_admin))
+
+ [
+ Assistant::Function::CreateRule,
+ Assistant::Function::UpdateRule,
+ Assistant::Function::ApplyRule,
+ Assistant::Function::PreviewRule
+ ].each do |fn_class|
+ assert_includes admin_classes, fn_class
+ assert_not_includes guest_classes, fn_class
+ end
+ end
+
+ test "guest registry keeps the rule read tools" do
+ guest_classes = Assistant.function_classes(family_guest)
+
+ assert_includes guest_classes, Assistant::Function::GetRules
+ assert_includes guest_classes, Assistant::Function::GetRuleOptions
+ end
+
setup do
@chat = chats(:two)
@message = @chat.messages.create!(