graphql-armory 0.1.0.pre7 → 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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -1
  3. data/README.md +5 -4
  4. data/docs/authorization.md +133 -6
  5. data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
  6. data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
  7. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  8. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  9. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  10. data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
  11. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  12. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  13. data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
  14. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  15. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  16. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  17. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  18. data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
  19. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  20. data/lib/graphql/armory/authorization/audit.rb +50 -191
  21. data/lib/graphql/armory/authorization/authorize.rb +9 -3
  22. data/lib/graphql/armory/authorization/configuration.rb +30 -2
  23. data/lib/graphql/armory/authorization/definitions.rb +114 -0
  24. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  25. data/lib/graphql/armory/authorization/integration/field.rb +13 -5
  26. data/lib/graphql/armory/authorization/integration/object.rb +1 -1
  27. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  28. data/lib/graphql/armory/authorization/integration.rb +22 -1
  29. data/lib/graphql/armory/authorization/rule.rb +5 -0
  30. data/lib/graphql/armory/authorization.rb +4 -1
  31. data/lib/graphql/armory/version.rb +1 -1
  32. metadata +14 -1
@@ -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
@@ -3,6 +3,18 @@
3
3
  require "set"
4
4
  require "graphql/armory/authorization/audit/report"
5
5
  require "graphql/armory/authorization/audit/command"
6
+ require "graphql/armory/authorization/audit/findings"
7
+ require "graphql/armory/authorization/audit/library_methods"
8
+ require "graphql/armory/authorization/audit/denial_hooks"
9
+ require "graphql/armory/authorization/audit/own_checks"
10
+ require "graphql/armory/authorization/audit/overrides"
11
+ require "graphql/armory/authorization/audit/foreign_policies"
12
+ require "graphql/armory/authorization/audit/type_rules"
13
+ require "graphql/armory/authorization/audit/edge_rules"
14
+ require "graphql/armory/authorization/audit/interface_rules"
15
+ require "graphql/armory/authorization/audit/input_loads"
16
+ require "graphql/armory/authorization/audit/visibility_models"
17
+ require "graphql/armory/authorization/audit/introspection_fields"
6
18
 
7
19
  module GraphQL
8
20
  module Armory
@@ -10,9 +22,18 @@ module GraphQL
10
22
  # Walks a schema, without running a query, and reports everything that is not covered:
11
23
  # types nobody protected, types without a rule or a policy, fields with no rule on a bypassed
12
24
  # type, and `loads:` that skip the scope. Every declared bypass is listed with its reason.
25
+ #
26
+ # Each check is an object of its own, built for one run, so one Audit can serve several threads.
13
27
  class Audit
14
28
  RELAY_TYPES = [GraphQL::Types::Relay::BaseConnection, GraphQL::Types::Relay::BaseEdge,
15
29
  GraphQL::Types::Relay::PageInfo].freeze
30
+ GENERATED_RELAY_FIELDS = { GraphQL::Types::Relay::BaseConnection => %i[edges nodes page_info],
31
+ GraphQL::Types::Relay::BaseEdge => %i[node cursor] }.freeze
32
+
33
+ # A library's method is defined in its files and held by a class it names. Either alone misleads: anyone can name
34
+ # a module under `GraphQL::`, and an alias keeps graphql-ruby's location on an app's class.
35
+ GRAPHQL_RUBY_PATH = "#{File.dirname(GraphQL::Schema.method(:from_definition).source_location.first)}/"
36
+ ARMORY_PATH = "#{File.expand_path('..', __dir__)}/"
16
37
 
17
38
  attr_reader :adapter
18
39
 
@@ -32,210 +53,48 @@ module GraphQL
32
53
 
33
54
  def call(schema)
34
55
  findings = Findings.new
35
- unless schema.respond_to?(:armory_configuration) && schema.armory_configuration
56
+ listing_error = nil
57
+ definitions = Definitions.(schema) { |error| listing_error = error }
58
+ if schema.respond_to?(:armory_configuration) && schema.armory_configuration
59
+ DenialHooks.(schema, findings)
60
+ OwnChecks.(schema, definitions, findings)
61
+ Overrides.(schema, definitions, findings)
62
+ else
36
63
  findings.problem(:plugin, "Schema", "never installed the plugin: add `use GraphQL::Armory::Authorization, adapter: ...`")
