graphql-armory 0.1.0.pre6 → 0.1.0.pre8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +43 -1
- data/README.md +6 -5
- data/docs/authorization.md +133 -6
- data/docs/keyset-connections.md +95 -0
- data/docs/persisted-queries.md +16 -0
- data/docs/rate-limiting.md +20 -0
- data/docs/telemetry.md +1 -0
- data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
- data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
- data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
- data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
- data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
- data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
- data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
- data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
- data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
- data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
- data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
- data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
- data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
- data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
- data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
- data/lib/graphql/armory/authorization/audit.rb +50 -191
- data/lib/graphql/armory/authorization/authorize.rb +9 -3
- data/lib/graphql/armory/authorization/configuration.rb +30 -2
- data/lib/graphql/armory/authorization/definitions.rb +114 -0
- data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
- data/lib/graphql/armory/authorization/integration/field.rb +13 -5
- data/lib/graphql/armory/authorization/integration/object.rb +3 -1
- data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
- data/lib/graphql/armory/authorization/integration.rb +22 -1
- data/lib/graphql/armory/authorization/rule.rb +5 -0
- data/lib/graphql/armory/authorization.rb +4 -1
- data/lib/graphql/armory/keyset/controls.rb +36 -0
- data/lib/graphql/armory/keyset.rb +279 -0
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory.rb +1 -0
- metadata +17 -1
|
@@ -8,7 +8,7 @@ module GraphQL
|
|
|
8
8
|
module Adapters
|
|
9
9
|
# Answers the core's four questions with Pundit's public API.
|
|
10
10
|
#
|
|
11
|
-
# The policy is found from the model through Pundit::PolicyFinder, so `policy_class`
|
|
11
|
+
# The policy is found from the model's class through Pundit::PolicyFinder, so `self.policy_class`
|
|
12
12
|
# overrides and namespaced models work. A policy must answer `true`, `false` or `nil`:
|
|
13
13
|
# anything else (a lazy, a record) is handed back as is and the core refuses it.
|
|
14
14
|
class Pundit
|
|
@@ -17,19 +17,21 @@ module GraphQL
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def authorized?(user, subject, rule)
|
|
20
|
-
policy = policy_class(rule, subject).new(user, subject)
|
|
21
20
|
query = query_for(rule)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
policies_for(rule, subject).each do |policy_class|
|
|
22
|
+
policy = policy_class.new(user, subject)
|
|
23
|
+
raise AdapterError, "#{policy.class} does not define #{query}" unless policy.respond_to?(query)
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
answer = policy.public_send(query)
|
|
26
|
+
return answer unless true.equal?(answer)
|
|
27
|
+
end
|
|
28
|
+
true
|
|
27
29
|
end
|
|
28
30
|
|
|
29
31
|
def scope(user, items, rule)
|
|
30
|
-
return items.select { |item| authorized?(user, item, rule)
|
|
32
|
+
return items.select { |item| true.equal?(authorized?(user, item, rule)) } if items.is_a?(Array)
|
|
31
33
|
|
|
32
|
-
policy = policy_class(rule, rule.model || items)
|
|
34
|
+
policy = policy_class(rule, normalize(rule.model) || record_target(items))
|
|
33
35
|
scope_class = scope_constant(policy)
|
|
34
36
|
raise MissingScopeError, "#{policy} has no Scope class" if scope_class.nil?
|
|
35
37
|
|
|
@@ -50,10 +52,65 @@ module GraphQL
|
|
|
50
52
|
problems
|
|
51
53
|
end
|
|
52
54
|
|
|
55
|
+
# The policy a rule that names none asks about this model.
|
|
56
|
+
def found_policy(model)
|
|
57
|
+
finder(normalize(model)).policy
|
|
58
|
+
end
|
|
59
|
+
|
|
53
60
|
private
|
|
54
61
|
|
|
62
|
+
# A rule's `policy:` answers alone. Otherwise the declared model's policy, which is the one the audit reads, and
|
|
63
|
+
# the record's class's must both allow it: one alone lets a record of another class, or a subclass with a laxer
|
|
64
|
+
# policy, past the first, or a subclass past its own stricter policy.
|
|
65
|
+
def policies_for(rule, subject)
|
|
66
|
+
return [rule.policy] if rule.policy
|
|
67
|
+
|
|
68
|
+
model = normalize(rule.model) if rule.model
|
|
69
|
+
declared = finder(model).policy if model
|
|
70
|
+
target = model_named?(subject, model) ? subject : record_target(subject)
|
|
71
|
+
own = finder(target).policy
|
|
72
|
+
return [declared, own].compact.uniq if own
|
|
73
|
+
return [declared] if declared && kind_of_model?(subject, model)
|
|
74
|
+
|
|
75
|
+
raise ::Pundit::NotDefinedError, "no policy found for #{subject_name(target)}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# A Symbol names a policy only when it is the `model:` the schema author wrote: any other is a record, maybe the
|
|
79
|
+
# client's text, as a String is.
|
|
80
|
+
def model_named?(subject, model)
|
|
81
|
+
Symbol === subject && subject.equal?(model)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def record_target(subject)
|
|
85
|
+
Symbol === subject ? ::Symbol : policy_subject(subject)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Only a record of the model inherits its policy: anything else with no policy of its own raises, as it always did.
|
|
89
|
+
def kind_of_model?(subject, model)
|
|
90
|
+
return false unless model.is_a?(Module)
|
|
91
|
+
|
|
92
|
+
target = policy_subject(subject)
|
|
93
|
+
target.is_a?(Module) ? target <= model : model === target
|
|
94
|
+
end
|
|
95
|
+
|
|
55
96
|
def policy_class(rule, subject)
|
|
56
|
-
rule.policy || finder(subject).policy
|
|
97
|
+
rule.policy || finder(policy_subject(subject)).policy || raise(::Pundit::NotDefinedError, "no policy found for #{subject_name(subject)}")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Pundit asks the object for `policy_class` and `model_name`, and reads an Array as a namespace. A record's
|
|
101
|
+
# answers can be the client's data (an OpenStruct from JSON), so only its class is asked. A relation's class
|
|
102
|
+
# names no model, so its `klass` is read, but only on a real relation: `klass` on anything else is an answer too.
|
|
103
|
+
def policy_subject(subject)
|
|
104
|
+
return subject if Module === subject || Symbol === subject
|
|
105
|
+
return subject.klass if defined?(::ActiveRecord::Relation) && ::ActiveRecord::Relation === subject
|
|
106
|
+
|
|
107
|
+
::Kernel.instance_method(:class).bind_call(subject)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Never `inspect`: Pundit's own message quotes the record, and on a relation it runs the query.
|
|
111
|
+
# Nor the record's own `class` or `is_a?`: on a Struct or OpenStruct, `class` can be a data member.
|
|
112
|
+
def subject_name(subject)
|
|
113
|
+
Module === subject || Symbol === subject ? subject : ::Kernel.instance_method(:class).bind_call(subject)
|
|
57
114
|
end
|
|
58
115
|
|
|
59
116
|
def query_for(rule)
|
|
@@ -61,12 +118,13 @@ module GraphQL
|
|
|
61
118
|
end
|
|
62
119
|
|
|
63
120
|
def finder(subject)
|
|
64
|
-
::Pundit::PolicyFinder.new(
|
|
121
|
+
::Pundit::PolicyFinder.new(subject)
|
|
65
122
|
end
|
|
66
123
|
|
|
67
|
-
# Pundit reads a Symbol as a model name but a String as a plain object.
|
|
68
|
-
|
|
69
|
-
|
|
124
|
+
# Pundit reads a Symbol as a model name but a String as a plain object. Only for a model the schema author
|
|
125
|
+
# wrote: a String a type wraps is a record, maybe the client's text, and must not name its own policy.
|
|
126
|
+
def normalize(model)
|
|
127
|
+
model.is_a?(String) ? model.to_sym : model
|
|
70
128
|
end
|
|
71
129
|
|
|
72
130
|
# Looks the Scope up through the policy's ancestors but never in Object, so a
|
|
@@ -8,12 +8,13 @@ module GraphQL
|
|
|
8
8
|
class Substitute
|
|
9
9
|
attr_reader :asked, :scoped_for
|
|
10
10
|
|
|
11
|
-
def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil)
|
|
11
|
+
def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil, raises_on_check: nil)
|
|
12
12
|
@allow = allow
|
|
13
13
|
@actor = actor
|
|
14
14
|
@scoped = scoped
|
|
15
15
|
@raises = raises
|
|
16
16
|
@raises_on_scope = raises_on_scope
|
|
17
|
+
@raises_on_check = raises_on_check
|
|
17
18
|
@asked = []
|
|
18
19
|
@scoped_for = []
|
|
19
20
|
end
|
|
@@ -37,6 +38,8 @@ module GraphQL
|
|
|
37
38
|
end
|
|
38
39
|
|
|
39
40
|
def check(_type, _rule)
|
|
41
|
+
raise @raises_on_check if @raises_on_check
|
|
42
|
+
|
|
40
43
|
[]
|
|
41
44
|
end
|
|
42
45
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# A method the schema defines on itself wins over the plugin's, so `on_unauthorized:` no longer decides
|
|
8
|
+
# what that denial looks like. The check runs here because `use` usually comes before those methods.
|
|
9
|
+
module DenialHooks
|
|
10
|
+
def self.call(schema, findings)
|
|
11
|
+
declared = schema.armory_configuration.own_denial_hooks
|
|
12
|
+
Configuration::DENIAL_HOOKS.each do |hook|
|
|
13
|
+
path = "Schema.#{hook}"
|
|
14
|
+
own = schema.method(hook).owner != Integration::Schema
|
|
15
|
+
if own && declared.key?(hook)
|
|
16
|
+
findings.bypass(path, declared[hook])
|
|
17
|
+
elsif own
|
|
18
|
+
findings.problem(:hook, path, "the schema's own method runs instead of the plugin's, so on_unauthorized: does not apply and a denial " \
|
|
19
|
+
"may reveal what it hides: remove it, or declare it in own_denial_hooks: with a reason")
|
|
20
|
+
elsif declared.key?(hook)
|
|
21
|
+
findings.problem(:hook, path, "named in own_denial_hooks: but the plugin's hook is the one that runs: remove it from the option")
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# A direct edge subclass runs the plugin's check, but only where an edge is built: `nodes` never builds one.
|
|
8
|
+
module EdgeRules
|
|
9
|
+
def self.call(definitions, findings)
|
|
10
|
+
definitions.each do |type|
|
|
11
|
+
next unless type.kind.object? && type <= GraphQL::Types::Relay::BaseEdge
|
|
12
|
+
|
|
13
|
+
rule = type.armory_rule if type.respond_to?(:armory_rule)
|
|
14
|
+
next if rule.nil? || rule.none? || !nodes_field_beside?(type, definitions)
|
|
15
|
+
|
|
16
|
+
findings.problem(:ignored_rule, type.graphql_name, "the connection's nodes field returns the nodes without building an edge, so this rule does not " \
|
|
17
|
+
"cover it: put the rule on the field that returns the connection (on a root field, on a resolver: " \
|
|
18
|
+
"a field rule asks the parent's policy), or remove the nodes field with has_nodes_field(false)")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.nodes_field_beside?(edge, definitions)
|
|
23
|
+
definitions.any? do |type|
|
|
24
|
+
next false unless type.kind.object? && !Definitions.introspection?(type)
|
|
25
|
+
|
|
26
|
+
fields = type.all_field_definitions
|
|
27
|
+
fields.any? { |field| field.graphql_name == "nodes" } &&
|
|
28
|
+
fields.any? { |field| field.graphql_name == "edges" && field.type.unwrap == edge }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
private_class_method :nodes_field_beside?
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# Collects what one audit finds.
|
|
8
|
+
class Findings
|
|
9
|
+
def initialize
|
|
10
|
+
@problems = []
|
|
11
|
+
@bypasses = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def problem(kind, path, message)
|
|
15
|
+
@problems << Report::Problem.new(kind, path, message)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def problem?(kind, path = nil)
|
|
19
|
+
@problems.any? { |problem| problem.kind == kind && (path.nil? || problem.path == path) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def bypass(path, reason)
|
|
23
|
+
@bypasses << Report::Bypass.new(path, reason)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def report
|
|
27
|
+
Report.new(problems: @problems.sort_by { |problem| [problem.path, problem.kind.to_s] },
|
|
28
|
+
bypasses: @bypasses.sort_by(&:path))
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# A rule asks the policy of the class that defines it, and a field never sees its resolved type: a rule on an
|
|
8
|
+
# interface, a parent, an input object or a plain resolver does not ask the `policy:` of the type that uses it.
|
|
9
|
+
class ForeignPolicies
|
|
10
|
+
def initialize(adapter = Adapters::Null.new, findings = Findings.new)
|
|
11
|
+
@adapter = adapter
|
|
12
|
+
@findings = findings
|
|
13
|
+
@seen = Set.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call(type, policy)
|
|
17
|
+
Definitions.fields_at_every_level(type).each do |field|
|
|
18
|
+
path = "#{type.graphql_name}.#{field.original_name}"
|
|
19
|
+
note(field, path, policy, model_or_nil(type)) if field.respond_to?(:armory_declared)
|
|
20
|
+
note_resolver(field.resolver, path, policy) if field.resolver.respond_to?(:armory_rule)
|
|
21
|
+
arguments(field.all_argument_definitions, path, policy, Set.new)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A connection or edge is not audited as a type, but a field rule on it runs under the same terms.
|
|
26
|
+
def relay(type)
|
|
27
|
+
policy = type.armory_rule&.policy if type.respond_to?(:armory_rule)
|
|
28
|
+
call(type, policy) if policy
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# A class without Base has no rule of its own, but what it leads to still runs its rules. An argument, a Base
|
|
32
|
+
# resolver's included, asks the policy of the class that defines it, not of the type that uses it.
|
|
33
|
+
def arguments(arguments, path, policy, seen)
|
|
34
|
+
arguments.each do |argument|
|
|
35
|
+
if argument.respond_to?(:armory_declared)
|
|
36
|
+
note(argument, "#{path}(#{argument.keyword})", policy, model_or_nil(argument))
|
|
37
|
+
end
|
|
38
|
+
input = argument.type.unwrap
|
|
39
|
+
next unless input.respond_to?(:kind) && input.kind.input_object? && seen.add?(input)
|
|
40
|
+
|
|
41
|
+
arguments(input.all_argument_definitions, path, policy, seen)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# A Base resolver covers its field and asks its own policy, so the type's `policy:` never sees that rule.
|
|
48
|
+
def note_resolver(resolver, path, policy)
|
|
49
|
+
rule = resolver.armory_rule
|
|
50
|
+
return if rule.nil? || rule.none?
|
|
51
|
+
|
|
52
|
+
asked = rule.policy
|
|
53
|
+
same = asked ? asked.equal?(policy) : found?(model_or_nil(resolver), policy)
|
|
54
|
+
return if same || !@seen.add?([resolver, path, policy])
|
|
55
|
+
|
|
56
|
+
@findings.problem(:foreign_policy, path, "its resolver's rule asks #{asked || "the policy found from its model"}, not #{policy}, the `policy:` of this type. " \
|
|
57
|
+
"Name the same `policy:` on the resolver")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def note(member, path, policy, model)
|
|
61
|
+
ruled = member.armory_visible_when || ![nil, :none].include?(member.armory_declared)
|
|
62
|
+
return if !ruled || asks_policy?(member, policy, model) || !@seen.add?([member, path, policy])
|
|
63
|
+
|
|
64
|
+
asked = member.armory_policy || "the policy found from the record"
|
|
65
|
+
@findings.problem(:foreign_policy, path, "its rule asks #{asked}, not #{policy}, the `policy:` of this type: it is defined on a class that does not name that policy. " \
|
|
66
|
+
"Move the rule to the class that names it and remove the original, or name the `policy:` on the class that defines the rule, if it can hold one")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# With no `policy:` of its own, a rule asks what the adapter finds from the model, which may be the same policy.
|
|
70
|
+
def asks_policy?(member, policy, model)
|
|
71
|
+
asked = member.armory_policy
|
|
72
|
+
return asked.equal?(policy) if asked
|
|
73
|
+
|
|
74
|
+
found?(model, policy)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def found?(model, policy)
|
|
78
|
+
!model.nil? && @adapter.respond_to?(:found_policy) && @adapter.found_policy(model).equal?(policy)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def model_or_nil(owner)
|
|
82
|
+
owner.armory_model
|
|
83
|
+
rescue ConfigurationError
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# An input object loads its own `loads:` arguments; without Base they skip the user's scope.
|
|
8
|
+
module InputLoads
|
|
9
|
+
def self.call(definitions, findings)
|
|
10
|
+
definitions.each do |input|
|
|
11
|
+
next unless input.kind.input_object? && !Definitions.introspection?(input)
|
|
12
|
+
next if input.respond_to?(:armory_scoped_loads?)
|
|
13
|
+
|
|
14
|
+
# A mutation's generated input (`from_resolver`) is loaded, scoped, by the mutation itself.
|
|
15
|
+
input.all_argument_definitions.select(&:loads).reject(&:from_resolver?).each do |argument|
|
|
16
|
+
name = GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)
|
|
17
|
+
findings.problem(:unscoped_load, "#{input.graphql_name}(#{name})",
|
|
18
|
+
"its input object class does not include Base, so it loads through the app's object_from_id, not the user's scope")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# Interface fields are reported once, on the interface that declares them, not on every implementer.
|
|
8
|
+
# Undeclared ones are denied when an implementer is a bypass; ones whose field class lacks Base are
|
|
9
|
+
# never checked, so on a bypassed implementer they are open; `authorize: true` names no action.
|
|
10
|
+
module InterfaceRules
|
|
11
|
+
def self.call(definitions, findings)
|
|
12
|
+
definitions.each do |type|
|
|
13
|
+
audit(type, definitions, findings) if type.kind.interface? && !Definitions.introspection?(type)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.audit(interface, definitions, findings)
|
|
18
|
+
bypassed = Integration.any_bypass?(definitions.select { |type| type.kind.object? && type <= interface })
|
|
19
|
+
interface.own_fields.values.flatten.each do |field|
|
|
20
|
+
path = "#{interface.graphql_name}.#{field.original_name}"
|
|
21
|
+
unless field.respond_to?(:armory_declared)
|
|
22
|
+
findings.problem(:unprotected, path, "its field class does not include Base, so it is not checked on a bypassed implementer") if bypassed
|
|
23
|
+
next
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
case field.armory_declared
|
|
27
|
+
when true
|
|
28
|
+
findings.problem(:field_without_rule, path, "uses authorize: true, but an interface has no action of its own, so it is always denied")
|
|
29
|
+
when nil
|
|
30
|
+
findings.problem(:field_without_rule, path, "declares no rule and an implementing type is bypassed, so it is denied") if bypassed
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
private_class_method :audit
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# `Schema.introspection` can add or redefine entry points and dynamic fields. A client reaches them from any type,
|
|
8
|
+
# with no type of the app around them, so no type's rule runs. The built-in ones hold no data of the app.
|
|
9
|
+
class IntrospectionFields
|
|
10
|
+
BUILT_IN = { EntryPoints: :entry_points, DynamicFields: :dynamic_fields }.freeze
|
|
11
|
+
|
|
12
|
+
def self.call(schema, findings)
|
|
13
|
+
new(findings).(schema)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(findings = Findings.new)
|
|
17
|
+
@findings = findings
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(schema)
|
|
21
|
+
system = schema.introspection_system
|
|
22
|
+
BUILT_IN.each do |holder, reader|
|
|
23
|
+
system.public_send(reader).each { |field| note(field, GraphQL::Introspection.const_get(holder)) }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# graphql-ruby serves a copy of the holder's fields, on a copy of the holder.
|
|
30
|
+
def note(field, built_in)
|
|
31
|
+
name = field.graphql_name
|
|
32
|
+
return if field.owner.superclass.fields[name].equal?(built_in.fields[name])
|
|
33
|
+
|
|
34
|
+
@findings.problem(:unprotected, "#{field.owner.graphql_name}.#{name}", "is a custom introspection field, so no type's rule runs on it: move it to a type with a rule")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# Tells a library's method from an app's.
|
|
8
|
+
module LibraryMethods
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def holder_of(klass, method)
|
|
12
|
+
owner = method.owner
|
|
13
|
+
klass.ancestors.find { |ancestor| ancestor.singleton_class == owner } || owner
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def library_method?(method, holder, *paths)
|
|
17
|
+
file = method.source_location&.first
|
|
18
|
+
!file.nil? && file.start_with?(*paths) && holder.name&.start_with?("GraphQL::") == true
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# With no Schema::Visibility, a field hidden from a user falls back to another definition of its name, losing its
|
|
8
|
+
# rule, arguments, return type and extensions. Many things can hide it (the field, its resolver, its return type,
|
|
9
|
+
# the schema) and only a request knows, so every redefinition is reported until it declares `override_reason:`.
|
|
10
|
+
class Overrides
|
|
11
|
+
def self.call(schema, definitions, findings)
|
|
12
|
+
new(definitions, findings).(schema)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(definitions = [], findings = Findings.new)
|
|
16
|
+
@definitions = definitions
|
|
17
|
+
@findings = findings
|
|
18
|
+
@override_paths = {}.compare_by_identity
|
|
19
|
+
@falling_back = {}.compare_by_identity
|
|
20
|
+
@duplicates = Set.new.compare_by_identity
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(schema)
|
|
24
|
+
@definitions.each do |type|
|
|
25
|
+
next if Definitions.introspection?(type)
|
|
26
|
+
|
|
27
|
+
note_redefinitions(type.graphql_name, type.ancestors, :own_fields, :original_name)
|
|
28
|
+
note_redefinitions(type.graphql_name, type.ancestors, :own_arguments, :graphql_name) if type.kind.input_object?
|
|
29
|
+
Definitions.fields_at_every_level(type).each do |field|
|
|
30
|
+
levels = [field] + (field.resolver ? field.resolver.ancestors : [])
|
|
31
|
+
note_redefinitions("#{type.graphql_name}.#{field.original_name}", levels, :own_arguments, :graphql_name)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
no_fallback = visibility_without_fallback?(schema)
|
|
35
|
+
@override_paths.each do |member, path|
|
|
36
|
+
falls_back = @falling_back.key?(member) && (!no_fallback || @duplicates.include?(member))
|
|
37
|
+
note_override(falls_back ? @falling_back[member] : path, member, falls_back)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# Nearest level first; only a lone definition on the farthest level has nothing to fall back to. Duplicates on
|
|
44
|
+
# one level are noted apart: a visibility profile skips the levels below, but still picks one of those.
|
|
45
|
+
def note_redefinitions(prefix, ancestors, reader, name_reader)
|
|
46
|
+
levels = ancestors.select { |ancestor| ancestor.respond_to?(reader) }
|
|
47
|
+
levels.flat_map { |level| level.public_send(reader).keys }.uniq.each do |name|
|
|
48
|
+
definitions = levels.filter_map { |level| [level.public_send(reader)[name]].flatten if level.public_send(reader).key?(name) }
|
|
49
|
+
definitions.flatten.each { |member| @override_paths[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
|
|
50
|
+
falling = definitions.last.size == 1 ? definitions.flatten[0...-1] : definitions.flatten
|
|
51
|
+
falling.each { |member| @falling_back[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
|
|
52
|
+
definitions.select { |level| level.size > 1 }.flatten.each { |member| @duplicates << member }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# graphql-ruby skips the fallback to a lower level only when the request carries a visibility profile. A static
|
|
57
|
+
# profile runs with a plain Hash, so only `Schema::Visibility` with no profiles, not turned off below, rules it out.
|
|
58
|
+
def visibility_without_fallback?(schema)
|
|
59
|
+
visibility = schema.visibility
|
|
60
|
+
return false unless visibility
|
|
61
|
+
return false if schema.respond_to?(:use_visibility_profile?) && !schema.use_visibility_profile?
|
|
62
|
+
|
|
63
|
+
visibility.instance_variable_get(:@profiles).to_h.empty?
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def note_override(path, member, falls_back)
|
|
67
|
+
reason = member.armory_override_reason if member.respond_to?(:armory_override_reason)
|
|
68
|
+
if falls_back && reason
|
|
69
|
+
@findings.bypass(path, reason)
|
|
70
|
+
elsif falls_back
|
|
71
|
+
@findings.problem(:ignored_rule, path, "is defined more than once: hidden from a user, it falls back to the other definition, and its own rule, arguments, type and extensions do not apply. " \
|
|
72
|
+
"Remove one or declare override_reason:; for a definition on an ancestor, Schema::Visibility with dynamic: true and no profiles also stops the fallback")
|
|
73
|
+
elsif reason
|
|
74
|
+
@findings.problem(:ignored_rule, path, "declares override_reason: but nothing falls back from it: remove it")
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|