Stop streaming the Enable Banking panel to the family

Merged
JA
2mo ago
+195-17

Summary

After an Enable Banking sync, EnableBankingItem::SyncCompleteEvent broadcast the admin-only Settings › Providers panel on the family Turbo stream, which every family member subscribes to. The panel's form filled in the stored client_certificate, the connection's private key, so every member's browser received it. This PR stops the broadcast. It also makes the panel stop sending the stored credentials (the key and the application ID) back to the browser at all, the same way the Brex panel handles its token.

Closes #4043

Fork pre-flight PR: jaysbeekay/sure#351 (same branch, same commit; its CI and review history are there).

What changed

  • EnableBankingItem::SyncCompleteEvent: the panel broadcast_replace_to is removed. family.broadcast_sync_complete (the sync toast) still runs, and it already makes each browser re-fetch its own page on an authenticated request, so an admin on the settings page still sees the new status.
  • _enable_banking_panel.html.erb: the certificate text_area renders blank (value: ""). Once a certificate is stored, the field's help text reads "Leave blank to keep the current certificate". The new key settings.providers.enable_banking_panel.keep_client_certificate_hint is translated in all 16 settings locales, as Brex's is. It renders only while the field is editable: once the connection is authenticated the fields are disabled and not submitted, so the hint is hidden there. The PEM example placeholder is unchanged.
  • _enable_banking_panel.html.erb, application ID: the text_field renders empty (value: nil), so the stored ID is no longer echoed back either. The existing update placeholder already reads "Enter new ID to update". The lock condition the panel wrote out three times is now one local, credentials_locked.
  • EnableBankingItemsController#enable_banking_item_params: on update, a blank client_certificate is dropped, so saving the form without pasting a new one keeps the stored certificate. This is the same line BrexItemsController uses for its token. The same rule covers application_id: one list of credentials, dropped when blank on a persisted item. Create is unaffected and still requires both (validates :application_id, presence: true and validates :client_certificate, presence: true, on: :create).
  • config/initializers/filter_parameter_logging.rb: :application_id added, so it stays out of request logs.

