-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a7d294
commit 15add2b
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module GraphQL | ||
# Detects types that implement Node interface and not have `.authorized?` check. | ||
# Such types can be fetched by ID and therefore should have type level check to | ||
# avoid accidental information exposure. | ||
# | ||
# @example | ||
# # good | ||
# | ||
# class UserType < BaseType | ||
# implements GraphQL::Types::Relay::Node | ||
# | ||
# field :uuid, ID, null: false | ||
# | ||
# def self.authorized?(object, context) | ||
# super && object.owner == context[:viewer] | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# | ||
# class UserType < BaseType | ||
# implements GraphQL::Types::Relay::Node | ||
# | ||
# field :uuid, ID, null: false | ||
# end | ||
# | ||
class NotAuthorizedNodeType < Base | ||
MSG = ".authorized? should be defined for types implementing Node interface." | ||
|
||
# @!method implements_node_type?(node) | ||
def_node_matcher :implements_node_type?, <<~PATTERN | ||
`(send nil? :implements | ||
(const | ||
(const | ||
(const | ||
(const nil? :GraphQL) :Types) :Relay) :Node)) | ||
PATTERN | ||
|
||
# @!method has_authorized_method?(node) | ||
def_node_matcher :has_authorized_method?, <<~PATTERN | ||
`(:defs (:self) :authorized? ...) | ||
PATTERN | ||
|
||
def on_class(node) | ||
add_offense(node) if implements_node_type?(node) && !has_authorized_method?(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::GraphQL::NotAuthorizedNodeType, :config do | ||
context "when type not implements Node interface" do | ||
specify do | ||
expect_no_offenses(<<~RUBY, "graphql/types/user_type.rb") | ||
class UserType < BaseType | ||
field :uuid, ID, null: false | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when type not implements Node interface" do | ||
context "when type has .authorized? check" do | ||
specify do | ||
expect_no_offenses(<<~RUBY, "graphql/types/user_type.rb") | ||
class UserType < BaseType | ||
implements GraphQL::Types::Relay::Node | ||
field :uuid, ID, null: false | ||
def self.authorized?(object, context) | ||
super && object.owner == context[:viewer] | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when type has no .authorized? check" do | ||
specify do | ||
expect_offense(<<~RUBY, "graphql/types/user_type.rb") | ||
class UserType < BaseType | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ .authorized? should be defined for types implementing Node interface. | ||
implements GraphQL::Types::Relay::Node | ||
field :uuid, ID, null: false | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |