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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +43 -1
  3. data/README.md +6 -5
  4. data/docs/authorization.md +133 -6
  5. data/docs/keyset-connections.md +95 -0
  6. data/docs/persisted-queries.md +16 -0
  7. data/docs/rate-limiting.md +20 -0
  8. data/docs/telemetry.md +1 -0
  9. data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
  10. data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
  11. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  12. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  13. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  14. data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
  15. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  16. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  17. data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
  18. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  19. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  20. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  21. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  22. data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
  23. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  24. data/lib/graphql/armory/authorization/audit.rb +50 -191
  25. data/lib/graphql/armory/authorization/authorize.rb +9 -3
  26. data/lib/graphql/armory/authorization/configuration.rb +30 -2
  27. data/lib/graphql/armory/authorization/definitions.rb +114 -0
  28. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  29. data/lib/graphql/armory/authorization/integration/field.rb +13 -5
  30. data/lib/graphql/armory/authorization/integration/object.rb +3 -1
  31. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  32. data/lib/graphql/armory/authorization/integration.rb +22 -1
  33. data/lib/graphql/armory/authorization/rule.rb +5 -0
  34. data/lib/graphql/armory/authorization.rb +4 -1
  35. data/lib/graphql/armory/keyset/controls.rb +36 -0
  36. data/lib/graphql/armory/keyset.rb +279 -0
  37. data/lib/graphql/armory/version.rb +1 -1
  38. data/lib/graphql/armory.rb +1 -0
  39. metadata +17 -1
@@ -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
 
@@ -7,21 +7,49 @@ module GraphQL
7
7
  class Configuration
8
8
  MODES = %i[null error raise].freeze
9
9
 
10
- attr_reader :adapter, :on_unauthorized, :sinks
10
+ DENIAL_HOOKS = %i[unauthorized_field unauthorized_object].freeze
11
11
 
12
- def initialize(adapter:, on_unauthorized: :null, sinks: [])
12
+ OWN_CHECKS = %i[authorized? authorizes? authorized_new wrap unauthorized_object visible? scope_items reauthorize_scoped_objects].freeze
13
+
14
+ attr_reader :adapter, :on_unauthorized, :sinks, :own_denial_hooks, :own_checks
15
+
16
+ def initialize(adapter:, on_unauthorized: :null, sinks: [], own_denial_hooks: {}, own_checks: {})
13
17
  unless MODES.include?(on_unauthorized)
14
18
  raise ArgumentError, "on_unauthorized must be one of #{MODES.inspect}, got #{on_unauthorized.inspect}"
15
19
  end
20
+ check_own_denial_hooks(own_denial_hooks)
21
+ check_own_checks(own_checks)
16
22
 
23
+ @own_checks = own_checks.to_h { |holder, methods| [holder, methods.to_h { |name, reason| [name, reason.dup.freeze] }.freeze] }.freeze
17
24
  @adapter = resolve(adapter)
18
25
  @on_unauthorized = on_unauthorized
26
+ @own_denial_hooks = own_denial_hooks.to_h { |hook, reason| [hook, reason.dup.freeze] }.freeze
19
27
  Telemetry.build(sinks: sinks) # raises at boot when the sinks are not callables
20
28
  @sinks = sinks
21
29
  end
22
30
 
23
31
  private
24
32
 
33
+ def check_own_denial_hooks(hooks)
34
+ valid = hooks.is_a?(Hash) && hooks.all? do |hook, reason|
35
+ DENIAL_HOOKS.include?(hook) && reason.is_a?(String) && !reason.strip.empty?
36
+ end
37
+ return if valid
38
+
39
+ raise ArgumentError, "own_denial_hooks must map #{DENIAL_HOOKS.inspect} to the reason you keep your own, got #{hooks.inspect}"
40
+ end
41
+
42
+ def check_own_checks(checks)
43
+ valid = checks.is_a?(Hash) && checks.all? do |holder, methods|
44
+ holder.is_a?(Module) && methods.is_a?(Hash) && methods.all? do |name, reason|
45
+ OWN_CHECKS.include?(name) && reason.is_a?(String) && !reason.strip.empty?
46
+ end
47
+ end
48
+ return if valid
49
+
50
+ raise ArgumentError, "own_checks must map a class to #{OWN_CHECKS.inspect} and the reason you keep each, got #{checks.inspect}"
51
+ end
52
+
25
53
  def resolve(adapter)
