graphql-armory 0.1.0.pre7 → 0.1.0.pre9

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 (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -0
  3. data/README.md +6 -5
  4. data/docs/authorization.md +220 -10
  5. data/docs/encrypted-ids.md +1 -1
  6. data/docs/persisted-queries.md +111 -19
  7. data/docs/rate-limiting.md +32 -11
  8. data/docs/telemetry.md +31 -8
  9. data/exe/graphql-armory-publish +6 -0
  10. data/lib/graphql/armory/authorization/adapters/cancan.rb +17 -4
  11. data/lib/graphql/armory/authorization/adapters/pundit.rb +79 -14
  12. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  13. data/lib/graphql/armory/authorization/audit/baseline.rb +191 -0
  14. data/lib/graphql/armory/authorization/audit/command.rb +57 -8
  15. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  16. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  17. data/lib/graphql/armory/authorization/audit/findings.rb +35 -0
  18. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  19. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  20. data/lib/graphql/armory/authorization/audit/interface_rules.rb +42 -0
  21. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  22. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  23. data/lib/graphql/armory/authorization/audit/observed_rules.rb +58 -0
  24. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  25. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  26. data/lib/graphql/armory/authorization/audit/payload_reuse.rb +95 -0
  27. data/lib/graphql/armory/authorization/audit/plain_resolvers.rb +85 -0
  28. data/lib/graphql/armory/authorization/audit/reach.rb +75 -0
  29. data/lib/graphql/armory/authorization/audit/report.rb +7 -1
  30. data/lib/graphql/armory/authorization/audit/type_rules.rb +282 -0
  31. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  32. data/lib/graphql/armory/authorization/audit.rb +59 -192
  33. data/lib/graphql/armory/authorization/authorize.rb +35 -9
  34. data/lib/graphql/armory/authorization/configuration.rb +32 -3
  35. data/lib/graphql/armory/authorization/definitions.rb +121 -0
  36. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  37. data/lib/graphql/armory/authorization/integration/field.rb +36 -6
  38. data/lib/graphql/armory/authorization/integration/object.rb +9 -1
  39. data/lib/graphql/armory/authorization/integration/resolver.rb +2 -0
  40. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  41. data/lib/graphql/armory/authorization/integration.rb +36 -4
  42. data/lib/graphql/armory/authorization/rspec.rb +10 -4
  43. data/lib/graphql/armory/authorization/rule.rb +24 -3
  44. data/lib/graphql/armory/authorization/scope.rb +6 -6
  45. data/lib/graphql/armory/authorization.rb +4 -1
  46. data/lib/graphql/armory/ids.rb +11 -11
  47. data/lib/graphql/armory/persisted_queries/manifest.rb +2 -0
  48. data/lib/graphql/armory/persisted_queries/manifests.rb +69 -0
  49. data/lib/graphql/armory/persisted_queries/publisher.rb +127 -0
  50. data/lib/graphql/armory/persisted_queries/redis_store.rb +137 -0
  51. data/lib/graphql/armory/persisted_queries.rb +55 -16
  52. data/lib/graphql/armory/railtie.rb +10 -0
  53. data/lib/graphql/armory/rate_limits/analysis_guard.rb +79 -0
  54. data/lib/graphql/armory/rate_limits/concurrency.rb +91 -14
  55. data/lib/graphql/armory/rate_limits/points.rb +91 -14
  56. data/lib/graphql/armory/rate_limits/time.rb +107 -19
  57. data/lib/graphql/armory/rate_limits.rb +29 -5
  58. data/lib/graphql/armory/telemetry.rb +71 -9
  59. data/lib/graphql/armory/version.rb +1 -1
  60. data/lib/graphql/armory.rb +1 -0
  61. data/lib/tasks/graphql_armory.rake +11 -4
  62. metadata +28 -3
@@ -0,0 +1,282 @@
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), write_roots = [])
10
+ @write_roots = write_roots
11
+ @adapter = adapter
12
+ @findings = findings
13
+ @foreign_policies = foreign_policies
14
+ end
15
+
16
+ def call(type)
17
+ path = type.graphql_name
18
+ unless type.respond_to?(:armory_rule)
19
+ @findings.problem(:unprotected, path, "its base class does not include GraphQL::Armory::Authorization::Base, so it is open")
20
+ return
21
+ end
22
+
23
+ rule = type.armory_rule
24
+ if rule.nil?
25
+ @findings.problem(:no_rule, path, "has no rule and no default_action, so it is denied")
26
+ elsif rule.visible_when && (answer = graphql_ruby_answer(type, :visible?))
27
+ @findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers visible? here, so visible_when: never runs: remove it")
28
+ elsif rule.none?
29
+ @findings.bypass(path, rule.reason)
30
+ audit_fields(type, model: nil, bypassed: !rule.covers_fields?, reported: Set.new)
31
+ @foreign_policies.(type, rule.policy) if rule.policy
32
+ elsif (answer = graphql_ruby_answer(type, :authorized?))
33
+ @findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers authorized? here, so this rule never runs: " \
34
+ "put the rule on the field that returns it (on a root field, on a resolver: a field rule asks " \
35
+ "the parent's policy), and declare `authorize :none, reason: \"...\", covers_fields: true` on this class")
36
+ else
37
+ model = model_of(type, path)
38
+ reported = Set.new
39
+ check(path, model, rule, reported) if model
40
+ check(path, model, Rule.new(rule.visible_when, policy: rule.policy), reported) if model && rule.visible_when
41
+ @foreign_policies.(type, rule.policy) if rule.policy
42
+ audit_fields(type, model: model, bypassed: false, reported: reported)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ # ConnectionBehaviors and EdgeBehaviors, included after Base, bring an `authorized?` and a `visible?` that win
49
+ # over the plugin's. An app's method may call super, so this follows the chain to the first library that answers.
50
+ def graphql_ruby_answer(type, name)
51
+ method = type.method(name)
52
+ until method.nil? || method.owner == Integration::Object
53
+ return method.owner if LibraryMethods.library_method?(method, LibraryMethods.holder_of(type, method), GRAPHQL_RUBY_PATH)
54
+
55
+ method = method.super_method
56
+ end
57
+ end
58
+
59
+ # A field an implementer inherits from an interface is reported on the interface.
60
+ def interface_field?(field)
61
+ field.owner.respond_to?(:kind) && field.owner.kind.interface?
62
+ end
63
+
64
+ def model_of(type, path)
65
+ type.armory_model
66
+ rescue ConfigurationError => error
67
+ @findings.problem(:model, path, error.message)
68
+ nil
69
+ end
70
+
71
+ # An adapter problem shows once per type, however many fields declare an action of their own.
72
+ def check(path, model, rule, reported)
73
+ @adapter.check(model, rule).each do |message|
74
+ @findings.problem(:adapter, path, message) if reported.add?(message)
75
+ end
76
+ end
77
+
78
+ # Every definition, the shadowed ones included: a hidden override falls back to them, so they serve requests too.
79
+ # Nothing here loads a field's type, so a shadowed field whose type cannot load raises nothing.
80
+ def audit_fields(type, model:, bypassed:, reported:)
81
+ Definitions.fields_at_every_level(type).each do |field|
82
+ audit_field(type, field, "#{type.graphql_name}.#{field.original_name}", model: model, bypassed: bypassed, reported: reported)
83
+ end
84
+ end
85
+
86
+ def audit_field(type, field, path, model:, bypassed:, reported:)
87
+ audit_scope(type, field, path)
88
+ audit_loads(field, path)
89
+ # An inherited interface field is reported as uncovered on its interface; every other check still runs here.
90
+ inherited = bypassed && interface_field?(field)
91
+ PlainResolvers.report(@findings, path, field) if served_with_no_rule?(type, field)
92
+ if bypassed && !inherited && !field.respond_to?(:armory_declared)
93
+ 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 " \
94
+ "authorized? answers true, so the field is served with no check. Use a field class that includes Base")
95
+ end
96
+ return unless field.respond_to?(:armory_declared)
97
+ return audit_resolver(field, path) if field.resolver.respond_to?(:armory_rule)
98
+
99
+ audit_declaration(type, field, path, model: model, bypassed: bypassed && !inherited, reported: reported)
100
+ audit_inherited_rule(type, field, path, model) if model
101
+ audit_visibility(field, path, model: model, reported: reported)
102
+ audit_arguments(field.all_argument_definitions, path, reported: reported)
103
+ end
104
+
105
+ # Asked of the type that serves it and of the class that defines it, as runtime does: a parent that does not cover it denies it.
106
+ def served_with_no_rule?(type, field)
107
+ return false unless PlainResolvers.covering?(type.armory_rule) && !interface_field?(field) && PlainResolvers.open?(field, write_root: @write_roots.include?(type))
108
+
109
+ !(field.respond_to?(:armory_denied_by_owner?) && field.armory_denied_by_owner?)
110
+ end
111
+
112
+ # A field runs under its owner: graphql-ruby never tells it the concrete type. A rule defined above that type asks
113
+ # the owner's model, or only the record's class, while this audit reads the type's model.
114
+ def audit_inherited_rule(type, field, path, model)
115
+ return if field.owner.equal?(type) || [nil, :none].include?(field.armory_declared) || field.armory_policy
116
+ return if field.owner.respond_to?(:armory_model) && owner_model(field.owner).equal?(model)
117
+
118
+ @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 " \
119
+ "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)}")
120
+ end
121
+
122
+ def owner_model(owner)
123
+ owner.armory_model
124
+ rescue ConfigurationError, GraphQL::RequiredImplementationMissingError
125
+ nil
126
+ end
127
+
128
+ def name_of(owner)
129
+ owner.respond_to?(:graphql_name) ? owner.graphql_name : owner.inspect
130
+ rescue GraphQL::RequiredImplementationMissingError
131
+ owner.inspect
132
+ end
133
+
134
+ # A resolver or mutation carries its own rule: no rule is denied, `:none` is listed, an action is checked.
135
+ def audit_resolver(field, path)
136
+ resolver = field.resolver
137
+ rule = resolver.armory_rule
138
+ # A RelayClassicMutation's field holds only the generated `input`: its arguments are on the resolver.
139
+ arguments = (field.all_argument_definitions + resolver.all_argument_definitions).uniq
140
+ @foreign_policies.arguments(arguments, path, rule.policy, Set.new) if rule&.policy
141
+ return @findings.problem(:no_rule, path, "its mutation or resolver declares no `authorize`, so it is denied") if rule.nil?
142
+ audit_arguments(arguments, path, reported: Set.new)
143
+ return @findings.bypass(path, rule.reason) if rule.none?
144
+
145
+ model = begin
146
+ resolver.armory_model
147
+ rescue ConfigurationError => error
148
+ return @findings.problem(:model, path, error.message)
149
+ end
150
+ check(path, model, rule, Set.new)
151
+ end
152
+
153
+ # The action a field is hidden with is asked of the policy like any other, so it must exist.
154
+ def audit_visibility(field, path, model:, reported:)
155
+ return unless model && field.armory_visible_when
156
+
157
+ check(path.split(".").first, model, Rule.new(field.armory_visible_when, policy: field.armory_policy, attribute: field.original_name), reported)
158
+ end
159
+
160
+ def audit_declaration(type, field, path, model:, bypassed:, reported:)
161
+ case (declared = field.armory_declared)
162
+ when :none
163
+ @findings.bypass(path, field.armory_reason)
164
+ when nil, true
165
+ if denied_payload_field?(field)
166
+ @findings.problem(:field_without_rule, path, "declares no rule and #{name_of(field.owner)}, which defines it, has none that covers it, so the mutation runs and this field answers null: " \
167
+ "a client cannot tell whether it worked. Give the field `authorize: :none, reason: \"...\"`, or use a shared `payload_type`; a public mutation adds `covers_fields: true`")
168
+ elsif bypassed && declared.nil? && served_by_owner?(type, field)
169
+ @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 " \
170
+ "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")
171
+ elsif bypassed
172
+ @findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, so it is denied")
173
+ end
174
+ else
175
+ check(path.split(".").first, model, Rule.new(declared, policy: field.armory_policy), reported) if model
176
+ end
177
+ end
178
+
179
+ def denied_payload_field?(field)
180
+ owner = field.owner
181
+ owner.is_a?(Class) && owner <= GraphQL::Schema::Resolver && field.armory_denied_by_owner?
182
+ end
183
+
184
+ # A field runs under its owner's rule, not the resolved type's. Inherited by a bypassed type from a parent whose
185
+ # rule covers it, or that knows no rule at all, graphql-ruby serves it after a type check that checked nothing.
186
+ def served_by_owner?(type, field)
187
+ owner = field.owner
188
+ return false if owner.equal?(type) || !owner.is_a?(Class)
189
+ return true unless owner.respond_to?(:armory_rule)
190
+
191
+ rule = owner.armory_rule
192
+ !rule.nil? && (!rule.none? || rule.covers_fields?)
193
+ end
194
+
195
+ # An argument that declares a rule is listed (a bypass with its reason) or checked like a field's action.
196
+ def audit_arguments(arguments, path, reported:)
197
+ arguments.each do |argument|
198
+ next unless argument.respond_to?(:armory_declared)
199
+
200
+ apath = "#{path}(#{argument.keyword})"
201
+ declared = argument.armory_declared
202
+ @findings.bypass(apath, argument.armory_reason) if declared == :none
203
+ [declared, argument.armory_visible_when].compact.reject { |action| action == :none }.each do |action|
204
+ audit_argument_action(argument, apath, action, reported)
205
+ end
206
+ end
207
+ end
208
+
209
+ def audit_argument_action(argument, path, action, reported)
210
+ check(path.split(".").first, argument.armory_model, Rule.new(action, policy: argument.armory_policy, attribute: argument.keyword), reported)
211
+ rescue ConfigurationError => error
212
+ @findings.problem(:model, path, error.message)
213
+ end
214
+
215
+ # A list that skips the scope hands over every record: a rule on each one hides its fields, not how many there are.
216
+ def audit_scope(type, field, path)
217
+ return if field.scoped? || generated_list?(type, field) || !list_of_records?(field)
218
+
219
+ @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: " \
220
+ "remove the option, or write the type as [Type]")
221
+ end
222
+
223
+ # A connection applies the scope on the field that returns it, and graphql-ruby generates these two with scope: false.
224
+ # A hand-written one of the same name returns something else or reads another method.
225
+ def generated_list?(type, field)
226
+ owner = field.owner
227
+ return false unless Definitions.connection?(owner) && %i[edges nodes].include?(field.original_name)
228
+
229
+ element = field.type.unwrap
230
+ element = element.node_type if field.original_name == :edges && element.is_a?(Class) && element.include?(GraphQL::Types::Relay::EdgeBehaviors)
231
+ field.resolver.nil? && field.resolver_method == field.original_name && field.method_sym == field.original_name &&
232
+ field.dig_keys.nil? && element.equal?(owner.node_type) &&
233
+ type.instance_method(field.original_name).owner == GraphQL::Types::Relay::ConnectionBehaviors
234
+ rescue GraphQL::Error
235
+ false
236
+ end
237
+
238
+ def list_of_records?(field)
239
+ type = field.type
240
+ connection = Definitions.connection?(type.unwrap)
241
+ return false unless connection || field.connection? || (type.respond_to?(:list?) && type.list?)
242
+
243
+ # A late-bound element has no kind yet: it may be a record.
244
+ element = (type.unwrap.node_type if connection) || type.unwrap
245
+ !(element.respond_to?(:kind) && element.kind.leaf?)
246
+ rescue GraphQL::Error
247
+ false
248
+ end
249
+
250
+ # Whoever owns an argument loads it: the field, or its resolver. Only Base owners go through the scope.
251
+ def audit_loads(field, path)
252
+ arguments = field.all_argument_definitions.reject do |argument|
253
+ argument.owner.equal?(field) ? scoped_field?(field) : field.resolver.respond_to?(:armory_rule)
254
+ end
255
+
256
+ opted_out_loads(arguments, Set.new).each do |name|
257
+ @findings.problem(:unscoped_load, "#{path}(#{name})", "loads through the app's object_from_id, not through the user's scope")
258
+ end
259
+ end
260
+
261
+ def scoped_field?(field)
262
+ field.respond_to?(:armory_load_through_scope?) && field.armory_load_through_scope?
263
+ end
264
+
265
+ # Execution::Next loads `loads:` nested in input objects with the field's loader, so a field's opt-out
266
+ # reaches them too: list them as `filter.invoice_id`. `seen` stops recursive input types.
267
+ def opted_out_loads(arguments, seen, prefix = nil)
268
+ arguments.flat_map do |argument|
269
+ name = [prefix, GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)].compact.join(".")
270
+ next [name] if argument.loads
271
+
272
+ input = argument.type.unwrap
273
+ next [] unless input.respond_to?(:kind) && input.kind.input_object? && seen.add?(input)
274
+
275
+ opted_out_loads(input.all_argument_definitions, seen, name)
276
+ end
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end
282
+ 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
@@ -2,7 +2,24 @@
2
2
 