Deliberately not changed:

  • The account card broadcast in the same event. That one belongs to #3630, which is handled separately.
  • encrypts :application_id. The application ID goes in the clear in the JWT header (kid) of every request Sure signs (Provider::EnableBanking#generate_jwt), so it identifies the key rather than being the secret. Encrypting it at rest would be a separate change with its own backfill. filter_parameters now keeps it out of the logs.

Demo data

No demo data needed: demo families have no Enable Banking connection.

Screenshots

The visible changes are on the credential fields of Settings › Providers › Enable Banking for a saved connection. The certificate field is now empty, showing the usual PEM example placeholder, with the help text "Leave blank to keep the current certificate" under it, instead of the stored PEM. The application ID field is empty too, showing its existing "Enter new ID to update" placeholder. When the connection is authenticated, both fields stay disabled as before and the hint isn't shown. Nothing else in the layout changes.

Migration notes

No migration.

Verification

Tests were written first. On main (a92dec1e6), three of the new tests failed:

  • EnableBankingItem::SyncCompleteEventTest "a finished sync does not stream the settings panel to the family": a stream targeting enable_banking-providers-panel was present.
  • EnableBankingItemsControllerTest "the panel does not send the stored certificate back to the browser": the PEM was in the page.
  • "an update with a blank certificate keeps the stored one": the stored certificate was overwritten.

These pass before and after, as guards should:

  • "a finished sync still refreshes the connection card and the sync toast" (renamed and narrowed in the review round below);
  • "an update with a new certificate replaces the stored one";
  • "a create still requires a certificate".

Breaking the fix on purpose fails a test each time (first round):

BreakageCaught by
Panel broadcast put backdoes not stream the settings panel
value: "" removed from the fielddoes not send the stored certificate back
Help text removeddoes not send the stored certificate back
Keep-on-blank line removedblank certificate keeps the stored one
Sync toast dropped from the eventstill refreshes the card and the toast

Review round (review comment 6059318886)

Four commits answer the four points:

  • 66b0ce155 Stop pre-filling the Enable Banking application ID
  • d9bdd8f3a Assert only the sync toast after an Enable Banking sync
  • 9017e897f Show the keep-certificate hint only on an editable field
  • 745b2a06c Translate the keep-certificate hint into Brazilian Portuguese

New tests, each written first:

  • "the panel does not send the stored application id back to the browser". It failed before the fix: the stored ID was in the page.
  • "an update with a blank application id keeps the stored one". It failed before the fix with a 422: the blank overwrote the stored ID, and presence validation rejected it.
  • "the application id is filtered from logs". It failed without the initializer change, with application_id unfiltered.
  • "the panel hides the keep-certificate hint while the credentials are locked". It failed before the fix: the hint was in the page.
  • Guards that pass before and after: "an update with a new application id replaces the stored one" and "a create still requires an application id".

The sync-toast test is renamed to "a finished sync still sends the sync toast" and no longer expects the enable_banking_item_<id> card target, which #4046 removes on purpose. With the card broadcast deleted, the old test fails and the new one passes.

BreakageCaught by
Application ID pre-filled againdoes not send the stored application id back
Keep-on-blank covers only the certificateblank application id keeps the stored one
Keep-on-blank covers only the application IDblank certificate keeps the stored one
Credentials dropped even when presentnew application id replaces / new certificate replaces
Application ID presence validation removeda create still requires an application id
:application_id removed from filter_parametersthe application id is filtered from logs
Hint shown on the locked fieldhides the keep-certificate hint while locked
Hint never showndoes not send the stored certificate back
Fields never lockedhides the keep-certificate hint while locked
Sync toast dropped from the eventa finished sync still sends the sync toast

One mutation survives, and it's equivalent: dropping blank credentials on create as well. On a new record the missing attribute is nil, and the presence validation rejects it either way.

Commands:

  • bin/rails test on 745b2a06c: 10,845 runs, 46,239 assertions, 0 failures, 0 errors, 33 skips (TZ=UTC). Enable Banking controller tests: 24 runs, 0 failures. bin/rubocop on the changed Ruby files: no offenses. erb_lint on the panel: no errors. bin/brakeman --no-pager: 0 warnings.
  • bin/rails test on d9a55178d: 10,839 runs, 46,221 assertions, 0 failures, 0 errors, 33 skips (run with TZ=UTC; without it, one holdings test that uses Date.today fails when the run crosses local midnight).
  • Enable Banking model/controller tests and Settings::ProvidersControllerTest: 157 runs, 0 failures.
  • bin/rubocop on the changed Ruby files: no offenses.
  • erb_lint on the panel: no errors.
  • bin/brakeman --no-pager: 0 warnings.
  • No JavaScript changed.

Secret scan

The test certificate is generated with OpenSSL::PKey::RSA and identified by a line of its body, and the panel's PEM placeholder line isn't touched. So the diff adds no private-key header, which the security-scan (pipelock) job rejects.

Blast radius

  • enable_banking_item_params has two callers, create and update. In create, @enable_banking_item is nil when the params are read, so the guard doesn't apply.
  • When the connection is authenticated, the fields are disabled and aren't submitted, so the keep-on-blank rule doesn't apply and the hint is hidden.
  • application_id is also read by Provider::EnableBanking (JWT kid), the provider adapter, the new_connection copy in authorize and Family#create_enable_banking_item!. All of them read the stored value and are unchanged.
  • filter_parameters matches partially, so Doorkeeper's integer application_id columns on oauth_access_grants and oauth_access_tokens now also read [FILTERED] in inspect output. No OAuth request parameter is named application_id.
  • Locales: every settings locale with an Enable Banking panel block now carries the hint. config/locales/views/application/fr.yml repeats the French panel keys, and settings/fr.yml supplies the hint.
  • The panel is rendered by Settings::ProvidersController and EnableBankingItemsController (both admin-only), and no longer by the event.
  • new_connection copies the certificate server-side and doesn't read the form.
  • Siblings: git grep broadcast_replace_to over app/models and app/jobs finds no other settings/providers/* partial sent on the family stream.

Summary by CodeRabbit

  • Bug Fixes
    • Leaving the client certificate or application ID blank when updating an Enable Banking connection now preserves the saved credential; supplied replacements are saved. New connections still require credentials.
    • Completed syncs no longer send the admin-only provider settings panel or stored certificate to family members’ views. Connection updates and sync notifications continue to appear.
  • Usability
    • Credential fields no longer display saved values, and locked credentials cannot be edited.
    • Translated guidance explains that leaving the certificate field blank retains the current certificate.