The personal finance app for everyone (by everyone)
Ruby
fix(api): scope holdings API to accessible accounts (#2706)
pro39588b2d792dab0a
diff --git a/app/controllers/api/v1/holdings_controller.rb b/app/controllers/api/v1/holdings_controller.rbindex 7f08dfe3b1..519c691655 100644--- a/app/controllers/api/v1/holdings_controller.rb+++ b/app/controllers/api/v1/holdings_controller.rb@@ -7,8 +7,7 @@ class Api::V1::HoldingsController < Api::V1::BaseControllerbefore_action :set_holding, only: [ :show ]def index- family = current_resource_owner.family- holdings_query = family.holdings.joins(:account).where(accounts: { status: [ "draft", "active" ] })+ holdings_query = accessible_holdingsholdings_query = apply_filters(holdings_query)holdings_query = holdings_query.includes(:account, :security).chronological@@ -36,12 +35,24 @@ class Api::V1::HoldingsController < Api::V1::BaseControllerprivatedef set_holding- family = current_resource_owner.family- @holding = family.holdings.joins(:account).where(accounts: { status: %w[draft active] }).find(params[:id])+ @holding = accessible_holdings.find(params[:id])rescue ActiveRecord::RecordNotFoundrender json: { error: "not_found", message: "Holding not found" }, status: :not_foundend+ # Holdings restricted to accounts the token owner can access (owned or shared),+ # not the whole family. Mirrors Api::V1::BalancesController and the web+ # HoldingsController, both of which scope through Account.accessible_by.+ def accessible_holdings+ Holding+ .joins(:account)+ .where(accounts: { status: %w[draft active], id: accessible_account_ids })+ end++ def accessible_account_ids+ @accessible_account_ids ||= current_resource_owner.family.accounts.accessible_by(current_resource_owner).select(:id)+ end+def ensure_read_scopeauthorize_scope!(:read)enddiff --git a/test/controllers/api/v1/holdings_controller_test.rb b/test/controllers/api/v1/holdings_controller_test.rbnew file mode 100644index 0000000000..6cf51d966d--- /dev/null+++ b/test/controllers/api/v1/holdings_controller_test.rb@@ -0,0 +1,95 @@+# frozen_string_literal: true++require "test_helper"++class Api::V1::HoldingsControllerTest < ActionDispatch::IntegrationTest+ setup do+ @user = users(:family_admin)+ @family = @user.family+ @user.api_keys.active.destroy_all++ @api_key = ApiKey.create!(+ user: @user,+ name: "Test Read Key",+ scopes: [ "read" ],+ source: "web",+ display_key: "test_read_#{SecureRandom.hex(8)}"+ )++ @account = accounts(:investment)+ @holding = holdings(:one)++ # An account owned by another family member, never shared with @user.+ @other_member = users(:family_member)+ @private_account = @family.accounts.create!(+ name: "Member Brokerage",+ accountable: Investment.new,+ balance: 5000,+ currency: "USD",+ owner: @other_member+ )+ @private_holding = @private_account.holdings.create!(+ security: securities(:msft),+ date: Date.current,+ qty: 5,+ price: 100,+ amount: 500,+ currency: "USD"+ )+ end++ test "lists holdings scoped to accessible accounts" do+ get api_v1_holdings_url, headers: api_headers(@api_key)++ assert_response :success+ holding_ids = JSON.parse(response.body)["holdings"].map { |holding| holding["id"] }+ assert_includes holding_ids, @holding.id+ assert_not_includes holding_ids, @private_holding.id+ end++ test "account_ids filter cannot reach inaccessible accounts" do+ get api_v1_holdings_url,+ params: { account_ids: [ @private_account.id ] },+ headers: api_headers(@api_key)++ assert_response :success+ holding_ids = JSON.parse(response.body)["holdings"].map { |holding| holding["id"] }+ assert_not_includes holding_ids, @private_holding.id+ end++ test "account_id filter cannot reach inaccessible accounts" do+ get api_v1_holdings_url,+ params: { account_id: @private_account.id },+ headers: api_headers(@api_key)++ assert_response :success+ holding_ids = JSON.parse(response.body)["holdings"].map { |holding| holding["id"] }+ assert_not_includes holding_ids, @private_holding.id+ end++ test "shows an accessible holding" do+ get api_v1_holding_url(@holding), headers: api_headers(@api_key)++ assert_response :success+ response_data = JSON.parse(response.body)+ assert_equal @holding.id, response_data["id"]+ end++ test "returns not found for an inaccessible holding" do+ get api_v1_holding_url(@private_holding), headers: api_headers(@api_key)++ assert_response :not_found+ end++ test "requires authentication" do+ get api_v1_holdings_url++ assert_response :unauthorized+ end++ private++ def api_headers(api_key)+ { "X-Api-Key" => api_key.display_key }+ end+end