graphql-armory 0.1.0.pre7 → 0.1.0.pre8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -1
  3. data/README.md +5 -4
  4. data/docs/authorization.md +133 -6
  5. data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
  6. data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
  7. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  8. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  9. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  10. data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
  11. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  12. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  13. data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
  14. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  15. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  16. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  17. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  18. data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
  19. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  20. data/lib/graphql/armory/authorization/audit.rb +50 -191
  21. data/lib/graphql/armory/authorization/authorize.rb +9 -3
  22. data/lib/graphql/armory/authorization/configuration.rb +30 -2
  23. data/lib/graphql/armory/authorization/definitions.rb +114 -0
  24. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  25. data/lib/graphql/armory/authorization/integration/field.rb +13 -5
  26. data/lib/graphql/armory/authorization/integration/object.rb +1 -1
  27. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  28. data/lib/graphql/armory/authorization/integration.rb +22 -1
  29. data/lib/graphql/armory/authorization/rule.rb +5 -0
  30. data/lib/graphql/armory/authorization.rb +4 -1
  31. data/lib/graphql/armory/version.rb +1 -1
  32. metadata +14 -1
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A rule asks the policy of the class that defines it, and a field never sees its resolved type: a rule on an
8
+ # interface, a parent, an input object or a plain resolver does not ask the `policy:` of the type that uses it.
9
+ class ForeignPolicies
10
+ def initialize(adapter = Adapters::Null.new, findings = Findings.new)
11
+ @adapter = adapter
12
+ @findings = findings
13
+ @seen = Set.new
14
+ end
15
+
16
+ def call(type, policy)
17
+ Definitions.fields_at_every_level(type).each do |field|
18
+ path = "#{type.graphql_name}.#{field.original_name}"
19
+ note(field, path, policy, model_or_nil(type)) if field.respond_to?(:armory_declared)
20
+ note_resolver(field.resolver, path, policy) if field.resolver.respond_to?(:armory_rule)
21
+ arguments(field.all_argument_definitions, path, policy, Set.new)
22
+ end
23
+ end
24
+
25
+ # A connection or edge is not audited as a type, but a field rule on it runs under the same terms.
26
+ def relay(type)
27
+ policy = type.armory_rule&.policy if type.respond_to?(:armory_rule)
28
+ call(type, policy) if policy
29
+ end
30
+
31
+ # A class without Base has no rule of its own, but what it leads to still runs its rules. An argument, a Base
32
+ # resolver's included, asks the policy of the class that defines it, not of the type that uses it.
33
+ def arguments(arguments, path, policy, seen)
34
+ arguments.each do |argument|
35
+ if argument.respond_to?(:armory_declared)
36
+ note(argument, "#{path}(#{argument.keyword})", policy, model_or_nil(argument))
37
+ end
38
+ input = argument.type.unwrap
39
+ next unless input.respond_to?(:kind) && input.kind.input_object? && seen.add?(input)
40
+
41
+ arguments(input.all_argument_definitions, path, policy, seen)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ # A Base resolver covers its field and asks its own policy, so the type's `policy:` never sees that rule.
48
+ def note_resolver(resolver, path, policy)
49
+ rule = resolver.armory_rule
50
+ return if rule.nil? || rule.none?
51
+
52
+ asked = rule.policy
53
+ same = asked ? asked.equal?(policy) : found?(model_or_nil(resolver), policy)
54
+ return if same || !@seen.add?([resolver, path, policy])
55
+
56
+ @findings.problem(:foreign_policy, path, "its resolver's rule asks #{asked || "the policy found from its model"}, not #{policy}, the `policy:` of this type. " \
57
+ "Name the same `policy:` on the resolver")
58
+ end
59
+
60
+ def note(member, path, policy, model)
61
+ ruled = member.armory_visible_when || ![nil, :none].include?(member.armory_declared)
62
+ return if !ruled || asks_policy?(member, policy, model) || !@seen.add?([member, path, policy])
63
+
64
+ asked = member.armory_policy || "the policy found from the record"
65
+ @findings.problem(:foreign_policy, path, "its rule asks #{asked}, not #{policy}, the `policy:` of this type: it is defined on a class that does not name that policy. " \
66
+ "Move the rule to the class that names it and remove the original, or name the `policy:` on the class that defines the rule, if it can hold one")
67
+ end
68
+
69
+ # With no `policy:` of its own, a rule asks what the adapter finds from the model, which may be the same policy.
70
+ def asks_policy?(member, policy, model)
71
+ asked = member.armory_policy
72
+ return asked.equal?(policy) if asked
73
+
74
+ found?(model, policy)
75
+ end
76
+
77
+ def found?(model, policy)
78
+ !model.nil? && @adapter.respond_to?(:found_policy) && @adapter.found_policy(model).equal?(policy)
79
+ end
80
+
81
+ def model_or_nil(owner)
82
+ owner.armory_model
83
+ rescue ConfigurationError
84
+ nil
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # An input object loads its own `loads:` arguments; without Base they skip the user's scope.
8
+ module InputLoads
9
+ def self.call(definitions, findings)
10
+ definitions.each do |input|
11
+ next unless input.kind.input_object? && !Definitions.introspection?(input)
12
+ next if input.respond_to?(:armory_scoped_loads?)
13
+
14
+ # A mutation's generated input (`from_resolver`) is loaded, scoped, by the mutation itself.
15
+ input.all_argument_definitions.select(&:loads).reject(&:from_resolver?).each do |argument|
16
+ name = GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)
17
+ findings.problem(:unscoped_load, "#{input.graphql_name}(#{name})",
18
+ "its input object class does not include Base, so it loads through the app's object_from_id, not the user's scope")
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # Interface fields are reported once, on the interface that declares them, not on every implementer.
8
+ # Undeclared ones are denied when an implementer is a bypass; ones whose field class lacks Base are
9
+ # never checked, so on a bypassed implementer they are open; `authorize: true` names no action.
10
+ module InterfaceRules
11
+ def self.call(definitions, findings)
12
+ definitions.each do |type|
13
+ audit(type, definitions, findings) if type.kind.interface? && !Definitions.introspection?(type)
14
+ end
15
+ end
16
+
17
+ def self.audit(interface, definitions, findings)
18
+ bypassed = Integration.any_bypass?(definitions.select { |type| type.kind.object? && type <= interface })
19
+ interface.own_fields.values.flatten.each do |field|
20
+ path = "#{interface.graphql_name}.#{field.original_name}"
21
+ unless field.respond_to?(:armory_declared)
22
+ findings.problem(:unprotected, path, "its field class does not include Base, so it is not checked on a bypassed implementer") if bypassed
23
+ next
24
+ end
25
+
26
+ case field.armory_declared
27
+ when true
28
+ findings.problem(:field_without_rule, path, "uses authorize: true, but an interface has no action of its own, so it is always denied")
29
+ when nil
30
+ findings.problem(:field_without_rule, path, "declares no rule and an implementing type is bypassed, so it is denied") if bypassed
31
+ end
32
+ end
33
+ end
34
+ private_class_method :audit
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # `Schema.introspection` can add or redefine entry points and dynamic fields. A client reaches them from any type,
8
+ # with no type of the app around them, so no type's rule runs. The built-in ones hold no data of the app.
9
+ class IntrospectionFields
10
+ BUILT_IN = { EntryPoints: :entry_points, DynamicFields: :dynamic_fields }.freeze
11
+
12
+ def self.call(schema, findings)
13
+ new(findings).(schema)
14
+ end
15
+
16
+ def initialize(findings = Findings.new)
17
+ @findings = findings
18
+ end
19
+
20
+ def call(schema)
21
+ system = schema.introspection_system
22
+ BUILT_IN.each do |holder, reader|
23
+ system.public_send(reader).each { |field| note(field, GraphQL::Introspection.const_get(holder)) }
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # graphql-ruby serves a copy of the holder's fields, on a copy of the holder.
30
+ def note(field, built_in)
31
+ name = field.graphql_name
32
+ return if field.owner.superclass.fields[name].equal?(built_in.fields[name])
33
+
34
+ @findings.problem(:unprotected, "#{field.owner.graphql_name}.#{name}", "is a custom introspection field, so no type's rule runs on it: move it to a type with a rule")
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # Tells a library's method from an app's.
8
+ module LibraryMethods
9
+ module_function
10
+
11
+ def holder_of(klass, method)
12
+ owner = method.owner
13
+ klass.ancestors.find { |ancestor| ancestor.singleton_class == owner } || owner
14
+ end
15
+
16
+ def library_method?(method, holder, *paths)
17
+ file = method.source_location&.first
18
+ !file.nil? && file.start_with?(*paths) && holder.name&.start_with?("GraphQL::") == true
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ 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