26
54
  case adapter
27
55
  when :pundit then Adapters::Pundit.new
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Authorization
8
+ # Every definition a schema holds, hidden or not. `schema.types` is read with no request, so it leaves out what is
9
+ # hidden there and a request can still reach: a type or field hidden by `visible?`, or one of several definitions of
10
+ # a name. This follows the definitions themselves from the roots and `orphan_types`.
11
+ module Definitions
12
+ def self.call(schema, &on_listing_error)
13
+ found = Set.new.compare_by_identity
14
+ pending = [schema.query, schema.mutation, schema.subscription, *schema.orphan_types, *directive_types(schema), *introspection_field_types(schema), *listed_types(schema, &on_listing_error)]
15
+ until pending.empty?
16
+ type = pending.pop
17
+ next if !type.respond_to?(:kind) || !found.add?(type)
18
+
19
+ pending.concat(referenced_types(type))
20
+ end
21
+ found.to_a
22
+ end
23
+
24
+ # With only static profiles, graphql-ruby refuses to list types without naming one: the roots still lead everywhere.
25
+ # A `visible_when:` that cannot name its model raises here, on the first listing only. The roots still lead to the
26
+ # rest, and the audit passes a block because it must not say ok after that.
27
+ def self.listed_types(schema)
28
+ schema.types.values
29
+ rescue ConfigurationError => error
30
+ yield error if block_given?
31
+ []
32
+ rescue ArgumentError
33
+ raise unless schema.visibility
34
+
35
+ []
36
+ end
37
+ private_class_method :listed_types
38
+
39
+ # A directive argument is client input like any other, and only `schema.types` used to reach its input objects.
40
+ def self.directive_types(schema)
41
+ schema.directives.values.flat_map { |directive| arguments_of(directive) }.compact
42
+ end
43
+ private_class_method :directive_types
44
+
45
+ # `introspection(true)` is a flag anyone can set, and graphql-ruby serves such a type like any other. The `__` prefix
46
+ # is reserved for introspection by the GraphQL spec: built-in types, what `Schema.introspection` replaces them with,
47
+ # and their copies all carry it.
48
+ def self.introspection?(type)
49
+ type.introspection? && type.graphql_name.start_with?("__")
50
+ end
51
+
52
+ # `Schema.introspection` can add entry points and dynamic fields, which no type holds and a client can reach.
53
+ def self.introspection_field_types(schema)
54
+ system = schema.introspection_system
55
+ (system.entry_points + system.dynamic_fields).flat_map { |field| references_of(field) }.flatten.compact
56
+ end
57
+ private_class_method :introspection_field_types
58
+
59
+ def self.connection?(type)
60
+ type.is_a?(Class) && type.include?(GraphQL::Types::Relay::ConnectionBehaviors)
61
+ end
62
+
63
+ def self.referenced_types(type)
64
+ references = []
65
+ references << fields_of(type)
66
+ references << arguments_of(type) if type.kind.input_object?
67
+ references.concat(type.interface_type_memberships.map(&:abstract_type)) if type.respond_to?(:interface_type_memberships)
68
+ references.concat(type.orphan_types) if type.kind.interface?
69
+ references.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
70
+ references << type.node_type if connection?(type)
71
+ references.flatten.compact
72
+ end
73
+ private_class_method :referenced_types
74
+
75
+ # Every definition of every field, nearest first: a hidden one falls back to the next of its name, and
76
+ # `all_field_definitions` merges them by name. `field ... do ... end` blocks are loaded here, because only
77
+ # `schema.types` or a request loads them, and the audit reads arguments from them.
78
+ def self.fields_at_every_level(type)
79
+ return [] unless type.respond_to?(:all_field_definitions)
80
+
81
+ fields = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) }.flat_map { |ancestor| ancestor.own_fields.values.flatten }
82
+ fields.each { |field| field.ensure_loaded if field.respond_to?(:ensure_loaded) }
83
+ fields.uniq
84
+ end
85
+
86
+ # A shadowed definition is only served by a fallback, so one whose type cannot load fails nothing that runs today.
87
+ def self.shadowed?(type, field)
88
+ !type.all_field_definitions.include?(field)
89
+ end
90
+
91
+ def self.fields_of(type)
92
+ fields_at_every_level(type).map do |field|
93
+ references_of(field)
94
+ rescue GraphQL::Error
95
+ raise unless shadowed?(type, field)
96
+
97
+ nil
98
+ end
99
+ end
100
+ private_class_method :fields_of
101
+
102
+ def self.references_of(field)
103
+ [field.type.unwrap, arguments_of(field)]
104
+ end
105
+ private_class_method :references_of
106
+
107
+ def self.arguments_of(member)
108
+ member.all_argument_definitions.flat_map { |argument| [argument.type.unwrap, argument.loads] }
109
+ end
110
+ private_class_method :arguments_of
111
+ end
112
+ end
113
+ end
114
+ end
@@ -17,10 +17,11 @@ module GraphQL
17
17
  # The question is asked of the parent object when it is an instance of the model, else of the model's
