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
@@ -8,12 +8,13 @@ module GraphQL
8
8
  class Substitute
9
9
  attr_reader :asked, :scoped_for
10
10
 
11
- def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil)
11
+ def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil, raises_on_check: nil)
12
12
  @allow = allow
13
13
  @actor = actor
14
14
  @scoped = scoped
15
15
  @raises = raises
16
16
  @raises_on_scope = raises_on_scope
17
+ @raises_on_check = raises_on_check
17
18
  @asked = []
18
19
  @scoped_for = []
19
20
  end
@@ -37,6 +38,8 @@ module GraphQL
37
38
  end
38
39
 
39
40
  def check(_type, _rule)
41
+ raise @raises_on_check if @raises_on_check
42
+
40
43
  []
41
44
  end
42
45
  end
@@ -0,0 +1,191 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Authorization
8
+ class Audit
9
+ # The problems a team accepted for now, so adopting the gem on a large schema fails the build only on new ones.
10
+ # A report's `ok?` never changes because of it: a schema with problems is not ok, with or without a baseline.
11
+ #
12
+ # Baseline.load("config/graphql_armory_baseline.json").(report).passes?
13
+ #
14
+ # An entry accepts one problem as it was: how many times it appeared, and what it reached (see Reach). A problem that
15
+ # grew, because a field or a subclass was added to what it covers, is not the problem someone accepted.
16
+ class Baseline
17
+ VERSION = 1
18
+ Entry = Struct.new(:kind, :path, :message, :count, :covers) do
19
+ def key = [kind, path, message]
20
+ def to_h = { "kind" => kind, "path" => path, "message" => message, "count" => count, "covers" => covers }
21
+ end
22
+
23
+ # What a report comes to against a baseline. It has no `ok?`, so nobody reads a pass as a clean schema.
24
+ class Result
25
+ attr_reader :report, :new_problems, :accepted, :gone, :grown
26
+
27
+ def initialize(report:, new_problems:, accepted:, gone:, grown: {})
28
+ @report = report
29
+ @new_problems = new_problems.freeze
30
+ @accepted = accepted.freeze
31
+ @gone = gone.freeze
32
+ @grown = grown.freeze
33
+ freeze
34
+ end
35
+
36
+ # An accepted problem that is gone fails too: left in the file, it could come back unseen.
37
+ def passes?
38
+ new_problems.empty? && gone.empty?
39
+ end
40
+
41
+ def to_s
42
+ sections = [
43
+ section("#{new_problems.size} new #{plural('problem', new_problems.size)}:", new_problems.map { |problem| new_line(problem) }),
44
+ section("#{gone.size} accepted #{plural('problem', gone.size)} no longer found (remove them from the baseline):", gone.map { |entry| "#{entry.path} (#{entry.kind}): #{entry.message}" }),
45
+ section("#{accepted.size} accepted in the baseline, still open:", accepted),
46
+ bypasses_text
47
+ ].compact
48
+ sections.unshift("Passes only because of its baseline.") if passes? && !accepted.empty?
49
+ sections.unshift("No problems.") if passes? && accepted.empty?
50
+ "#{sections.join("\n\n")}\n"
51
+ end
52
+
53
+ private
54
+
55
+ def new_line(problem)
56
+ extra = grown[problem]
57
+ extra ? "#{problem} (it now also reaches #{extra.join(', ')})" : problem.to_s
58
+ end
59
+
60
+ def section(title, lines)
61
+ return if lines.empty?
62
+
63
+ [title, *lines.map { |line| " #{line}" }].join("\n")
64
+ end
65
+
66
+ def bypasses_text
67
+ section("#{report.bypasses.size} declared #{report.bypasses.size == 1 ? 'bypass' : 'bypasses'}:", report.bypasses.map { |bypass| "#{bypass.path}: #{bypass.reason}" })
68
+ end
69
+
70
+ def plural(word, count)
71
+ count == 1 ? word : "#{word}s"
72
+ end
73
+ end
74
+
75
+ # Every problem of a report, accepted as it is now.
76
+ def self.from(report)
77
+ new(report.problems.group_by { |problem| [problem.kind.to_s, problem.path, problem.message] }.map do |(kind, path, message), problems|
78
+ Entry.new(kind, path, message, problems.size, report.reach_of(problems.first))
79
+ end)
80
+ end
81
+
82
+ def self.load(path)
83
+ data = begin
84
+ JSON.parse(File.read(path, mode: "r:bom|utf-8"))
85
+ rescue JSON::ParserError, ArgumentError, EncodingError
86
+ # Not the parser's message: it quotes the file's content.
87
+ raise ConfigurationError, "the baseline #{path} is not valid JSON"
88
+ rescue SystemCallError => error
89
+ raise ConfigurationError, "the baseline #{path} cannot be read (#{error.class})"
90
+ end
91
+ unless data.is_a?(Hash) && data["version"].eql?(VERSION) && data["problems"].is_a?(Array)
92
+ raise ConfigurationError, "the baseline #{path} needs version #{VERSION} and a list of problems"
93
+ end
94
+
95
+ new(data["problems"].map { |entry| entry_from(entry, path) })
96
+ end
97
+
98
+ # Creates the file with every problem. A file that exists only ever loses what is gone: accepting a new problem is
99
+ # a manual edit, so it shows in review. Answers what was written.
100
+ def self.write(report, path)
101
+ baseline = from(report)
102
+ kept = File.exist?(path) ? load(path).kept_from(baseline) : baseline
103
+ File.write(path, "#{JSON.pretty_generate('version' => VERSION, 'problems' => kept.entries.map(&:to_h))}\n")
104
+ kept
105
+ rescue SystemCallError => error
106
+ raise ConfigurationError, "the baseline #{path} cannot be written (#{error.class})"
107
+ end
108
+
109
+ # With no count or covers an entry accepts the narrowest thing: one problem that reaches nothing.
110
+ def self.entry_from(entry, path)
111
+ unless entry.is_a?(Hash) && %w[kind path message].all? { |key| entry[key].is_a?(String) }
112
+ raise ConfigurationError, "the baseline #{path} needs a kind, a path and a message in each problem"
113
+ end
114
+
115
+ count = entry.fetch("count", 1)
116
+ covers = entry.fetch("covers", [])
117
+ unless count.is_a?(Integer) && count.positive? && covers.is_a?(Array) && covers.all?(String)
118
+ raise ConfigurationError, "the baseline #{path} needs a positive count and a list of Strings in covers"
119
+ end
120
+
121
+ Entry.new(*entry.values_at("kind", "path", "message"), count, covers)
122
+ end
123
+ private_class_method :entry_from
124
+
125
+ attr_reader :entries
126
+
127
+ # What a baseline never accepts: with no plugin, no rule runs at all, so there is nothing to adopt gradually; an own
128
+ # check or a denial hook reaches whatever uses it later, which no entry can pin down, and each already has its
129
+ # declaration with a reason (`own_checks:`, `own_denial_hooks:`).
130
+ REFUSED = { "plugin" => "a baseline cannot accept a schema that never installed the plugin: install it first",
131
+ "own_check" => "a baseline cannot accept an own check: remove it, or declare it in own_checks: with a reason",
132
+ "hook" => "a baseline cannot accept a denial hook: remove it, or declare it in own_denial_hooks: with a reason",
133
+ "observed" => "a baseline cannot accept observe: it serves what the rule would refuse, and it already says why" }.freeze
134
+
135
+ # `new` is inert: it accepts nothing, so every problem is new.
136
+ def initialize(entries = [])
137
+ refused = entries.find { |entry| REFUSED.key?(entry.kind) }
138
+ raise ConfigurationError, "#{REFUSED.fetch(refused.kind)} (#{refused.path})" if refused
139
+
140
+ @entries = entries.map { |entry| Entry.new(entry.kind, entry.path, entry.message, entry.count || 1, entry.covers || []) }.uniq(&:key).sort_by(&:to_a).freeze
141
+ end
142
+
143
+ def call(report)
144
+ by_key = entries.to_h { |entry| [entry.key, entry] }
145
+ seen = Hash.new(0)
146
+ grown = {}
147
+ accepted, new_problems = report.problems.partition do |problem|
148
+ entry = by_key[key_of(problem)]
149
+ next false unless entry
150
+
151
+ extra = beyond(report.reach_of(problem), entry.covers)
152
+ grown[problem] = extra unless extra.empty?
153
+ extra.empty? && (seen[entry.key] += 1) <= entry.count
154
+ end
155
+ found = report.problems.map { |problem| key_of(problem) }
156
+ Result.new(report: report, new_problems: new_problems, accepted: accepted, grown: grown,
157
+ gone: entries.reject { |entry| found.include?(entry.key) })
158
+ end
159
+
160
+ # This baseline without what `other` no longer holds: nothing grows.
161
+ def kept_from(other)
162
+ current = other.entries.to_h { |entry| [entry.key, entry] }
163
+ self.class.new(entries.filter_map do |entry|
164
+ now = current[entry.key]
165
+ next unless now
166
+
167
+ Entry.new(entry.kind, entry.path, entry.message, [entry.count, now.count].min, common(entry.covers, now.covers))
168
+ end)
169
+ end
170
+
171
+ private
172
+
173
+ def key_of(problem)
174
+ [problem.kind.to_s, problem.path, problem.message]
175
+ end
176
+
177
+ # What `now` holds beyond `accepted`, counting repeats: two classes may share a name.
178
+ def beyond(now, accepted)
179
+ left = accepted.tally
180
+ now.reject { |item| left[item].to_i.positive? && (left[item] -= 1) }
181
+ end
182
+
183
+ def common(one, other)
184
+ left = other.tally
185
+ one.select { |item| left[item].to_i.positive? && (left[item] -= 1) }
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -6,25 +6,74 @@ module GraphQL
6
6
  class Audit
