fix(wise): redirect after token submission instead of rendering inline (#2730)

* fix(wise): redirect after token submission instead of rendering inline

The success path in create rendered select_profiles directly (200 OK),
which Turbo rejects for standard form submissions ('Form responses must
redirect to another location'). Now it redirects, carrying the encrypted
token through the session. Session-expired fallbacks now point at
settings_providers_path instead of new_wise_item_path, which has no view.

* test(wise): update controller specs for redirect-based create flow

* fix(wise): read pending token from session, not client params

link_profiles decrypted params[:encrypted_pending_token], even though create
already stores the encrypted token server-side in the session. The client
round-trip was unnecessary and untrusted; now link_profiles reads directly
from session[:wise_pending_encrypted_token].
This commit is contained in:
Blaž Dular
2026-07-21 01:28:53 +02:00
committed by GitHub
parent 835ebf43bf
commit 7b0f98b1d6
2 changed files with 29 additions and 31 deletions

View File

@@ -14,15 +14,19 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTest
]
end
# create renders select_profiles directly — token must NOT appear in the session
# create redirects to select_profiles (Turbo requires a redirect from a standard
# form submission) — the encrypted token travels via the session, not the response body.
test "create renders select_profiles and keeps token out of session" do
test "create redirects to select_profiles and keeps raw token out of the session" do
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
assert_response :success
assert_redirected_to select_profiles_wise_items_path
assert_nil session[:wise_pending_token], "raw API token must not be stored in the session"
assert session[:wise_pending_encrypted_token].present?
follow_redirect!
assert_select "input[name='encrypted_pending_token']"
end
@@ -30,6 +34,7 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTest
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
follow_redirect!
encrypted = css_select("input[name='encrypted_pending_token']").first["value"]
assert encrypted.present?, "hidden encrypted_pending_token field must be present"
@@ -55,44 +60,37 @@ class WiseItemsControllerTest < ActionDispatch::IntegrationTest
assert_nil session[:wise_pending_token]
end
# link_profiles uses the encrypted hidden field, not the session
# link_profiles reads the encrypted token from the session (set by create) —
# the client no longer needs to (and cannot) supply or tamper with it via params.
test "link_profiles creates WiseItems using the encrypted token" do
test "link_profiles creates WiseItems using the session-held encrypted token" do
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
encrypted = css_select("input[name='encrypted_pending_token']").first["value"]
assert_difference "WiseItem.count", 1 do
post link_profiles_wise_items_url, params: {
encrypted_pending_token: encrypted,
profile_ids: [ "99999999" ]
}
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
end
assert_redirected_to settings_providers_path
assert_equal "live_token_abc", @family.wise_items.find_by!(profile_id: "99999999").token
assert_nil session[:wise_pending_profiles]
assert_nil session[:wise_pending_encrypted_token]
end
test "link_profiles redirects to new when encrypted token is missing" do
post link_profiles_wise_items_url, params: {
encrypted_pending_token: "",
profile_ids: [ "99999999" ]
}
test "link_profiles redirects to providers when there is no pending session" do
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
assert_redirected_to new_wise_item_path
assert_redirected_to settings_providers_path
end
test "link_profiles redirects to new when encrypted token is tampered" do
test "link_profiles redirects to providers when the session token cannot be decrypted" do
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
post link_profiles_wise_items_url, params: {
encrypted_pending_token: "tampered_garbage_value",
profile_ids: [ "99999999" ]
}
session[:wise_pending_encrypted_token] = "corrupted_garbage_value"
assert_redirected_to new_wise_item_path
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
assert_redirected_to settings_providers_path
end
end