The personal finance app for everyone (by everyone)
Ruby
Stop streaming the Enable Banking panel to the family (#4044)
jaysbeekay94e71a8c2380
diff --git a/app/controllers/enable_banking_items_controller.rb b/app/controllers/enable_banking_items_controller.rbindex d4bcb48c92..277ae7e991 100644--- a/app/controllers/enable_banking_items_controller.rb+++ b/app/controllers/enable_banking_items_controller.rb@@ -542,13 +542,21 @@ class EnableBankingItemsController < ApplicationControllerenddef enable_banking_item_params- params.require(:enable_banking_item).permit(+ permitted = params.require(:enable_banking_item).permit(:name,:sync_start_date,:country_code,:application_id,:client_certificate)+ # The panel never pre-fills the stored credentials, so a blank one on+ # update means "keep the current value".+ if @enable_banking_item&.persisted?+ %i[application_id client_certificate].each do |credential|+ permitted.delete(credential) if permitted[credential].blank?+ end+ end+ permittedenddef enable_banking_callback_urldiff --git a/app/models/enable_banking_item/sync_complete_event.rb b/app/models/enable_banking_item/sync_complete_event.rbindex 586241e9e5..9cb14f2fc6 100644--- a/app/models/enable_banking_item/sync_complete_event.rb+++ b/app/models/enable_banking_item/sync_complete_event.rb@@ -24,14 +24,9 @@ class EnableBankingItem::SyncCompleteEventlocals: { enable_banking_item: enable_banking_item })- # Update the Settings > Providers panel- enable_banking_items = family.enable_banking_items.ordered.includes(:syncs)- enable_banking_item.broadcast_replace_to(- family,- target: "enable_banking-providers-panel",- partial: "settings/providers/enable_banking_panel",- locals: { enable_banking_items: enable_banking_items, family: family }- )+ # The Settings > Providers panel is not streamed here: it is admin-only and+ # holds the connection's credentials, and every family member subscribes to+ # this stream. The sync toast below makes each browser re-fetch its own page.# Let family handle sync notificationsfamily.broadcast_sync_completediff --git a/app/views/settings/providers/_enable_banking_panel.html.erb b/app/views/settings/providers/_enable_banking_panel.html.erbindex 152bb102c5..f6ec9d938a 100644--- a/app/views/settings/providers/_enable_banking_panel.html.erb+++ b/app/views/settings/providers/_enable_banking_panel.html.erb@@ -16,12 +16,13 @@<% end %><%- # Use local family variable if available (e.g., from Sidekiq broadcast), otherwise fall back to Current.family (HTTP requests)+ # Use the local family variable if one is passed, otherwise fall back to Current.familyfamily = local_assigns[:family] || Current.familyenable_banking_item = family.enable_banking_items.first_or_initialize(name: "Enable Banking Connection")is_new_record = enable_banking_item.new_record?# Check if there are any authenticated connections (have session_id)has_authenticated_connections = family.enable_banking_items.where.not(session_id: nil).exists?+ credentials_locked = has_authenticated_connections && !is_new_record%><%= styled_form_with model: enable_banking_item,@@ -68,7 +69,7 @@{ label: true, include_blank: t("settings.providers.enable_banking_panel.select_country") },{ label: t("settings.providers.enable_banking_panel.country_label"), class: "form-field__input" } %>- <% if has_authenticated_connections && !is_new_record %>+ <% if credentials_locked %><%= render DS::Alert.new(variant: :warning,title: t("settings.providers.enable_banking_panel.config_locked_title"),@@ -79,15 +80,17 @@<%= form.text_field :application_id,label: t("settings.providers.enable_banking_panel.application_id_label"),placeholder: is_new_record ? t("settings.providers.enable_banking_panel.application_id_placeholder_new") : t("settings.providers.enable_banking_panel.application_id_placeholder_update"),- value: enable_banking_item.application_id,- disabled: has_authenticated_connections && !is_new_record %>+ value: nil,+ disabled: credentials_locked %><%= form.text_area :client_certificate,label: t("settings.providers.enable_banking_panel.client_certificate_label"),placeholder: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",+ value: "",+ help_text: (t("settings.providers.enable_banking_panel.keep_client_certificate_hint") unless is_new_record || credentials_locked),rows: 6,class: "form-field__input font-mono text-xs",- disabled: has_authenticated_connections && !is_new_record %>+ disabled: credentials_locked %><div class="flex justify-end"><%= form.submit is_new_record ? t("settings.providers.enable_banking_panel.save_and_connect") : t("settings.providers.enable_banking_panel.update_connection") %>diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rbindex f173226b8c..478bfc39ca 100644--- a/config/initializers/filter_parameter_logging.rb+++ b/config/initializers/filter_parameter_logging.rb@@ -5,7 +5,7 @@# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.Rails.application.config.filter_parameters += [:passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :openai_access_token,- :client_id, :consumer_key, :snaptrade_user_id, :snaptrade_user_secret,+ :client_id, :application_id, :consumer_key, :snaptrade_user_id, :snaptrade_user_secret,:oauth_access_token, :oauth_refresh_token, :code_verifier, :code_challenge,/\Apin\z/i,# One-time codes that act as credentials: MFA TOTP/backup codes, OAuth anddiff --git a/config/locales/views/settings/ca.yml b/config/locales/views/settings/ca.ymlindex e8aa975d7d..993297974d 100644--- a/config/locales/views/settings/ca.yml+++ b/config/locales/views/settings/ca.yml@@ -331,6 +331,7 @@ ca:application_id_placeholder_update: Introdueix el nou ID per actualitzarcallback_url_instruction: Per a l'URL de retorn de trucada, fes servir %{callback_url}.client_certificate_label: Certificat de client (amb clau privada)+ keep_client_certificate_hint: "Deixa-ho en blanc per mantenir el certificat actual"config_locked_message: Desconnecta tots els bancs vinculats abans de canviaraquestes credencials.config_locked_title: Configuració bloquejadadiff --git a/config/locales/views/settings/de.yml b/config/locales/views/settings/de.ymlindex 5a9804c234..864f1e1c23 100644--- a/config/locales/views/settings/de.yml+++ b/config/locales/views/settings/de.yml@@ -254,6 +254,7 @@ de:application_id_placeholder_new: Application ID eintragenapplication_id_placeholder_update: Neue ID eintragen, um sie zu ändernclient_certificate_label: Client-Zertifikat (mit Private Key)+ keep_client_certificate_hint: "Leer lassen, um das aktuelle Zertifikat beizubehalten"config_locked_message: Trenn alle verknüpften Banken, bevor du diese Zugangsdaten änderst.config_locked_title: Konfiguration gesperrtconfigured: Eingerichtetdiff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex e7d392fcde..685adee275 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -601,6 +601,7 @@ en:application_id_placeholder_new: "Enter application ID"application_id_placeholder_update: "Enter new ID to update"client_certificate_label: "Client Certificate (with Private Key)"+ keep_client_certificate_hint: "Leave blank to keep the current certificate"save_and_connect: "Save and connect"update_connection: "Update connection"connected_bank: "Connected Bank"diff --git a/config/locales/views/settings/es.yml b/config/locales/views/settings/es.ymlindex c9ae713d3f..e889c2278b 100644--- a/config/locales/views/settings/es.yml+++ b/config/locales/views/settings/es.yml@@ -455,6 +455,7 @@ es:application_id_placeholder_new: Introduce el Application IDapplication_id_placeholder_update: Introduce un nuevo ID para actualizarloclient_certificate_label: Certificado de cliente (con clave privada)+ keep_client_certificate_hint: "Déjalo en blanco para conservar el certificado actual"save_and_connect: Guardar y conectarupdate_connection: Actualizar conexiónconnected_bank: Banco conectadodiff --git a/config/locales/views/settings/fr.yml b/config/locales/views/settings/fr.ymlindex 0d2e578c89..b5eca021dd 100644--- a/config/locales/views/settings/fr.yml+++ b/config/locales/views/settings/fr.yml@@ -344,6 +344,7 @@ fr:application_id_placeholder_update: Saisissez le nouvel identifiant pour mettre à jourcallback_url_instruction: Pour l'URL de rappel, utilisez %{callback_url}.client_certificate_label: Certificat client (avec clé privée)+ keep_client_certificate_hint: "Laissez vide pour conserver le certificat actuel"config_locked_message: Déconnectez toutes les banques liées avant de modifier ces identifiants.config_locked_title: Configuration verrouilléeconfigured: Configurédiff --git a/config/locales/views/settings/hu.yml b/config/locales/views/settings/hu.ymlindex a19e9416be..72424b9cf9 100644--- a/config/locales/views/settings/hu.yml+++ b/config/locales/views/settings/hu.yml@@ -372,6 +372,7 @@ hu:application_id_placeholder_new: "Add meg az alkalmazásazonosítót"application_id_placeholder_update: "Add meg az új azonosítót a frissítéshez"client_certificate_label: "Ügyfél-tanúsítvány (privát kulccsal)"+ keep_client_certificate_hint: "Hagyd üresen az aktuális tanúsítvány megtartásához"save_and_connect: "Mentés és csatlakozás"update_connection: "Kapcsolat frissítése"connected_bank: "Csatlakoztatott bank"diff --git a/config/locales/views/settings/it.yml b/config/locales/views/settings/it.ymlindex c055f4a8af..305a3bcf27 100644--- a/config/locales/views/settings/it.yml+++ b/config/locales/views/settings/it.yml@@ -449,6 +449,7 @@ it:application_id_placeholder_new: "Inserisci ID applicazione"application_id_placeholder_update: "Inserisci nuovo ID per aggiornare"client_certificate_label: "Certificato client (con chiave privata)"+ keep_client_certificate_hint: "Lascia vuoto per mantenere il certificato attuale"save_and_connect: "Salva e connetti"update_connection: "Aggiorna connessione"connected_bank: "Banca collegata"diff --git a/config/locales/views/settings/pl.yml b/config/locales/views/settings/pl.ymlindex 630b395e06..363cab1186 100644--- a/config/locales/views/settings/pl.yml+++ b/config/locales/views/settings/pl.yml@@ -435,6 +435,7 @@ pl:application_id_placeholder_update: Wpisz nowe ID, aby zaktualizowaćcallback_url_instruction: Dla URL callback użyj %{callback_url}.client_certificate_label: Certyfikat klienta (z kluczem prywatnym)+ keep_client_certificate_hint: "Pozostaw puste, aby zachować bieżący certyfikat"config_locked_message: Rozłącz wszystkie połączone banki, zanim zmienisz tedane uwierzytelniające.config_locked_title: Konfiguracja zablokowanadiff --git a/config/locales/views/settings/pt-BR.yml b/config/locales/views/settings/pt-BR.ymlindex 7b5bd4f357..f4b4603190 100644--- a/config/locales/views/settings/pt-BR.yml+++ b/config/locales/views/settings/pt-BR.yml@@ -193,3 +193,4 @@ pt-BR:enable_banking_panel:callback_url_instruction: "Para a URL de retorno de chamada, use %{callback_url}."connection_error: Erro de conexão+ keep_client_certificate_hint: "Deixe em branco para manter o certificado atual"diff --git a/config/locales/views/settings/pt-PT.yml b/config/locales/views/settings/pt-PT.ymlindex c395a79243..d8856417b5 100644--- a/config/locales/views/settings/pt-PT.yml+++ b/config/locales/views/settings/pt-PT.yml@@ -536,6 +536,7 @@ pt-PT:application_id_placeholder_new: "Introduza o application ID"application_id_placeholder_update: "Introduza o novo ID para atualizar"client_certificate_label: "Client Certificate (com Chave Privada)"+ keep_client_certificate_hint: "Deixe em branco para manter o certificado atual"save_and_connect: "Guardar e ligar"update_connection: "Atualizar ligação"connected_bank: "Banco Ligado"diff --git a/config/locales/views/settings/ru.yml b/config/locales/views/settings/ru.ymlindex 203f29b776..0102c9d896 100644--- a/config/locales/views/settings/ru.yml+++ b/config/locales/views/settings/ru.yml@@ -369,6 +369,7 @@ ru:application_id_placeholder_update: Введите новый ID для обновленияcallback_url_instruction: Для URL обратного вызова используйте %{callback_url}.client_certificate_label: Клиентский сертификат (с приватным ключом)+ keep_client_certificate_hint: "Оставьте пустым, чтобы сохранить текущий сертификат"config_locked_message: Отключите все связанные банки перед изменением этихучётных данных.config_locked_title: Конфигурация заблокированаdiff --git a/config/locales/views/settings/tr.yml b/config/locales/views/settings/tr.ymlindex f983094ab3..9861284916 100644--- a/config/locales/views/settings/tr.yml+++ b/config/locales/views/settings/tr.yml@@ -377,6 +377,7 @@ tr:application_id_placeholder_update: Güncellemek için yeni ID girincallback_url_instruction: Geri çağırma URL'si için %{callback_url} kullanın.client_certificate_label: İstemci Sertifikası (Özel Anahtar ile)+ keep_client_certificate_hint: "Mevcut sertifikayı korumak için boş bırakın"config_locked_message: Bu kimlik bilgilerini değiştirmeden önce bağlı tümbankaların bağlantısını kesin.config_locked_title: Yapılandırma kilitlidiff --git a/config/locales/views/settings/uk.yml b/config/locales/views/settings/uk.ymlindex 72683e0166..054b6c40c1 100644--- a/config/locales/views/settings/uk.yml+++ b/config/locales/views/settings/uk.yml@@ -548,6 +548,7 @@ uk:application_id_placeholder_new: "Введіть ідентифікатор програми"application_id_placeholder_update: "Введіть новий ID для оновлення"client_certificate_label: "Сертифікат клієнта (з приватним ключем)"+ keep_client_certificate_hint: "Залиште порожнім, щоб зберегти поточний сертифікат"save_and_connect: "Зберегти та підключити"update_connection: "Оновити з'єднання"connected_bank: "Підключений банк"diff --git a/config/locales/views/settings/vi.yml b/config/locales/views/settings/vi.ymlindex 5db13c2a0d..145b239a0c 100644--- a/config/locales/views/settings/vi.yml+++ b/config/locales/views/settings/vi.yml@@ -416,6 +416,7 @@ vi:application_id_placeholder_new: "Nhập ID ứng dụng"application_id_placeholder_update: "Nhập ID mới để cập nhật"client_certificate_label: "Chứng chỉ khách hàng (kèm khóa riêng)"+ keep_client_certificate_hint: "Để trống để giữ chứng chỉ hiện tại"save_and_connect: "Lưu và kết nối"update_connection: "Cập nhật kết nối"connected_bank: "Ngân hàng đã kết nối"diff --git a/config/locales/views/settings/zh-CN.yml b/config/locales/views/settings/zh-CN.ymlindex 453f4605c3..75f40780de 100644--- a/config/locales/views/settings/zh-CN.yml+++ b/config/locales/views/settings/zh-CN.yml@@ -417,6 +417,7 @@ zh-CN:application_id_placeholder_new: 输入 application IDapplication_id_placeholder_update: 输入新 ID 以更新client_certificate_label: Client Certificate(包含 Private Key)+ keep_client_certificate_hint: "留空以保留当前证书"save_and_connect: 保存并连接update_connection: 更新连接connected_bank: 已连接的银行diff --git a/config/locales/views/settings/zh-TW.yml b/config/locales/views/settings/zh-TW.ymlindex 261eb363fc..7599df4cff 100644--- a/config/locales/views/settings/zh-TW.yml+++ b/config/locales/views/settings/zh-TW.yml@@ -501,6 +501,7 @@ zh-TW:application_id_placeholder_new: 輸入 Application IDapplication_id_placeholder_update: 輸入新的 ID 以更新client_certificate_label: Client Certificate(含私密金鑰)+ keep_client_certificate_hint: "留空以保留目前的憑證"save_and_connect: 儲存並連線update_connection: 更新連線connected_bank: 已連結的銀行diff --git a/test/controllers/enable_banking_items_controller_test.rb b/test/controllers/enable_banking_items_controller_test.rbindex b57cb29e2f..d6d8d19740 100644--- a/test/controllers/enable_banking_items_controller_test.rb+++ b/test/controllers/enable_banking_items_controller_test.rb@@ -87,6 +87,121 @@ class EnableBankingItemsControllerTest < ActionDispatch::IntegrationTestassert_equal "Renamed Connection", @item.reload.nameend+ # The panel never sends a stored certificate back to the browser, so a save+ # that leaves the field blank must keep the one already stored.+ test "an update with a blank certificate keeps the stored one" do+ stored = @item.client_certificate++ patch enable_banking_item_url(@item),+ params: { enable_banking_item: { name: "Renamed Connection", client_certificate: "" } },+ as: :turbo_stream++ assert_turbo_stream action: "replace", target: "enable_banking-providers-panel"+ @item.reload+ assert_equal "Renamed Connection", @item.name+ assert_equal stored, @item.client_certificate+ end++ test "an update with a new certificate replaces the stored one" do+ replacement = OpenSSL::PKey::RSA.new(2048).to_pem++ patch enable_banking_item_url(@item),+ params: { enable_banking_item: { client_certificate: replacement } },+ as: :turbo_stream++ assert_equal replacement, @item.reload.client_certificate+ end++ test "a create still requires a certificate" do+ @item.destroy!++ assert_no_difference "EnableBankingItem.count" do+ post enable_banking_items_url,+ params: { enable_banking_item: { country_code: "DE", application_id: "app", client_certificate: "" } },+ as: :turbo_stream+ end++ assert_response :unprocessable_entity+ end++ test "the panel does not send the stored certificate back to the browser" do+ get connect_form_settings_providers_url(provider_key: "enable_banking")++ assert_response :success+ assert_not_includes response.body, @item.client_certificate.lines.second.strip+ assert_select "textarea[name='enable_banking_item[client_certificate]']" do |fields|+ assert_equal "", fields.first.text.strip+ end+ assert_includes response.body, I18n.t("settings.providers.enable_banking_panel.keep_client_certificate_hint")+ end++ # An authenticated connection locks the credential fields, and a disabled+ # field is never submitted, so "leave blank to keep" does not apply there.+ test "the panel hides the keep-certificate hint while the credentials are locked" do+ @item.update!(session_id: "authenticated-session")++ get connect_form_settings_providers_url(provider_key: "enable_banking")++ assert_response :success+ assert_select "textarea[name='enable_banking_item[client_certificate]'][disabled]"+ assert_not_includes response.body, I18n.t("settings.providers.enable_banking_panel.keep_client_certificate_hint")+ end++ # The application id is the key id of every signed request, so the panel+ # treats it like the certificate: never echoed back, blank keeps it.+ test "the panel does not send the stored application id back to the browser" do+ @item.update_columns(application_id: "stored-app-id-#{SecureRandom.hex(8)}")++ get connect_form_settings_providers_url(provider_key: "enable_banking")++ assert_response :success+ assert_not_includes response.body, @item.application_id+ assert_select "input[name='enable_banking_item[application_id]']" do |fields|+ assert_nil fields.first["value"]+ end+ end++ test "the application id is filtered from logs" do+ parameter_filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)+ filtered_params = parameter_filter.filter(+ enable_banking_item: { application_id: "stored-app-id", country_code: "DE" }+ )++ assert_equal "[FILTERED]", filtered_params.dig(:enable_banking_item, :application_id)+ assert_equal "DE", filtered_params.dig(:enable_banking_item, :country_code)+ end++ test "an update with a blank application id keeps the stored one" do+ patch enable_banking_item_url(@item),+ params: { enable_banking_item: { name: "Renamed Connection", application_id: "" } },+ as: :turbo_stream++ assert_turbo_stream action: "replace", target: "enable_banking-providers-panel"+ @item.reload+ assert_equal "Renamed Connection", @item.name+ assert_equal "test_app_id", @item.application_id+ end++ test "an update with a new application id replaces the stored one" do+ patch enable_banking_item_url(@item),+ params: { enable_banking_item: { application_id: "replacement_app_id" } },+ as: :turbo_stream++ assert_equal "replacement_app_id", @item.reload.application_id+ end++ test "a create still requires an application id" do+ @item.destroy!++ assert_no_difference "EnableBankingItem.count" do+ post enable_banking_items_url,+ params: { enable_banking_item: { country_code: "DE", application_id: "", client_certificate: OpenSSL::PKey::RSA.new(2048).to_pem } },+ as: :turbo_stream+ end++ assert_response :unprocessable_entity+ end+test "invalid create from the page shows the error in the panel" dopost enable_banking_items_url,params: { enable_banking_item: { country_code: "", application_id: "" } },diff --git a/test/controllers/settings/providers_controller_test.rb b/test/controllers/settings/providers_controller_test.rbindex ef7095f24e..f458ed9eab 100644--- a/test/controllers/settings/providers_controller_test.rb+++ b/test/controllers/settings/providers_controller_test.rb@@ -218,8 +218,7 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestend# Saves and errors from a row or the drawer re-render the panel by replacing- # "<turbo_id>-providers-panel", and EnableBankingItem::SyncCompleteEvent does- # the same when a sync finishes. SnapTrade never streams to it.+ # "<turbo_id>-providers-panel". SnapTrade never streams to it.test "every streamed panel renders the root its streams replace" doSettings::ProvidersController::FAMILY_PANELS.reject { |panel| panel[:key] == "snaptrade" }.each do |panel|get connect_form_settings_providers_url(provider_key: panel[:key])diff --git a/test/models/enable_banking_item/sync_complete_event_test.rb b/test/models/enable_banking_item/sync_complete_event_test.rbnew file mode 100644index 0000000000..8ca2b67b17--- /dev/null+++ b/test/models/enable_banking_item/sync_complete_event_test.rb@@ -0,0 +1,42 @@+require "test_helper"+require "openssl"+require "turbo/broadcastable/test_helper"++class EnableBankingItem::SyncCompleteEventTest < ActiveSupport::TestCase+ include Turbo::Broadcastable::TestHelper++ setup do+ @family = families(:dylan_family)+ @item = @family.enable_banking_items.create!(+ name: "Test Connection",+ country_code: "DE",+ application_id: "test_app_id",+ client_certificate: OpenSSL::PKey::RSA.new(2048).to_pem+ )+ # A line of the key's body, which only a render of the stored value contains.+ @certificate_marker = @item.client_certificate.lines.second.strip+ Current.reset+ end++ # Every member of the family subscribes to the family stream, but the+ # Settings > Providers panel is admin-only and its form holds the stored+ # client certificate.+ test "a finished sync does not stream the settings panel to the family" do+ streams = capture_turbo_stream_broadcasts(@family) do+ EnableBankingItem::SyncCompleteEvent.new(@item).broadcast+ end++ assert_empty streams.select { |stream| stream["target"] == "enable_banking-providers-panel" }+ streams.each { |stream| assert_not_includes stream.to_html, @certificate_marker }+ end++ # The toast is what makes each browser re-fetch its own page now that the+ # panel is not streamed.+ test "a finished sync still sends the sync toast" do+ targets = capture_turbo_stream_broadcasts(@family) do+ EnableBankingItem::SyncCompleteEvent.new(@item).broadcast+ end.map { |stream| stream["target"] }++ assert_includes targets, "sync-toast"+ end+end