37
64
  end
38
65
 
39
- audited_types(schema).each { |type| audit_type(type, findings) }
40
- schema.types.each_value { |type| audit_interface(type, schema, findings) if type.kind.interface? && !type.introspection? }
41
- schema.types.each_value { |type| audit_input_loads(type, findings) if type.kind.input_object? && !type.introspection? }
66
+ foreign_policies = ForeignPolicies.new(adapter, findings)
67
+ type_rules = TypeRules.new(adapter, findings, foreign_policies)
68
+ audited_types(definitions).each { |type| type_rules.(type) }
69
+ definitions.each { |type| foreign_policies.relay(type) if type.kind.object? && RELAY_TYPES.any? { |relay| type <= relay } }
70
+ EdgeRules.(definitions, findings)
71
+ InterfaceRules.(definitions, findings)
72
+ InputLoads.(definitions, findings)
73
+ VisibilityModels.(schema, definitions, findings)
74
+ IntrospectionFields.(schema, findings)
75
+ findings.problem(:model, "Schema", listing_error.message) if listing_error && !findings.problem?(:model)
42
76
  findings.report
43
77
  end
44
78
 
45
79
  private
46
80
 
47
- # Walks `schema.types`, which a schema that hides types with `visible?` would filter.
48
- def audited_types(schema)
49
- schema.types.values.select do |type|
50
- type.kind.object? && !type.introspection? && RELAY_TYPES.none? { |relay| type <= relay }
51
- end
81
+ # PageInfo holds no data of the app, until a subclass, its parent or an interface adds a field.
82
+ def extended_page_info?(type)
83
+ type < GraphQL::Types::Relay::PageInfo &&
84
+ Definitions.fields_at_every_level(type).any? { |field| !field.owner.equal?(GraphQL::Types::Relay::PageInfo) }
52
85
  end
53
86
 
54
- def audit_type(type, findings)
55
- path = type.graphql_name
56
- unless type.respond_to?(:armory_rule)
57
- findings.problem(:unprotected, path, "its base class does not include GraphQL::Armory::Authorization::Base, so it is open")
58
- return
59
- end
60
-
61
- rule = type.armory_rule
62
- if rule.nil?
63
- findings.problem(:no_rule, path, "has no rule and no default_action, so it is denied")
64
- elsif rule.none?
65
- findings.bypass(path, rule.reason)
66
- audit_fields(type, findings, model: nil, bypassed: !rule.covers_fields?, reported: Set.new)
67
- else
68
- model = model_of(type, path, findings)
69
- reported = Set.new
70
- check(path, model, rule, findings, reported) if model
71
- check(path, model, Rule.new(rule.visible_when, policy: rule.policy), findings, reported) if model && rule.visible_when
72
- audit_fields(type, findings, model: model, bypassed: false, reported: reported)
87
+ # A connection or an edge holds only what graphql-ruby generates, until a subclass, its parent or an interface adds a field.
88
+ def extended_relay?(type)
89
+ GENERATED_RELAY_FIELDS.any? do |relay, names|
90
+ type < relay && Definitions.fields_at_every_level(type).any? { |field| !names.include?(field.original_name) }
73
91
  end
74
92
  end
75
93
 
