Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: secure subscription endpoints

David Battersbye26dbdda292e
diff --git a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
index 68b85673810..733f4a8e82b 100644
--- a/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
+++ b/plugins/discourse-subscriptions/app/controllers/discourse_subscriptions/user/subscriptions_controller.rb
@@ -9,6 +9,7 @@ module DiscourseSubscriptions
requires_plugin PLUGIN_NAME
before_action :set_api_key
+ before_action :find_subscription, only: %i[update destroy]
requires_login
def index
@@ -73,14 +74,12 @@ module DiscourseSubscriptions
def update
params.require(:payment_method)
- subscription = Subscription.where(external_id: params[:id]).first
begin
- attach_method_to_customer(subscription.customer_id, params[:payment_method])
- subscription =
- ::Stripe::Subscription.update(
- params[:id],
- { default_payment_method: params[:payment_method] },
- )
+ attach_method_to_customer(@subscription.customer_id, params[:payment_method])
+ ::Stripe::Subscription.update(
+ params[:id],
+ { default_payment_method: params[:payment_method] },
+ )
render json: success_json
rescue ::Stripe::InvalidRequestError
render_json_error I18n.t("discourse_subscriptions.card.invalid")
@@ -93,6 +92,21 @@ module DiscourseSubscriptions
customer = Customer.find(customer_id)
::Stripe::PaymentMethod.attach(method, { customer: customer.customer_id })
end
+
+ def find_subscription
+ @subscription ||=
+ Subscription.joins(:customer).find_by(
+ external_id: params[:id],
+ customer: {
+ user_id: current_user.id,
+ },
+ )
+
+ if @subscription.nil?
+ render_json_error I18n.t("discourse_subscriptions.subscription_not_found"),
+ status: 404 and return
+ end
+ end
end
end
end
diff --git a/plugins/discourse-subscriptions/config/locales/server.en.yml b/plugins/discourse-subscriptions/config/locales/server.en.yml
index ff7edbf12a4..940832acefb 100644
--- a/plugins/discourse-subscriptions/config/locales/server.en.yml
+++ b/plugins/discourse-subscriptions/config/locales/server.en.yml
@@ -1,6 +1,7 @@
en:
discourse_subscriptions:
customer_not_found: Customer not found
+ subscription_not_found: Subscription not found
card:
declined: Card Declined
invalid: Card Invalid
diff --git a/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb b/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
index 8adec510af4..dbbcdba1633 100644
--- a/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
+++ b/plugins/discourse-subscriptions/spec/requests/user/subscriptions_controller_spec.rb
@@ -132,4 +132,34 @@ RSpec.describe DiscourseSubscriptions::User::SubscriptionsController do
end
end
end
+
+ context "when authenticated as another user" do
+ fab!(:user_1, :user)
+ fab!(:user_2, :user)
+ fab!(:customer) { Fabricate(:customer, user_id: user_1.id, customer_id: "001") }
+ fab!(:subscription) { Fabricate(:subscription, customer_id: customer.id, external_id: "abc") }
+
+ before { sign_in(user_2) }
+
+ describe "destroy" do
+ it "does not allow user to cancel a subscription that is not theirs" do
+ ::Stripe::Subscription.expects(:update).never
+
+ delete "/s/user/subscriptions/abc.json"
+
+ expect(response.status).to eq(404)
+ end
+ end
+
+ describe "update" do
+ it "does not allow user to update a subscription that is not theirs" do
+ ::Stripe::PaymentMethod.expects(:attach).never
+ ::Stripe::Subscription.expects(:update).never
+
+ put "/s/user/subscriptions/abc.json", params: { payment_method: "x" }
+
+ expect(response.status).to eq(404)
+ end
+ end
+ end
end