18
18
  # class (a root field has no record). The model is the one the field's type wraps, or `model:`.
19
19
  module Argument
20
- def initialize(*args, authorize: nil, reason: nil, model: nil, visible_when: nil, **kwargs, &block)
20
+ def initialize(*args, authorize: nil, reason: nil, model: nil, visible_when: nil, override_reason: nil, **kwargs, &block)
21
21
  unless [nil, :none].include?(authorize) || authorize.is_a?(Symbol)
22
22
  raise ArgumentError, "an argument's `authorize:` is an action or :none, not #{authorize.inspect}"
23
23
  end
24
+ raise ConfigurationError, "override_reason: must say why the override is safe" if !override_reason.nil? && override_reason.to_s.strip.empty?
24
25
 
25
26
  if authorize && authorize != :none && kwargs.key?(:default_value)
26
27
  raise ArgumentError,
@@ -32,32 +33,49 @@ module GraphQL
32
33
  @armory_reason = reason
33
34
  @armory_model = model
34
35
  @armory_visible_when = visible_when&.to_sym
36
+ @armory_override_reason = override_reason
35
37
  Rule.none(reason: reason) if authorize == :none
36
38
  super(*args, **kwargs, &block)
37
39
  end
38
40
 
39
- attr_reader :armory_declared, :armory_reason, :armory_visible_when
41
+ attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
40
42
 
41
43
  def armory_model
42
- @armory_model || armory_field_model
44
+ return armory_field_model unless @armory_model
45
+ return @armory_model unless @armory_model.is_a?(String)
46
+
47
+ armory_named_model
43
48
  end
44
49
 
45
50
  def authorized?(object, value, context)
46
51
  return super if @armory_declared.nil? || @armory_declared == :none
47
52
 
48
53
  model = armory_model
49
- subject = object.is_a?(model) ? object : model
50
- Integration.allowed?(subject, Rule.new(@armory_declared, attribute: keyword), context) && super
54
+ subject = Module === model && object.is_a?(model) ? object : model
55
+ Integration.allowed?(subject, Rule.new(@armory_declared, policy: armory_policy, attribute: keyword, model: model), context) && super
51
56
  end
52
57
 
53
58
  def visible?(context)
54
59
  return super unless @armory_visible_when
55
60
 
56
- super && Integration.visible?(armory_model, Rule.new(@armory_visible_when, attribute: keyword), context)
61
+ super && Integration.visible?(armory_model, Rule.new(@armory_visible_when, policy: armory_policy, attribute: keyword, model: armory_model), context)
62
+ end
63
+
64
+ # The `policy:` of the type or resolver the argument belongs to.
65
+ def armory_policy
66
+ type = owner.respond_to?(:owner) ? owner.owner : owner
67
+ type.armory_rule&.policy if type.respond_to?(:armory_rule)
57
68
  end
58
69
 
59
70
  private
60
71
 
72
+ # `is_a?` needs a Module, and Pundit reads a String as a record. A name with no constant stays a model name.
73
+ def armory_named_model
74
+ Object.const_get(@armory_model)
75
+ rescue NameError
76
+ @armory_model.to_sym
77
+ end
78
+
61
79
  def armory_field_model
62
80
  type = owner.respond_to?(:owner) ? owner.owner : owner
63
81
  model = type.armory_model if type.respond_to?(:armory_model)
