Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Redact the Jev endpoint in the self-hosting settings panel (#4401)

jaysbeekay66c74f6d60a2
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rb
index b781e8af4e..6b16ae7540 100644
--- a/app/controllers/settings/hostings_controller.rb
+++ b/app/controllers/settings/hostings_controller.rb
@@ -245,7 +245,12 @@ class Settings::HostingsController < ApplicationController
if hosting_params.key?(:jev_endpoint)
raw_endpoint = hosting_params[:jev_endpoint].to_s.strip
- if raw_endpoint.blank?
+ stored_endpoint = Setting.jev_endpoint
+ if stored_endpoint.present? && raw_endpoint == Provider::Jev.redacted_endpoint(stored_endpoint)
+ # The field shows the stored endpoint redacted, and auto-submits on
+ # blur, so this is the page echoing it back, not a change. Saving it
+ # would strip the credentials the operator put in the URL.
+ elsif raw_endpoint.blank?
Setting.jev_endpoint = nil
else
# Provider::Jev owns the rule so settings, JEV_ENDPOINT and eval-time
diff --git a/app/views/settings/hostings/_jev_settings.html.erb b/app/views/settings/hostings/_jev_settings.html.erb
index 0db1742acb..c2610de1f7 100644
--- a/app/views/settings/hostings/_jev_settings.html.erb
+++ b/app/views/settings/hostings/_jev_settings.html.erb
@@ -28,7 +28,7 @@
<%= form.text_field :jev_endpoint,
label: t(".endpoint_label"),
placeholder: t(".endpoint_placeholder"),
- value: @jev_endpoint_input || Provider::Jev.effective_endpoint,
+ value: @jev_endpoint_input || Provider::Jev.redacted_endpoint(Provider::Jev.effective_endpoint),
autocomplete: "off",
autocapitalize: "none",
spellcheck: "false",
diff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rb
index 654d15c3f2..c98e7d44b7 100644
--- a/test/controllers/settings/hostings_controller_test.rb
+++ b/test/controllers/settings/hostings_controller_test.rb
@@ -496,6 +496,52 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
end
end
+ # A gateway endpoint can carry its key in the userinfo or the query string,
+ # and `show` has no admin gate: any member who turns on preview features sees
+ # this panel once the family's provider is Jev. Signed in as that member
+ # rather than the admin, because that is who the endpoint must not reach.
+ test "the jev panel shows a member the endpoint without its credentials" do
+ with_self_hosting do
+ Setting.jev_endpoint = "https://gw-user:[email protected]/v1/jev?api_key=qk-secret" # pipelock:ignore Credential in URL
+ member = users(:family_member)
+ member.family.update!(categorization_provider: "jev")
+ member.update!(preferences: (member.preferences || {}).merge("preview_features_enabled" => true))
+ sign_in member
+
+ get settings_hosting_url
+
+ assert_response :success
+ assert_select "input[name='setting[jev_endpoint]']", 1
+ assert_select "input[name='setting[jev_endpoint]'][value='https://gw.example.com/v1/jev']"
+ assert_no_match(/gw-secret|qk-secret|gw-user/, response.body)
+ end
+ end
+
+ # The field auto-submits on blur, so the redacted value it now shows comes
+ # straight back. Saving it would silently strip the operator's credentials.
+ test "submitting the redacted jev endpoint back keeps the stored one" do
+ with_self_hosting do
+ stored = "https://gw-user:[email protected]/v1/jev?api_key=qk-secret" # pipelock:ignore Credential in URL
+ Setting.jev_endpoint = stored
+
+ patch settings_hosting_url, params: { setting: { jev_endpoint: "https://gw.example.com/v1/jev" } }
+
+ assert_equal stored, Setting.jev_endpoint
+ end
+ end
+
+ # Only the echoed redacted value is held back: a different endpoint still
+ # replaces a stored one that carries credentials.
+ test "submitting a different jev endpoint replaces a stored one with credentials" do
+ with_self_hosting do
+ Setting.jev_endpoint = "https://gw-user:[email protected]/v1/jev?api_key=qk-secret" # pipelock:ignore Credential in URL
+
+ patch settings_hosting_url, params: { setting: { jev_endpoint: "https://other.example.com/v1/jev" } }
+
+ assert_equal "https://other.example.com/v1/jev", Setting.jev_endpoint
+ end
+ end
+
test "rejects non-URL jev endpoint" do
with_self_hosting do
Setting.jev_endpoint = nil