3
3
  require "set"
4
4
  require "graphql/armory/authorization/audit/report"
5
+ require "graphql/armory/authorization/audit/baseline"
6
+ require "graphql/armory/authorization/audit/reach"
5
7
  require "graphql/armory/authorization/audit/command"
8
+ require "graphql/armory/authorization/audit/findings"
9
+ require "graphql/armory/authorization/audit/library_methods"
10
+ require "graphql/armory/authorization/audit/denial_hooks"
11
+ require "graphql/armory/authorization/audit/own_checks"
12
+ require "graphql/armory/authorization/audit/plain_resolvers"
13
+ require "graphql/armory/authorization/audit/overrides"
14
+ require "graphql/armory/authorization/audit/foreign_policies"
15
+ require "graphql/armory/authorization/audit/type_rules"
16
+ require "graphql/armory/authorization/audit/observed_rules"
17
+ require "graphql/armory/authorization/audit/edge_rules"
18
+ require "graphql/armory/authorization/audit/interface_rules"
19
+ require "graphql/armory/authorization/audit/payload_reuse"
20
+ require "graphql/armory/authorization/audit/input_loads"
21
+ require "graphql/armory/authorization/audit/visibility_models"
22
+ require "graphql/armory/authorization/audit/introspection_fields"
6
23
 