76
- # Interface fields are reported once, on the interface that declares them, not on every implementer.
77
- # Undeclared ones are denied when an implementer is a bypass; ones whose field class lacks Base are
78
- # never checked, so on a bypassed implementer they are open; `authorize: true` names no action.
79
- def audit_interface(interface, schema, findings)
80
- bypassed = Integration.bypassed_implementer?(interface, schema)
81
- interface.own_fields.each_value do |field|
82
- path = "#{interface.graphql_name}.#{field.original_name}"
83
- unless field.respond_to?(:armory_declared)
84
- findings.problem(:unprotected, path, "its field class does not include Base, so it is not checked on a bypassed implementer") if bypassed
85
- next
86
- end
87
-
88
- case field.armory_declared
89
- when true
90
- findings.problem(:field_without_rule, path, "uses authorize: true, but an interface has no action of its own, so it is always denied")
91
- when nil
92
- findings.problem(:field_without_rule, path, "declares no rule and an implementing type is bypassed, so it is denied") if bypassed
93
- end
94
- end
95
- end
96
-
97
- # A field an implementer inherits from an interface is reported on the interface.
98
- def interface_field?(field)
99
- field.owner.respond_to?(:kind) && field.owner.kind.interface?
100
- end
101
-
102
- def model_of(type, path, findings)
103
- type.armory_model
104
- rescue ConfigurationError => error
105
- findings.problem(:model, path, error.message)
106
- nil
107
- end
108
-
109
- # An adapter problem shows once per type, however many fields declare an action of their own.
110
- def check(path, model, rule, findings, reported)
111
- adapter.check(model, rule).each do |message|
112
- findings.problem(:adapter, path, message) if reported.add?(message)
113
- end
114
- end
115
-
116
- def audit_fields(type, findings, model:, bypassed:, reported:)
117
- type.all_field_definitions.each do |field|
118
- path = "#{type.graphql_name}.#{field.original_name}"
119
- # An inherited interface field is reported as uncovered on its interface; every other check still runs here.
120
- inherited = bypassed && interface_field?(field)
121
- next findings.problem(:field_without_rule, path, "its field class does not include Base, so it is denied") if bypassed && !inherited && !field.respond_to?(:armory_declared)
122
- next unless field.respond_to?(:armory_declared)
123
- next audit_resolver(field, path, findings) if field.resolver.respond_to?(:armory_rule)
124
-
125
- audit_declaration(field, path, findings, model: model, bypassed: bypassed && !inherited, reported: reported)
126
- audit_visibility(field, path, findings, model: model, reported: reported)
127
- audit_loads(field, path, findings)
128
- audit_arguments(field, path, findings, reported: reported)
129
- end
130
- end
131
-
132
- # A resolver or mutation carries its own rule: no rule is denied, `:none` is listed, an action is checked.
133
- def audit_resolver(field, path, findings)
134
- resolver = field.resolver
135
- rule = resolver.armory_rule
136
- return findings.problem(:no_rule, path, "its mutation or resolver declares no `authorize`, so it is denied") if rule.nil?
137
- return findings.bypass(path, rule.reason) if rule.none?
138
-
139
- model = begin
140
- resolver.armory_model
141
- rescue ConfigurationError => error
142
- return findings.problem(:model, path, error.message)
143
- end
144
- check(path, model, rule, findings, Set.new)
145
- end
146
-
147
- # The action a field is hidden with is asked of the policy like any other, so it must exist.
148
- def audit_visibility(field, path, findings, model:, reported:)
149
- return unless model && field.armory_visible_when
150
-
151
- check(path.split(".").first, model, Rule.new(field.armory_visible_when, attribute: field.original_name), findings, reported)
152
- end
153
-
154
- def audit_declaration(field, path, findings, model:, bypassed:, reported:)
155
- case (declared = field.armory_declared)
156
- when :none
157
- findings.bypass(path, field.armory_reason)
158
- when nil, true
159
- if 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), findings, reported) if model
164
- end
165
- end
166
-
167
- # An argument that declares a rule is listed (a bypass with its reason) or checked like a field's action.
168
- def audit_arguments(field, path, findings, reported:)
169
- field.all_argument_definitions.each do |argument|
170
- next unless argument.respond_to?(:armory_declared)
171
-
172
- apath = "#{path}(#{argument.keyword})"
173
- declared = argument.armory_declared
174
- findings.bypass(apath, argument.armory_reason) if declared == :none
175
- [declared, argument.armory_visible_when].compact.reject { |action| action == :none }.each do |action|
176
- audit_argument_action(argument, apath, action, findings, reported)
177
- end
178
- end
179
- end
180
-
181
- def audit_argument_action(argument, path, action, findings, reported)
182
- check(path.split(".").first, argument.armory_model, Rule.new(action, attribute: argument.keyword), findings, reported)
183
- rescue ConfigurationError => error
184
- findings.problem(:model, path, error.message)
185
- end
186
-
187
- def audit_loads(field, path, findings)
188
- return if field.armory_load_through_scope?
189
-
190
- opted_out_loads(field.all_argument_definitions, Set.new).each do |name|
191
- findings.problem(:unscoped_load, "#{path}(#{name})", "loads through the app's object_from_id, not through the user's scope")
192
- end
193
- end
194
-
195
- # Execution::Next loads `loads:` nested in input objects with the field's loader, so a field's opt-out
196
- # reaches them too: list them as `filter.invoice_id`. `seen` stops recursive input types.
197
- def opted_out_loads(arguments, seen, prefix = nil)
198
- arguments.flat_map do |argument|
199
- name = [prefix, GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)].compact.join(".")
200
- next [name] if argument.loads
201
-
202
- input = argument.type.unwrap
203
- next [] unless input.kind.input_object? && seen.add?(input)
204
-
205
- opted_out_loads(input.all_argument_definitions, seen, name)
206
- end
207
- end
208
-
209
- # An input object loads its own `loads:` arguments; without Base they skip the user's scope.
210
- def audit_input_loads(input, findings)
211
- return if input.respond_to?(:armory_scoped_loads?)
212
-
213
- # A mutation's generated input (`from_resolver`) is loaded, scoped, by the mutation itself.
214
- input.all_argument_definitions.select(&:loads).reject(&:from_resolver?).each do |argument|
215
- name = GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)
216
- findings.problem(:unscoped_load, "#{input.graphql_name}(#{name})",
217
- "its input object class does not include Base, so it loads through the app's object_from_id, not the user's scope")
218
- end
219
- end
220
-
221
- # Collects what one audit finds.
222
- class Findings
223
- def initialize
224
- @problems = []
225
- @bypasses = []
226
- end
227
-
228
- def problem(kind, path, message)
229
- @problems << Report::Problem.new(kind, path, message)
230
- end
231
-
232
- def bypass(path, reason)
233
- @bypasses << Report::Bypass.new(path, reason)
234
- end
235
-
236
- def report
237
- Report.new(problems: @problems.sort_by { |problem| [problem.path, problem.kind.to_s] },
238
- bypasses: @bypasses.sort_by(&:path))
94
+ def audited_types(definitions)
95
+ definitions.select do |type|
96
+ type.kind.object? && !Definitions.introspection?(type) &&
97
+ (extended_page_info?(type) || extended_relay?(type) || RELAY_TYPES.none? { |relay| type <= relay })
239
98
  end