7
7
  # What `rake graphql_armory:audit[YourSchema]` runs: prints the report and answers an exit status.
8
8
  class Command
9
+ # The rake task's action. The app's Rake file may name the task first, to add a prerequisite, so the task existing does
10
+ # not mean this ran: the file looks for this object among its actions.
11
+ RAKE_ACTION = proc do |_task, args|
12
+ Rake::Task[:environment].invoke if Rake::Task.task_defined?(:environment)
13
+ # By position: an app that declares the task again renames its arguments.
14
+ status = new.(args[:schema], args.to_a[1])
15
+ exit(status) unless status.zero?
16
+ end
17
+
18
+ # The baseline task's action: writes the file, or removes from it what is gone.
19
+ BASELINE_ACTION = proc do |_task, args|
20
+ Rake::Task[:environment].invoke if Rake::Task.task_defined?(:environment)
21
+ status = new.write_baseline(args[:schema], args.to_a[1])
22
+ exit(status) unless status.zero?
23
+ end
24
+
9
25
  def initialize(out: $stdout)
10
26
  @out = out
11
27
  end
12
28
 
13
- def call(schema_name)
14
- return fail_with("Name the schema: rake graphql_armory:audit[YourSchema]") if schema_name.to_s.empty?
29
+ def call(schema_name, baseline = nil)
30
+ with_schema(schema_name, "audit") do |schema|
31
+ report = Audit.(schema)
32
+ next judged(report, baseline) unless baseline.to_s.empty?
33
+
34
+ @out.print(report)
35
+ report.ok? ? 0 : 1
36
+ end
37
+ end
38
+
39
+ def write_baseline(schema_name, path)
40
+ return fail_with("Name the file: rake graphql_armory:audit_baseline[YourSchema,path]") if path.to_s.empty?
41
+
42
+ with_schema(schema_name, "audit_baseline") do |schema|
43
+ wrote(Audit.(schema), path)
44
+ end
45
+ end
46
+
47
+ private
15
48
 
