Skip to content

Commit 8f37dee

Browse files
committed
fix: don't accept private arguments over JSON:API endpoints
closes #427
1 parent 130c648 commit 8f37dee

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

lib/ash_json_api/request.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,12 @@ defmodule AshJsonApi.Request do
11501150

11511151
Enum.reduce(attributes, request, fn {key, value}, request ->
11521152
case Enum.find(action.arguments, fn argument ->
1153-
AshJsonApi.Resource.Info.apply_argument_name_mapping(
1154-
arg_names,
1155-
action.name,
1156-
argument.name
1157-
) == key
1153+
argument.public? &&
1154+
AshJsonApi.Resource.Info.apply_argument_name_mapping(
1155+
arg_names,
1156+
action.name,
1157+
argument.name
1158+
) == key
11581159
end) do
11591160
nil ->
11601161
request
@@ -1177,8 +1178,9 @@ defmodule AshJsonApi.Request do
11771178
matching_argument =
11781179
Enum.find(
11791180
action.arguments,
1180-
&(AshJsonApi.Resource.Info.apply_argument_name_mapping(arg_names, action.name, &1.name) ==
1181-
key)
1181+
&(&1.public? &&
1182+
AshJsonApi.Resource.Info.apply_argument_name_mapping(arg_names, action.name, &1.name) ==
1183+
key)
11821184
)
11831185

11841186
matching_accept =

test/acceptance/patch_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ defmodule Test.Acceptance.PatchTest do
204204
route "/forbidden_update/:id"
205205
end
206206

207+
patch :private_arg_update do
208+
route "/private_arg_update/:id"
209+
end
210+
207211
related :author, :read
208212
patch_relationship :author
209213
end
@@ -241,6 +245,14 @@ defmodule Test.Acceptance.PatchTest do
241245
end)
242246
end
243247

248+
update :private_arg_update do
249+
accept([:name])
250+
251+
argument :email, :string do
252+
public?(false)
253+
end
254+
end
255+
244256
action :forbidden_update, :struct do
245257
constraints(instance_of: __MODULE__)
246258
argument(:id, :uuid, allow_nil?: false)
@@ -435,6 +447,22 @@ defmodule Test.Acceptance.PatchTest do
435447
})
436448
end
437449

450+
test "public?: false arguments on update actions cannot be set via PATCH", %{post: post} do
451+
response =
452+
Domain
453+
|> patch(
454+
"/posts/private_arg_update/#{post.id}",
455+
%{
456+
data: %{attributes: %{email: "should_not_work@test.com"}}
457+
},
458+
status: 422
459+
)
460+
461+
assert Enum.any?(response.resp_body["errors"], fn error ->
462+
error["code"] == "no_such_input"
463+
end)
464+
end
465+
438466
@tag :attributes
439467
test "private attributes are not rendered in the payload", %{post: post} do
440468
Domain

0 commit comments

Comments
 (0)