Skip to content

Commit 21d7c60

Browse files
joshknshoes
andauthored
Remove controller and live view param whitelisting (#2381)
Ecto Changesets are the right way to do whitelisting, so this isn't needed. (Built upon #2380) --------- Co-authored-by: Nate Shoemaker <[email protected]>
1 parent 54628ed commit 21d7c60

File tree

4 files changed

+13
-40
lines changed

4 files changed

+13
-40
lines changed

lib/nerves_hub_web.ex

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ defmodule NervesHubWeb do
3939

4040
# Routes generation with the ~p sigil
4141
unquote(verified_routes())
42-
43-
def whitelist(params, keys) do
44-
keys
45-
|> Enum.filter(fn x -> !is_nil(params[to_string(x)]) end)
46-
|> Map.new(fn x -> {x, params[to_string(x)]} end)
47-
end
4842
end
4943
end
5044

@@ -66,12 +60,6 @@ defmodule NervesHubWeb do
6660
unquote(verified_routes())
6761

6862
action_fallback(NervesHubWeb.API.FallbackController)
69-
70-
def whitelist(params, keys) do
71-
keys
72-
|> Enum.filter(fn x -> !is_nil(params[to_string(x)]) end)
73-
|> Map.new(fn x -> {x, params[to_string(x)]} end)
74-
end
7563
end
7664
end
7765

@@ -127,12 +115,6 @@ defmodule NervesHubWeb do
127115
|> assign(:tab_hint, tab)
128116
end
129117

130-
def whitelist(params, keys) do
131-
keys
132-
|> Enum.filter(fn x -> !is_nil(params[to_string(x)]) end)
133-
|> Map.new(fn x -> {x, params[to_string(x)]} end)
134-
end
135-
136118
def analytics_enabled?(), do: Application.get_env(:nerves_hub, :analytics_enabled)
137119

138120
unquote(tab_component_functions())

lib/nerves_hub_web/controllers/api/deployment_group_controller.ex

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ defmodule NervesHubWeb.API.DeploymentGroupController do
55
alias NervesHub.AuditLogs.DeploymentGroupTemplates
66
alias NervesHub.Firmwares
77
alias NervesHub.ManagedDeployments
8-
alias NervesHub.ManagedDeployments.DeploymentGroup
98

109
security([%{}, %{"bearer_auth" => []}])
1110
tags(["Deployment Groups"])
1211

1312
plug(:validate_role, [org: :manage] when action in [:create, :update, :delete])
1413
plug(:validate_role, [org: :view] when action in [:index, :show])
1514

16-
@whitelist_fields [:name, :org_id, :firmware_id, :conditions, :is_active, :product_id]
17-
1815
operation(:index, summary: "List all Deployment Groups for a Product")
1916

2017
def index(%{assigns: %{product: product}} = conn, _params) do
@@ -68,13 +65,9 @@ defmodule NervesHubWeb.API.DeploymentGroupController do
6865
}) do
6966
with {:ok, deployment_group} <-
7067
ManagedDeployments.get_deployment_group_by_name(product, name),
71-
{:ok, deployment_group_params} <- update_params(product, deployment_group_params),
72-
deployment_group_params = whitelist(deployment_group_params, @whitelist_fields),
73-
{:ok, %DeploymentGroup{} = updated_deployment_group} <-
74-
ManagedDeployments.update_deployment_group(
75-
deployment_group,
76-
deployment_group_params
77-
) do
68+
params = update_params(product, deployment_group_params),
69+
{:ok, updated_deployment_group} <-
70+
ManagedDeployments.update_deployment_group(deployment_group, params) do
7871
DeploymentGroupTemplates.audit_deployment_updated(user, deployment_group)
7972

8073
render(conn, :show, deployment_group: updated_deployment_group)
@@ -95,10 +88,6 @@ defmodule NervesHubWeb.API.DeploymentGroupController do
9588
params
9689
|> maybe_active_from_state()
9790
|> maybe_firmware_id(product)
98-
|> case do
99-
%{} = params -> {:ok, params}
100-
err -> err
101-
end
10291
end
10392

10493
defp maybe_active_from_state(%{"state" => state} = params) do
@@ -109,8 +98,12 @@ defmodule NervesHubWeb.API.DeploymentGroupController do
10998
defp maybe_active_from_state(params), do: params
11099

111100
defp maybe_firmware_id(%{"firmware" => uuid} = params, product) do
112-
with {:ok, firmware} <- Firmwares.get_firmware_by_product_and_uuid(product, uuid) do
113-
Map.put(params, "firmware_id", firmware.id)
101+
case Firmwares.get_firmware_by_product_and_uuid(product, uuid) do
102+
{:ok, firmware} ->
103+
Map.put(params, "firmware_id", firmware.id)
104+
105+
_ ->
106+
params
114107
end
115108
end
116109

lib/nerves_hub_web/controllers/api/firmware_controller.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ defmodule NervesHubWeb.API.FirmwareController do
2323
operation(:create, summary: "Upload a Firmware for a Product")
2424

2525
def create(%{assigns: %{org: org, product: product}} = conn, params) do
26-
params = whitelist(params, [:firmware])
27-
2826
Logger.info("System Memory:" <> inspect(:memsup.get_system_memory_data()))
2927

30-
with {%{path: filepath}, _params} <- Map.pop(params, :firmware),
28+
with {%{path: filepath}, _params} <- Map.pop(params, "firmware"),
3129
{:ok, firmware} <- Firmwares.create_firmware(org, filepath) do
3230
firmware = Repo.preload(firmware, :product)
3331

lib/nerves_hub_web/live/orgs/new.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ defmodule NervesHubWeb.Live.Orgs.New do
1515
end
1616

1717
def handle_event("save_org", %{"org" => org_params}, socket) do
18-
params = org_params |> whitelist([:name])
19-
20-
case Accounts.create_org(socket.assigns.user, params) do
18+
socket.assigns.user
19+
|> Accounts.create_org(org_params)
20+
|> case do
2121
{:ok, org} ->
2222
{:noreply,
2323
socket

0 commit comments

Comments
 (0)