Skip to content

Commit e506b13

Browse files
Validate group inputs on invites
Prevents a very difficult but theoretically possible insertion of a group id into invite requests
1 parent 3c68779 commit e506b13

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

apps/core/lib/core/services/accounts.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ defmodule Core.Services.Accounts do
290290
|> allow(user, :create)
291291
|> when_ok(:insert)
292292
end)
293+
|> add_operation(:validate, fn %{invite: invite} ->
294+
with %Invite{groups: [_ | _] = groups} <- Core.Repo.preload(invite, [:groups]),
295+
true <- Enum.any?(groups, & &1.account_id != aid) do
296+
{:error, "you cannot invite users to groups in other accounts"}
297+
else
298+
_ -> {:ok, invite}
299+
end
300+
end)
293301
|> execute(extract: :invite)
294302
|> notify(:create, user)
295303
end

apps/core/lib/core/services/base.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Core.Services.Base do
1818
_ -> []
1919
end
2020
end
21+
def find_bindings(%User{id: id}), do: [%{user_id: id}]
2122
def find_bindings(_), do: []
2223

2324
def ok(val), do: {:ok, val}

apps/core/test/services/accounts_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ defmodule Core.Services.AccountsTest do
338338
assert invite.account_id == account.id
339339
end
340340

341+
test "you can invite users into an accounts groups", %{user: user, account: account} do
342+
group = insert(:group, account: account)
343+
{:ok, invite} = Accounts.create_invite(%{
344+
345+
invite_groups: [%{group_id: group.id}]
346+
}, user)
347+
348+
assert invite.email == "[email protected]"
349+
assert invite.secure_id
350+
assert invite.account_id == account.id
351+
352+
%{groups: [%{id: id}]} = Core.Repo.preload(invite, [:groups])
353+
assert id == group.id
354+
end
355+
356+
test "you cannot invite users into another account's groups", %{user: user} do
357+
group = insert(:group)
358+
{:error, _} = Accounts.create_invite(%{
359+
360+
invite_groups: [%{group_id: group.id}]
361+
}, user)
362+
end
363+
341364
test "it will not accept invalid emails", %{user: user} do
342365
{:error, _} = Accounts.create_invite(%{email: "invalidemail"}, user)
343366
end

apps/graphql/test/mutations/repository_mutation_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ defmodule GraphQl.RepositoryMutationsTest do
337337
assert provider["redirectUris"] == ["example.com"]
338338
assert provider["configuration"]["issuer"] == "https://oidc.plural.sh/"
339339

340-
[%{"group" => g}] = provider["bindings"]
341-
assert g["id"] == group.id
340+
assert Enum.any?(provider["bindings"], & get_in(&1, ["group", "id"]) == group.id)
341+
assert Enum.any?(provider["bindings"], & get_in(&1, ["user", "id"]) == installation.user.id)
342342
end
343343

344344
test "it will create an user-bound oidc provider" do
@@ -385,8 +385,8 @@ defmodule GraphQl.RepositoryMutationsTest do
385385
assert provider["redirectUris"] == ["example.com"]
386386
assert provider["configuration"]["issuer"] == "https://oidc.plural.sh/"
387387

388-
[%{"group" => g}] = provider["bindings"]
389-
assert g["id"] == group.id
388+
assert Enum.any?(provider["bindings"], & get_in(&1, ["group", "id"]) == group.id)
389+
assert Enum.any?(provider["bindings"], & get_in(&1, ["user", "id"]) == user.id)
390390
end
391391
end
392392

0 commit comments

Comments
 (0)