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
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# A method an app defines wins over what the plugin and graphql-ruby decide: `authorized?` and `authorizes?`
|
|
8
|
+
# can skip a rule, and graphql-ruby calls `unauthorized_object` on whatever loads a record, so returning the
|
|
9
|
+
# object runs the code on a refused record. The audit cannot read them: each is reported once, where it is
|
|
10
|
+
# defined, until it is declared. Methods that come from graphql-ruby or from this gem are not the app's.
|
|
11
|
+
class OwnChecks
|
|
12
|
+
def self.call(schema, definitions, findings)
|
|
13
|
+
new(definitions, findings).(schema.armory_configuration.own_checks)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(definitions = [], findings = Findings.new)
|
|
17
|
+
@definitions = definitions
|
|
18
|
+
@findings = findings
|
|
19
|
+
@found = {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(declared)
|
|
23
|
+
@definitions.each do |type|
|
|
24
|
+
next unless type.kind.object? && !Definitions.introspection?(type) && type.respond_to?(:armory_rule)
|
|
25
|
+
|
|
26
|
+
%i[authorized? authorizes? authorized_new wrap].each { |name| note_class_check(type, name) }
|
|
27
|
+
note_class_check(type, :visible?) if type.armory_rule&.visible_when
|
|
28
|
+
Definitions.fields_at_every_level(type).each { |field| note_field_checks(field) }
|
|
29
|
+
end
|
|
30
|
+
@definitions.each do |type|
|
|
31
|
+
next if Definitions.introspection?(type) || !type.respond_to?(:scope_items) || !(type.kind.object? || type.kind.interface? || type.kind.union?)
|
|
32
|
+
|
|
33
|
+
note_class_check(type, :scope_items)
|
|
34
|
+
note_reauthorization(type)
|
|
35
|
+
end
|
|
36
|
+
@definitions.each do |type|
|
|
37
|
+
next unless type.kind.input_object? && !Definitions.introspection?(type)
|
|
38
|
+
|
|
39
|
+
%i[authorized? unauthorized_object].each { |name| note_class_check(type, name) }
|
|
40
|
+
note_argument_checks(type.all_argument_definitions)
|
|
41
|
+
end
|
|
42
|
+
report(declared)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def note_field_checks(field)
|
|
48
|
+
%i[authorized? authorizes? unauthorized_object].each { |name| note_instance_check(field.class, name) }
|
|
49
|
+
note_visibility_check(field)
|
|
50
|
+
note_argument_checks(field.all_argument_definitions)
|
|
51
|
+
resolver = field.resolver
|
|
52
|
+
return unless resolver
|
|
53
|
+
|
|
54
|
+
note_instance_check(resolver, :unauthorized_object)
|
|
55
|
+
note_class_check(resolver, :authorizes?)
|
|
56
|
+
note_instance_check(resolver, :authorized?) if resolver.respond_to?(:armory_rule)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def note_argument_checks(arguments)
|
|
60
|
+
arguments.each do |argument|
|
|
61
|
+
note_instance_check(argument.class, :authorized?)
|
|
62
|
+
note_visibility_check(argument)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Off, records that came through the scope skip `authorized?`: harmless only if the scope is as strict as the rule.
|
|
67
|
+
# The interpreter asks the field's return type, Execution::Next the resolved object, and each path differs, so it
|
|
68
|
+
# is reported wherever it is off, without judging whether it skips anything: declaring it takes one line.
|
|
69
|
+
def note_reauthorization(type)
|
|
70
|
+
readers = type.is_a?(Class) ? [type] : type.ancestors.select { |ancestor| ancestor.respond_to?(:reauthorize_scoped_objects, true) }
|
|
71
|
+
readers.each { |reader| note_class_check(reader, :reauthorize_scoped_objects) }
|
|
72
|
+
|
|
73
|
+
setter = reauthorization_setter(type)
|
|
74
|
+
@found[[setter, :reauthorize_scoped_objects]] = "#{setter.name || setter.inspect}.reauthorize_scoped_objects" if setter&.instance_variable_get(:@reauthorize_scoped_objects) == false
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The nearest that set the value, read from the classes so no method of the app runs: graphql-ruby's reader follows
|
|
78
|
+
# superclasses, not the interfaces a class implements. A connection reads only its own, then its node's, which
|
|
79
|
+
# `Definitions` reaches and the audit reads as a type of its own.
|
|
80
|
+
def reauthorization_setter(type)
|
|
81
|
+
return type if Definitions.connection?(type)
|
|
82
|
+
|
|
83
|
+
chain = type.is_a?(Class) ? type.ancestors.grep(Class) : type.ancestors
|
|
84
|
+
chain.find { |ancestor| !ancestor.instance_variable_get(:@reauthorize_scoped_objects).nil? }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Apps define `visible?` for their own reasons: it only matters where a `visible_when:` depends on it.
|
|
88
|
+
def note_visibility_check(member)
|
|
89
|
+
note_instance_check(member.class, :visible?) if member.respond_to?(:armory_visible_when) && member.armory_visible_when
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def note_instance_check(klass, name)
|
|
93
|
+
return unless klass.method_defined?(name) || klass.private_method_defined?(name)
|
|
94
|
+
|
|
95
|
+
note_own_check(klass, klass.instance_method(name), name, "#")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def note_class_check(klass, name)
|
|
99
|
+
return unless klass.respond_to?(name, true)
|
|
100
|
+
|
|
101
|
+
note_own_check(klass, klass.method(name), name, ".")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Follows the whole chain: a method that only calls super must not hide one below it that answers true.
|
|
105
|
+
def note_own_check(klass, method, name, separator)
|
|
106
|
+
while method
|
|
107
|
+
holder = LibraryMethods.holder_of(klass, method)
|
|
108
|
+
@found[[holder, name]] = "#{holder.name || holder.inspect}#{separator}#{name}" unless LibraryMethods.library_method?(method, holder, GRAPHQL_RUBY_PATH, ARMORY_PATH)
|
|
109
|
+
method = method.super_method
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def report(declared)
|
|
114
|
+
@found.each do |(holder, name), path|
|
|
115
|
+
reason = declared.fetch(holder, {})[name]
|
|
116
|
+
next @findings.bypass(path, reason) if reason
|
|
117
|
+
|
|
118
|
+
@findings.problem(:own_check, path, "#{risk(name)}. Remove it, or declare it in own_checks: with a reason")
|
|
119
|
+
end
|
|
120
|
+
declared.each do |holder, methods|
|
|
121
|
+
methods.each_key do |name|
|
|
122
|
+
next if @found.key?([holder, name])
|
|
123
|
+
|
|
124
|
+
@findings.problem(:own_check, "#{holder.name || holder.inspect} #{name}", "named in own_checks: but no check of the schema is defined there: remove it from the option")
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def risk(name)
|
|
130
|
+
case name
|
|
131
|
+
when :unauthorized_object then "defined by the app: if it returns the object, the code runs on the refused record, and a message it raises reaches the client"
|
|
132
|
+
when :authorizes? then "defined by the app: if it answers false, Execution::Next does not run authorized?, so the rule is skipped"
|
|
133
|
+
when :authorized_new, :wrap then "defined by the app: it decides whether authorized? runs, so on the interpreter it can skip the rule"
|
|
134
|
+
when :scope_items then "defined by the app: it replaces the user's scope, so a list can hold records the policy's scope excludes, and connections count them"
|
|
135
|
+
when :reauthorize_scoped_objects then "set to false: records that come through the scope skip the rule, so a user the rule refuses gets them"
|
|
136
|
+
when :visible? then "defined by the app: if it does not call super, visible_when: does not hide this from a user it should"
|
|
137
|
+
else "defined by the app, it wins over the plugin's: if it does not call super it can skip the rule"
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# An object type's rule, and what its fields, resolvers and arguments declare under it.
|
|
8
|
+
class TypeRules
|
|
9
|
+
def initialize(adapter = Adapters::Null.new, findings = Findings.new, foreign_policies = ForeignPolicies.new(adapter, findings))
|
|
10
|
+
@adapter = adapter
|
|
11
|
+
@findings = findings
|
|
12
|
+
@foreign_policies = foreign_policies
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(type)
|
|
16
|
+
path = type.graphql_name
|
|
17
|
+
unless type.respond_to?(:armory_rule)
|
|
18
|
+
@findings.problem(:unprotected, path, "its base class does not include GraphQL::Armory::Authorization::Base, so it is open")
|
|
19
|
+
return
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
rule = type.armory_rule
|
|
23
|
+
if rule.nil?
|
|
24
|
+
@findings.problem(:no_rule, path, "has no rule and no default_action, so it is denied")
|
|
25
|
+
elsif rule.visible_when && (answer = graphql_ruby_answer(type, :visible?))
|
|
26
|
+
@findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers visible? here, so visible_when: never runs: remove it")
|
|
27
|
+
elsif rule.none?
|
|
28
|
+
@findings.bypass(path, rule.reason)
|
|
29
|
+
audit_fields(type, model: nil, bypassed: !rule.covers_fields?, reported: Set.new)
|
|
30
|
+
@foreign_policies.(type, rule.policy) if rule.policy
|
|
31
|
+
elsif (answer = graphql_ruby_answer(type, :authorized?))
|
|
32
|
+
@findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers authorized? here, so this rule never runs: " \
|
|
33
|
+
"put the rule on the field that returns it (on a root field, on a resolver: a field rule asks " \
|
|
34
|
+
"the parent's policy), and declare `authorize :none, reason: \"...\", covers_fields: true` on this class")
|
|
35
|
+
else
|
|
36
|
+
model = model_of(type, path)
|
|
37
|
+
reported = Set.new
|
|
38
|
+
check(path, model, rule, reported) if model
|
|
39
|
+
check(path, model, Rule.new(rule.visible_when, policy: rule.policy), reported) if model && rule.visible_when
|
|
40
|
+
@foreign_policies.(type, rule.policy) if rule.policy
|
|
41
|
+
audit_fields(type, model: model, bypassed: false, reported: reported)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# ConnectionBehaviors and EdgeBehaviors, included after Base, bring an `authorized?` and a `visible?` that win
|
|
48
|
+
# over the plugin's. An app's method may call super, so this follows the chain to the first library that answers.
|
|
49
|
+
def graphql_ruby_answer(type, name)
|
|
50
|
+
method = type.method(name)
|
|
51
|
+
until method.nil? || method.owner == Integration::Object
|
|
52
|
+
return method.owner if LibraryMethods.library_method?(method, LibraryMethods.holder_of(type, method), GRAPHQL_RUBY_PATH)
|
|
53
|
+
|
|
54
|
+
method = method.super_method
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# A field an implementer inherits from an interface is reported on the interface.
|
|
59
|
+
def interface_field?(field)
|
|
60
|
+
field.owner.respond_to?(:kind) && field.owner.kind.interface?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def model_of(type, path)
|
|
64
|
+
type.armory_model
|
|
65
|
+
rescue ConfigurationError => error
|
|
66
|
+
@findings.problem(:model, path, error.message)
|
|
67
|
+
nil
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# An adapter problem shows once per type, however many fields declare an action of their own.
|
|
71
|
+
def check(path, model, rule, reported)
|
|
72
|
+
@adapter.check(model, rule).each do |message|
|
|
73
|
+
@findings.problem(:adapter, path, message) if reported.add?(message)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Every definition, the shadowed ones included: a hidden override falls back to them, so they serve requests too.
|
|
78
|
+
# Nothing here loads a field's type, so a shadowed field whose type cannot load raises nothing.
|
|
79
|
+
def audit_fields(type, model:, bypassed:, reported:)
|
|
80
|
+
Definitions.fields_at_every_level(type).each do |field|
|
|
81
|
+
audit_field(type, field, "#{type.graphql_name}.#{field.original_name}", model: model, bypassed: bypassed, reported: reported)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def audit_field(type, field, path, model:, bypassed:, reported:)
|
|
86
|
+
audit_scope(type, field, path)
|
|
87
|
+
audit_loads(field, path)
|
|
88
|
+
# An inherited interface field is reported as uncovered on its interface; every other check still runs here.
|
|
89
|
+
inherited = bypassed && interface_field?(field)
|
|
90
|
+
if bypassed && !inherited && !field.respond_to?(:armory_declared)
|
|
91
|
+
return @findings.problem(:field_without_rule, path, "its field class does not include Base, so no rule runs on it: its type is bypassed and graphql-ruby's own " \
|
|
92
|
+
"authorized? answers true, so the field is served with no check. Use a field class that includes Base")
|
|
93
|
+
end
|
|
94
|
+
return unless field.respond_to?(:armory_declared)
|
|
95
|
+
return audit_resolver(field, path) if field.resolver.respond_to?(:armory_rule)
|
|
96
|
+
|
|
97
|
+
audit_declaration(type, field, path, model: model, bypassed: bypassed && !inherited, reported: reported)
|
|
98
|
+
audit_inherited_rule(type, field, path, model) if model
|
|
99
|
+
audit_visibility(field, path, model: model, reported: reported)
|
|
100
|
+
audit_arguments(field.all_argument_definitions, path, reported: reported)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# A field runs under its owner: graphql-ruby never tells it the concrete type. A rule defined above that type asks
|
|
104
|
+
# the owner's model, or only the record's class, while this audit reads the type's model.
|
|
105
|
+
def audit_inherited_rule(type, field, path, model)
|
|
106
|
+
return if field.owner.equal?(type) || [nil, :none].include?(field.armory_declared) || field.armory_policy
|
|
107
|
+
return if field.owner.respond_to?(:armory_model) && owner_model(field.owner).equal?(model)
|
|
108
|
+
|
|
109
|
+
@findings.problem(:foreign_model, path, "its rule is defined on #{name_of(field.owner)}, so it never asks the policy of #{model}, the model of this type: a record of another class " \
|
|
110
|
+
"or a laxer subclass gets past it. Redefine the field on this type with its `authorize:` and an `override_reason:`, or name the same model on #{name_of(field.owner)}")
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def owner_model(owner)
|
|
114
|
+
owner.armory_model
|
|
115
|
+
rescue ConfigurationError, GraphQL::RequiredImplementationMissingError
|
|
116
|
+
nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def name_of(owner)
|
|
120
|
+
owner.respond_to?(:graphql_name) ? owner.graphql_name : owner.inspect
|
|
121
|
+
rescue GraphQL::RequiredImplementationMissingError
|
|
122
|
+
owner.inspect
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A resolver or mutation carries its own rule: no rule is denied, `:none` is listed, an action is checked.
|
|
126
|
+
def audit_resolver(field, path)
|
|
127
|
+
resolver = field.resolver
|
|
128
|
+
rule = resolver.armory_rule
|
|
129
|
+
# A RelayClassicMutation's field holds only the generated `input`: its arguments are on the resolver.
|
|
130
|
+
arguments = (field.all_argument_definitions + resolver.all_argument_definitions).uniq
|
|
131
|
+
@foreign_policies.arguments(arguments, path, rule.policy, Set.new) if rule&.policy
|
|
132
|
+
return @findings.problem(:no_rule, path, "its mutation or resolver declares no `authorize`, so it is denied") if rule.nil?
|
|
133
|
+
audit_arguments(arguments, path, reported: Set.new)
|
|
134
|
+
return @findings.bypass(path, rule.reason) if rule.none?
|
|
135
|
+
|
|
136
|
+
model = begin
|
|
137
|
+
resolver.armory_model
|
|
138
|
+
rescue ConfigurationError => error
|
|
139
|
+
return @findings.problem(:model, path, error.message)
|
|
140
|
+
end
|
|
141
|
+
check(path, model, rule, Set.new)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The action a field is hidden with is asked of the policy like any other, so it must exist.
|
|
145
|
+
def audit_visibility(field, path, model:, reported:)
|
|
146
|
+
return unless model && field.armory_visible_when
|
|
147
|
+
|
|
148
|
+
check(path.split(".").first, model, Rule.new(field.armory_visible_when, policy: field.armory_policy, attribute: field.original_name), reported)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def audit_declaration(type, field, path, model:, bypassed:, reported:)
|
|
152
|
+
case (declared = field.armory_declared)
|
|
153
|
+
when :none
|
|
154
|
+
@findings.bypass(path, field.armory_reason)
|
|
155
|
+
when nil, true
|
|
156
|
+
if bypassed && declared.nil? && served_by_owner?(type, field)
|
|
157
|
+
@findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, but it is inherited from #{field.owner.name || field.owner.inspect}: a field runs " \
|
|
158
|
+
"under the class that defines it, and no rule ran on this type, so the field is served with no check. Give it authorize: where it is defined, or remove it")
|
|
159
|
+
elsif bypassed
|
|
160
|
+
@findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, so it is denied")
|
|
161
|
+
end
|
|
162
|
+
else
|
|
163
|
+
check(path.split(".").first, model, Rule.new(declared, policy: field.armory_policy), reported) if model
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# A field runs under its owner's rule, not the resolved type's. Inherited by a bypassed type from a parent whose
|
|
168
|
+
# rule covers it, or that knows no rule at all, graphql-ruby serves it after a type check that checked nothing.
|
|
169
|
+
def served_by_owner?(type, field)
|
|
170
|
+
owner = field.owner
|
|
171
|
+
return false if owner.equal?(type) || !owner.is_a?(Class)
|
|
172
|
+
return true unless owner.respond_to?(:armory_rule)
|
|
173
|
+
|
|
174
|
+
rule = owner.armory_rule
|
|
175
|
+
!rule.nil? && (!rule.none? || rule.covers_fields?)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# An argument that declares a rule is listed (a bypass with its reason) or checked like a field's action.
|
|
179
|
+
def audit_arguments(arguments, path, reported:)
|
|
180
|
+
arguments.each do |argument|
|
|
181
|
+
next unless argument.respond_to?(:armory_declared)
|
|
182
|
+
|
|
183
|
+
apath = "#{path}(#{argument.keyword})"
|
|
184
|
+
declared = argument.armory_declared
|
|
185
|
+
@findings.bypass(apath, argument.armory_reason) if declared == :none
|
|
186
|
+
[declared, argument.armory_visible_when].compact.reject { |action| action == :none }.each do |action|
|
|
187
|
+
audit_argument_action(argument, apath, action, reported)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def audit_argument_action(argument, path, action, reported)
|
|
193
|
+
check(path.split(".").first, argument.armory_model, Rule.new(action, policy: argument.armory_policy, attribute: argument.keyword), reported)
|
|
194
|
+
rescue ConfigurationError => error
|
|
195
|
+
@findings.problem(:model, path, error.message)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# A list that skips the scope hands over every record: a rule on each one hides its fields, not how many there are.
|
|
199
|
+
def audit_scope(type, field, path)
|
|
200
|
+
return if field.scoped? || generated_list?(type, field) || !list_of_records?(field)
|
|
201
|
+
|
|
202
|
+
@findings.problem(:unscoped_list, path, "skips the user's scope, with scope: false or a list type graphql-ruby does not detect, so a refused user sees how many records it holds: " \
|
|
203
|
+
"remove the option, or write the type as [Type]")
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# A connection applies the scope on the field that returns it, and graphql-ruby generates these two with scope: false.
|
|
207
|
+
# A hand-written one of the same name returns something else or reads another method.
|
|
208
|
+
def generated_list?(type, field)
|
|
209
|
+
owner = field.owner
|
|
210
|
+
return false unless Definitions.connection?(owner) && %i[edges nodes].include?(field.original_name)
|
|
211
|
+
|
|
212
|
+
element = field.type.unwrap
|
|
213
|
+
element = element.node_type if field.original_name == :edges && element.is_a?(Class) && element.include?(GraphQL::Types::Relay::EdgeBehaviors)
|
|
214
|
+
field.resolver.nil? && field.resolver_method == field.original_name && field.method_sym == field.original_name &&
|
|
215
|
+
field.dig_keys.nil? && element.equal?(owner.node_type) &&
|
|
216
|
+
type.instance_method(field.original_name).owner == GraphQL::Types::Relay::ConnectionBehaviors
|
|
217
|
+
rescue GraphQL::Error
|
|
218
|
+
false
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def list_of_records?(field)
|
|
222
|
+
type = field.type
|
|
223
|
+
connection = Definitions.connection?(type.unwrap)
|
|
224
|
+
return false unless connection || field.connection? || (type.respond_to?(:list?) && type.list?)
|
|
225
|
+
|
|
226
|
+
# A late-bound element has no kind yet: it may be a record.
|
|
227
|
+
element = (type.unwrap.node_type if connection) || type.unwrap
|
|
228
|
+
!(element.respond_to?(:kind) && element.kind.leaf?)
|
|
229
|
+
rescue GraphQL::Error
|
|
230
|
+
false
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Whoever owns an argument loads it: the field, or its resolver. Only Base owners go through the scope.
|
|
234
|
+
def audit_loads(field, path)
|
|
235
|
+
arguments = field.all_argument_definitions.reject do |argument|
|
|
236
|
+
argument.owner.equal?(field) ? scoped_field?(field) : field.resolver.respond_to?(:armory_rule)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
opted_out_loads(arguments, Set.new).each do |name|
|
|
240
|
+
@findings.problem(:unscoped_load, "#{path}(#{name})", "loads through the app's object_from_id, not through the user's scope")
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def scoped_field?(field)
|
|
245
|
+
field.respond_to?(:armory_load_through_scope?) && field.armory_load_through_scope?
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Execution::Next loads `loads:` nested in input objects with the field's loader, so a field's opt-out
|
|
249
|
+
# reaches them too: list them as `filter.invoice_id`. `seen` stops recursive input types.
|
|
250
|
+
def opted_out_loads(arguments, seen, prefix = nil)
|
|
251
|
+
arguments.flat_map do |argument|
|
|
252
|
+
name = [prefix, GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)].compact.join(".")
|
|
253
|
+
next [name] if argument.loads
|
|
254
|
+
|
|
255
|
+
input = argument.type.unwrap
|
|
256
|
+
next [] unless input.respond_to?(:kind) && input.kind.input_object? && seen.add?(input)
|
|
257
|
+
|
|
258
|
+
opted_out_loads(input.all_argument_definitions, seen, name)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
class Audit
|
|
7
|
+
# A `visible_when:` is asked of a model at request time, so one that cannot name it raises there. Listing the
|
|
8
|
+
# schema hits it only once, so every location is read here, with no request and no policy.
|
|
9
|
+
class VisibilityModels
|
|
10
|
+
def self.call(schema, definitions, findings)
|
|
11
|
+
new(findings).(schema, definitions)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def initialize(findings = Findings.new)
|
|
15
|
+
@findings = findings
|
|
16
|
+
@seen = Set.new.compare_by_identity
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(schema, definitions)
|
|
20
|
+
definitions.each do |type|
|
|
21
|
+
next if Definitions.introspection?(type)
|
|
22
|
+
|
|
23
|
+
note_type(type)
|
|
24
|
+
note_arguments(type.graphql_name, type.all_argument_definitions) if type.kind.input_object?
|
|
25
|
+
Definitions.fields_at_every_level(type).each do |field|
|
|
26
|
+
# With Schema::Visibility a request never falls back to a shadowed definition, so it never asks it.
|
|
27
|
+
next if schema.visibility && Definitions.shadowed?(type, field)
|
|
28
|
+
|
|
29
|
+
path = "#{path_owner(type, field)}.#{field.original_name}"
|
|
30
|
+
note_field(field, path)
|
|
31
|
+
note_arguments(path, field.all_argument_definitions)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
schema.directives.each_value { |directive| note_arguments("@#{directive.graphql_name}", directive.all_argument_definitions) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def note_type(type)
|
|
40
|
+
return unless type.respond_to?(:armory_rule) && type.armory_rule&.visible_when && @seen.add?(type)
|
|
41
|
+
|
|
42
|
+
report(type.graphql_name, model_problem { type.armory_model })
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def note_field(field, path)
|
|
46
|
+
return unless field.respond_to?(:armory_visible_when) && field.armory_visible_when && @seen.add?(field)
|
|
47
|
+
|
|
48
|
+
problem = if field.owner.respond_to?(:armory_model)
|
|
49
|
+
model_problem { field.owner.armory_model }
|
|
50
|
+
else
|
|
51
|
+
"uses visible_when:, but what defines it cannot name a model, so a request that reads it raises: put it on an object type that includes Base"
|
|
52
|
+
end
|
|
53
|
+
report(path, problem)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def note_arguments(path, arguments)
|
|
57
|
+
arguments.each do |argument|
|
|
58
|
+
next unless argument.respond_to?(:armory_visible_when) && argument.armory_visible_when && @seen.add?(argument)
|
|
59
|
+
|
|
60
|
+
report("#{path}(#{argument.keyword})", model_problem { argument.armory_model })
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# An interface's field is reported on the interface, whichever implementer the walk reaches first.
|
|
65
|
+
def path_owner(type, field)
|
|
66
|
+
field.owner.respond_to?(:kind) && field.owner.kind.interface? ? field.owner.graphql_name : type.graphql_name
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def report(path, problem)
|
|
70
|
+
@findings.problem(:model, path, problem) if problem && !@findings.problem?(:model, path)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def model_problem
|
|
74
|
+
yield
|
|
75
|
+
nil
|
|
76
|
+
rescue ConfigurationError, GraphQL::RequiredImplementationMissingError => error
|
|
77
|
+
error.message
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|