7
24
  module GraphQL
8
25
  module Armory
@@ -10,9 +27,18 @@ module GraphQL
10
27
  # Walks a schema, without running a query, and reports everything that is not covered:
11
28
  # types nobody protected, types without a rule or a policy, fields with no rule on a bypassed
12
29
  # type, and `loads:` that skip the scope. Every declared bypass is listed with its reason.
30
+ #
31
+ # Each check is an object of its own, built for one run, so one Audit can serve several threads.
13
32
  class Audit
14
33
  RELAY_TYPES = [GraphQL::Types::Relay::BaseConnection, GraphQL::Types::Relay::BaseEdge,
15
34
  GraphQL::Types::Relay::PageInfo].freeze
35
+ GENERATED_RELAY_FIELDS = { GraphQL::Types::Relay::BaseConnection => %i[edges nodes page_info],
36
+ GraphQL::Types::Relay::BaseEdge => %i[node cursor] }.freeze
37
+
38
+ # A library's method is defined in its files and held by a class it names. Either alone misleads: anyone can name
39
+ # a module under `GraphQL::`, and an alias keeps graphql-ruby's location on an app's class.
40
+ GRAPHQL_RUBY_PATH = "#{File.dirname(GraphQL::Schema.method(:from_definition).source_location.first)}/"
41
+ ARMORY_PATH = "#{File.expand_path('..', __dir__)}/"
16
42
 