16
- schema = Object.const_get(schema_name)
17
- report = Audit.(schema)
18
- @out.print(report)
19
- report.ok? ? 0 : 1
49
+ def judged(report, baseline)
50
+ result = Baseline.load(baseline).(report)
51
+ @out.print(result)
52
+ result.passes? ? 0 : 1
53
+ rescue ConfigurationError => error
54
+ fail_with(error.message)
55
+ end
56
+
57
+ def wrote(report, path)
58
+ written = Baseline.write(report, path)
59
+ not_accepted = Baseline.new(written.entries).(report).new_problems.size
60
+ @out.puts("#{path}: #{written.entries.size} accepted. Fix them: this list only ever shrinks.")
61
+ @out.puts("#{not_accepted} not accepted: they still fail the audit.") if not_accepted.positive?
62
+ 0
63
+ rescue ConfigurationError => error
64
+ fail_with(error.message)
65
+ end
66
+
67
+ def with_schema(schema_name, task)
68
+ return fail_with("Name the schema: rake graphql_armory:#{task}[YourSchema]") if schema_name.to_s.empty?
69
+
70
+ yield Object.const_get(schema_name)
20
71
  rescue NameError => error
21
72
  raise unless error.name.to_s == schema_name.to_s.split("::").last
22
73
 
23
74
  fail_with("No schema named #{schema_name}")
24
75
  end
25
76
 
26
- private
27
-
28
77
  def fail_with(message)
