Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(settings): allow clearing an encrypted provider API key (#2544)

minion1227e5608ca6d9ce
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rb
index f3082fd2b1..f988aca368 100644
--- a/app/controllers/settings/hostings_controller.rb
+++ b/app/controllers/settings/hostings_controller.rb
@@ -151,13 +151,7 @@ class Settings::HostingsController < ApplicationController
sync_auto_sync_scheduler!
end
- if hosting_params.key?(:openai_access_token)
- token_param = hosting_params[:openai_access_token].to_s.strip
- # Ignore blanks and redaction placeholders to prevent accidental overwrite
- unless token_param.blank? || token_param == "********"
- Setting.openai_access_token = token_param
- end
- end
+ update_encrypted_setting(:openai_access_token)
# Validate OpenAI configuration before updating
if hosting_params.key?(:openai_uri_base) || hosting_params.key?(:openai_model)
@@ -179,12 +173,7 @@ class Settings::HostingsController < ApplicationController
Setting.openai_json_mode = hosting_params[:openai_json_mode].presence
end
- if hosting_params.key?(:anthropic_access_token)
- token_param = hosting_params[:anthropic_access_token].to_s.strip
- unless token_param.blank? || token_param == "********"
- Setting.anthropic_access_token = token_param
- end
- end
+ update_encrypted_setting(:anthropic_access_token)
if hosting_params.key?(:anthropic_base_url)
raw_base_url = hosting_params[:anthropic_base_url].to_s.strip
@@ -241,12 +230,7 @@ class Settings::HostingsController < ApplicationController
Setting.external_assistant_url = hosting_params[:external_assistant_url]
end
- if hosting_params.key?(:external_assistant_token)
- token_param = hosting_params[:external_assistant_token].to_s.strip
- unless token_param.blank? || token_param == "********"
- Setting.external_assistant_token = token_param
- end
- end
+ update_encrypted_setting(:external_assistant_token)
if hosting_params.key?(:external_assistant_agent_id)
Setting.external_assistant_agent_id = hosting_params[:external_assistant_agent_id]
@@ -319,7 +303,13 @@ class Settings::HostingsController < ApplicationController
def update_encrypted_setting(param_key)
return unless hosting_params.key?(param_key)
value = hosting_params[param_key].to_s.strip
- Setting.public_send(:"#{param_key}=", value) unless value.blank? || value == "********"
+
+ # "********" is the masked placeholder rendered for an existing key; it
+ # means "leave the stored value untouched". A blank submission, however,
+ # is an explicit request to clear the key, so persist nil in that case.
+ return if value == "********"
+
+ Setting.public_send(:"#{param_key}=", value.presence)
end
def current_user_timezone
diff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rb
index 085139df98..cd328164e0 100644
--- a/test/controllers/settings/hostings_controller_test.rb
+++ b/test/controllers/settings/hostings_controller_test.rb
@@ -27,7 +27,7 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
teardown do
# These tests persist global Setting.* values; reset them so state can't
# leak into later (order-dependent) tests.
- %i[anthropic_access_token anthropic_base_url anthropic_model llm_provider rentcast_api_key realie_api_key].each do |key|
+ %i[anthropic_access_token anthropic_base_url anthropic_model llm_provider twelve_data_api_key openai_access_token external_assistant_token rentcast_api_key realie_api_key].each do |key|
Setting.public_send("#{key}=", nil)
end
end
@@ -150,6 +150,25 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
end
end
+ test "can clear an encrypted api key by submitting a blank value" do
+ with_self_hosting do
+ patch settings_hosting_url, params: { setting: { twelve_data_api_key: "1234567890" } }
+ assert_equal "1234567890", Setting.twelve_data_api_key
+
+ patch settings_hosting_url, params: { setting: { twelve_data_api_key: "" } }
+ assert_nil Setting.twelve_data_api_key
+ end
+ end
+
+ test "submitting the masked placeholder leaves an encrypted api key unchanged" do
+ with_self_hosting do
+ patch settings_hosting_url, params: { setting: { twelve_data_api_key: "1234567890" } }
+
+ patch settings_hosting_url, params: { setting: { twelve_data_api_key: "********" } }
+ assert_equal "1234567890", Setting.twelve_data_api_key
+ end
+ end
+
test "can update onboarding state when self hosting is enabled" do
sign_in users(:sure_support_staff)
@@ -174,6 +193,29 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
end
end
+ # Regression: issue #2465 symptom for the OpenAI token. Blanking the field
+ # (the form auto-submits on blur) must clear the stored value, not silently
+ # keep the old one.
+ test "can clear openai access token by submitting a blank value" do
+ with_self_hosting do
+ Setting.openai_access_token = "previous-token"
+
+ patch settings_hosting_url, params: { setting: { openai_access_token: "" } }
+
+ assert_nil Setting.openai_access_token
+ end
+ end
+
+ test "ignores redacted openai token placeholder" do
+ with_self_hosting do
+ Setting.openai_access_token = "previous-token"
+
+ patch settings_hosting_url, params: { setting: { openai_access_token: "********" } }
+
+ assert_equal "previous-token", Setting.openai_access_token
+ end
+ end
+
test "can update anthropic access token when self hosting is enabled" do
with_self_hosting do
patch settings_hosting_url, params: { setting: { anthropic_access_token: "fake-anthropic-key-for-tests" } }
@@ -182,6 +224,17 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
end
end
+ # Regression: issue #2465 symptom for the Anthropic token.
+ test "can clear anthropic access token by submitting a blank value" do
+ with_self_hosting do
+ Setting.anthropic_access_token = "previous-token"
+
+ patch settings_hosting_url, params: { setting: { anthropic_access_token: "" } }
+
+ assert_nil Setting.anthropic_access_token
+ end
+ end
+
test "ignores redacted anthropic token placeholder" do
with_self_hosting do
Setting.anthropic_access_token = "previous-token"
@@ -445,6 +498,19 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
Setting.external_assistant_token = nil
end
+ # Regression: issue #2465 symptom for the external assistant token.
+ test "can clear external assistant token by submitting a blank value" do
+ with_self_hosting do
+ Setting.external_assistant_token = "real-secret"
+
+ patch settings_hosting_url, params: { setting: { external_assistant_token: "" } }
+
+ assert_nil Setting.external_assistant_token
+ end
+ ensure
+ Setting.external_assistant_token = nil
+ end
+
test "disconnect external assistant clears settings and resets type" do
with_self_hosting do
with_env_overrides("EXTERNAL_ASSISTANT_URL" => nil, "EXTERNAL_ASSISTANT_TOKEN" => nil) do