Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Render confirm dialog bodies as text (#3813)

GFRdd397cd5a259
diff --git a/app/helpers/custom_confirm.rb b/app/helpers/custom_confirm.rb
index b4bc52be62..4451be41c3 100644
--- a/app/helpers/custom_confirm.rb
+++ b/app/helpers/custom_confirm.rb
@@ -1,13 +1,8 @@
# The shape of data expected by `confirm_dialog_controller.js` to override the
-# default browser confirm API via Turbo.
+# default browser confirm API via Turbo. The dialog renders every field as text,
+# so record names can be interpolated as they are.
class CustomConfirm
class << self
- # `body` is the one field the dialog renders as HTML — confirm_dialog_controller
- # assigns it to innerHTML so bodies such as accounts' `confirm_body_html` can
- # carry markup, while title and button label go through textContent. Every
- # caller passes a user-named record here, so the name is escaped on its way
- # into the body: without it a record named "<img src=x onerror=…>" executes
- # as soon as someone opens the confirmation.
# `titleize` / `downcase` are English-shaped and stay applied to the record
# name so the English copy is unchanged; a locale that needs different
# casing can absorb it in its own string.
@@ -16,9 +11,7 @@ class CustomConfirm
destructive: true,
high_severity: high_severity,
title: I18n.t("shared.custom_confirm.resource_deletion_title", resource: resource_name.titleize),
- # Escaped, unlike the other two: this is the only field the dialog
- # renders as HTML.
- body: I18n.t("shared.custom_confirm.resource_deletion_body", resource: ERB::Util.html_escape(resource_name.downcase)),
+ body: I18n.t("shared.custom_confirm.resource_deletion_body", resource: resource_name.downcase),
btn_text: I18n.t("shared.custom_confirm.resource_deletion_btn_text", resource: resource_name.titleize)
)
end
diff --git a/app/javascript/controllers/confirm_dialog_controller.js b/app/javascript/controllers/confirm_dialog_controller.js
index 12d6055778..0b9af9f836 100644
--- a/app/javascript/controllers/confirm_dialog_controller.js
+++ b/app/javascript/controllers/confirm_dialog_controller.js
@@ -39,7 +39,10 @@ export default class extends Controller {
});
this.titleTarget.textContent = data.title || "Are you sure?";
- this.subtitleTarget.innerHTML =
+ // Text, never HTML: bodies interpolate record names (merchants, accounts,
+ // brokerages, tickers) that come from bank feeds, providers and other
+ // family members, however the caller built the confirmation.
+ this.subtitleTarget.textContent =
data.body || "This action cannot be undone.";
}
diff --git a/app/models/goal.rb b/app/models/goal.rb
index fc765d4805..948b57d035 100644
--- a/app/models/goal.rb
+++ b/app/models/goal.rb
@@ -929,12 +929,7 @@ class Goal < ApplicationRecord
destructive: true,
high_severity: true,
title: I18n.t("goals.show.confirm_delete_title"),
- # Escaped: the dialog assigns `body` to innerHTML (so bodies like the
- # accounts' confirm_body_html can carry <p>), so a goal named
- # "<img src=x onerror=…>" would otherwise run when a family member opens
- # the confirmation. Only `body` needs this — the dialog sets its title and
- # button label with textContent.
- body: I18n.t("goals.show.confirm_delete_body", name: ERB::Util.html_escape(name)),
+ body: I18n.t("goals.show.confirm_delete_body", name: name),
btn_text: I18n.t("goals.show.confirm_delete_cta")
)
end
diff --git a/test/helpers/custom_confirm_test.rb b/test/helpers/custom_confirm_test.rb
index 7fc7c6be1b..4c4d51808b 100644
--- a/test/helpers/custom_confirm_test.rb
+++ b/test/helpers/custom_confirm_test.rb
@@ -1,19 +1,15 @@
require "test_helper"
class CustomConfirmTest < ActiveSupport::TestCase
- # `confirm_dialog_controller` assigns `body` to innerHTML so bodies like
- # accounts' `confirm_body_html` can carry markup. Every caller passes a
- # user-named record, so an account named "<img src=x onerror=…>" would run
- # when someone opened the confirmation.
- test "escapes the resource name in the HTML-rendered body" do
- data = CustomConfirm.for_resource_deletion("<img src=x onerror=alert(1)>").to_data_attribute
-
- assert_includes data[:body], "&lt;img src=x onerror=alert(1)&gt;"
- assert_no_match(/<img/, data[:body])
+ # `confirm_dialog_controller` renders `body` with textContent, so a record
+ # named "<img src=x onerror=…>" shows as that text. Escaping it here as well
+ # would print the entities: "rock &amp; roll" instead of "rock & roll".
+ test "passes the resource name through as text" do
+ assert_includes CustomConfirm.for_resource_deletion("<img src=x onerror=alert(1)>").to_data_attribute[:body],
+ "<img src=x onerror=alert(1)>"
+ assert_includes CustomConfirm.for_resource_deletion("Rock & Roll").to_data_attribute[:body], "rock & roll"
end
- # Title and button label reach the dialog through textContent, so they are
- # inert — pinned here so a future move to innerHTML doesn't pass silently.
test "keeps the English copy the hardcoded strings produced" do
data = CustomConfirm.for_resource_deletion("rule").to_data_attribute
diff --git a/test/models/goal_test.rb b/test/models/goal_test.rb
index 209d3e1c11..a7c53d441f 100644
--- a/test/models/goal_test.rb
+++ b/test/models/goal_test.rb
@@ -48,15 +48,12 @@ class GoalTest < ActiveSupport::TestCase
assert_equal 100, vacances.progress_percent.to_i
end
- # The confirm dialog assigns `body` to innerHTML, so an unescaped goal name
- # would execute when a family member opens the delete confirmation.
- test "deletion_confirm escapes the goal name in the dialog body" do
- @goal.name = "<img src=x onerror=alert(1)>"
+ # The confirm dialog renders `body` as text, so the goal name goes in as it
+ # is; escaping it would show "&amp;" in the dialog.
+ test "deletion_confirm names the goal as it is written" do
+ @goal.name = "Rock & Roll <3"
- body = @goal.deletion_confirm.to_data_attribute[:body]
-
- assert_includes body, "&lt;img src=x onerror=alert(1)&gt;"
- assert_no_match(/<img/, body)
+ assert_includes @goal.deletion_confirm.to_data_attribute[:body], "Rock & Roll <3"
end
# Derived, not a hardcoded list: a locale added later ships a goals YAML
diff --git a/test/system/confirm_dialog_test.rb b/test/system/confirm_dialog_test.rb
new file mode 100644
index 0000000000..68bc8b5ca4
--- /dev/null
+++ b/test/system/confirm_dialog_test.rb
@@ -0,0 +1,29 @@
+require "application_system_test_case"
+
+# Confirmation bodies interpolate names that come from bank feeds, providers and
+# other family members, so the dialog has to show them as text.
+class ConfirmDialogTest < ApplicationSystemTestCase
+ setup do
+ @user = users(:family_admin)
+ login_as @user
+ end
+
+ test "shows a provider merchant's name as text instead of rendering it" do
+ name = %(<img src=x onerror="document.body.dataset.injected='yes'"> Coffee)
+ merchant = ProviderMerchant.create!(name: name, source: "plaid")
+ Transaction.joins(entry: :account).merge(@user.accessible_accounts).first.update!(merchant: merchant)
+
+ visit family_merchants_path
+
+ within "tr", text: "Coffee" do
+ find("[data-DS--menu-target='button']").click
+ end
+ click_on I18n.t("family_merchants.provider_merchant.remove")
+
+ within "#confirm-dialog" do
+ assert_text I18n.t("family_merchants.provider_merchant.remove_confirm_body", name: name)
+ assert_no_selector "img"
+ end
+ assert_nil page.evaluate_script("document.body.dataset.injected")
+ end
+end