Skip to content

Commit 48bd2fb

Browse files
authored
Remove support for legacy Paddle webhook passthrough formats (#4939)
* Remove support for legacy Paddle webhook passthrough formats * Drop support for legacy and user_id only passthrough entirely * Make codespell CI check happy
1 parent 8d238cd commit 48bd2fb

File tree

4 files changed

+15
-69
lines changed

4 files changed

+15
-69
lines changed

lib/plausible/billing/billing.ex

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule Plausible.Billing do
22
use Plausible
33
use Plausible.Repo
44
require Plausible.Billing.Subscription.Status
5-
alias Plausible.Auth
65
alias Plausible.Billing.Subscription
76
alias Plausible.Teams
87

@@ -133,48 +132,28 @@ defmodule Plausible.Billing do
133132
end
134133

135134
defp get_team!(%{"passthrough" => passthrough}) do
136-
case parse_passthrough!(passthrough) do
137-
{:team_id, team_id} ->
138-
Teams.get!(team_id)
139-
140-
{:user_id, user_id} ->
141-
user = Repo.get!(Auth.User, user_id)
142-
{:ok, team} = Teams.get_or_create(user)
143-
team
144-
end
135+
passthrough
136+
|> parse_passthrough!()
137+
|> Teams.get!()
145138
end
146139

147140
defp get_team!(_params) do
148141
raise "Missing passthrough"
149142
end
150143

151144
defp parse_passthrough!(passthrough) do
152-
{user_id, team_id} =
145+
team_id =
153146
case String.split(to_string(passthrough), ";") do
154-
["ee:true", "user:" <> user_id, "team:" <> team_id] ->
155-
{user_id, team_id}
156-
157-
["ee:true", "user:" <> user_id] ->
158-
{user_id, "0"}
159-
160-
# NOTE: legacy pattern, to be removed in a follow-up
161-
["user:" <> user_id, "team:" <> team_id] ->
162-
{user_id, team_id}
163-
164-
# NOTE: legacy pattern, to be removed in a follow-up
165-
[user_id] ->
166-
{user_id, "0"}
147+
["ee:true", "user:" <> _user_id, "team:" <> team_id] ->
148+
team_id
167149

168150
_ ->
169151
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"
170152
end
171153

172-
case {Integer.parse(user_id), Integer.parse(team_id)} do
173-
{{user_id, ""}, {0, ""}} when user_id > 0 ->
174-
{:user_id, user_id}
175-
176-
{{_user_id, ""}, {team_id, ""}} when team_id > 0 ->
177-
{:team_id, team_id}
154+
case Integer.parse(team_id) do
155+
{team_id, ""} when team_id > 0 ->
156+
team_id
178157

179158
_ ->
180159
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"

test/plausible/billing/billing_test.exs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ defmodule Plausible.BillingTest do
172172
user = new_user()
173173
Repo.delete!(user)
174174

175-
assert_raise Ecto.NoResultsError, fn ->
175+
assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
176176
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
177177
|> Billing.subscription_created()
178178
end
@@ -209,41 +209,6 @@ defmodule Plausible.BillingTest do
209209
assert subscription.currency_code == "EUR"
210210
end
211211

212-
test "supports user without a team case" do
213-
user = new_user()
214-
215-
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
216-
|> Billing.subscription_created()
217-
218-
subscription =
219-
user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
220-
221-
assert subscription.paddle_subscription_id == @subscription_id
222-
assert subscription.next_bill_date == ~D[2019-06-01]
223-
assert subscription.last_bill_date == ~D[2019-05-01]
224-
assert subscription.next_bill_amount == "6.00"
225-
assert subscription.currency_code == "EUR"
226-
end
227-
228-
test "supports old format without prefix" do
229-
user = new_user()
230-
{:ok, team} = Plausible.Teams.get_or_create(user)
231-
232-
%{@subscription_created_params | "passthrough" => "user:#{user.id};team:#{team.id}"}
233-
|> Billing.subscription_created()
234-
235-
assert user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
236-
end
237-
238-
test "supports old format without prefix for user without a team" do
239-
user = new_user()
240-
241-
%{@subscription_created_params | "passthrough" => user.id}
242-
|> Billing.subscription_created()
243-
244-
assert user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
245-
end
246-
247212
test "unlocks sites if user has any locked sites" do
248213
user = new_user()
249214
site = new_site(owner: user, locked: true)

test/plausible_web/controllers/api/paddle_controller_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ defmodule PlausibleWeb.Api.PaddleControllerTest do
3333
test "is verified when signature is correct", %{conn: conn} do
3434
insert(:user, id: 235)
3535

36-
conn = post(conn, Routes.paddle_path(conn, :webhook), @webhook_body)
37-
assert conn.status == 200
36+
# NOTE: signature check happens sooner
37+
assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
38+
post(conn, Routes.paddle_path(conn, :webhook), @webhook_body)
39+
end
3840
end
3941

4042
test "not verified when signature is corrupted", %{conn: conn} do

test/plausible_web/live/sites_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ defmodule PlausibleWeb.Live.SitesTest do
151151

152152
{:ok, lv, _html} = live(conn, "/sites")
153153

154-
type_into_input(lv, "filter_text", "firs")
154+
type_into_input(lv, "filter_text", "first")
155155
html = render(lv)
156156

157157
assert html =~ "first.example.com"

0 commit comments

Comments
 (0)