graphql-armory 0.1.0.pre8 → 0.1.0.pre10
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +64 -0
- data/README.md +7 -7
- data/docs/authorization.md +123 -5
- data/docs/encrypted-ids.md +1 -1
- data/docs/keyset-connections.md +28 -4
- data/docs/persisted-queries.md +111 -19
- data/docs/rate-limiting.md +37 -13
- data/docs/telemetry.md +31 -8
- data/exe/graphql-armory-publish +6 -0
- data/lib/graphql/armory/authorization/adapters/cancan.rb +7 -3
- data/lib/graphql/armory/authorization/adapters/pundit.rb +8 -1
- data/lib/graphql/armory/authorization/audit/baseline.rb +191 -0
- data/lib/graphql/armory/authorization/audit/command.rb +57 -8
- data/lib/graphql/armory/authorization/audit/findings.rb +3 -2
- data/lib/graphql/armory/authorization/audit/interface_rules.rb +4 -1
- data/lib/graphql/armory/authorization/audit/observed_rules.rb +58 -0
- data/lib/graphql/armory/authorization/audit/payload_reuse.rb +95 -0
- data/lib/graphql/armory/authorization/audit/plain_resolvers.rb +85 -0
- data/lib/graphql/armory/authorization/audit/reach.rb +75 -0
- data/lib/graphql/armory/authorization/audit/report.rb +7 -1
- data/lib/graphql/armory/authorization/audit/type_rules.rb +19 -2
- data/lib/graphql/armory/authorization/audit.rb +12 -4
- data/lib/graphql/armory/authorization/authorize.rb +28 -8
- data/lib/graphql/armory/authorization/configuration.rb +4 -3
- data/lib/graphql/armory/authorization/definitions.rb +7 -0
- data/lib/graphql/armory/authorization/integration/field.rb +26 -4
- data/lib/graphql/armory/authorization/integration/object.rb +8 -0
- data/lib/graphql/armory/authorization/integration/resolver.rb +2 -0
- data/lib/graphql/armory/authorization/integration.rb +14 -3
- data/lib/graphql/armory/authorization/rspec.rb +10 -4
- data/lib/graphql/armory/authorization/rule.rb +19 -3
- data/lib/graphql/armory/authorization/scope.rb +6 -6
- data/lib/graphql/armory/authorization.rb +1 -1
- data/lib/graphql/armory/ids.rb +11 -11
- data/lib/graphql/armory/keyset/encoder_check.rb +113 -0
- data/lib/graphql/armory/keyset.rb +23 -3
- data/lib/graphql/armory/persisted_queries/manifest.rb +2 -0
- data/lib/graphql/armory/persisted_queries/manifests.rb +69 -0
- data/lib/graphql/armory/persisted_queries/publisher.rb +127 -0
- data/lib/graphql/armory/persisted_queries/redis_store.rb +137 -0
- data/lib/graphql/armory/persisted_queries.rb +55 -16
- data/lib/graphql/armory/railtie.rb +10 -0
- data/lib/graphql/armory/rate_limits/analysis_guard.rb +175 -0
- data/lib/graphql/armory/rate_limits/concurrency.rb +93 -14
- data/lib/graphql/armory/rate_limits/invalid_queries.rb +75 -0
- data/lib/graphql/armory/rate_limits/points.rb +129 -15
- data/lib/graphql/armory/rate_limits/time.rb +140 -22
- data/lib/graphql/armory/rate_limits.rb +30 -5
- data/lib/graphql/armory/telemetry.rb +71 -9
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory.rb +1 -0
- data/lib/tasks/graphql_armory.rake +11 -4
- metadata +17 -3
|
@@ -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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
@out.print(
|
|
19
|
-
|
|
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
|
|
@@ -23,9 +23,10 @@ module GraphQL
|
|
|
23
23
|
@bypasses << Report::Bypass.new(path, reason)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def report
|
|
26
|
+
def report(reach = nil)
|
|
27
27
|
Report.new(problems: @problems.sort_by { |problem| [problem.path, problem.kind.to_s] },
|
|
28
|
-
bypasses: @bypasses.sort_by(&:path)
|
|
28
|
+
bypasses: @bypasses.sort_by(&:path),
|
|
29
|
+
reach: reach)
|
|
29
30
|
end
|
|
30
31
|
end
|
|
31
32
|
end
|
|
@@ -15,9 +15,12 @@ module GraphQL
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def self.audit(interface, definitions, findings)
|
|
18
|
-
|
|
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) }
|
|
19
21
|
interface.own_fields.values.flatten.each do |field|
|
|
20
22
|
path = "#{interface.graphql_name}.#{field.original_name}"
|
|
23
|
+
PlainResolvers.report(findings, path, field) if covered && !bypassed && PlainResolvers.open?(field)
|
|
21
24
|
unless field.respond_to?(:armory_declared)
|
|
22
25
|
findings.problem(:unprotected, path, "its field class does not include Base, so it is not checked on a bypassed implementer") if bypassed
|
|
23
26
|
next
|
|
@@ -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,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
|