17
43
  attr_reader :adapter
18
44
 
@@ -32,210 +58,51 @@ module GraphQL
32
58
 
33
59
  def call(schema)
34
60
  findings = Findings.new
35
- unless schema.respond_to?(:armory_configuration) && schema.armory_configuration
61
+ listing_error = nil
62
+ definitions = Definitions.(schema) { |error| listing_error = error }
63
+ if schema.respond_to?(:armory_configuration) && schema.armory_configuration
64
+ DenialHooks.(schema, findings)
65
+ OwnChecks.(schema, definitions, findings)
66
+ Overrides.(schema, definitions, findings)
67
+ else
36
68
  findings.problem(:plugin, "Schema", "never installed the plugin: add `use GraphQL::Armory::Authorization, adapter: ...`")
37
69
  end
38
70
 
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? }
42
- findings.report
71
+ foreign_policies = ForeignPolicies.new(adapter, findings)
72
+ type_rules = TypeRules.new(adapter, findings, foreign_policies, [schema.mutation, schema.subscription].compact)
73
+ audited_types(definitions).each { |type| type_rules.(type) }
74
+ definitions.each { |type| foreign_policies.relay(type) if type.kind.object? && RELAY_TYPES.any? { |relay| type <= relay } }
75
+ EdgeRules.(definitions, findings)
76
+ InterfaceRules.(definitions, findings)
77
+ InputLoads.(definitions, findings)
78
+ PayloadReuse.(definitions, findings)
79
+ ObservedRules.(schema, definitions, findings)
80
+ VisibilityModels.(schema, definitions, findings)
81
+ IntrospectionFields.(schema, findings)
82
+ findings.problem(:model, "Schema", listing_error.message) if listing_error && !findings.problem?(:model)
83
+ findings.report(Reach.new(definitions))
43
84
  end
44
85
 
45
86
  private
46
87
 
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
88
+ # PageInfo holds no data of the app, until a subclass, its parent or an interface adds a field.
89
+ def extended_page_info?(type)
90
+ type < GraphQL::Types::Relay::PageInfo &&
91
+ Definitions.fields_at_every_level(type).any? { |field| !field.owner.equal?(GraphQL::Types::Relay::PageInfo) }
52
92
  end
53
93
 
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)
94
+ # A connection or an edge holds only what graphql-ruby generates, until a subclass, its parent or an interface adds a field,
95
+ # or gives a generated name a resolver of its own.
96
+ def extended_relay?(type)
97
+ GENERATED_RELAY_FIELDS.any? do |relay, names|
98
+ type < relay && Definitions.fields_at_every_level(type).any? { |field| !names.include?(field.original_name) || field.resolver }
73
99
  end
74
100
  end
75
101
 
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))
102
+ def audited_types(definitions)
103
+ definitions.select do |type|
104
+ type.kind.object? && !Definitions.introspection?(type) &&
105
+ (extended_page_info?(type) || extended_relay?(type) || RELAY_TYPES.none? { |relay| type <= relay })
239
106
  end
240
107
  end
241
108