240
99
  end
241
100
 
@@ -31,20 +31,26 @@ module GraphQL
31
31
  private
32
32
 
33
33
  def bypass(subject, rule)
34
- telemetry.record(:bypassed, type: subject.class.name, reason: rule.reason)
34
+ telemetry.record(:bypassed, type: class_name(subject), reason: rule.reason)
35
35
  true
36
36
  end
37
37
 
38
+ # The record's own `class` can be a data member, and the error for it quotes the record.
39
+ def class_name(subject)
40
+ ::Kernel.instance_method(:class).bind_call(subject).name
41
+ end
42
+
38
43
  def decide(subject, rule, context)
39
44
  answer = adapter.authorized?(adapter.actor(context), subject, rule)
40
45
 
41
46
  case answer
42
47
  when true, false, nil
43
48
  allowed = answer == true
44
- telemetry.record(allowed ? :allowed : :denied, type: subject.class.name, action: rule.action)
49
+ telemetry.record(allowed ? :allowed : :denied, type: class_name(subject), action: rule.action)
45
50
  allowed
46
51
  else
47
- raise AdapterError, "#{adapter.class} answered #{answer.inspect}; it must answer true, false or nil"
52
+ # The class alone: the answer may be the record, and on a relation `inspect` runs the query.
53
+ raise AdapterError, "#{adapter.class} answered a #{::Kernel.instance_method(:class).bind_call(answer)}; it must answer true, false or nil"
48
54
  end
49
55
  end
50
56