Skip to content

Commit 961cb86

Browse files
committed
Authorize arguments of input objects, too
1 parent 3c0dfc0 commit 961cb86

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/graphql/schema/argument.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,17 @@ def accessible?(context)
9494
end
9595

9696
def authorized?(obj, ctx)
97-
true
97+
arg_type = type.unwrap
98+
if arg_type.kind.input_object? && arg_type != @owner
99+
arg_type.arguments.each do |_name, input_obj_arg|
100+
if !input_obj_arg.authorized?(obj, ctx)
101+
return false
102+
end
103+
end
104+
true
105+
else
106+
true
107+
end
98108
end
99109

100110
def to_graphql

spec/graphql/authorization_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ def authorized?(parent_object, context)
2424
end
2525
end
2626

27+
class BaseInputObjectArgument < BaseArgument
28+
def authorized?(parent_object, context)
29+
super && parent_object != :hide3
30+
end
31+
end
32+
33+
class BaseInputObject < GraphQL::Schema::InputObject
34+
argument_class BaseInputObjectArgument
35+
end
36+
2737
class BaseField < GraphQL::Schema::Field
2838
def initialize(*args, edge_class: nil, **kwargs, &block)
2939
@edge_class = edge_class
@@ -243,6 +253,11 @@ class LandscapeFeature < BaseEnum
243253
value "TAR_PIT", role: :hidden
244254
end
245255

256+
class AddInput < BaseInputObject
257+
argument :left, Integer, required: true
258+
argument :right, Integer, required: true
259+
end
260+
246261
class Query < BaseObject
247262
def self.authorized?(obj, ctx)
248263
!ctx[:query_unauthorized]
@@ -347,6 +362,14 @@ def lazy_integers
347362
def replaced_object
348363
Replaceable.new
349364
end
365+
366+
field :add_inputs, Integer, null: true do
367+
argument :input, AddInput, required: true
368+
end
369+
370+
def add_inputs(input:)
371+
input[:left] + input[:right]
372+
end
350373
end
351374

352375
class DoHiddenStuff < GraphQL::Schema::RelayClassicMutation
@@ -737,6 +760,21 @@ def auth_execute(*args)
737760
assert_equal 5, visible_response["data"]["int2"]
738761
end
739762

763+
it "halts on unauthorized input object arguments, using the parent object" do
764+
query = "{ addInputs(input: { left: 3, right: 2 }) }"
765+
hidden_field_argument_response = auth_execute(query, root_value: :hide2)
766+
assert_nil hidden_field_argument_response["data"].fetch("addInputs")
767+
assert_equal ["Unauthorized Query: :hide2"], hidden_field_argument_response["errors"].map { |e| e["message"] }
768+
769+
hidden_input_obj_argument_response = auth_execute(query, root_value: :hide3)
770+
assert_nil hidden_input_obj_argument_response["data"].fetch("addInputs")
771+
assert_equal ["Unauthorized Query: :hide3"], hidden_input_obj_argument_response["errors"].map { |e| e["message"] }
772+
773+
visible_response = auth_execute(query)
774+
assert_equal 5, visible_response["data"]["addInputs"]
775+
refute visible_response.key?("errors")
776+
end
777+
740778
it "works with edges and connections" do
741779
query = <<-GRAPHQL
742780
{

0 commit comments

Comments
 (0)