discourse/plugins/discourse-subscriptions/spec/requests/hooks_controller_spec.rb
Alan Guo Xiang Tan c34f2aac8d SECURITY: Fixes for discourse-subscriptions
This PR combines two security fixes for the discourse-subscriptions plugin.

## Commits

### 1. [`8f0a8e98e9`](8f0a8e98e9) — SECURITY: Fix unauthorized group access via subscription finalize

The `/s/finalize` endpoint accepted client-supplied `plan` and
`transaction` parameters that determined which group a user would be
added to after completing a Stripe payment. During the 3D Secure
authentication flow, an authenticated user could complete a cheap
subscription in `SubscribeController#create` but supply a premium plan
ID when calling `#finalize`, granting themselves access to a higher-tier
group they never paid for.

This commit fixes the vulnerability by linking the two endpoints
server-side using `server_session`. When `#create` produces a
transaction requiring 3D Secure authentication (`incomplete` or `open`
status), it stores the transaction ID and plan ID in the server session.
`#finalize` then reads exclusively from the session instead of accepting
client parameters, and clears the entry after successful finalization.

On the frontend, `Transaction.finalize()` no longer sends any
parameters to the server.

(from #434)

---

### 2. [`dcf80dda61`](dcf80dda61) — SECURITY: Use per-request Stripe API key instead of global state

Replace `set_api_key` (which mutated global `::Stripe.api_key`) with
`set_stripe_api_key` (which stores the key in an instance variable).
All Stripe API calls now receive `{ api_key: @stripe_api_key }` as
the per-request opts parameter, following the stripe gem's documented
per-request configuration pattern. This prevents API key leakage
across concurrent requests in multi-threaded environments.

(from #590)

---

**Security Advisory:** https://github.com/discourse/discourse/security/advisories/GHSA-f866-8fcp-fgvv

---

**Security Advisory:** https://github.com/discourse/discourse/security/advisories/GHSA-9vg5-mp49-xghh
2026-03-31 15:12:45 +01:00

379 lines
11 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DiscourseSubscriptions::HooksController do
before do
SiteSetting.discourse_subscriptions_webhook_secret = "zascharoo"
SiteSetting.discourse_subscriptions_secret_key = "secret-key"
SiteSetting.discourse_subscriptions_enabled = true
end
it "rejects webhooks when webhook secret is blank" do
SiteSetting.discourse_subscriptions_webhook_secret = ""
post "/s/hooks.json", params: "{}", headers: { HTTP_STRIPE_SIGNATURE: "t=1,v1=abc" }
expect(response.status).to eq 403
end
it "constructs a webhook event" do
payload = "we-want-a-shrubbery"
headers = { HTTP_STRIPE_SIGNATURE: "stripe-webhook-signature" }
::Stripe::Webhook
.expects(:construct_event)
.with("we-want-a-shrubbery", "stripe-webhook-signature", "zascharoo")
.returns(type: "something")
post "/s/hooks.json", params: payload, headers: headers
expect(response.status).to eq 200
end
describe "event types" do
let(:user) { Fabricate(:user) }
let(:customer) do
Fabricate(:customer, customer_id: "c_575768", product_id: "p_8654", user_id: user.id)
end
let!(:subscription) do
Fabricate(:subscription, external_id: "sub_12345", customer_id: customer.id, status: nil)
end
let(:group) { Fabricate(:group, name: "subscribers-group") }
let(:event_data) do
{
object: {
customer: customer.customer_id,
plan: {
product: customer.product_id,
metadata: {
group_name: group.name,
},
},
},
}
end
let(:customer_subscription_deleted_data) do
{
object: {
id: subscription.external_id,
customer: customer.customer_id,
plan: {
product: customer.product_id,
metadata: {
group_name: group.name,
},
},
status: "canceled",
},
}
end
let(:checkout_session_completed_data) do
{
object: {
id: "cs_test_a1ENei5A9TGOaEketyV5qweiQR5CyJWHT5j8T3HheQY3uah3RxzKttVUKZ",
object: "checkout.session",
customer: customer.customer_id,
customer_email: user.email,
invoice: "in_1P9b7iEYXaQnncSh81AQtuHD",
metadata: {
},
mode: "subscription",
payment_status: "paid",
status: "complete",
submit_type: nil,
subscription: "sub_1P9b7iEYXaQnncSh3H3G9d2Y",
success_url: "http://localhost:4200/my/billing/subscriptions",
url: nil,
},
}
end
let(:checkout_session_completed_data_one_off) do
{
object: {
id: "cs_test_a1ENei5A9TGOaEketyV5qweiQR5CyJWHT5j8T3HheQY3uah3RxzKttVUKZ",
object: "checkout.session",
customer: nil,
customer_email: user.email,
invoice: nil,
metadata: {
},
mode: "subscription",
payment_intent: "pi_3PsohkGHcn",
payment_status: "paid",
status: "complete",
submit_type: nil,
subscription: nil,
success_url: "http://localhost:4200/my/billing/subscriptions",
url: nil,
},
}
end
let(:checkout_session_completed_bad_data) do
{
object: {
id: "cs_test_a1ENei5A9TGOaEketyV5qweiQR5CyJWHT5j8T3HheQY3uah3RxzKttVUKZ",
object: "checkout.session",
customer: nil,
customer_email: nil,
invoice: "in_1P9b7iEYXaQnncSh81AQtuHD",
metadata: {
},
mode: "subscription",
payment_status: "paid",
status: "complete",
submit_type: nil,
subscription: nil,
success_url: "http://localhost:4200/my/billing/subscriptions",
url: nil,
},
}
end
let(:list_line_items_data) do
{
data: [
{
id: "li_1P9YlFEYXaQnncShFBl7r0na",
object: "item",
description: "Exclusive Access",
price: {
id: "price_1OrmlvEYXaQnncShNahrpKvA",
metadata: {
group_name: group.name,
trial_period_days: "0",
},
nickname: "EA1",
product: "prod_PhB6IpGhEX14Hi",
},
},
],
}
end
describe "checkout.session.completed" do
before do
event = { type: "checkout.session.completed", data: checkout_session_completed_data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(
checkout_session_completed_data[:object][:id],
{ limit: 1 },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(list_line_items_data)
::Stripe::Subscription
.stubs(:update)
.with(
checkout_session_completed_data[:object][:subscription],
{ metadata: { user_id: user.id, username: user.username } },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(
{
id: checkout_session_completed_data[:object][:subscription],
object: "subscription",
metadata: {
user_id: user.id.to_s,
username: user.username,
},
},
)
::Stripe::Webhook.stubs(:construct_event).returns(event)
end
it "is successful" do
post "/s/hooks.json"
expect(response.status).to eq 200
end
describe "completing the subscription" do
it "adds the user to the group when completing the transaction" do
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1)
expect(response.status).to eq 200
end
end
end
describe "checkout.session.completed with bad data" do
before do
event = { type: "checkout.session.completed", data: checkout_session_completed_bad_data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(
checkout_session_completed_data[:object][:id],
{ limit: 1 },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
::Stripe::Customer
.stubs(:create)
.with(anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(id: "cus_1234")
end
it "is returns 422" do
post "/s/hooks.json"
expect(response.status).to eq 422
end
end
describe "checkout.session.completed for one-off purchase" do
before do
event = {
type: "checkout.session.completed",
data: checkout_session_completed_data_one_off,
}
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(
checkout_session_completed_data[:object][:id],
{ limit: 1 },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
::Stripe::Customer
.stubs(:create)
.with(anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(id: "cus_1234")
end
it "is returns 200" do
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1)
expect(response.status).to eq 200
end
end
describe "checkout.session.completed with anonymous user" do
before do
checkout_session_completed_bad_data[:object][:customer_email] = "anonymous@example.com"
data = checkout_session_completed_bad_data
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(
checkout_session_completed_data[:object][:id],
{ limit: 1 },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
::Stripe::Customer
.stubs(:create)
.with(anything, DiscourseSubscriptions::Stripe.request_opts)
.returns(id: "cus_1234")
end
it "is returns 422" do
post "/s/hooks.json"
expect(response.status).to eq 422
end
end
describe "checkout.session.completed with no customer email" do
before do
checkout_session_completed_bad_data[:object][:customer_email] = nil
data = checkout_session_completed_bad_data
event = { type: "checkout.session.completed", data: data }
::Stripe::Checkout::Session
.stubs(:list_line_items)
.with(
checkout_session_completed_data[:object][:id],
{ limit: 1 },
DiscourseSubscriptions::Stripe.request_opts,
)
.returns(list_line_items_data)
::Stripe::Webhook.stubs(:construct_event).returns(event)
end
it "is returns 422" do
post "/s/hooks.json"
expect(response.status).to eq 422
end
end
describe "customer.subscription.updated" do
before do
event = { type: "customer.subscription.updated", data: event_data }
::Stripe::Webhook.stubs(:construct_event).returns(event)
end
it "is successful" do
post "/s/hooks.json"
expect(response.status).to eq 200
end
describe "completing the subscription" do
it "does not add the user to the group" do
event_data[:object][:status] = "incomplete"
event_data[:previous_attributes] = { status: "incomplete" }
expect { post "/s/hooks.json" }.not_to change { user.groups.count }
expect(response.status).to eq 200
end
it "does not add the user to the group" do
event_data[:object][:status] = "incomplete"
event_data[:previous_attributes] = { status: "something-else" }
expect { post "/s/hooks.json" }.not_to change { user.groups.count }
expect(response.status).to eq 200
end
it "adds the user to the group when completing the transaction" do
event_data[:object][:status] = "complete"
event_data[:previous_attributes] = { status: "incomplete" }
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1)
expect(response.status).to eq 200
end
it "adds the user to the group when status is active" do
event_data[:object][:status] = "active"
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(1)
expect(response.status).to eq 200
end
end
end
describe "customer.subscription.deleted" do
before do
event = { type: "customer.subscription.deleted", data: customer_subscription_deleted_data }
::Stripe::Webhook.stubs(:construct_event).returns(event)
group.add(user)
end
it "deletes the customer" do
expect { post "/s/hooks.json" }.to change {
DiscourseSubscriptions::Subscription.where(status: "canceled").count
}.by(+1)
expect(response.status).to eq 200
end
it "removes the user from the group" do
expect { post "/s/hooks.json" }.to change { user.groups.count }.by(-1)
expect(response.status).to eq 200
end
end
end
end