@@ -19,7 +19,10 @@ module GraphQL
19
19
  module Field
20
20
  # `find_by: :id` looks the object up inside the user's scope instead of calling the resolver.
21
21
  # `loads:` arguments also load through the scope unless `load_through_scope: false`.
22
- def initialize(*args, authorize: nil, reason: nil, find_by: nil, load_through_scope: true, visible_when: nil, **kwargs, &block)
22
+ def initialize(*args, authorize: nil, reason: nil, find_by: nil, load_through_scope: true, visible_when: nil, override_reason: nil, **kwargs, &block)
23
+ raise ConfigurationError, "override_reason: must say why the override is safe" if !override_reason.nil? && override_reason.to_s.strip.empty?
24
+
25
+ @armory_override_reason = override_reason
23
26
  @armory_declared = authorize
24
27
  @armory_reason = reason
25
28
  @armory_visible_when = visible_when&.to_sym
@@ -29,7 +32,7 @@ module GraphQL
29
32
  extension(Integration::FindBy, key: find_by) if find_by
30
33
  end
31
34
 
32
- attr_reader :armory_declared, :armory_reason, :armory_visible_when
35
+ attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
33
36
 
34
37
  def armory_load_through_scope?
35
38
  @armory_load_through_scope
@@ -57,15 +60,20 @@ module GraphQL
57
60
  raise ConfigurationError, "#{owner.graphql_name}.#{original_name} uses visible_when:, so its type must include Base"
58
61
  end
59
62
 
60
- rule = Rule.new(@armory_visible_when, attribute: original_name)
63
+ rule = Rule.new(@armory_visible_when, policy: armory_policy, attribute: original_name)
61
64
  super && Integration.visible?(owner.armory_model, rule, context)
62
65
  end
63
66
 
67
+ # The `policy:` of the type that defines the field: a rule on the field asks it another question.
68
+ def armory_policy
69
+ owner.armory_rule&.policy if owner.respond_to?(:armory_rule)
70
+ end
71
+
64
72
  def authorized?(object, args, context)
65
73
  rule = armory_rule_for(owner, context)
66
74
  return false if rule.nil?
67
75
 
68
- (rule == :covered || Integration.allowed?(object, rule, context)) && super
76
+ (rule == :covered || Integration.allowed?(object, Integration.with_owner_model(rule, owner), context)) && super
69
77
  end
70
78
 
71
79
  private
@@ -92,7 +100,7 @@ module GraphQL
92
100
  when :none
93
101
  Rule.none(reason: @armory_reason)
94
102
  else
95
- Rule.new(@armory_declared, attribute: original_name)
103
+ Rule.new(@armory_declared, policy: armory_policy, attribute: original_name)
96
104
  end
97
105
  end
98
106
  end
@@ -41,6 +41,8 @@ module GraphQL
41
41
 
42
42
  # Runs before pagination, so connections page and count the scoped list.
43
43
  def scope_items(items, context)
44
+ return items.rescoped { |relation| scope_items(relation, context) } if items.is_a?(Keyset)
45
+
44
46
  super(Integration.scoped(items, armory_rule, context), context)
45
47
  end
46
48
 
@@ -58,7 +60,7 @@ module GraphQL
58
60
  rule = armory_rule
59
61
  return false if rule.nil?
60
62
 
61
- Integration.allowed?(object, rule, context) && super
63
+ Integration.allowed?(object, Integration.with_owner_model(rule, self), context) && super
62
64
  end
63
65
  end
64
66
  end
@@ -16,6 +16,13 @@ module GraphQL
16
16
  (superclass.armory_configuration if superclass.respond_to?(:armory_configuration))
17
17
  end
18
18
 
19
+ # Read when a query first needs it, after the schema is defined; each schema holds its own.
20
+ def armory_implementers(interface)
21
+ @armory_definitions ||= Definitions.(self)
22
+ @armory_implementers ||= {}.compare_by_identity
23
+ @armory_implementers[interface] ||= @armory_definitions.select { |type| type.kind.object? && type <= interface }
24
+ end
25
+
19
26
  # `loads:` asks the schema which type an object is. For an object type the answer is known:
20
27
  # the lookup went through that type's model. Abstract types and foreign objects are the app's call.
21
28
  def resolve_type(type, object, context)