Skip to content

Commit b697f7b

Browse files
committed
fix: second attempt at securing mollie redirect
1 parent 1207191 commit b697f7b

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

app/controllers/members/payments_controller.rb

+26-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,19 @@ def pay_activities
4747
)
4848
if payment.save
4949
# Check URI for safety (supresses brakeman warning)
50-
url = (URI.parse(payment.payment_uri) if payment.payment_uri =~ URI::DEFAULT_PARSER.make_regexp)
51-
redirect_to(url)
50+
url = begin
51+
URI.parse(payment.payment_uri)
52+
rescue StandardError
53+
nil
54+
end
55+
56+
# Check if it's a valid URI and matches your whitelist of acceptable domains (e.g., only http(s)://example.com)
57+
if url.is_a?(URI::HTTP) && ['mollie.com'].include?(url.host)
58+
redirect_to(url)
59+
else
60+
# Fallback to a safe default redirect if the URI is invalid or not in the whitelist
61+
redirect_to(root_path)
62+
end
5263
else
5364
flash[:notice] = I18n.t('failed', scope: 'activerecord.errors.models.payment')
5465
redirect_to(member_payments_path)
@@ -107,8 +118,19 @@ def add_funds
107118

108119
if payment.save
109120
# Check URI for safety (supresses brakeman warning)
110-
url = (URI.parse(payment.payment_uri) if payment.payment_uri =~ URI::DEFAULT_PARSER.make_regexp)
111-
redirect_to(url)
121+
url = begin
122+
URI.parse(payment.payment_uri)
123+
rescue StandardError
124+
nil
125+
end
126+
127+
# Check if it's a valid URI and matches your whitelist of acceptable domains (e.g., only http(s)://example.com)
128+
if url.is_a?(URI::HTTP) && ['mollie.com'].include?(url.host)
129+
redirect_to(url)
130+
else
131+
# Fallback to a safe default redirect if the URI is invalid or not in the whitelist
132+
redirect_to(root_path)
133+
end
112134
else
113135
flash[:warning] = I18n.t('failed', scope: 'activerecord.errors.models.payment')
114136
redirect_to(members_home_path)

0 commit comments

Comments
 (0)