29
78
  @out.puts(message)
30
79
  1
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A method the schema defines on itself wins over the plugin's, so `on_unauthorized:` no longer decides
8
+ # what that denial looks like. The check runs here because `use` usually comes before those methods.
9
+ module DenialHooks
10
+ def self.call(schema, findings)
11
+ declared = schema.armory_configuration.own_denial_hooks
12
+ Configuration::DENIAL_HOOKS.each do |hook|
13
+ path = "Schema.#{hook}"
14
+ own = schema.method(hook).owner != Integration::Schema
15
+ if own && declared.key?(hook)
16
+ findings.bypass(path, declared[hook])
17
+ elsif own
18
+ findings.problem(:hook, path, "the schema's own method runs instead of the plugin's, so on_unauthorized: does not apply and a denial " \
19
+ "may reveal what it hides: remove it, or declare it in own_denial_hooks: with a reason")
20
+ elsif declared.key?(hook)
21
+ findings.problem(:hook, path, "named in own_denial_hooks: but the plugin's hook is the one that runs: remove it from the option")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A direct edge subclass runs the plugin's check, but only where an edge is built: `nodes` never builds one.
8
+ module EdgeRules
9
+ def self.call(definitions, findings)
10
+ definitions.each do |type|
11
+ next unless type.kind.object? && type <= GraphQL::Types::Relay::BaseEdge
12
+
13
+ rule = type.armory_rule if type.respond_to?(:armory_rule)
14
+ next if rule.nil? || rule.none? || !nodes_field_beside?(type, definitions)
15
+
16
+ findings.problem(:ignored_rule, type.graphql_name, "the connection's nodes field returns the nodes without building an edge, so this rule does not " \
17
+ "cover it: put the rule on the field that returns the connection (on a root field, on a resolver: " \
18
+ "a field rule asks the parent's policy), or remove the nodes field with has_nodes_field(false)")
19
+ end
20
+ end
21
+
22
+ def self.nodes_field_beside?(edge, definitions)
23
+ definitions.any? do |type|
24
+ next false unless type.kind.object? && !Definitions.introspection?(type)
25
+
26
+ fields = type.all_field_definitions
27
+ fields.any? { |field| field.graphql_name == "nodes" } &&
28
+ fields.any? { |field| field.graphql_name == "edges" && field.type.unwrap == edge }
29
+ end
30
+ end
31
+ private_class_method :nodes_field_beside?
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # Collects what one audit finds.
8
+ class Findings
9
+ def initialize
10
+ @problems = []
11
+ @bypasses = []
12
+ end
13
+
14
+ def problem(kind, path, message)
15
+ @problems << Report::Problem.new(kind, path, message)
16
+ end
17
+
18
+ def problem?(kind, path = nil)
19
+ @problems.any? { |problem| problem.kind == kind && (path.nil? || problem.path == path) }
20
+ end
21
+
22
+ def bypass(path, reason)
23
+ @bypasses << Report::Bypass.new(path, reason)
24
+ end
25
+
26
+ def report(reach = nil)
27
+ Report.new(problems: @problems.sort_by { |problem| [problem.path, problem.kind.to_s] },
28
+ bypasses: @bypasses.sort_by(&:path),
29
+ reach: reach)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -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,42 @@
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
+ implementers = definitions.select { |type| type.kind.object? && type <= interface }
19
+ bypassed = Integration.any_bypass?(implementers)
20
+ covered = implementers.any? { |type| type.respond_to?(:armory_rule) && PlainResolvers.covering?(type.armory_rule) }
21
+ interface.own_fields.values.flatten.each do |field|
22
+ path = "#{interface.graphql_name}.#{field.original_name}"
23
+ PlainResolvers.report(findings, path, field) if covered && !bypassed && PlainResolvers.open?(field)
24
+ unless field.respond_to?(:armory_declared)
25
+ findings.problem(:unprotected, path, "its field class does not include Base, so it is not checked on a bypassed implementer") if bypassed
26
+ next
27
+ end
28
+
29
+ case field.armory_declared
30
+ when true
31
+ findings.problem(:field_without_rule, path, "uses authorize: true, but an interface has no action of its own, so it is always denied")
32
+ when nil
33
+ findings.problem(:field_without_rule, path, "declares no rule and an implementing type is bypassed, so it is denied") if bypassed
34
+ end
35
+ end
36
+ end
37
+ private_class_method :audit
38
+ end
39
+ end
40
+ end
41
+ end
42
+ 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