Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Proposal: add inaccessible_field feature #1769

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/graphql/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ def final_value(memo)
end
context = memo[:context]
err = InaccessibleFieldsError.new(fields: fields, irep_nodes: nodes, context: context)
context.schema.inaccessible_fields(err)

# TODO: fix
if context.schema.respond_to?(:inaccessible_field)
context[:inaccessible_nodes] = memo[:inaccessible_nodes]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be added to the context regardless. The only conditional thing should be skipping inaccessible_fields.

else
context.schema.inaccessible_fields(err)
end
else
nil
end
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/schema/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ def authorized?(object, context)
#
# Eventually, we might hook up field instances to execution in another way. TBD.
def resolve_field(obj, args, ctx)
if ctx[:inaccessible_nodes] && ctx[:inaccessible_nodes].include?(ctx.irep_node)
ctx.schema.inaccessible_field(self)
end

ctx.schema.after_lazy(obj) do |after_obj|
# First, apply auth ...
query_ctx = ctx.query.context
Expand Down
24 changes: 24 additions & 0 deletions spec/graphql/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,30 @@ def auth_execute(*args)
end
end

# TODO: fix
describe "applying the accessible? method when inaccessible_field is defined on schema" do
class AuthTest::Schema
def self.inaccessible_field(field)
raise GraphQL::ExecutionError.new("#{field.owner.graphql_name}.#{field.graphql_name} is not accessible")
end
end

it "works with fields and arguments" do
queries = {
"{ inaccessible }" => ["Query.inaccessible is not accessible"],
"{ int2(inaccessible: 1) }" => ["Query.int2 is not accessible"],
}

queries.each do |query_str, errors|
res = auth_execute(query_str, context: { hide: true })
assert_equal errors, res.fetch("errors").map { |e| e["message"] }

res = auth_execute(query_str, context: { hide: false })
refute res.key?("errors")
end
end
end

describe "applying the authorized? method" do
it "halts on unauthorized objects" do
query = "{ unauthorizedObject { __typename } }"
Expand Down