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,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A rule under `observe:` serves what it would refuse, so it is a problem until someone enforces it.
8
+ class ObservedRules
9
+ def self.call(schema, definitions, findings)
10
+ definitions.each do |type|
11
+ rule = type.armory_rule if type.respond_to?(:armory_rule)
12
+ report(findings, type.graphql_name, rule.observe, unobservable(type)) if rule&.observed?
13
+ next unless type.respond_to?(:fields)
14
+
15
+ Definitions.fields_at_every_level(type).each do |field|
16
+ reason = observed_reason(field)
17
+ report(findings, "#{type.graphql_name}.#{field.graphql_name}", reason, field.resolver ? field : nil) if reason
18
+ end
19
+ end
20
+ custom_introspection(schema).each { |field| report(findings, "#{field.owner.graphql_name}.#{field.graphql_name}", field.armory_observe) }
21
+ end
22
+
23
+ def self.report(findings, path, reason, resolver_field = nil)
24
+ effect = if resolver_field
25
+ "raises on every query, because #{resolver_field.graphql_name} has a resolver or a mutation, which can write (#{reason}): remove observe:"
26
+ else
27
+ "serves what this rule would refuse (#{reason}): remove it to enforce the rule"
28
+ end
29
+ findings.problem(:observed, path, "observe: #{effect}")
30
+ end
31
+
32
+ # Its own observe:, or `authorize: true` on an owner whose rule is observed: if that owner is only a parent, no
33
+ # type reports its rule.
34
+ def self.observed_reason(field)
35
+ return field.armory_observe if field.respond_to?(:armory_observe) && field.armory_observe
36
+ return unless field.respond_to?(:armory_declared) && field.armory_declared == true
37
+
38
+ rule = field.owner.armory_rule if field.owner.respond_to?(:armory_rule)
39
+ rule.observe if rule&.observed?
40
+ end
41
+
42
+ def self.unobservable(type)
43
+ type.all_field_definitions.find(&:resolver)
44
+ end
45
+ private_class_method :observed_reason, :unobservable
46
+
47
+ # Definitions holds what these fields return, not the classes that own them.
48
+ def self.custom_introspection(schema)
49
+ system = schema.introspection_system
50
+ (system.entry_points + system.dynamic_fields).select { |field| field.respond_to?(:armory_observe) && field.armory_observe }
51
+ end
52
+ private_class_method :custom_introspection
53
+ private_class_method :report
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # With no Schema::Visibility, a field hidden from a user falls back to another definition of its name, losing its
8
+ # rule, arguments, return type and extensions. Many things can hide it (the field, its resolver, its return type,
9
+ # the schema) and only a request knows, so every redefinition is reported until it declares `override_reason:`.
10
+ class Overrides
11
+ def self.call(schema, definitions, findings)
12
+ new(definitions, findings).(schema)
13
+ end
14
+
15
+ def initialize(definitions = [], findings = Findings.new)
16
+ @definitions = definitions
17
+ @findings = findings
18
+ @override_paths = {}.compare_by_identity
19
+ @falling_back = {}.compare_by_identity
20
+ @duplicates = Set.new.compare_by_identity
21
+ end
22
+
23
+ def call(schema)
24
+ @definitions.each do |type|
25
+ next if Definitions.introspection?(type)
26
+
27
+ note_redefinitions(type.graphql_name, type.ancestors, :own_fields, :original_name)
28
+ note_redefinitions(type.graphql_name, type.ancestors, :own_arguments, :graphql_name) if type.kind.input_object?
29
+ Definitions.fields_at_every_level(type).each do |field|
30
+ levels = [field] + (field.resolver ? field.resolver.ancestors : [])
31
+ note_redefinitions("#{type.graphql_name}.#{field.original_name}", levels, :own_arguments, :graphql_name)
32
+ end
33
+ end
34
+ no_fallback = visibility_without_fallback?(schema)
35
+ @override_paths.each do |member, path|
36
+ falls_back = @falling_back.key?(member) && (!no_fallback || @duplicates.include?(member))
37
+ note_override(falls_back ? @falling_back[member] : path, member, falls_back)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # Nearest level first; only a lone definition on the farthest level has nothing to fall back to. Duplicates on
44
+ # one level are noted apart: a visibility profile skips the levels below, but still picks one of those.
45
+ def note_redefinitions(prefix, ancestors, reader, name_reader)
46
+ levels = ancestors.select { |ancestor| ancestor.respond_to?(reader) }
47
+ levels.flat_map { |level| level.public_send(reader).keys }.uniq.each do |name|
48
+ definitions = levels.filter_map { |level| [level.public_send(reader)[name]].flatten if level.public_send(reader).key?(name) }
49
+ definitions.flatten.each { |member| @override_paths[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
50
+ falling = definitions.last.size == 1 ? definitions.flatten[0...-1] : definitions.flatten
51
+ falling.each { |member| @falling_back[member] ||= "#{prefix}.#{member.public_send(name_reader)}" }
52
+ definitions.select { |level| level.size > 1 }.flatten.each { |member| @duplicates << member }
53
+ end
54
+ end
55
+
56
+ # graphql-ruby skips the fallback to a lower level only when the request carries a visibility profile. A static
57
+ # profile runs with a plain Hash, so only `Schema::Visibility` with no profiles, not turned off below, rules it out.
58
+ def visibility_without_fallback?(schema)
59
+ visibility = schema.visibility
60
+ return false unless visibility
61
+ return false if schema.respond_to?(:use_visibility_profile?) && !schema.use_visibility_profile?
62
+
63
+ visibility.instance_variable_get(:@profiles).to_h.empty?
64
+ end
65
+
66
+ def note_override(path, member, falls_back)
67
+ reason = member.armory_override_reason if member.respond_to?(:armory_override_reason)
68
+ if falls_back && reason
69
+ @findings.bypass(path, reason)
70
+ elsif falls_back
71
+ @findings.problem(:ignored_rule, path, "is defined more than once: hidden from a user, it falls back to the other definition, and its own rule, arguments, type and extensions do not apply. " \
72
+ "Remove one or declare override_reason:; for a definition on an ancestor, Schema::Visibility with dynamic: true and no profiles also stops the fallback")
73
+ elsif reason
74
+ @findings.problem(:ignored_rule, path, "declares override_reason: but nothing falls back from it: remove it")
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A method an app defines wins over what the plugin and graphql-ruby decide: `authorized?` and `authorizes?`
8
+ # can skip a rule, and graphql-ruby calls `unauthorized_object` on whatever loads a record, so returning the
9
+ # object runs the code on a refused record. The audit cannot read them: each is reported once, where it is
10
+ # defined, until it is declared. Methods that come from graphql-ruby or from this gem are not the app's.
11
+ class OwnChecks
12
+ def self.call(schema, definitions, findings)
13
+ new(definitions, findings).(schema.armory_configuration.own_checks)
14
+ end
15
+
16
+ def initialize(definitions = [], findings = Findings.new)
17
+ @definitions = definitions
18
+ @findings = findings
19
+ @found = {}
20
+ end
21
+
22
+ def call(declared)
23
+ @definitions.each do |type|
24
+ next unless type.kind.object? && !Definitions.introspection?(type) && type.respond_to?(:armory_rule)
25
+
26
+ %i[authorized? authorizes? authorized_new wrap].each { |name| note_class_check(type, name) }
27
+ note_class_check(type, :visible?) if type.armory_rule&.visible_when
28
+ Definitions.fields_at_every_level(type).each { |field| note_field_checks(field) }
29
+ end
30
+ @definitions.each do |type|
31
+ next if Definitions.introspection?(type) || !type.respond_to?(:scope_items) || !(type.kind.object? || type.kind.interface? || type.kind.union?)
32
+
33
+ note_class_check(type, :scope_items)
34
+ note_reauthorization(type)
35
+ end
36
+ @definitions.each do |type|
37
+ next unless type.kind.input_object? && !Definitions.introspection?(type)
38
+
39
+ %i[authorized? unauthorized_object].each { |name| note_class_check(type, name) }
40
+ note_argument_checks(type.all_argument_definitions)
41
+ end
42
+ report(declared)
43
+ end
44
+
45
+ private
46
+
47
+ def note_field_checks(field)
48
+ %i[authorized? authorizes? unauthorized_object].each { |name| note_instance_check(field.class, name) }
49
+ note_visibility_check(field)
50
+ note_argument_checks(field.all_argument_definitions)
51
+ resolver = field.resolver
52
+ return unless resolver
53
+
54
+ note_instance_check(resolver, :unauthorized_object)
55
+ note_class_check(resolver, :authorizes?)
56
+ note_instance_check(resolver, :authorized?) if resolver.respond_to?(:armory_rule)
57
+ end
58
+
59
+ def note_argument_checks(arguments)
60
+ arguments.each do |argument|
61
+ note_instance_check(argument.class, :authorized?)
62
+ note_visibility_check(argument)
63
+ end
64
+ end
65
+
66
+ # Off, records that came through the scope skip `authorized?`: harmless only if the scope is as strict as the rule.
67
+ # The interpreter asks the field's return type, Execution::Next the resolved object, and each path differs, so it
68
+ # is reported wherever it is off, without judging whether it skips anything: declaring it takes one line.
69
+ def note_reauthorization(type)
70
+ readers = type.is_a?(Class) ? [type] : type.ancestors.select { |ancestor| ancestor.respond_to?(:reauthorize_scoped_objects, true) }
71
+ readers.each { |reader| note_class_check(reader, :reauthorize_scoped_objects) }
72
+
73
+ setter = reauthorization_setter(type)
74
+ @found[[setter, :reauthorize_scoped_objects]] = "#{setter.name || setter.inspect}.reauthorize_scoped_objects" if setter&.instance_variable_get(:@reauthorize_scoped_objects) == false
75
+ end
76
+
77
+ # The nearest that set the value, read from the classes so no method of the app runs: graphql-ruby's reader follows
78
+ # superclasses, not the interfaces a class implements. A connection reads only its own, then its node's, which
79
+ # `Definitions` reaches and the audit reads as a type of its own.
80
+ def reauthorization_setter(type)
81
+ return type if Definitions.connection?(type)
82
+
83
+ chain = type.is_a?(Class) ? type.ancestors.grep(Class) : type.ancestors
84
+ chain.find { |ancestor| !ancestor.instance_variable_get(:@reauthorize_scoped_objects).nil? }
85
+ end
86
+
87
+ # Apps define `visible?` for their own reasons: it only matters where a `visible_when:` depends on it.
88
+ def note_visibility_check(member)
89
+ note_instance_check(member.class, :visible?) if member.respond_to?(:armory_visible_when) && member.armory_visible_when
90
+ end
91
+
92
+ def note_instance_check(klass, name)
93
+ return unless klass.method_defined?(name) || klass.private_method_defined?(name)
94
+
95
+ note_own_check(klass, klass.instance_method(name), name, "#")
96
+ end
97
+
98
+ def note_class_check(klass, name)
99
+ return unless klass.respond_to?(name, true)
100
+
101
+ note_own_check(klass, klass.method(name), name, ".")
102
+ end
103
+
104
+ # Follows the whole chain: a method that only calls super must not hide one below it that answers true.
105
+ def note_own_check(klass, method, name, separator)
106
+ while method
107
+ holder = LibraryMethods.holder_of(klass, method)
108
+ @found[[holder, name]] = "#{holder.name || holder.inspect}#{separator}#{name}" unless LibraryMethods.library_method?(method, holder, GRAPHQL_RUBY_PATH, ARMORY_PATH)
109
+ method = method.super_method
110
+ end
111
+ end
112
+
113
+ def report(declared)
114
+ @found.each do |(holder, name), path|
115
+ reason = declared.fetch(holder, {})[name]
116
+ next @findings.bypass(path, reason) if reason
117
+
118
+ @findings.problem(:own_check, path, "#{risk(name)}. Remove it, or declare it in own_checks: with a reason")
119
+ end
120
+ declared.each do |holder, methods|
121
+ methods.each_key do |name|
122
+ next if @found.key?([holder, name])
123
+
124
+ @findings.problem(:own_check, "#{holder.name || holder.inspect} #{name}", "named in own_checks: but no check of the schema is defined there: remove it from the option")
125
+ end
126
+ end
127
+ end
128
+
129
+ def risk(name)
130
+ case name
131
+ when :unauthorized_object then "defined by the app: if it returns the object, the code runs on the refused record, and a message it raises reaches the client"
132
+ when :authorizes? then "defined by the app: if it answers false, Execution::Next does not run authorized?, so the rule is skipped"
133
+ when :authorized_new, :wrap then "defined by the app: it decides whether authorized? runs, so on the interpreter it can skip the rule"
134
+ when :scope_items then "defined by the app: it replaces the user's scope, so a list can hold records the policy's scope excludes, and connections count them"
135
+ when :reauthorize_scoped_objects then "set to false: records that come through the scope skip the rule, so a user the rule refuses gets them"
136
+ when :visible? then "defined by the app: if it does not call super, visible_when: does not hide this from a user it should"
137
+ else "defined by the app, it wins over the plugin's: if it does not call super it can skip the rule"
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A payload's fields are covered by the mutation's rule, so they check nothing themselves. A field that returns the
8
+ # payload without being a mutation with a rule serves them with none run: a user the mutation refuses gets the data.
9
+ # It reads the definitions and not the schema's lookups, which filter by visibility and may need a profile.
10
+ class PayloadReuse
11
+ def self.call(definitions, findings)
12
+ new(findings).(definitions)
13
+ end
14
+
15
+ def initialize(findings = Findings.new)
16
+ @findings = findings
17
+ end
18
+
19
+ def call(definitions)
20
+ objects = definitions.select { |type| type.kind.object? }
21
+ fields = objects.flat_map { |type| Definitions.fields_at_every_level(type).map { |field| [type, field] } }
22
+ guarded = fields.filter_map { |_type, field| field.resolver.payload_type if ruled_resolver?(field.resolver) }.uniq
23
+ payloads = objects.select do |type|
24
+ next false if own_rule?(type)
25
+
26
+ guarded.any? { |payload| type <= payload } || generated_by_ruled_mutation?(type) || holds_field_of_ruled_mutation?(type)
27
+ end
28
+ return if payloads.empty?
29
+
30
+ named = definitions.to_h { |type| [type.graphql_name, type] }
31
+ fields.each { |type, field| note(type, field, objects, payloads, named) }
32
+ end
33
+
34
+ private
35
+
36
+ # graphql-ruby marks the payload it generates with its mutation, which the schema may not hold at all.
37
+ def generated_by_ruled_mutation?(type)
38
+ type.ancestors.any? { |ancestor| ancestor.respond_to?(:mutation) && ruled_resolver?(ancestor.mutation) }
39
+ end
40
+
41
+ # A field keeps the mutation that defined it as its owner, wherever it ends up.
42
+ def holds_field_of_ruled_mutation?(type)
43
+ Definitions.fields_at_every_level(type).any? do |field|
44
+ field.respond_to?(:armory_declared) && field.armory_declared.nil? && ruled_resolver?(field.owner)
45
+ end
46
+ end
47
+
48
+ def ruled_resolver?(resolver)
49
+ return false unless resolver.respond_to?(:payload_type) && resolver.respond_to?(:armory_rule)
50
+
51
+ rule = resolver.armory_rule
52
+ !rule.nil? && !rule.none?
53
+ end
54
+
55
+ # A type with a rule of its own checks itself wherever it is returned: `payload_type Types::Invoice`.
56
+ def own_rule?(type)
57
+ return false unless type.respond_to?(:armory_rule)
58
+
59
+ rule = type.armory_rule
60
+ !rule.nil? && !rule.none?
61
+ end
62
+
63
+ def note(type, field, objects, payloads, named)
64
+ target = returned_type(field, named) or return
65
+ reused = members(target, objects) & payloads
66
+ return if reused.empty? || own_payload?(field, target)
67
+
68
+ @findings.problem(:payload_outside_mutation, "#{type.graphql_name}.#{field.original_name}",
69
+ "returns #{reused.map(&:graphql_name).sort.join(", ")}, a mutation's payload, whose fields are covered by that mutation's rule: " \
70
+ "here no such rule runs, so they are served with no check. Return a type of its own, or, for a public child of a mutation with a rule, give the parent's field `authorize: :none, reason: \"...\"`")
71
+ end
72
+
73
+ # A shadowed definition may name a type that never resolved; it cannot serve anything.
74
+ def returned_type(field, named)
75
+ type = field.type.unwrap
76
+ type.is_a?(GraphQL::Schema::LateBoundType) ? named[type.graphql_name] : type
77
+ rescue NameError, GraphQL::Error
78
+ nil
79
+ end
80
+
81
+ def members(target, objects)
82
+ return target.all_possible_types if target.kind.union?
83
+ return objects.select { |type| type <= target } if target.kind.interface?
84
+
85
+ [target]
86
+ end
87
+
88
+ def own_payload?(field, target)
89
+ ruled_resolver?(field.resolver) && field.resolver.payload_type == target
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A type's covers_fields says a rule runs elsewhere, and on a resolver or mutation with no Base none does: it runs
8
+ # with no check. A plain method under the same type is the declaration's own meaning, and it is listed by the type.
9
+ module PlainResolvers
10
+ PROBLEM = "its resolver or mutation does not include GraphQL::Armory::Authorization::Base, so no rule of this gem runs on it and the type's covers_fields serves it, unless its own code refuses. " \
11
+ "Include Base and declare `authorize`, or declare `authorize: :none, reason: \"...\"` on the field"
12
+ PLAIN_FIELD_PROBLEM = "its resolver or mutation does not include GraphQL::Armory::Authorization::Base and neither does its field class, so no rule of this gem runs on it and the type's covers_fields " \
13
+ "serves it, unless its own code refuses. Include Base in the resolver and declare `authorize`, or use a field class that includes Base and declare `authorize: :none, reason: \"...\"`"
14
+
15
+ def self.report(findings, path, field)
16
+ findings.problem(:unprotected, path, field.respond_to?(:armory_declared) ? PROBLEM : PLAIN_FIELD_PROBLEM)
17
+ end
18
+
19
+ def self.covering?(rule)
20
+ !rule.nil? && rule.none? && rule.covers_fields?
21
+ end
22
+
23
+ # A field that names its own rule, or that its parent mutation's rule already answered, does not depend on covers_fields.
24
+ # Under a write root, what it answers with comes after what it did, whichever class it is.
25
+ def self.open?(field, write_root: false)
26
+ resolver = field.resolver
27
+ return false if resolver.nil? || resolver.respond_to?(:armory_rule)
28
+ return false if field.respond_to?(:armory_declared) && !field.armory_declared.nil?
29
+ return false if ruled_owner?(field.owner)
30
+
31
+ write_root || !refused_by_its_type?(field, resolver)
32
+ end
33
+
34
+ def self.ruled_owner?(owner)
35
+ return false unless owner.is_a?(Class) && owner <= GraphQL::Schema::Resolver && owner.respond_to?(:armory_rule)
36
+
37
+ rule = owner.armory_rule
38
+ # With on_denied: :errors a refusal answers a live payload, so its fields still run.
39
+ !rule.nil? && !rule.none? && owner.armory_on_denied != :errors
40
+ end
41
+ private_class_method :ruled_owner?
42
+
43
+ # What a plain read answers with is refused by the rule of that type. A mutation has already written by then, and an
44
+ # interface may gain an implementer with no rule. A type that does not load yet counts as neither.
45
+ def self.refused_by_its_type?(field, resolver)
46
+ return false if resolver <= GraphQL::Schema::Mutation || resolver <= GraphQL::Schema::Subscription
47
+
48
+ ruled?(field.type.unwrap)
49
+ rescue GraphQL::Error, NameError
50
+ false
51
+ end
52
+ private_class_method :refused_by_its_type?
53
+
54
+ def self.ruled?(element)
55
+ return generated_connection?(element) && ruled?(element.node_type) if Definitions.connection?(element)
56
+
57
+ if element.respond_to?(:kind) && element.kind.union?
58
+ members = element.type_memberships.map(&:object_type)
59
+ return !members.empty? && members.all? { |member| ruled?(member) }
60
+ end
61
+ return false unless element.is_a?(Class) && element.respond_to?(:armory_rule)
62
+
63
+ rule = element.armory_rule
64
+ !rule.nil? && !rule.none?
65
+ end
66
+ private_class_method :ruled?
67
+
68
+ # A field a connection or its edge adds is served by that class, whatever the rule on its nodes.
69
+ def self.generated_connection?(connection)
70
+ fields = Definitions.fields_at_every_level(connection)
71
+ edges = fields.select { |field| field.original_name == :edges }.map { |field| field.type.unwrap }
72
+ only_generated?(fields, GENERATED_RELAY_FIELDS.fetch(GraphQL::Types::Relay::BaseConnection)) &&
73
+ edges.all? { |edge| only_generated?(Definitions.fields_at_every_level(edge), GENERATED_RELAY_FIELDS.fetch(GraphQL::Types::Relay::BaseEdge)) }
74
+ end
75
+ private_class_method :generated_connection?
76
+
77
+ def self.only_generated?(fields, names)
78
+ fields.all? { |field| names.include?(field.original_name) && field.resolver.nil? }
79
+ end
80
+ private_class_method :only_generated?
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # What a problem reaches beyond its own name: every definition that is, inherits from or implements what its path
8
+ # names, with their fields and `loads:`. A problem on a type or a base class says nothing about what is added to it
9
+ # later, so a baseline compares this to tell the problem someone accepted from a larger one.
10
+ class Reach
11
+ LIBRARY = [::Object, ::BasicObject, ::Kernel, ::Comparable].freeze
12
+
13
+ def initialize(definitions)
14
+ @definitions = definitions
15
+ end
16
+
17
+ def call(path)
18
+ head, member = path.delete_suffix(")").split(/[.(]/)
19
+ @definitions.select { |type| holds?(type, head) }.flat_map { |type| of(type, member) }.sort
20
+ end
21
+
22
+ private
23
+
24
+ def holds?(type, head)
25
+ type.graphql_name == head || type.ancestors.any? { |ancestor| app_class?(ancestor) && (ancestor.name == head || named?(ancestor, head)) }
26
+ end
27
+
28
+ # Ruby's and graphql-ruby's own classes are in every type's ancestors, and graphql-ruby's answer `graphql_name` too: a
29
+ # type named `Object` or `Member` must not reach everything.
30
+ def app_class?(ancestor)
31
+ !LIBRARY.include?(ancestor) && !ancestor.name&.start_with?("GraphQL::")
32
+ end
33
+
34
+ def named?(ancestor, head)
35
+ ancestor.respond_to?(:graphql_name) && ancestor.graphql_name == head
36
+ rescue GraphQL::Error
37
+ false # an anonymous class with no name of its own
38
+ end
39
+
40
+ # A problem on one field reaches that field: the others of its type have their own problems.
41
+ def of(type, member)
42
+ label = type.name || type.graphql_name
43
+ return ["type:#{label}", *input_loads(label, type)] if type.kind.input_object?
44
+ return ["type:#{label}"] unless type.respond_to?(:fields)
45
+
46
+ fields = Definitions.fields_at_every_level(type)
47
+ fields = fields.select { |field| field.graphql_name == member || field.original_name.to_s == member } if member
48
+ ["type:#{label}", *fields.flat_map { |field| of_field(label, field) }]
49
+ end
50
+
51
+ def input_loads(label, type)
52
+ type.all_argument_definitions.filter_map { |argument| argument.loads && "loads:#{label}.#{argument.graphql_name}:#{argument.loads.graphql_name}" }
53
+ end
54
+
55
+ def of_field(label, field)
56
+ ["field:#{label}.#{field.graphql_name}:#{signature(field)}",
57
+ *field.all_argument_definitions.filter_map { |argument| loads_of(label, field, argument) }]
58
+ end
59
+
60
+ # A field whose type cannot be read is what some problems are about: it must not stop the audit.
61
+ def signature(field)
62
+ field.type.to_type_signature
63
+ rescue StandardError, LoadError
64
+ "unreadable"
65
+ end
66
+
67
+ def loads_of(label, field, argument)
68
+ loads = argument.loads
69
+ "loads:#{label}.#{field.graphql_name}.#{argument.graphql_name}:#{loads.graphql_name}" if loads
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -16,12 +16,18 @@ module GraphQL
16
16
 
17
17
  attr_reader :problems, :bypasses
18
18
 
19
- def initialize(problems:, bypasses:)
19
+ def initialize(problems:, bypasses:, reach: nil)
20
20
  @problems = problems.freeze
21
21
  @bypasses = bypasses.freeze
22
+ @reach = reach
22
23
  freeze
23
24
  end
24
25
 
26
+ # What a problem reaches beyond its own name: see Reach. Worked out only for a baseline, so a plain audit never reads it.
27
+ def reach_of(problem)
28
+ @reach ? @reach.(problem.path) : []
29
+ end
30
+
25
31
  def ok?
26
32
  problems.empty?
27
33
  end