Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Stop CoinStats, Indexa and Sophtron panels rendering stored credentials (#5315)

jaysbeekay200a4998caa1
diff --git a/app/controllers/coinstats_items_controller.rb b/app/controllers/coinstats_items_controller.rb
index 2dd32e59dd..4b2a5a6c0f 100644
--- a/app/controllers/coinstats_items_controller.rb
+++ b/app/controllers/coinstats_items_controller.rb
@@ -143,11 +143,13 @@ class CoinstatsItemsController < ApplicationController
end
def coinstats_item_params
- params.require(:coinstats_item).permit(
+ permitted = params.require(:coinstats_item).permit(
:name,
:sync_start_date,
:api_key
)
+ permitted.delete(:api_key) if @coinstats_item&.persisted? && permitted[:api_key].blank?
+ permitted
end
def validate_api_key(api_key)
diff --git a/app/controllers/indexa_capital_items_controller.rb b/app/controllers/indexa_capital_items_controller.rb
index ee0f168609..5746e63b94 100644
--- a/app/controllers/indexa_capital_items_controller.rb
+++ b/app/controllers/indexa_capital_items_controller.rb
@@ -241,7 +241,7 @@ class IndexaCapitalItemsController < ApplicationController
end
def indexa_capital_item_params
- params.require(:indexa_capital_item).permit(
+ permitted = params.require(:indexa_capital_item).permit(
:name,
:sync_start_date,
:api_token,
@@ -249,6 +249,21 @@ class IndexaCapitalItemsController < ApplicationController
:document,
:password
)
+ if @indexa_capital_item&.persisted?
+ if permitted[:api_token].blank?
+ permitted.delete(:api_token)
+ # A stored token takes precedence over username/password, so a new
+ # password without a token clears it to make the switch take effect,
+ # but only once username, document and password will all be saved.
+ permitted[:api_token] = nil if permitted[:password].present? && switching_to_password_credentials?(permitted)
+ end
+ permitted.delete(:password) if permitted[:password].blank?
+ end
+ permitted
+ end
+
+ def switching_to_password_credentials?(permitted)
+ %i[username document password].all? { |field| permitted.fetch(field, @indexa_capital_item.public_send(field)).present? }
end
def link_indexa_capital_account(indexa_capital_account, accountable_type)
diff --git a/app/controllers/sophtron_items_controller.rb b/app/controllers/sophtron_items_controller.rb
index c76d6b7e8d..6e9cb91048 100644
--- a/app/controllers/sophtron_items_controller.rb
+++ b/app/controllers/sophtron_items_controller.rb
@@ -1192,7 +1192,12 @@ class SophtronItemsController < ApplicationController
end
def sophtron_params
- params.require(:sophtron_item).permit(:name, :user_id, :access_key, :base_url, :sync_start_date)
+ permitted = params.require(:sophtron_item).permit(:name, :user_id, :access_key, :base_url, :sync_start_date)
+ if @sophtron_item&.persisted?
+ permitted.delete(:user_id) if permitted[:user_id].blank?
+ permitted.delete(:access_key) if permitted[:access_key].blank?
+ end
+ permitted
end
def connection_context_params
diff --git a/app/views/settings/providers/_coinstats_panel.html.erb b/app/views/settings/providers/_coinstats_panel.html.erb
index 4352db196e..9dba339eb2 100644
--- a/app/views/settings/providers/_coinstats_panel.html.erb
+++ b/app/views/settings/providers/_coinstats_panel.html.erb
@@ -28,8 +28,9 @@
class: "space-y-3" do |form| %>
<%= form.text_field :api_key,
label: t("coinstats_items.new.api_key_label"),
- placeholder: t("coinstats_items.new.api_key_placeholder"),
- type: :password %>
+ placeholder: is_new_record ? t("coinstats_items.new.api_key_placeholder") : t("coinstats_items.new.api_key_placeholder_update"),
+ type: :password,
+ value: nil %>
<div class="flex justify-end">
<%= form.submit is_new_record ? t("coinstats_items.new.configure") : t("coinstats_items.new.update_configuration") %>
diff --git a/app/views/settings/providers/_indexa_capital_panel.html.erb b/app/views/settings/providers/_indexa_capital_panel.html.erb
index 44602d1bbc..36b12ee754 100644
--- a/app/views/settings/providers/_indexa_capital_panel.html.erb
+++ b/app/views/settings/providers/_indexa_capital_panel.html.erb
@@ -26,7 +26,8 @@
<%= form.text_field :api_token,
label: t("indexa_capital_items.panel.fields.api_token.label"),
placeholder: is_new_record ? t("indexa_capital_items.panel.fields.api_token.placeholder_new") : t("indexa_capital_items.panel.fields.api_token.placeholder_update"),
- type: :password %>
+ type: :password,
+ value: nil %>
<p class="text-xs text-secondary px-1 -mt-1"><%= t("indexa_capital_items.panel.fields.api_token.description") %></p>
<%= render DS::Disclosure.new(variant: :inline) do |disclosure| %>
@@ -49,7 +50,8 @@
<%= form.text_field :password,
label: t("indexa_capital_items.panel.fields.password.label"),
placeholder: is_new_record ? t("indexa_capital_items.panel.fields.password.placeholder_new") : t("indexa_capital_items.panel.fields.password.placeholder_update"),
- type: :password %>
+ type: :password,
+ value: nil %>
</div>
<% end %>
diff --git a/app/views/settings/providers/_sophtron_panel.html.erb b/app/views/settings/providers/_sophtron_panel.html.erb
index 3a315d8bdf..15906aa951 100644
--- a/app/views/settings/providers/_sophtron_panel.html.erb
+++ b/app/views/settings/providers/_sophtron_panel.html.erb
@@ -27,12 +27,14 @@
<%= form.text_field :user_id,
label: t("sophtron_items.sophtron_panel.fields.user_id.label"),
placeholder: is_new_record ? t("sophtron_items.sophtron_panel.fields.user_id.placeholder_new") : t("sophtron_items.sophtron_panel.fields.user_id.placeholder_edit"),
- type: :password %>
+ type: :password,
+ value: nil %>
<%= form.text_field :access_key,
label: t("sophtron_items.sophtron_panel.fields.access_key.label"),
placeholder: is_new_record ? t("sophtron_items.sophtron_panel.fields.access_key.placeholder_new") : t("sophtron_items.sophtron_panel.fields.access_key.placeholder_edit"),
- type: :password %>
+ type: :password,
+ value: nil %>
<%= form.text_field :base_url,
label: t("sophtron_items.sophtron_panel.fields.base_url.label"),
diff --git a/config/locales/views/coinstats_items/en.yml b/config/locales/views/coinstats_items/en.yml
index 982aa8db36..e3fea30a14 100644
--- a/config/locales/views/coinstats_items/en.yml
+++ b/config/locales/views/coinstats_items/en.yml
@@ -52,6 +52,7 @@ en:
step3_html: After a successful connection, visit the <a href="%{accounts_url}" class="link" data-turbo-frame="_top">Accounts</a> tab to set up your crypto accounts.
api_key_label: API Key
api_key_placeholder: Required
+ api_key_placeholder_update: Enter new API key to update
configure: Configure
update_configuration: Reconfigure
default_name: CoinStats Connection
diff --git a/test/controllers/coinstats_items_controller_test.rb b/test/controllers/coinstats_items_controller_test.rb
index 30c3545b80..d382b4bad6 100644
--- a/test/controllers/coinstats_items_controller_test.rb
+++ b/test/controllers/coinstats_items_controller_test.rb
@@ -76,6 +76,29 @@ class CoinstatsItemsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Renamed CoinStats", @coinstats_item.reload.name
end
+ test "the re-rendered panel leaves the API key field empty instead of echoing the stored key" do
+ patch coinstats_item_url(@coinstats_item), params: { coinstats_item: { name: "Renamed CoinStats" } }, as: :turbo_stream
+
+ assert_select "form[action=?] input[name=?]", coinstats_item_path(@coinstats_item), "coinstats_item[api_key]" do |inputs|
+ assert_nil inputs.first["value"]
+ assert_equal I18n.t("coinstats_items.new.api_key_placeholder_update"), inputs.first["placeholder"]
+ end
+ assert_not_includes response.body, "test_api_key_123"
+ end
+
+ test "update with the API key left blank changes the other fields and keeps the stored key" do
+ Provider::Coinstats.any_instance.expects(:get_blockchains).never
+
+ patch coinstats_item_url(@coinstats_item),
+ params: { coinstats_item: { name: "Renamed CoinStats", api_key: "" } },
+ as: :turbo_stream
+
+ assert_turbo_stream action: "replace", target: "coinstats-providers-panel"
+ @coinstats_item.reload
+ assert_equal "Renamed CoinStats", @coinstats_item.name
+ assert_equal "test_api_key_123", @coinstats_item.api_key
+ end
+
test "invalid create from the page shows the error in the panel" do
post coinstats_items_url,
params: { coinstats_item: { name: "New CoinStats Connection", api_key: "" } },
@@ -128,6 +151,7 @@ class CoinstatsItemsControllerTest < ActionDispatch::IntegrationTest
@coinstats_item.reload
assert_equal "Updated Name", @coinstats_item.name
+ assert_equal "new_valid_api_key", @coinstats_item.api_key
end
test "should not update coinstats item with invalid api key" do
diff --git a/test/controllers/indexa_capital_items_controller_test.rb b/test/controllers/indexa_capital_items_controller_test.rb
index d5228036a9..940f3f944f 100644
--- a/test/controllers/indexa_capital_items_controller_test.rb
+++ b/test/controllers/indexa_capital_items_controller_test.rb
@@ -60,6 +60,110 @@ class IndexaCapitalItemsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Updated Name", @item.reload.name
end
+ test "the re-rendered panel leaves the API token and password fields empty instead of echoing them" do
+ @item.update!(password: "ix-stored-password-7a2d")
+
+ patch indexa_capital_item_url(@item), params: { indexa_capital_item: { name: "Updated Name" } }, as: :turbo_stream
+
+ %w[api_token password].each do |field|
+ assert_select "form[action=?] input[name=?]", indexa_capital_item_path(@item), "indexa_capital_item[#{field}]" do |inputs|
+ assert_nil inputs.first["value"], "#{field} should not be rendered"
+ end
+ end
+ assert_not_includes response.body, "test_api_token_123"
+ assert_not_includes response.body, "ix-stored-password-7a2d"
+ end
+
+ test "update with the API token and password left blank keeps both" do
+ @item.update!(password: "ix-stored-password-7a2d")
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { name: "Updated Name", api_token: "", password: "" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "Updated Name", @item.name
+ assert_equal "test_api_token_123", @item.api_token
+ assert_equal "ix-stored-password-7a2d", @item.password
+ end
+
+ test "update with a new API token replaces only the token" do
+ @item.update!(password: "ix-stored-password-7a2d")
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { api_token: "ix-rotated-token", password: "" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "ix-rotated-token", @item.api_token
+ assert_equal "ix-stored-password-7a2d", @item.password
+ end
+
+ test "update with a new password and a blank API token switches to username and password" do
+ assert_equal "test_api_token_123", @item.api_token
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { api_token: "", username: "[email protected]", document: "87654321B", password: "ix-new-password" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_nil @item.api_token
+ assert_equal "ix-new-password", @item.password
+ assert_equal "[email protected]", @item.username
+ end
+
+ test "update with a new password but no username or document keeps the API token" do
+ [
+ { username: "", document: "" },
+ { username: "[email protected]", document: "" }
+ ].each do |partial|
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { api_token: "", password: "ix-new-password" }.merge(partial) },
+ as: :turbo_stream
+
+ assert_response :success
+ @item.reload
+ assert_equal "test_api_token_123", @item.api_token, "token cleared for #{partial.inspect}"
+ assert @item.credentials_configured?
+ end
+ end
+
+ test "update with a new password keeps the API token when the stored username is cleared" do
+ @item.update!(username: "[email protected]", document: "12345678A")
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { api_token: "", username: "", document: "12345678A", password: "ix-new-password" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "test_api_token_123", @item.api_token
+ assert_equal "", @item.username
+ end
+
+ test "update without a new password keeps the API token even with full stored credentials" do
+ @item.update!(username: "[email protected]", document: "12345678A", password: "ix-stored-password-7a2d")
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { name: "Renamed" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "Renamed", @item.name
+ assert_equal "test_api_token_123", @item.api_token
+ end
+
+ test "update with a new password switches to the stored username and document" do
+ @item.update!(username: "[email protected]", document: "12345678A")
+
+ patch indexa_capital_item_url(@item),
+ params: { indexa_capital_item: { api_token: "", password: "ix-new-password" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_nil @item.api_token
+ assert @item.credentials_configured?
+ end
+
test "invalid create from the page shows the error in the panel" do
post indexa_capital_items_url,
params: { indexa_capital_item: { name: "New Connection" } },
diff --git a/test/controllers/settings/providers_controller_test.rb b/test/controllers/settings/providers_controller_test.rb
index 1e619b8b0e..b398bb36b9 100644
--- a/test/controllers/settings/providers_controller_test.rb
+++ b/test/controllers/settings/providers_controller_test.rb
@@ -796,6 +796,31 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTest
assert_match(/Query ID/i, response.body)
end
+ test "GET connect_form does not render stored provider credentials" do
+ coinstats = families(:dylan_family).coinstats_items.create!(name: "CoinStats", api_key: "cs-stored-key-91b0")
+ indexa = indexa_capital_items(:configured_with_token)
+ indexa.update!(password: "ix-stored-password-7a2d")
+
+ {
+ "coinstats" => [ coinstats_item_path(coinstats), { "coinstats_item[api_key]" => "cs-stored-key-91b0" } ],
+ "indexa_capital" => [ indexa_capital_item_path(indexa), {
+ "indexa_capital_item[api_token]" => indexa.api_token,
+ "indexa_capital_item[password]" => "ix-stored-password-7a2d"
+ } ]
+ }.each do |provider_key, (form_action, secrets)|
+ get connect_form_settings_providers_path(provider_key: provider_key)
+
+ assert_response :success
+ secrets.each do |field, stored|
+ assert stored.present?, "#{field} needs a stored value for this test to mean anything"
+ assert_select "form[action=?] input[name=?]", form_action, field do |inputs|
+ assert_nil inputs.first["value"], "#{provider_key} renders #{field}"
+ end
+ assert_not_includes response.body, stored, "#{provider_key} leaks #{field}"
+ end
+ end
+ end
+
test "GET connect_form lists every Interactive Brokers connection" do
families(:dylan_family).ibkr_items.create!(name: "IBKR Ireland", query_id: "Q1", token: "T1")
families(:dylan_family).ibkr_items.create!(name: "IBKR US", query_id: "Q2", token: "T2")
diff --git a/test/controllers/sophtron_items_controller_test.rb b/test/controllers/sophtron_items_controller_test.rb
index 39d5bda476..b93a8bed2f 100644
--- a/test/controllers/sophtron_items_controller_test.rb
+++ b/test/controllers/sophtron_items_controller_test.rb
@@ -245,6 +245,48 @@ class SophtronItemsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Renamed Sophtron", @item.reload.name
end
+ test "the re-rendered panel leaves the user ID and access key fields empty instead of echoing them" do
+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")
+ .to_return(status: 200, body: "")
+
+ patch sophtron_item_url(@item), params: { sophtron_item: { name: "Renamed Sophtron" } }, as: :turbo_stream
+
+ %w[user_id access_key].each do |field|
+ assert_select "form[action=?] input[name=?]", sophtron_item_path(@item), "sophtron_item[#{field}]" do |inputs|
+ assert_nil inputs.first["value"], "#{field} should not be rendered"
+ end
+ end
+ assert_not_includes response.body, "developer-user"
+ assert_not_includes response.body, Base64.strict_encode64("secret-key")
+ end
+
+ test "update with the user ID and access key left blank keeps both" do
+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")
+ .to_return(status: 200, body: "")
+
+ patch sophtron_item_url(@item),
+ params: { sophtron_item: { name: "Renamed Sophtron", user_id: "", access_key: "" } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "Renamed Sophtron", @item.name
+ assert_equal "developer-user", @item.user_id
+ assert_equal Base64.strict_encode64("secret-key"), @item.access_key
+ end
+
+ test "update with a new user ID and access key replaces the stored ones" do
+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")
+ .to_return(status: 200, body: "")
+
+ patch sophtron_item_url(@item),
+ params: { sophtron_item: { user_id: "replacement-user", access_key: Base64.strict_encode64("new-secret-key") } },
+ as: :turbo_stream
+
+ @item.reload
+ assert_equal "replacement-user", @item.user_id
+ assert_equal Base64.strict_encode64("new-secret-key"), @item.access_key
+ end
+
test "connection_status renders MFA challenge when Sophtron asks for security answers" do
@item.update!(user_institution_id: "ui-1", current_job_id: "job-1")
provider = mock