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
|
@@ -6,7 +6,8 @@ module GraphQL
|
|
|
6
6
|
class Audit
|
|
7
7
|
# An object type's rule, and what its fields, resolvers and arguments declare under it.
|
|
8
8
|
class TypeRules
|
|
9
|
-
def initialize(adapter = Adapters::Null.new, findings = Findings.new, foreign_policies = ForeignPolicies.new(adapter, findings))
|
|
9
|
+
def initialize(adapter = Adapters::Null.new, findings = Findings.new, foreign_policies = ForeignPolicies.new(adapter, findings), write_roots = [])
|
|
10
|
+
@write_roots = write_roots
|
|
10
11
|
@adapter = adapter
|
|
11
12
|
@findings = findings
|
|
12
13
|
@foreign_policies = foreign_policies
|
|
@@ -87,6 +88,7 @@ module GraphQL
|
|
|
87
88
|
audit_loads(field, path)
|
|
88
89
|
# An inherited interface field is reported as uncovered on its interface; every other check still runs here.
|
|
89
90
|
inherited = bypassed && interface_field?(field)
|
|
91
|
+
PlainResolvers.report(@findings, path, field) if served_with_no_rule?(type, field)
|
|
90
92
|
if bypassed && !inherited && !field.respond_to?(:armory_declared)
|
|
91
93
|
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 " \
|
|
92
94
|
"authorized? answers true, so the field is served with no check. Use a field class that includes Base")
|
|
@@ -100,6 +102,13 @@ module GraphQL
|
|
|
100
102
|
audit_arguments(field.all_argument_definitions, path, reported: reported)
|
|
101
103
|
end
|
|
102
104
|
|
|
105
|
+
# Asked of the type that serves it and of the class that defines it, as runtime does: a parent that does not cover it denies it.
|
|
106
|
+
def served_with_no_rule?(type, field)
|
|
107
|
+
return false unless PlainResolvers.covering?(type.armory_rule) && !interface_field?(field) && PlainResolvers.open?(field, write_root: @write_roots.include?(type))
|
|
108
|
+
|
|
109
|
+
!(field.respond_to?(:armory_denied_by_owner?) && field.armory_denied_by_owner?)
|
|
110
|
+
end
|
|
111
|
+
|
|
103
112
|
# A field runs under its owner: graphql-ruby never tells it the concrete type. A rule defined above that type asks
|
|
104
113
|
# the owner's model, or only the record's class, while this audit reads the type's model.
|
|
105
114
|
def audit_inherited_rule(type, field, path, model)
|
|
@@ -153,7 +162,10 @@ module GraphQL
|
|
|
153
162
|
when :none
|
|
154
163
|
@findings.bypass(path, field.armory_reason)
|
|
155
164
|
when nil, true
|
|
156
|
-
if
|
|
165
|
+
if denied_payload_field?(field)
|
|
166
|
+
@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
|
+
elsif bypassed && declared.nil? && served_by_owner?(type, field)
|
|
157
169
|
@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 " \
|
|
158
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")
|
|
159
171
|
elsif bypassed
|
|
@@ -164,6 +176,11 @@ module GraphQL
|
|
|
164
176
|
end
|
|
165
177
|
end
|
|
166
178
|
|
|
179
|
+
def denied_payload_field?(field)
|
|
180
|
+
owner = field.owner
|
|
181
|
+
owner.is_a?(Class) && owner <= GraphQL::Schema::Resolver && field.armory_denied_by_owner?
|
|
182
|
+
end
|
|
183
|
+
|
|
167
184
|
# A field runs under its owner's rule, not the resolved type's. Inherited by a bypassed type from a parent whose
|
|
168
185
|
# rule covers it, or that knows no rule at all, graphql-ruby serves it after a type check that checked nothing.
|
|
169
186
|
def served_by_owner?(type, field)
|
|
@@ -2,16 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
require "set"
|
|
4
4
|
require "graphql/armory/authorization/audit/report"
|
|
5
|
+
require "graphql/armory/authorization/audit/baseline"
|
|
6
|
+
require "graphql/armory/authorization/audit/reach"
|
|
5
7
|
require "graphql/armory/authorization/audit/command"
|
|
6
8
|
require "graphql/armory/authorization/audit/findings"
|
|
7
9
|
require "graphql/armory/authorization/audit/library_methods"
|
|
8
10
|
require "graphql/armory/authorization/audit/denial_hooks"
|
|
9
11
|
require "graphql/armory/authorization/audit/own_checks"
|
|
12
|
+
require "graphql/armory/authorization/audit/plain_resolvers"
|
|
10
13
|
require "graphql/armory/authorization/audit/overrides"
|
|
11
14
|
require "graphql/armory/authorization/audit/foreign_policies"
|
|
12
15
|
require "graphql/armory/authorization/audit/type_rules"
|
|
16
|
+
require "graphql/armory/authorization/audit/observed_rules"
|
|
13
17
|
require "graphql/armory/authorization/audit/edge_rules"
|
|
14
18
|
require "graphql/armory/authorization/audit/interface_rules"
|
|
19
|
+
require "graphql/armory/authorization/audit/payload_reuse"
|
|
15
20
|
require "graphql/armory/authorization/audit/input_loads"
|
|
16
21
|
require "graphql/armory/authorization/audit/visibility_models"
|
|
17
22
|
require "graphql/armory/authorization/audit/introspection_fields"
|
|
@@ -64,16 +69,18 @@ module GraphQL
|
|
|
64
69
|
end
|
|
65
70
|
|
|
66
71
|
foreign_policies = ForeignPolicies.new(adapter, findings)
|
|
67
|
-
type_rules = TypeRules.new(adapter, findings, foreign_policies)
|
|
72
|
+
type_rules = TypeRules.new(adapter, findings, foreign_policies, [schema.mutation, schema.subscription].compact)
|
|
68
73
|
audited_types(definitions).each { |type| type_rules.(type) }
|
|
69
74
|
definitions.each { |type| foreign_policies.relay(type) if type.kind.object? && RELAY_TYPES.any? { |relay| type <= relay } }
|
|
70
75
|
EdgeRules.(definitions, findings)
|
|
71
76
|
InterfaceRules.(definitions, findings)
|
|
72
77
|
InputLoads.(definitions, findings)
|
|
78
|
+
PayloadReuse.(definitions, findings)
|
|
79
|
+
ObservedRules.(schema, definitions, findings)
|
|
73
80
|
VisibilityModels.(schema, definitions, findings)
|
|
74
81
|
IntrospectionFields.(schema, findings)
|
|
75
82
|
findings.problem(:model, "Schema", listing_error.message) if listing_error && !findings.problem?(:model)
|
|
76
|
-
findings.report
|
|
83
|
+
findings.report(Reach.new(definitions))
|
|
77
84
|
end
|
|
78
85
|
|
|
79
86
|
private
|
|
@@ -84,10 +91,11 @@ module GraphQL
|
|
|
84
91
|
Definitions.fields_at_every_level(type).any? { |field| !field.owner.equal?(GraphQL::Types::Relay::PageInfo) }
|
|
85
92
|
end
|
|
86
93
|
|
|
87
|
-
# A connection or an edge holds only what graphql-ruby generates, until a subclass, its parent or an interface adds a field
|
|
94
|
+
# A connection or an edge holds only what graphql-ruby generates, until a subclass, its parent or an interface adds a field,
|
|
95
|
+
# or gives a generated name a resolver of its own.
|
|
88
96
|
def extended_relay?(type)
|
|
89
97
|
GENERATED_RELAY_FIELDS.any? do |relay, names|
|
|
90
|
-
type < relay && Definitions.fields_at_every_level(type).any? { |field| !names.include?(field.original_name) }
|
|
98
|
+
type < relay && Definitions.fields_at_every_level(type).any? { |field| !names.include?(field.original_name) || field.resolver }
|
|
91
99
|
end
|
|
92
100
|
end
|
|
93
101
|
|
|
@@ -14,24 +14,42 @@ module GraphQL
|
|
|
14
14
|
@telemetry = telemetry
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def self.build(context, adapter:, sinks: [])
|
|
18
|
-
new(adapter: adapter, memo: Memo.build(context), telemetry: Telemetry.build(sinks: sinks))
|
|
17
|
+
def self.build(context, adapter:, sinks: [], on_sink_error: nil)
|
|
18
|
+
new(adapter: adapter, memo: Memo.build(context), telemetry: Telemetry.build(sinks: sinks, on_sink_error: on_sink_error))
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def self.call(subject, rule, context, adapter:, sinks: [])
|
|
22
|
-
build(context, adapter: adapter, sinks: sinks).(subject, rule, context)
|
|
21
|
+
def self.call(subject, rule, context, adapter:, sinks: [], on_sink_error: nil)
|
|
22
|
+
build(context, adapter: adapter, sinks: sinks, on_sink_error: on_sink_error).(subject, rule, context)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def call(subject, rule, context)
|
|
26
|
-
return bypass(subject, rule) if rule.none?
|
|
26
|
+
return bypass(subject, rule, context) if rule.none?
|
|
27
27
|
|
|
28
28
|
memo.fetch(subject, rule) { decide(subject, rule, context) }
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
private
|
|
32
32
|
|
|
33
|
-
def bypass(subject, rule)
|
|
34
|
-
telemetry.record(:bypassed, type: class_name(subject), reason: rule.reason)
|
|
33
|
+
def bypass(subject, rule, context)
|
|
34
|
+
telemetry.record(:bypassed, context: context, type: class_name(subject), reason: rule.reason)
|
|
35
|
+
true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Observing opens one thing only: a clear "no" to a read, with someone listening, from an adapter someone configured.
|
|
39
|
+
# A mutation or a subscription keeps refusing, even on a read inside its response: only a query runs nothing it cannot undo.
|
|
40
|
+
def observes?(rule, context)
|
|
41
|
+
rule.observed? && telemetry.listening? && !adapter.is_a?(Adapters::Null) && read_only?(context)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A partial always answers `query?`, whatever the operation that ran it: only the query it came from can tell.
|
|
45
|
+
def read_only?(context)
|
|
46
|
+
query = context.query if context.respond_to?(:query)
|
|
47
|
+
query = query.query while defined?(GraphQL::Query::Partial) && query.is_a?(GraphQL::Query::Partial)
|
|
48
|
+
query.respond_to?(:query?) && query.query? == true
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def observe(subject, rule, context)
|
|
52
|
+
telemetry.record(:observed, context: context, type: class_name(subject), action: rule.action, reason: rule.observe)
|
|
35
53
|
true
|
|
36
54
|
end
|
|
37
55
|
|
|
@@ -45,8 +63,10 @@ module GraphQL
|
|
|
45
63
|
|
|
46
64
|
case answer
|
|
47
65
|
when true, false, nil
|
|
66
|
+
return observe(subject, rule, context) if answer != true && observes?(rule, context)
|
|
67
|
+
|
|
48
68
|
allowed = answer == true
|
|
49
|
-
telemetry.record(allowed ? :allowed : :denied, type: class_name(subject), action: rule.action)
|
|
69
|
+
telemetry.record(allowed ? :allowed : :denied, context: context, type: class_name(subject), action: rule.action)
|
|
50
70
|
allowed
|
|
51
71
|
else
|
|
52
72
|
# The class alone: the answer may be the record, and on a relation `inspect` runs the query.
|
|
@@ -11,9 +11,9 @@ module GraphQL
|
|
|
11
11
|
|
|
12
12
|
OWN_CHECKS = %i[authorized? authorizes? authorized_new wrap unauthorized_object visible? scope_items reauthorize_scoped_objects].freeze
|
|
13
13
|
|
|
14
|
-
attr_reader :adapter, :on_unauthorized, :sinks, :own_denial_hooks, :own_checks
|
|
14
|
+
attr_reader :adapter, :on_unauthorized, :sinks, :on_sink_error, :own_denial_hooks, :own_checks
|
|
15
15
|
|
|
16
|
-
def initialize(adapter:, on_unauthorized: :null, sinks: [], own_denial_hooks: {}, own_checks: {})
|
|
16
|
+
def initialize(adapter:, on_unauthorized: :null, sinks: [], on_sink_error: nil, own_denial_hooks: {}, own_checks: {})
|
|
17
17
|
unless MODES.include?(on_unauthorized)
|
|
18
18
|
raise ArgumentError, "on_unauthorized must be one of #{MODES.inspect}, got #{on_unauthorized.inspect}"
|
|
19
19
|
end
|
|
@@ -24,8 +24,9 @@ module GraphQL
|
|
|
24
24
|
@adapter = resolve(adapter)
|
|
25
25
|
@on_unauthorized = on_unauthorized
|
|
26
26
|
@own_denial_hooks = own_denial_hooks.to_h { |hook, reason| [hook, reason.dup.freeze] }.freeze
|
|
27
|
-
Telemetry.build(sinks: sinks) # raises at boot when the sinks are not callables
|
|
27
|
+
Telemetry.build(sinks: sinks, on_sink_error: on_sink_error) # raises at boot when the sinks are not callables
|
|
28
28
|
@sinks = sinks
|
|
29
|
+
@on_sink_error = on_sink_error
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
private
|
|
@@ -66,12 +66,19 @@ module GraphQL
|
|
|
66
66
|
references << arguments_of(type) if type.kind.input_object?
|
|
67
67
|
references.concat(type.interface_type_memberships.map(&:abstract_type)) if type.respond_to?(:interface_type_memberships)
|
|
68
68
|
references.concat(type.orphan_types) if type.kind.interface?
|
|
69
|
+
references.concat(included_interfaces(type)) if type.kind.interface?
|
|
69
70
|
references.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
|
|
70
71
|
references << type.node_type if connection?(type)
|
|
71
72
|
references.flatten.compact
|
|
72
73
|
end
|
|
73
74
|
private_class_method :referenced_types
|
|
74
75
|
|
|
76
|
+
# An interface brought in with `include` makes no type membership, and its fields are served all the same.
|
|
77
|
+
def self.included_interfaces(type)
|
|
78
|
+
type.ancestors.select { |ancestor| !ancestor.equal?(type) && ancestor.respond_to?(:kind) && ancestor.kind.interface? }
|
|
79
|
+
end
|
|
80
|
+
private_class_method :included_interfaces
|
|
81
|
+
|
|
75
82
|
# Every definition of every field, nearest first: a hidden one falls back to the next of its name, and
|
|
76
83
|
# `all_field_definitions` merges them by name. `field ... do ... end` blocks are loaded here, because only
|
|
77
84
|
# `schema.types` or a request loads them, and the audit reads arguments from them.
|
|
@@ -10,6 +10,7 @@ module GraphQL
|
|
|
10
10
|
# field :id, ID, authorize: :none, reason: "ids are public" # declared bypass
|
|
11
11
|
# field :owner_id, ID, authorize: true # the parent type's own action
|
|
12
12
|
# field :notes, String, authorize: :manage, visible_when: :export # hidden from users who cannot export
|
|
13
|
+
# field :notes, String, authorize: :manage, observe: "rollout" # served and recorded, see Authorize
|
|
13
14
|
#
|
|
14
15
|
# A field with no rule is covered by its type's rule, unless the type is bypassed:
|
|
15
16
|
# coverage does not propagate from a bypass, so that field is denied.
|
|
@@ -19,9 +20,16 @@ module GraphQL
|
|
|
19
20
|
module Field
|
|
20
21
|
# `find_by: :id` looks the object up inside the user's scope instead of calling the resolver.
|
|
21
22
|
# `loads:` arguments also load through the scope unless `load_through_scope: false`.
|
|
22
|
-
def initialize(*args, authorize: nil, reason: nil, find_by: nil, load_through_scope: true, visible_when: nil, override_reason: nil, **kwargs, &block)
|
|
23
|
+
def initialize(*args, authorize: nil, reason: nil, observe: nil, find_by: nil, load_through_scope: true, visible_when: nil, override_reason: nil, **kwargs, &block)
|
|
23
24
|
raise ConfigurationError, "override_reason: must say why the override is safe" if !override_reason.nil? && override_reason.to_s.strip.empty?
|
|
24
25
|
|
|
26
|
+
if !observe.nil? && (!authorize.is_a?(Symbol) || authorize == :none)
|
|
27
|
+
raise ArgumentError, "observe: needs an action of the field's own: `authorize: :manage, observe: \"...\"`"
|
|
28
|
+
end
|
|
29
|
+
raise ArgumentError, "observe: does not apply to a field with a resolver or a mutation: what it runs cannot be undone" if !observe.nil? && kwargs[:resolver_class]
|
|
30
|
+
Rule.new(authorize, observe: observe) unless observe.nil?
|
|
31
|
+
|
|
32
|
+
@armory_observe = observe
|
|
25
33
|
@armory_override_reason = override_reason
|
|
26
34
|
@armory_declared = authorize
|
|
27
35
|
@armory_reason = reason
|
|
@@ -32,7 +40,7 @@ module GraphQL
|
|
|
32
40
|
extension(Integration::FindBy, key: find_by) if find_by
|
|
33
41
|
end
|
|
34
42
|
|
|
35
|
-
attr_reader :armory_declared, :armory_reason, :armory_visible_when, :armory_override_reason
|
|
43
|
+
attr_reader :armory_declared, :armory_reason, :armory_observe, :armory_visible_when, :armory_override_reason
|
|
36
44
|
|
|
37
45
|
def armory_load_through_scope?
|
|
38
46
|
@armory_load_through_scope
|
|
@@ -72,10 +80,24 @@ module GraphQL
|
|
|
72
80
|
def authorized?(object, args, context)
|
|
73
81
|
rule = armory_rule_for(owner, context)
|
|
74
82
|
return false if rule.nil?
|
|
83
|
+
if resolver && rule.is_a?(Rule) && rule.observed?
|
|
84
|
+
raise ConfigurationError, "#{owner.graphql_name}.#{graphql_name} has a resolver or a mutation, which can write, so it cannot use an observed rule"
|
|
85
|
+
end
|
|
75
86
|
|
|
76
87
|
(rule == :covered || Integration.allowed?(object, Integration.with_owner_model(rule, owner), context)) && super
|
|
77
88
|
end
|
|
78
89
|
|
|
90
|
+
# Asked of a field with no resolver. True when it declares no rule of its own and the class that declared it does not cover it. It asks that class
|
|
91
|
+
# and not the type being served: a field a payload inherits keeps the parent mutation as its owner.
|
|
92
|
+
def armory_denied_by_owner?
|
|
93
|
+
return false unless [nil, true].include?(@armory_declared) && owner.respond_to?(:armory_rule)
|
|
94
|
+
|
|
95
|
+
rule = owner.armory_rule
|
|
96
|
+
return true if rule.nil?
|
|
97
|
+
|
|
98
|
+
@armory_declared.nil? ? rule.none? && !rule.covers_fields? : rule.none?
|
|
99
|
+
end
|
|
100
|
+
|
|
79
101
|
private
|
|
80
102
|
|
|
81
103
|
# A resolver or mutation that includes Base enforces its own rule, so the field is covered by it.
|
|
@@ -94,13 +116,13 @@ module GraphQL
|
|
|
94
116
|
return Integration.bypassed_implementer?(type, context.schema) ? nil : :covered if type.respond_to?(:kind) && type.kind.interface?
|
|
95
117
|
return :covered unless type.respond_to?(:armory_rule)
|
|
96
118
|
|
|
97
|
-
|
|
119
|
+
armory_denied_by_owner? ? nil : :covered
|
|
98
120
|
when true
|
|
99
121
|
type_rule unless type_rule&.none?
|
|
100
122
|
when :none
|
|
101
123
|
Rule.none(reason: @armory_reason)
|
|
102
124
|
else
|
|
103
|
-
Rule.new(@armory_declared, policy: armory_policy, attribute: original_name)
|
|
125
|
+
Rule.new(@armory_declared, policy: armory_policy, attribute: original_name, observe: @armory_observe)
|
|
104
126
|
end
|
|
105
127
|
end
|
|
106
128
|
end
|
|
@@ -60,7 +60,15 @@ module GraphQL
|
|
|
60
60
|
rule = armory_rule
|
|
61
61
|
return false if rule.nil?
|
|
62
62
|
|
|
63
|
+
Integration.refuse_unobservable(self, context) if rule.observed?
|
|
63
64
|
Integration.allowed?(object, Integration.with_owner_model(rule, self), context) && super
|
|
65
|
+
rescue GraphQL::UnauthorizedError
|
|
66
|
+
raise
|
|
67
|
+
rescue StandardError => error
|
|
68
|
+
# graphql-ruby reads what a rescue_from answers here as the authorization, on both engines, so a handler that logs
|
|
69
|
+
# and answers true would serve the object. The handler runs, its answer does not count. No public hook does this.
|
|
70
|
+
context.query.handle_or_reraise(error)
|
|
71
|
+
false
|
|
64
72
|
end
|
|
65
73
|
end
|
|
66
74
|
end
|
|
@@ -21,6 +21,8 @@ module GraphQL
|
|
|
21
21
|
def authorize(action, reason: nil, on_denied: :default, **options)
|
|
22
22
|
raise ArgumentError, "on_denied must be one of #{ON_DENIED.inspect}, got #{on_denied.inspect}" unless ON_DENIED.include?(on_denied)
|
|
23
23
|
|
|
24
|
+
raise ArgumentError, "observe: does not apply to a mutation or a resolver: what it runs cannot be undone" if options.key?(:observe)
|
|
25
|
+
|
|
24
26
|
@armory_on_denied = on_denied
|
|
25
27
|
@armory_rule = Rule.new(action, reason: reason, **options)
|
|
26
28
|
end
|
|
@@ -41,7 +41,7 @@ module GraphQL
|
|
|
41
41
|
return Adapters::Null.new.scope(nil, items, rule) if rule.nil?
|
|
42
42
|
|
|
43
43
|
configuration = configuration(context)
|
|
44
|
-
Scope.(items, rule, context, adapter: configuration.adapter, sinks: configuration.sinks)
|
|
44
|
+
Scope.(items, rule, context, adapter: configuration.adapter, sinks: configuration.sinks, on_sink_error: configuration.on_sink_error)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
# Looks an object up inside the user's scope, by primary key unless a key is given.
|
|
@@ -60,7 +60,7 @@ module GraphQL
|
|
|
60
60
|
|
|
61
61
|
# An in-memory scope yields an Array, which cannot look one record up: find it, then ask the rule.
|
|
62
62
|
record = model.find_by(conditions)
|
|
63
|
-
record if record && allowed?(record, rule, context)
|
|
63
|
+
record if record && allowed?(record, rule.enforced, context)
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
# Loads a `loads:` argument inside the user's scope. When the schema defines `object_from_id` (global
|
|
@@ -124,12 +124,23 @@ module GraphQL
|
|
|
124
124
|
end
|
|
125
125
|
private_class_method :declared_model
|
|
126
126
|
|
|
127
|
+
# 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
|
+
def self.refuse_unobservable(type, context)
|
|
130
|
+
known = context.namespace(:armory)[:observable] ||= {}.compare_by_identity
|
|
131
|
+
field = known.fetch(type) { known[type] = type.all_field_definitions.find(&:resolver) }
|
|
132
|
+
return unless field
|
|
133
|
+
|
|
134
|
+
raise ConfigurationError, "#{type.graphql_name} cannot be observed: its field #{field.graphql_name} has a resolver or a mutation, which can write. " \
|
|
135
|
+
"Remove observe:, or put it on its plain fields"
|
|
136
|
+
end
|
|
137
|
+
|
|
127
138
|
# One Authorize per query: building it (and its memo and telemetry) once per object is pure overhead.
|
|
128
139
|
def self.allowed?(subject, rule, context)
|
|
129
140
|
store = context.namespace(:armory)
|
|
130
141
|
authorize = store[:authorize] ||= begin
|
|
131
142
|
configuration = configuration(context)
|
|
132
|
-
Authorize.build(context, adapter: configuration.adapter, sinks: configuration.sinks)
|
|
143
|
+
Authorize.build(context, adapter: configuration.adapter, sinks: configuration.sinks, on_sink_error: configuration.on_sink_error)
|
|
133
144
|
end
|
|
134
145
|
|
|
135
146
|
authorize.(subject, rule, context)
|
|
@@ -5,10 +5,16 @@ require "rspec/expectations"
|
|
|
5
5
|
# `require "graphql/armory/authorization/rspec"` in your spec helper, then:
|
|
6
6
|
#
|
|
7
7
|
# expect(AppSchema).to be_fully_authorized
|
|
8
|
-
|
|
8
|
+
# expect(AppSchema).to be_fully_authorized(baseline: "config/graphql_armory_baseline.json") # fails only on new problems
|
|
9
|
+
RSpec::Matchers.define :be_fully_authorized do |*positional, baseline: nil|
|
|
10
|
+
raise ArgumentError, "be_fully_authorized takes baseline: path" unless positional.empty?
|
|
11
|
+
|
|
9
12
|
match do |schema|
|
|
10
|
-
|
|
11
|
-
@report.ok?
|
|
13
|
+
report = GraphQL::Armory::Authorization::Audit.(schema)
|
|
14
|
+
next (@report = report).ok? unless baseline
|
|
15
|
+
|
|
16
|
+
@report = GraphQL::Armory::Authorization::Audit::Baseline.load(baseline).(report)
|
|
17
|
+
@report.passes?
|
|
12
18
|
end
|
|
13
19
|
|
|
14
20
|
failure_message do |schema|
|
|
@@ -16,6 +22,6 @@ RSpec::Matchers.define :be_fully_authorized do
|
|
|
16
22
|
end
|
|
17
23
|
|
|
18
24
|
failure_message_when_negated do |schema|
|
|
19
|
-
"expected #{schema} not to be fully authorized, but the audit found no problems"
|
|
25
|
+
baseline ? "expected #{schema} not to pass its baseline, but it did:\n#{@report}" : "expected #{schema} not to be fully authorized, but the audit found no problems"
|
|
20
26
|
end
|
|
21
27
|
end
|
|
@@ -6,13 +6,13 @@ module GraphQL
|
|
|
6
6
|
# Immutable value holding what was declared, e.g. `authorize :read`.
|
|
7
7
|
# Each adapter reads the parts it needs (policy, subject, attribute, model, scope).
|
|
8
8
|
class Rule
|
|
9
|
-
attr_reader :action, :policy, :subject, :attribute, :model, :scope, :visible_when, :covers_fields, :reason
|
|
9
|
+
attr_reader :action, :policy, :subject, :attribute, :model, :scope, :visible_when, :covers_fields, :reason, :observe
|
|
10
10
|
|
|
11
11
|
def self.none(reason:)
|
|
12
12
|
new(:none, reason: reason)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def initialize(action, policy: nil, subject: nil, attribute: nil, model: nil, scope: nil, visible_when: nil, covers_fields: false, reason: nil)
|
|
15
|
+
def initialize(action, policy: nil, subject: nil, attribute: nil, model: nil, scope: nil, visible_when: nil, covers_fields: false, reason: nil, observe: nil)
|
|
16
16
|
raise ArgumentError, "a rule needs an action" if action.nil?
|
|
17
17
|
|
|
18
18
|
@action = action.to_sym
|
|
@@ -24,19 +24,35 @@ module GraphQL
|
|
|
24
24
|
@visible_when = visible_when&.to_sym
|
|
25
25
|
@covers_fields = covers_fields
|
|
26
26
|
@reason = reason
|
|
27
|
+
@observe = observe
|
|
27
28
|
|
|
28
29
|
raise ArgumentError, "a bypass needs a reason" if none? && reason.to_s.strip.empty?
|
|
29
30
|
|
|
30
31
|
raise ArgumentError, "covers_fields only applies to a bypass (`authorize :none`)" if covers_fields && !none?
|
|
32
|
+
raise ArgumentError, "observe: needs a text saying why this rule is observed, got #{observe.class}" unless observe.nil? || (observe.is_a?(String) && !observe.strip.empty?)
|
|
33
|
+
raise ArgumentError, "observe: does not apply to a bypass, which refuses nothing" if observe && none?
|
|
31
34
|
|
|
32
35
|
freeze
|
|
33
36
|
end
|
|
34
37
|
|
|
35
38
|
def with_model(model)
|
|
39
|
+
self.class.new(action, policy: policy, subject: subject, attribute: attribute, model: model, scope: scope,
|
|
40
|
+
visible_when: visible_when, covers_fields: covers_fields, reason: reason, observe: observe)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The same rule with no observation, for what must keep refusing: lookups through a scope.
|
|
44
|
+
def enforced
|
|
45
|
+
return self unless observed?
|
|
46
|
+
|
|
36
47
|
self.class.new(action, policy: policy, subject: subject, attribute: attribute, model: model, scope: scope,
|
|
37
48
|
visible_when: visible_when, covers_fields: covers_fields, reason: reason)
|
|
38
49
|
end
|
|
39
50
|
|
|
51
|
+
# A clear "no" to this rule, on a read, is served and recorded instead of refused. See Authorize.
|
|
52
|
+
def observed?
|
|
53
|
+
!observe.nil?
|
|
54
|
+
end
|
|
55
|
+
|
|
40
56
|
# A bypassed type whose fields are covered by something else, such as the payload of a mutation.
|
|
41
57
|
def covers_fields?
|
|
42
58
|
covers_fields
|
|
@@ -58,7 +74,7 @@ module GraphQL
|
|
|
58
74
|
protected
|
|
59
75
|
|
|
60
76
|
def members
|
|
61
|
-
[action, policy, subject, attribute, model, scope, visible_when, covers_fields, reason]
|
|
77
|
+
[action, policy, subject, attribute, model, scope, visible_when, covers_fields, reason, observe]
|
|
62
78
|
end
|
|
63
79
|
end
|
|
64
80
|
end
|
|
@@ -13,22 +13,22 @@ module GraphQL
|
|
|
13
13
|
@telemetry = telemetry
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def self.build(adapter:, sinks: [])
|
|
17
|
-
new(adapter: adapter, telemetry: Telemetry.build(sinks: sinks))
|
|
16
|
+
def self.build(adapter:, sinks: [], on_sink_error: nil)
|
|
17
|
+
new(adapter: adapter, telemetry: Telemetry.build(sinks: sinks, on_sink_error: on_sink_error))
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
def self.call(items, rule, context, adapter:, sinks: [])
|
|
21
|
-
build(adapter: adapter, sinks: sinks).(items, rule, context)
|
|
20
|
+
def self.call(items, rule, context, adapter:, sinks: [], on_sink_error: nil)
|
|
21
|
+
build(adapter: adapter, sinks: sinks, on_sink_error: on_sink_error).(items, rule, context)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def call(items, rule, context)
|
|
25
25
|
if rule.none?
|
|
26
|
-
telemetry.record(:bypassed, reason: rule.reason)
|
|
26
|
+
telemetry.record(:bypassed, context: context, reason: rule.reason)
|
|
27
27
|
return items
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
scoped = adapter.scope(adapter.actor(context), items, rule)
|
|
31
|
-
telemetry.record(:scoped, action: rule.action)
|
|
31
|
+
telemetry.record(:scoped, context: context, action: rule.action)
|
|
32
32
|
scoped
|
|
33
33
|
end
|
|
34
34
|
end
|
|
@@ -36,7 +36,7 @@ module GraphQL
|
|
|
36
36
|
module Authorization
|
|
37
37
|
# The schema plugin: `use GraphQL::Armory::Authorization, adapter: :pundit`.
|
|
38
38
|
# Options: `adapter:` (:pundit or an adapter object), `on_unauthorized:` (:null, :error or :raise),
|
|
39
|
-
# `sinks:` (callables that receive each telemetry signal), `own_checks:` (an app's own `authorized?`, `authorizes?`, `authorized_new`, `wrap`,
|
|
39
|
+
# `sinks:` (callables that receive each telemetry signal), `on_sink_error:` (called with the error and the signal when a sink raises), `own_checks:` (an app's own `authorized?`, `authorizes?`, `authorized_new`, `wrap`,
|
|
40
40
|
# `unauthorized_object`, `visible?`, `scope_items` or `reauthorize_scoped_objects(false)` on a class, with the reason, for the audit), `own_denial_hooks:` (the schema's own
|
|
41
41
|
# `unauthorized_field` or `unauthorized_object`, each with the reason, for the audit).
|
|
42
42
|
def self.use(schema_defn, **options)
|
data/lib/graphql/armory/ids.rb
CHANGED
|
@@ -16,8 +16,8 @@ module GraphQL
|
|
|
16
16
|
module IDs
|
|
17
17
|
SEPARATOR = "/"
|
|
18
18
|
|
|
19
|
-
def self.use(schema, encoder: nil, find: nil, sinks: [])
|
|
20
|
-
lookup = Lookup.build(encoder: encoder, find: find, sinks: sinks)
|
|
19
|
+
def self.use(schema, encoder: nil, find: nil, sinks: [], on_sink_error: nil)
|
|
20
|
+
lookup = Lookup.build(encoder: encoder, find: find, sinks: sinks, on_sink_error: on_sink_error)
|
|
21
21
|
schema.define_singleton_method(:id_from_object) { |object, type, _context| lookup.id_for(object, type) }
|
|
22
22
|
schema.define_singleton_method(:object_from_id) { |id, context| lookup.object_for(id, context) }
|
|
23
23
|
end
|
|
@@ -27,8 +27,8 @@ module GraphQL
|
|
|
27
27
|
attr_reader :encoder, :find, :telemetry
|
|
28
28
|
|
|
29
29
|
# Wires the default encoder unless one is given; `use` goes through here.
|
|
30
|
-
def self.build(encoder: nil, find: nil, sinks: [])
|
|
31
|
-
new(encoder: encoder || Encoder.default, find: find, telemetry: Telemetry.build(sinks: sinks))
|
|
30
|
+
def self.build(encoder: nil, find: nil, sinks: [], on_sink_error: nil)
|
|
31
|
+
new(encoder: encoder || Encoder.default, find: find, telemetry: Telemetry.build(sinks: sinks, on_sink_error: on_sink_error))
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
# `new` is inert: with no encoder wired it hands out no ID and honours none. It checks what it is given.
|
|
@@ -52,25 +52,25 @@ module GraphQL
|
|
|
52
52
|
|
|
53
53
|
def object_for(id, context)
|
|
54
54
|
type_name, key = encoder.decode(id).split(SEPARATOR, 2)
|
|
55
|
-
return refuse(:malformed) if key.nil? || key.empty?
|
|
55
|
+
return refuse(:malformed, context) if key.nil? || key.empty?
|
|
56
56
|
|
|
57
57
|
type = visible_type(type_name, context)
|
|
58
|
-
return refuse(:unknown_type) unless node_type?(type)
|
|
58
|
+
return refuse(:unknown_type, context) unless node_type?(type)
|
|
59
59
|
|
|
60
60
|
object = load(type, key, context)
|
|
61
|
-
return refuse(:not_found, type: type.graphql_name) if object.nil?
|
|
61
|
+
return refuse(:not_found, context, type: type.graphql_name) if object.nil?
|
|
62
62
|
|
|
63
|
-
telemetry.record(:id_resolved, type: type.graphql_name)
|
|
63
|
+
telemetry.record(:id_resolved, context: context, type: type.graphql_name)
|
|
64
64
|
object
|
|
65
65
|
rescue Encoder::InvalidError
|
|
66
|
-
refuse(:invalid)
|
|
66
|
+
refuse(:invalid, context)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
private
|
|
70
70
|
|
|
71
71
|
# Every refusal answers nil, like a missing record; the signal says why, never which ID.
|
|
72
|
-
def refuse(reason, **data)
|
|
73
|
-
telemetry.record(:id_refused, reason: reason, **data)
|
|
72
|
+
def refuse(reason, context, **data)
|
|
73
|
+
telemetry.record(:id_refused, context: context, reason: reason, **data)
|
|
74
74
|
nil
|
|
75
75
|
end
|
|
76
76
|
|