graphql-armory 0.1.0.pre9 → 0.1.0.pre11
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 +35 -0
- data/README.md +5 -5
- data/docs/authorization.md +45 -3
- data/docs/keyset-connections.md +28 -4
- data/docs/rate-limiting.md +7 -4
- data/lib/graphql/armory/authorization/audit/baseline.rb +3 -3
- data/lib/graphql/armory/authorization/audit/denial_hooks.rb +3 -3
- data/lib/graphql/armory/authorization/audit/edge_rules.rb +1 -1
- data/lib/graphql/armory/authorization/audit/findings.rb +5 -4
- data/lib/graphql/armory/authorization/audit/foreign_policies.rb +2 -2
- data/lib/graphql/armory/authorization/audit/input_loads.rb +1 -1
- data/lib/graphql/armory/authorization/audit/interface_rules.rb +3 -3
- data/lib/graphql/armory/authorization/audit/introspection_fields.rb +1 -1
- data/lib/graphql/armory/authorization/audit/observed_rules.rb +7 -17
- data/lib/graphql/armory/authorization/audit/overrides.rb +3 -3
- data/lib/graphql/armory/authorization/audit/own_checks.rb +6 -6
- data/lib/graphql/armory/authorization/audit/payload_reuse.rb +1 -1
- data/lib/graphql/armory/authorization/audit/plain_resolvers.rb +1 -1
- data/lib/graphql/armory/authorization/audit/report.rb +11 -4
- data/lib/graphql/armory/authorization/audit/type_rules.rb +32 -31
- data/lib/graphql/armory/authorization/audit/visibility_models.rb +5 -5
- data/lib/graphql/armory/authorization/audit.rb +2 -2
- data/lib/graphql/armory/authorization/integration/argument.rb +3 -0
- data/lib/graphql/armory/authorization/integration/field.rb +4 -0
- data/lib/graphql/armory/authorization/integration/object.rb +5 -1
- data/lib/graphql/armory/authorization/integration/resolver.rb +4 -0
- data/lib/graphql/armory/authorization/integration/root_check.rb +35 -0
- data/lib/graphql/armory/authorization/integration.rb +52 -5
- data/lib/graphql/armory/authorization/location.rb +79 -0
- data/lib/graphql/armory/authorization.rb +2 -0
- data/lib/graphql/armory/keyset/encoder_check.rb +113 -0
- data/lib/graphql/armory/keyset.rb +23 -3
- data/lib/graphql/armory/rate_limits/analysis_guard.rb +107 -11
- data/lib/graphql/armory/rate_limits/concurrency.rb +2 -0
- data/lib/graphql/armory/rate_limits/invalid_queries.rb +75 -0
- data/lib/graphql/armory/rate_limits/points.rb +44 -7
- data/lib/graphql/armory/rate_limits/time.rb +36 -6
- data/lib/graphql/armory/rate_limits.rb +1 -0
- data/lib/graphql/armory/version.rb +1 -1
- metadata +5 -1
|
@@ -16,28 +16,28 @@ module GraphQL
|
|
|
16
16
|
def call(type)
|
|
17
17
|
path = type.graphql_name
|
|
18
18
|
unless type.respond_to?(:armory_rule)
|
|
19
|
-
@findings.problem(:unprotected, path, "its base class does not include GraphQL::Armory::Authorization::Base, so it is open")
|
|
19
|
+
@findings.problem(:unprotected, path, "its base class does not include GraphQL::Armory::Authorization::Base, so it is open", at: type)
|
|
20
20
|
return
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
rule = type.armory_rule
|
|
24
24
|
if rule.nil?
|
|
25
|
-
@findings.problem(:no_rule, path, "has no rule and no default_action, so it is denied")
|
|
25
|
+
@findings.problem(:no_rule, path, "has no rule and no default_action, so it is denied", at: type)
|
|
26
26
|
elsif rule.visible_when && (answer = graphql_ruby_answer(type, :visible?))
|
|
27
|
-
@findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers visible? here, so visible_when: never runs: remove it")
|
|
27
|
+
@findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers visible? here, so visible_when: never runs: remove it", at: type)
|
|
28
28
|
elsif rule.none?
|
|
29
|
-
@findings.bypass(path, rule.reason)
|
|
29
|
+
@findings.bypass(path, rule.reason, at: type)
|
|
30
30
|
audit_fields(type, model: nil, bypassed: !rule.covers_fields?, reported: Set.new)
|
|
31
31
|
@foreign_policies.(type, rule.policy) if rule.policy
|
|
32
32
|
elsif (answer = graphql_ruby_answer(type, :authorized?))
|
|
33
33
|
@findings.problem(:ignored_rule, path, "graphql-ruby's #{answer} answers authorized? here, so this rule never runs: " \
|
|
34
34
|
"put the rule on the field that returns it (on a root field, on a resolver: a field rule asks " \
|
|
35
|
-
"the parent's policy), and declare `authorize :none, reason: \"...\", covers_fields: true` on this class")
|
|
35
|
+
"the parent's policy), and declare `authorize :none, reason: \"...\", covers_fields: true` on this class", at: type)
|
|
36
36
|
else
|
|
37
37
|
model = model_of(type, path)
|
|
38
38
|
reported = Set.new
|
|
39
|
-
check(path, model, rule, reported) if model
|
|
40
|
-
check(path, model, Rule.new(rule.visible_when, policy: rule.policy), reported) if model && rule.visible_when
|
|
39
|
+
check(path, model, rule, reported, type) if model
|
|
40
|
+
check(path, model, Rule.new(rule.visible_when, policy: rule.policy), reported, type) if model && rule.visible_when
|
|
41
41
|
@foreign_policies.(type, rule.policy) if rule.policy
|
|
42
42
|
audit_fields(type, model: model, bypassed: false, reported: reported)
|
|
43
43
|
end
|
|
@@ -64,14 +64,15 @@ module GraphQL
|
|
|
64
64
|
def model_of(type, path)
|
|
65
65
|
type.armory_model
|
|
66
66
|
rescue ConfigurationError => error
|
|
67
|
-
@findings.problem(:model, path, error.message)
|
|
67
|
+
@findings.problem(:model, path, error.message, at: type)
|
|
68
68
|
nil
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
# An adapter problem shows once per type, however many fields declare an action of their own.
|
|
72
|
-
|
|
71
|
+
# An adapter problem shows once per type, however many fields declare an action of their own. `at` is what declared
|
|
72
|
+
# the action: the type, a field, an argument or a resolver.
|
|
73
|
+
def check(path, model, rule, reported, at)
|
|
73
74
|
@adapter.check(model, rule).each do |message|
|
|
74
|
-
@findings.problem(:adapter, path, message) if reported.add?(message)
|
|
75
|
+
@findings.problem(:adapter, path, message, at: at) if reported.add?(message)
|
|
75
76
|
end
|
|
76
77
|
end
|
|
77
78
|
|
|
@@ -91,7 +92,7 @@ module GraphQL
|
|
|
91
92
|
PlainResolvers.report(@findings, path, field) if served_with_no_rule?(type, field)
|
|
92
93
|
if bypassed && !inherited && !field.respond_to?(:armory_declared)
|
|
93
94
|
return @findings.problem(:field_without_rule, path, "its field class does not include Base, so no rule runs on it: its type is bypassed and graphql-ruby's own " \
|
|
94
|
-
"authorized? answers true, so the field is served with no check. Use a field class that includes Base")
|
|
95
|
+
"authorized? answers true, so the field is served with no check. Use a field class that includes Base", at: field)
|
|
95
96
|
end
|
|
96
97
|
return unless field.respond_to?(:armory_declared)
|
|
97
98
|
return audit_resolver(field, path) if field.resolver.respond_to?(:armory_rule)
|
|
@@ -116,7 +117,7 @@ module GraphQL
|
|
|
116
117
|
return if field.owner.respond_to?(:armory_model) && owner_model(field.owner).equal?(model)
|
|
117
118
|
|
|
118
119
|
@findings.problem(:foreign_model, path, "its rule is defined on #{name_of(field.owner)}, so it never asks the policy of #{model}, the model of this type: a record of another class " \
|
|
119
|
-
"or a laxer subclass gets past it. Redefine the field on this type with its `authorize:` and an `override_reason:`, or name the same model on #{name_of(field.owner)}")
|
|
120
|
+
"or a laxer subclass gets past it. Redefine the field on this type with its `authorize:` and an `override_reason:`, or name the same model on #{name_of(field.owner)}", at: field)
|
|
120
121
|
end
|
|
121
122
|
|
|
122
123
|
def owner_model(owner)
|
|
@@ -138,41 +139,41 @@ module GraphQL
|
|
|
138
139
|
# A RelayClassicMutation's field holds only the generated `input`: its arguments are on the resolver.
|
|
139
140
|
arguments = (field.all_argument_definitions + resolver.all_argument_definitions).uniq
|
|
140
141
|
@foreign_policies.arguments(arguments, path, rule.policy, Set.new) if rule&.policy
|
|
141
|
-
return @findings.problem(:no_rule, path, "its mutation or resolver declares no `authorize`, so it is denied") if rule.nil?
|
|
142
|
+
return @findings.problem(:no_rule, path, "its mutation or resolver declares no `authorize`, so it is denied", at: resolver) if rule.nil?
|
|
142
143
|
audit_arguments(arguments, path, reported: Set.new)
|
|
143
|
-
return @findings.bypass(path, rule.reason) if rule.none?
|
|
144
|
+
return @findings.bypass(path, rule.reason, at: resolver) if rule.none?
|
|
144
145
|
|
|
145
146
|
model = begin
|
|
146
147
|
resolver.armory_model
|
|
147
148
|
rescue ConfigurationError => error
|
|
148
|
-
return @findings.problem(:model, path, error.message)
|
|
149
|
+
return @findings.problem(:model, path, error.message, at: resolver)
|
|
149
150
|
end
|
|
150
|
-
check(path, model, rule, Set.new)
|
|
151
|
+
check(path, model, rule, Set.new, resolver)
|
|
151
152
|
end
|
|
152
153
|
|
|
153
154
|
# The action a field is hidden with is asked of the policy like any other, so it must exist.
|
|
154
155
|
def audit_visibility(field, path, model:, reported:)
|
|
155
156
|
return unless model && field.armory_visible_when
|
|
156
157
|
|
|
157
|
-
check(path.split(".").first, model, Rule.new(field.armory_visible_when, policy: field.armory_policy, attribute: field.original_name), reported)
|
|
158
|
+
check(path.split(".").first, model, Rule.new(field.armory_visible_when, policy: field.armory_policy, attribute: field.original_name), reported, field)
|
|
158
159
|
end
|
|
159
160
|
|
|
160
161
|
def audit_declaration(type, field, path, model:, bypassed:, reported:)
|
|
161
162
|
case (declared = field.armory_declared)
|
|
162
163
|
when :none
|
|
163
|
-
@findings.bypass(path, field.armory_reason)
|
|
164
|
+
@findings.bypass(path, field.armory_reason, at: field)
|
|
164
165
|
when nil, true
|
|
165
166
|
if denied_payload_field?(field)
|
|
166
167
|
@findings.problem(:field_without_rule, path, "declares no rule and #{name_of(field.owner)}, which defines it, has none that covers it, so the mutation runs and this field answers null: " \
|
|
167
|
-
"a client cannot tell whether it worked. Give the field `authorize: :none, reason: \"...\"`, or use a shared `payload_type`; a public mutation adds `covers_fields: true`")
|
|
168
|
+
"a client cannot tell whether it worked. Give the field `authorize: :none, reason: \"...\"`, or use a shared `payload_type`; a public mutation adds `covers_fields: true`", at: field)
|
|
168
169
|
elsif bypassed && declared.nil? && served_by_owner?(type, field)
|
|
169
170
|
@findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, but it is inherited from #{field.owner.name || field.owner.inspect}: a field runs " \
|
|
170
|
-
"under the class that defines it, and no rule ran on this type, so the field is served with no check. Give it authorize: where it is defined, or remove it")
|
|
171
|
+
"under the class that defines it, and no rule ran on this type, so the field is served with no check. Give it authorize: where it is defined, or remove it", at: field)
|
|
171
172
|
elsif bypassed
|
|
172
|
-
@findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, so it is denied")
|
|
173
|
+
@findings.problem(:field_without_rule, path, "declares no rule and its type is bypassed, so it is denied", at: field)
|
|
173
174
|
end
|
|
174
175
|
else
|
|
175
|
-
check(path.split(".").first, model, Rule.new(declared, policy: field.armory_policy), reported) if model
|
|
176
|
+
check(path.split(".").first, model, Rule.new(declared, policy: field.armory_policy), reported, field) if model
|
|
176
177
|
end
|
|
177
178
|
end
|
|
178
179
|
|
|
@@ -199,7 +200,7 @@ module GraphQL
|
|
|
199
200
|
|
|
200
201
|
apath = "#{path}(#{argument.keyword})"
|
|
201
202
|
declared = argument.armory_declared
|
|
202
|
-
@findings.bypass(apath, argument.armory_reason) if declared == :none
|
|
203
|
+
@findings.bypass(apath, argument.armory_reason, at: argument) if declared == :none
|
|
203
204
|
[declared, argument.armory_visible_when].compact.reject { |action| action == :none }.each do |action|
|
|
204
205
|
audit_argument_action(argument, apath, action, reported)
|
|
205
206
|
end
|
|
@@ -207,9 +208,9 @@ module GraphQL
|
|
|
207
208
|
end
|
|
208
209
|
|
|
209
210
|
def audit_argument_action(argument, path, action, reported)
|
|
210
|
-
check(path.split(".").first, argument.armory_model, Rule.new(action, policy: argument.armory_policy, attribute: argument.keyword), reported)
|
|
211
|
+
check(path.split(".").first, argument.armory_model, Rule.new(action, policy: argument.armory_policy, attribute: argument.keyword), reported, argument)
|
|
211
212
|
rescue ConfigurationError => error
|
|
212
|
-
@findings.problem(:model, path, error.message)
|
|
213
|
+
@findings.problem(:model, path, error.message, at: argument)
|
|
213
214
|
end
|
|
214
215
|
|
|
215
216
|
# A list that skips the scope hands over every record: a rule on each one hides its fields, not how many there are.
|
|
@@ -217,7 +218,7 @@ module GraphQL
|
|
|
217
218
|
return if field.scoped? || generated_list?(type, field) || !list_of_records?(field)
|
|
218
219
|
|
|
219
220
|
@findings.problem(:unscoped_list, path, "skips the user's scope, with scope: false or a list type graphql-ruby does not detect, so a refused user sees how many records it holds: " \
|
|
220
|
-
"remove the option, or write the type as [Type]")
|
|
221
|
+
"remove the option, or write the type as [Type]", at: field)
|
|
221
222
|
end
|
|
222
223
|
|
|
223
224
|
# A connection applies the scope on the field that returns it, and graphql-ruby generates these two with scope: false.
|
|
@@ -253,8 +254,8 @@ module GraphQL
|
|
|
253
254
|
argument.owner.equal?(field) ? scoped_field?(field) : field.resolver.respond_to?(:armory_rule)
|
|
254
255
|
end
|
|
255
256
|
|
|
256
|
-
opted_out_loads(arguments, Set.new).each do |name|
|
|
257
|
-
@findings.problem(:unscoped_load, "#{path}(#{name})", "loads through the app's object_from_id, not through the user's scope")
|
|
257
|
+
opted_out_loads(arguments, Set.new).each do |name, argument|
|
|
258
|
+
@findings.problem(:unscoped_load, "#{path}(#{name})", "loads through the app's object_from_id, not through the user's scope", at: argument)
|
|
258
259
|
end
|
|
259
260
|
end
|
|
260
261
|
|
|
@@ -263,11 +264,11 @@ module GraphQL
|
|
|
263
264
|
end
|
|
264
265
|
|
|
265
266
|
# Execution::Next loads `loads:` nested in input objects with the field's loader, so a field's opt-out
|
|
266
|
-
# reaches them too: list them as `filter.invoice_id`. `seen` stops recursive input types.
|
|
267
|
+
# reaches them too: list them as `filter.invoice_id`. `seen` stops recursive input types. Answers [name, argument].
|
|
267
268
|
def opted_out_loads(arguments, seen, prefix = nil)
|
|
268
269
|
arguments.flat_map do |argument|
|
|
269
270
|
name = [prefix, GraphQL::Schema::Member::BuildType.underscore(argument.graphql_name)].compact.join(".")
|
|
270
|
-
next [name] if argument.loads
|
|
271
|
+
next [[name, argument]] if argument.loads
|
|
271
272
|
|
|
272
273
|
input = argument.type.unwrap
|
|
273
274
|
next [] unless input.respond_to?(:kind) && input.kind.input_object? && seen.add?(input)
|
|
@@ -39,7 +39,7 @@ module GraphQL
|
|
|
39
39
|
def note_type(type)
|
|
40
40
|
return unless type.respond_to?(:armory_rule) && type.armory_rule&.visible_when && @seen.add?(type)
|
|
41
41
|
|
|
42
|
-
report(type.graphql_name, model_problem { type.armory_model })
|
|
42
|
+
report(type.graphql_name, model_problem { type.armory_model }, type)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
def note_field(field, path)
|
|
@@ -50,14 +50,14 @@ module GraphQL
|
|
|
50
50
|
else
|
|
51
51
|
"uses visible_when:, but what defines it cannot name a model, so a request that reads it raises: put it on an object type that includes Base"
|
|
52
52
|
end
|
|
53
|
-
report(path, problem)
|
|
53
|
+
report(path, problem, field)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
def note_arguments(path, arguments)
|
|
57
57
|
arguments.each do |argument|
|
|
58
58
|
next unless argument.respond_to?(:armory_visible_when) && argument.armory_visible_when && @seen.add?(argument)
|
|
59
59
|
|
|
60
|
-
report("#{path}(#{argument.keyword})", model_problem { argument.armory_model })
|
|
60
|
+
report("#{path}(#{argument.keyword})", model_problem { argument.armory_model }, argument)
|
|
61
61
|
end
|
|
62
62
|
end
|
|
63
63
|
|
|
@@ -66,8 +66,8 @@ module GraphQL
|
|
|
66
66
|
field.owner.respond_to?(:kind) && field.owner.kind.interface? ? field.owner.graphql_name : type.graphql_name
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
def report(path, problem)
|
|
70
|
-
@findings.problem(:model, path, problem) if problem && !@findings.problem?(:model, path)
|
|
69
|
+
def report(path, problem, at)
|
|
70
|
+
@findings.problem(:model, path, problem, at: at) if problem && !@findings.problem?(:model, path)
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
def model_problem
|
|
@@ -65,7 +65,7 @@ module GraphQL
|
|
|
65
65
|
OwnChecks.(schema, definitions, findings)
|
|
66
66
|
Overrides.(schema, definitions, findings)
|
|
67
67
|
else
|
|
68
|
-
findings.problem(:plugin, "Schema", "never installed the plugin: add `use GraphQL::Armory::Authorization, adapter: ...`")
|
|
68
|
+
findings.problem(:plugin, "Schema", "never installed the plugin: add `use GraphQL::Armory::Authorization, adapter: ...`", at: schema)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
foreign_policies = ForeignPolicies.new(adapter, findings)
|
|
@@ -79,7 +79,7 @@ module GraphQL
|
|
|
79
79
|
ObservedRules.(schema, definitions, findings)
|
|
80
80
|
VisibilityModels.(schema, definitions, findings)
|
|
81
81
|
IntrospectionFields.(schema, findings)
|
|
82
|
-
findings.problem(:model, "Schema", listing_error.message) if listing_error && !findings.problem?(:model)
|
|
82
|
+
findings.problem(:model, "Schema", listing_error.message, at: schema) if listing_error && !findings.problem?(:model)
|
|
83
83
|
findings.report(Reach.new(definitions))
|
|
84
84
|
end
|
|
85
85
|
|
|
@@ -34,10 +34,13 @@ module GraphQL
|
|
|
34
34
|
@armory_model = model
|
|
35
35
|
@armory_visible_when = visible_when&.to_sym
|
|
36
36
|
@armory_override_reason = override_reason
|
|
37
|
+
@armory_location = Location.declared
|
|
37
38
|
Rule.none(reason: reason) if authorize == :none
|
|
38
39
|
super(*args, **kwargs, &block)
|
|
39
40
|
end
|
|
40
41
|
|
|
42
|
+
# Where the app declared this argument, for the audit.
|
|
43
|
+
attr_reader :armory_location
|
|
41
44
|
attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
|
|
42
45
|
|
|
43
46
|
def armory_model
|
|
@@ -35,11 +35,15 @@ module GraphQL
|
|
|
35
35
|
@armory_reason = reason
|
|
36
36
|
@armory_visible_when = visible_when&.to_sym
|
|
37
37
|
@armory_load_through_scope = load_through_scope
|
|
38
|
+
# Before `super`, which runs the block that declares the field's arguments.
|
|
39
|
+
@armory_location = Location.declared
|
|
38
40
|
Rule.none(reason: reason) if authorize == :none
|
|
39
41
|
super(*args, **kwargs, &block)
|
|
40
42
|
extension(Integration::FindBy, key: find_by) if find_by
|
|
41
43
|
end
|
|
42
44
|
|
|
45
|
+
# Where the app declared this field, for the audit.
|
|
46
|
+
attr_reader :armory_location
|
|
43
47
|
attr_reader :armory_declared, :armory_reason, :armory_observe, :armory_visible_when, :armory_override_reason
|
|
44
48
|
|
|
45
49
|
def armory_load_through_scope?
|
|
@@ -14,9 +14,13 @@ module GraphQL
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def authorize(action, reason: nil, **options)
|
|
17
|
+
@armory_rule_location = Location.declared
|
|
17
18
|
@armory_rule = Rule.new(action, reason: reason, **options)
|
|
18
19
|
end
|
|
19
20
|
|
|
21
|
+
# Where this class declared its own rule, for the audit; nil when it has none of its own.
|
|
22
|
+
attr_reader :armory_rule_location
|
|
23
|
+
|
|
20
24
|
# Own rule, then an inherited one, then the default action. Nil means: nothing covers this type.
|
|
21
25
|
def armory_rule
|
|
22
26
|
return @armory_rule if @armory_rule
|
|
@@ -60,7 +64,7 @@ module GraphQL
|
|
|
60
64
|
rule = armory_rule
|
|
61
65
|
return false if rule.nil?
|
|
62
66
|
|
|
63
|
-
Integration.refuse_unobservable(self, context)
|
|
67
|
+
Integration.refuse_unobservable(self, rule, context)
|
|
64
68
|
Integration.allowed?(object, Integration.with_owner_model(rule, self), context) && super
|
|
65
69
|
rescue GraphQL::UnauthorizedError
|
|
66
70
|
raise
|
|
@@ -24,9 +24,13 @@ module GraphQL
|
|
|
24
24
|
raise ArgumentError, "observe: does not apply to a mutation or a resolver: what it runs cannot be undone" if options.key?(:observe)
|
|
25
25
|
|
|
26
26
|
@armory_on_denied = on_denied
|
|
27
|
+
@armory_rule_location = Location.declared
|
|
27
28
|
@armory_rule = Rule.new(action, reason: reason, **options)
|
|
28
29
|
end
|
|
29
30
|
|
|
31
|
+
# Where this class declared its own rule, for the audit; nil when it has none of its own.
|
|
32
|
+
attr_reader :armory_rule_location
|
|
33
|
+
|
|
30
34
|
def armory_rule
|
|
31
35
|
@armory_rule || (superclass.armory_rule if superclass.respond_to?(:armory_rule))
|
|
32
36
|
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
module Integration
|
|
7
|
+
# Refuses an operation whose root type cannot be observed before anything runs. Inside the root type's own check,
|
|
8
|
+
# graphql-ruby hands the error to the app's `rescue_from`, which can turn it into an empty answer, and on the
|
|
9
|
+
# interpreter of graphql 2.6 its handling fails with a NoMethodError first. Raised here, it is the same
|
|
10
|
+
# ConfigurationError on every engine. The check in `Object#authorized?` stays for a query that runs with its own
|
|
11
|
+
# trace (`context[:trace]`).
|
|
12
|
+
module RootCheck
|
|
13
|
+
def execute_multiplex(multiplex:)
|
|
14
|
+
multiplex.queries.each { |query| RootCheck.(query) }
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# A partial runs on its own type, not the operation's root, and is left to that type's check. An invalid query
|
|
19
|
+
# keeps its validation errors (and what a rate limiter charges for them).
|
|
20
|
+
def self.call(query)
|
|
21
|
+
return if defined?(GraphQL::Query::Partial) && query.is_a?(GraphQL::Query::Partial)
|
|
22
|
+
|
|
23
|
+
operation = query.selected_operation
|
|
24
|
+
return unless operation && query.valid?
|
|
25
|
+
|
|
26
|
+
# Query#root_type is graphql 2.5+; the schema answers the same on 2.4.
|
|
27
|
+
type = query.schema.root_type_for_operation(operation.operation_type || "query")
|
|
28
|
+
rule = type.armory_rule if type.respond_to?(:armory_rule)
|
|
29
|
+
Integration.refuse_unobservable(type, rule, query.context) if rule
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "graphql/armory/authorization/integration/schema"
|
|
4
|
+
require "graphql/armory/authorization/integration/root_check"
|
|
4
5
|
require "graphql/armory/authorization/integration/object"
|
|
5
6
|
require "graphql/armory/authorization/integration/find_by"
|
|
6
7
|
require "graphql/armory/authorization/integration/field"
|
|
@@ -125,14 +126,60 @@ module GraphQL
|
|
|
125
126
|
private_class_method :declared_model
|
|
126
127
|
|
|
127
128
|
# Observing a type opens the only check that stands before everything inside it, whatever class its fields are. A
|
|
128
|
-
# resolver or a mutation can write, and a write cannot be undone, so a type holding one cannot be observed.
|
|
129
|
-
|
|
129
|
+
# resolver or a mutation can write, and a write cannot be undone, so a type holding one cannot be observed. Nor can
|
|
130
|
+
# a resolver field take an observed rule from the class that declared it (`authorize: true`). Both are refused
|
|
131
|
+
# here, at the type, because graphql-ruby prepares a field's arguments before the field's own check. Only what can
|
|
132
|
+
# run is read: a field a subclass overrides is not, unless the override is hidden from this user and the legacy
|
|
133
|
+
# Warden falls back to it.
|
|
134
|
+
def self.refuse_unobservable(type, rule, context)
|
|
130
135
|
known = context.namespace(:armory)[:observable] ||= {}.compare_by_identity
|
|
131
|
-
field = known.fetch(type)
|
|
136
|
+
field = known.fetch(type) do
|
|
137
|
+
unobservable = ->(each) { each.resolver && (rule.observed? || observed_reason(each)) }
|
|
138
|
+
known[type] = live_fields(type, context, unobservable).find(&unobservable)
|
|
139
|
+
end
|
|
132
140
|
return unless field
|
|
133
141
|
|
|
134
|
-
|
|
135
|
-
|
|
142
|
+
if rule.observed?
|
|
143
|
+
raise ConfigurationError, "#{type.graphql_name} cannot be observed: its field #{field.graphql_name} has a resolver or a mutation, which can write. " \
|
|
144
|
+
"Remove observe:, or put it on its plain fields"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
owner = field.owner.graphql_name
|
|
148
|
+
raise ConfigurationError, "#{type.graphql_name}.#{field.graphql_name} has a resolver or a mutation, which can write, so it cannot take the " \
|
|
149
|
+
"observed rule of #{owner} (#{observed_reason(field)}). Remove observe: from #{owner}, or give the field a rule of its own"
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The fields merged by name, plus, for each name whose shadowed definition would be refused, the definition the
|
|
153
|
+
# legacy Warden serves this user: it skips a hidden override and serves the parent's. Only those names are asked,
|
|
154
|
+
# so a type that is not observed, or has nothing shadowed to refuse, asks no policy and loads no field block.
|
|
155
|
+
# A Visibility profile never falls back.
|
|
156
|
+
def self.live_fields(type, context, unobservable)
|
|
157
|
+
fields = type.all_field_definitions
|
|
158
|
+
return fields if visibility_profile?(context)
|
|
159
|
+
|
|
160
|
+
levels = type.ancestors.select { |ancestor| ancestor.respond_to?(:own_fields) }
|
|
161
|
+
shadowed = levels.flat_map { |ancestor| ancestor.own_fields.values.flatten }.select(&unobservable) - fields
|
|
162
|
+
return fields if shadowed.empty?
|
|
163
|
+
|
|
164
|
+
served = shadowed.map(&:graphql_name).uniq.flat_map { |name| Array(type.get_field(name, context)) }
|
|
165
|
+
fields + served
|
|
166
|
+
end
|
|
167
|
+
private_class_method :live_fields
|
|
168
|
+
|
|
169
|
+
def self.visibility_profile?(context)
|
|
170
|
+
defined?(GraphQL::Schema::Visibility::Profile) && context.respond_to?(:types) &&
|
|
171
|
+
context.types.is_a?(GraphQL::Schema::Visibility::Profile)
|
|
172
|
+
end
|
|
173
|
+
private_class_method :visibility_profile?
|
|
174
|
+
|
|
175
|
+
# Why a field is observed: its own observe:, or `authorize: true` on an owner whose rule is observed. Nil when it is
|
|
176
|
+
# not. The audit asks the same question.
|
|
177
|
+
def self.observed_reason(field)
|
|
178
|
+
return field.armory_observe if field.respond_to?(:armory_observe) && field.armory_observe
|
|
179
|
+
return unless field.respond_to?(:armory_declared) && field.armory_declared == true
|
|
180
|
+
|
|
181
|
+
rule = field.owner.armory_rule if field.owner.respond_to?(:armory_rule)
|
|
182
|
+
rule.observe if rule&.observed?
|
|
136
183
|
end
|
|
137
184
|
|
|
138
185
|
# One Authorize per query: building it (and its memo and telemetry) once per object is pure overhead.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Authorization
|
|
6
|
+
# Where something of the app's schema was written, as "path:line", for the audit's report. graphql-ruby keeps no
|
|
7
|
+
# location for a field or an argument, so the plugin's field and argument classes record the line that declared them,
|
|
8
|
+
# and a type records the line of its `authorize`. Anything else falls back to the line of its class.
|
|
9
|
+
#
|
|
10
|
+
# Limits: a schema built from SDL has no Ruby lines; a field declared by a helper method points to the helper; a class
|
|
11
|
+
# reopened in several files gives the first one.
|
|
12
|
+
module Location
|
|
13
|
+
GRAPHQL_RUBY_PATH = "#{File.dirname(GraphQL::Schema.method(:from_definition).source_location.first)}/"
|
|
14
|
+
ARMORY_PATH = "#{File.expand_path('..', __dir__)}/"
|
|
15
|
+
# The directory the app started in, read once: a later `Dir.chdir` must not change the report's text. None when the
|
|
16
|
+
# directory is gone, and then paths stay absolute.
|
|
17
|
+
ROOT = begin
|
|
18
|
+
"#{Dir.pwd}/"
|
|
19
|
+
rescue SystemCallError
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The first line of the caller's stack outside graphql-ruby and this gem: where the app declared it. An `initialize`
|
|
24
|
+
# is skipped too: an app's field or argument class, or another gem's module, may define one that calls super into
|
|
25
|
+
# the plugin's, and its line is the same for every field.
|
|
26
|
+
def self.declared
|
|
27
|
+
found = caller_locations(2, 40)&.find do |location|
|
|
28
|
+
path = location.absolute_path || location.path
|
|
29
|
+
!path.start_with?("<", GRAPHQL_RUBY_PATH, ARMORY_PATH) && location.base_label != "initialize"
|
|
30
|
+
end
|
|
31
|
+
found && "#{found.absolute_path || found.path}:#{found.lineno}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# "path:line" for a method, a field, an argument, a type or a class, relative to the directory the app started in
|
|
35
|
+
# when it is inside it, or nil when there is no line to give.
|
|
36
|
+
def self.of(thing)
|
|
37
|
+
relative(absolute(thing))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.absolute(thing)
|
|
41
|
+
case thing
|
|
42
|
+
when nil then nil
|
|
43
|
+
when String then thing
|
|
44
|
+
when Method, UnboundMethod then join(thing.source_location)
|
|
45
|
+
when GraphQL::Schema::Field, GraphQL::Schema::Argument
|
|
46
|
+
(thing.armory_location if thing.respond_to?(:armory_location)) || absolute(thing.owner)
|
|
47
|
+
when Module
|
|
48
|
+
(thing.armory_rule_location if thing.respond_to?(:armory_rule_location)) || constant(thing)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
private_class_method :absolute
|
|
52
|
+
|
|
53
|
+
# Only when the name leads back to this class: a class may answer another constant's name. A location is never worth
|
|
54
|
+
# failing the audit, so a `name` that raises gives none.
|
|
55
|
+
def self.constant(mod)
|
|
56
|
+
name = mod.name
|
|
57
|
+
return if !name.is_a?(String) || name.empty? || !Object.const_get(name).equal?(mod)
|
|
58
|
+
|
|
59
|
+
join(Object.const_source_location(name))
|
|
60
|
+
rescue StandardError, NotImplementedError
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
private_class_method :constant
|
|
64
|
+
|
|
65
|
+
def self.join(pair)
|
|
66
|
+
pair && !pair.empty? && pair.first ? "#{pair.first}:#{pair.last}" : nil
|
|
67
|
+
end
|
|
68
|
+
private_class_method :join
|
|
69
|
+
|
|
70
|
+
def self.relative(location)
|
|
71
|
+
return location if location.nil? || ROOT.nil?
|
|
72
|
+
|
|
73
|
+
location.start_with?(ROOT) ? location.delete_prefix(ROOT) : location
|
|
74
|
+
end
|
|
75
|
+
private_class_method :relative
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "graphql/armory/authorization/errors"
|
|
4
|
+
require "graphql/armory/authorization/location"
|
|
4
5
|
require "graphql/armory/authorization/configuration"
|
|
5
6
|
require "graphql/armory/authorization/rule"
|
|
6
7
|
require "graphql/armory/authorization/memo"
|
|
@@ -42,6 +43,7 @@ module GraphQL
|
|
|
42
43
|
def self.use(schema_defn, **options)
|
|
43
44
|
schema_defn.extend(Integration::Schema)
|
|
44
45
|
schema_defn.armory_configuration = Configuration.new(**options)
|
|
46
|
+
schema_defn.trace_with(Integration::RootCheck)
|
|
45
47
|
end
|
|
46
48
|
end
|
|
47
49
|
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
class Keyset < GraphQL::Pagination::Connection
|
|
8
|
+
# Asks an encoder whether it can keep a keyset cursor private, raising `ConfigurationError` when it cannot. It catches
|
|
9
|
+
# what is common: no encryption, a reversible form, a fixed key, and a chain that decodes forged text. It cannot prove
|
|
10
|
+
# encryption. It has no dependencies, so `new` is ready at once and there is nothing to `build`.
|
|
11
|
+
class EncoderCheck
|
|
12
|
+
NEEDLE = "graphql-armory-probe"
|
|
13
|
+
# Shaped like a real cursor, because an encoder may parse what it is given. They differ in their first value.
|
|
14
|
+
PROBE = JSON.generate({ "v" => 1, "o" => "0" * 32, "k" => ["#{NEEDLE}-values"] }).freeze
|
|
15
|
+
# Differ from PROBE near the start and at the end: a chained mode with a fixed IV changes every block after a change
|
|
16
|
+
# near the start, and only the last block after a change at the end.
|
|
17
|
+
OTHERS = [PROBE.sub('"v":1', '"v":2'), PROBE.sub('-values"', '-valuez"')].map(&:freeze).freeze
|
|
18
|
+
|
|
19
|
+
def self.call(encoder)
|
|
20
|
+
new.(encoder)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def call(encoder)
|
|
24
|
+
# Every member of a chain decodes what a client sends, so each one is checked on its own.
|
|
25
|
+
encoder.encoders.each { |member| call(member) } if encoder.is_a?(Encoder::Versioned)
|
|
26
|
+
first = encoded(encoder, PROBE)
|
|
27
|
+
refuse("does not give back what it encoded") unless decoded(encoder, first) == PROBE
|
|
28
|
+
refuse("leaves the cursor readable: it holds the ordering values") if readable?(first)
|
|
29
|
+
if OTHERS.any? { |other| local?(first, encoded(encoder, other)) }
|
|
30
|
+
refuse("changes only part of its output when the input changes, as a fixed key or a fixed nonce does")
|
|
31
|
+
end
|
|
32
|
+
refuse("decodes forged text, so a client can make its own cursors") if forged?(encoder)
|
|
33
|
+
nil
|
|
34
|
+
rescue ConfigurationError => error
|
|
35
|
+
raise if error.equal?(@refusal)
|
|
36
|
+
|
|
37
|
+
refuse("could not be probed: #{error.class}")
|
|
38
|
+
rescue StandardError, NotImplementedError => error
|
|
39
|
+
# The class only: an encoder's message may hold a key.
|
|
40
|
+
refuse("could not be probed: #{error.class}")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def encoded(encoder, text)
|
|
46
|
+
answer = encoder.encode(text, nonce: true)
|
|
47
|
+
answer.is_a?(String) ? answer.b : refuse("answers #{answer.class}, not a String")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def decoded(encoder, text)
|
|
51
|
+
answer = encoder.decode(text, nonce: true)
|
|
52
|
+
answer.is_a?(String) ? answer.b : answer
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def readable?(output)
|
|
56
|
+
needle = NEEDLE.b
|
|
57
|
+
[output, output.reverse, output.tr("A-Za-z", "N-ZA-Mn-za-m")].any? do |form|
|
|
58
|
+
([form, form.delete("\0")] + unpacked(form)).any? { |text| text.include?(needle) }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Each long run of Base64 or hex inside the output, decoded from every offset, so a prefix of any length does not hide it.
|
|
63
|
+
def unpacked(form)
|
|
64
|
+
base64 = form.scan(%r{[A-Za-z0-9+/_-]{16,}}).flat_map do |run|
|
|
65
|
+
(0..3).map { |offset| run[offset..].tr("-_", "+/").unpack1("m").b }
|
|
66
|
+
end
|
|
67
|
+
hex = form.scan(/\h{16,}/).flat_map { |run| (0..1).map { |offset| [run[offset..]].pack("H*").b } }
|
|
68
|
+
base64 + hex
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# A one-byte change near the start changes most of what a sound encoder answers.
|
|
72
|
+
def local?(one, other)
|
|
73
|
+
size = [one.size, other.size].max
|
|
74
|
+
changed = (0...size).count { |index| one.getbyte(index) != other.getbyte(index) }
|
|
75
|
+
changed < size / 2
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def forged?(encoder)
|
|
79
|
+
[PROBE, [PROBE].pack("m0"), [PROBE].pack("m0").tr("+/", "-_").delete("=")].any? do |forgery|
|
|
80
|
+
decoded(encoder, forgery) == PROBE.b
|
|
81
|
+
rescue StandardError
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Remembers its own refusal, so an encoder that raises a ConfigurationError of its own is named by class, like any other.
|
|
87
|
+
def refuse(what)
|
|
88
|
+
@refusal = ConfigurationError.new(
|
|
89
|
+
"this cursor_encoder #{what}. A keyset cursor holds the row's ordering values: use a GraphQL::Armory::Encoder " \
|
|
90
|
+
"subclass, or `use GraphQL::Armory::Keyset, plain_cursors: true` if those values are not secret"
|
|
91
|
+
)
|
|
92
|
+
raise @refusal
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# What `use GraphQL::Armory::Keyset` adds to a schema. A subclass inherits it, so an encoder set there is checked too.
|
|
97
|
+
module SchemaChecks
|
|
98
|
+
def armory_keyset_plain_cursors
|
|
99
|
+
return @armory_keyset_plain_cursors unless @armory_keyset_plain_cursors.nil?
|
|
100
|
+
|
|
101
|
+
superclass.respond_to?(:armory_keyset_plain_cursors) ? superclass.armory_keyset_plain_cursors : false
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
attr_writer :armory_keyset_plain_cursors
|
|
105
|
+
|
|
106
|
+
def cursor_encoder(new_encoder = nil)
|
|
107
|
+
EncoderCheck.(new_encoder) if new_encoder && !armory_keyset_plain_cursors
|
|
108
|
+
super
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|