Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Return to the merchant page after editing a merchant (#4635)

Dov Benyomin Sohacheski382357e86035
diff --git a/app/controllers/concerns/store_location.rb b/app/controllers/concerns/store_location.rb
index 6359e5b469..f01dc509c7 100644
--- a/app/controllers/concerns/store_location.rb
+++ b/app/controllers/concerns/store_location.rb
@@ -42,7 +42,8 @@ private
end
# Only allow internal absolute paths (a single leading "/"). Blocks absolute
- # URLs, protocol-relative ("//evil"), and backslash tricks ("/\\evil") so a
+ # URLs, protocol-relative ("//evil"), backslash tricks ("/\\evil") and control
+ # characters, which browsers strip before resolving ("/\t/evil"), so a
# crafted ?return_to= can't open-redirect — including via a custom
# turbo_stream redirect, which Rails' redirect host-guard does NOT cover
# (the client `Turbo.visit`es the target and full-navigates cross-origin).
@@ -50,7 +51,7 @@ private
# is_a?(String) first: a crafted `?return_to[]=foo` makes params[:return_to]
# an Array, and Array#match? doesn't exist — without this guard the helper
# raises NoMethodError before the redirect hardening can reject it.
- value if value.is_a?(String) && value.present? && value.match?(%r{\A/(?![/\\])})
+ value if value.is_a?(String) && value.present? && value.match?(%r{\A/(?![/\\])[^[:cntrl:]]*\z})
end
def clear_previous_path
diff --git a/app/controllers/family_merchants_controller.rb b/app/controllers/family_merchants_controller.rb
index 14519dd8af..1ccf033931 100644
--- a/app/controllers/family_merchants_controller.rb
+++ b/app/controllers/family_merchants_controller.rb
@@ -81,14 +81,14 @@ class FamilyMerchantsController < ApplicationController
end
else
respond_to do |format|
- format.html { redirect_to family_merchants_path, notice: t(".success") }
- format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, family_merchants_path) }
+ format.html { redirect_to updated_merchant_return_path, notice: t(".success") }
+ format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, updated_merchant_return_path) }
end
end
elsif @merchant.update(merchant_params)
respond_to do |format|
- format.html { redirect_to family_merchants_path, notice: t(".success") }
- format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, family_merchants_path) }
+ format.html { redirect_to updated_merchant_return_path, notice: t(".success") }
+ format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, updated_merchant_return_path) }
end
else
render :edit, status: :unprocessable_entity
@@ -171,6 +171,10 @@ class FamilyMerchantsController < ApplicationController
name_changed || website_changed
end
+ def updated_merchant_return_path
+ safe_return_to(params[:return_to]) || family_merchants_path
+ end
+
def merchant_params
# Handle both family_merchant and provider_merchant param keys
key = params.key?(:family_merchant) ? :family_merchant : :provider_merchant
diff --git a/app/views/family_merchants/_form.html.erb b/app/views/family_merchants/_form.html.erb
index bcaaaba952..60446cfb12 100644
--- a/app/views/family_merchants/_form.html.erb
+++ b/app/views/family_merchants/_form.html.erb
@@ -2,6 +2,7 @@
<div data-controller="color-avatar">
<%= styled_form_with model: family_merchant, url: family_merchant.persisted? ? family_merchant_path(family_merchant) : family_merchants_path, class: "space-y-4" do |f| %>
+ <%= hidden_field_tag :return_to, params[:return_to] if params[:return_to].present? %>
<section class="space-y-4">
<% if family_merchant.errors.any? %>
<%= render "shared/form_errors", model: family_merchant %>
diff --git a/app/views/merchants/show.html.erb b/app/views/merchants/show.html.erb
index 3698aeebbf..8269863b69 100644
--- a/app/views/merchants/show.html.erb
+++ b/app/views/merchants/show.html.erb
@@ -16,7 +16,7 @@
text: t(".edit"),
icon: "pencil",
variant: "outline",
- href: edit_family_merchant_path(@merchant),
+ href: edit_family_merchant_path(@merchant, return_to: request.fullpath),
frame: :modal
) %>
<% end %>
diff --git a/test/controllers/concerns/store_location_test.rb b/test/controllers/concerns/store_location_test.rb
index b576645f01..b8f0b03e0f 100644
--- a/test/controllers/concerns/store_location_test.rb
+++ b/test/controllers/concerns/store_location_test.rb
@@ -14,6 +14,14 @@ class StoreLocationTest < ActionDispatch::IntegrationTest
assert_equal return_to, session[:return_to]
end
+ test "ignores a return_to with control characters" do
+ [ "/\t/evil.example", "/\n/evil.example", "/\r/evil.example" ].each do |return_to|
+ get accounts_path(return_to: return_to)
+
+ assert_nil session[:return_to]
+ end
+ end
+
test "drops the query string of a return_to too long for the session cookie" do
account_ids = Array.new(32) { SecureRandom.uuid }
return_to = "/transactions?" + { page: 1, q: { account_ids: account_ids } }.to_query
diff --git a/test/controllers/family_merchants_controller_test.rb b/test/controllers/family_merchants_controller_test.rb
index 68c0414367..265631e8f4 100644
--- a/test/controllers/family_merchants_controller_test.rb
+++ b/test/controllers/family_merchants_controller_test.rb
@@ -79,6 +79,26 @@ class FamilyMerchantsControllerTest < ActionDispatch::IntegrationTest
assert_equal "#000000", @merchant.reload.color
end
+ test "edit keeps return_to in the form" do
+ get edit_family_merchant_url(@merchant, return_to: merchant_path(@merchant))
+
+ assert_select "input[type=hidden][name=return_to][value=?]", merchant_path(@merchant)
+ end
+
+ test "update returns to the given path" do
+ patch family_merchant_url(@merchant), params: { return_to: merchant_path(@merchant), family_merchant: { name: "new name" } }
+
+ assert_redirected_to merchant_path(@merchant)
+ end
+
+ test "update ignores an external return_to" do
+ [ "//evil.example", "/\t/evil.example" ].each do |return_to|
+ patch family_merchant_url(@merchant), params: { return_to: return_to, family_merchant: { name: "new name" } }
+
+ assert_redirected_to family_merchants_path
+ end
+ end
+
test "should upload merchant logo" do
patch family_merchant_url(@merchant), params: {
family_merchant: { logo: file_fixture_upload("profile_image.png", "image/png", :binary) }
diff --git a/test/controllers/merchants_controller_test.rb b/test/controllers/merchants_controller_test.rb
index 92472aa798..21fce71785 100644
--- a/test/controllers/merchants_controller_test.rb
+++ b/test/controllers/merchants_controller_test.rb
@@ -21,6 +21,12 @@ class MerchantsControllerTest < ActionDispatch::IntegrationTest
assert_no_match "Amazon order", response.body
end
+ test "edit link returns to the merchant page it was opened from" do
+ get merchant_url(@merchant, page: 2)
+
+ assert_select "a[href=?]", edit_family_merchant_path(@merchant, return_to: merchant_path(@merchant, page: 2))
+ end
+
test "shows a provider merchant assigned to the family's transactions" do
provider = ProviderMerchant.create!(name: "Coffee Co", source: "plaid")
create_transaction(account: accounts(:depository), name: "Morning coffee", merchant: provider)