graphql-armory 0.1.0.pre13 → 0.1.0.pre15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +5 -5
  4. data/docs/authorization.md +4 -0
  5. data/docs/encrypted-ids.md +24 -2
  6. data/docs/persisted-queries.md +4 -0
  7. data/docs/rate-limiting.md +23 -0
  8. data/docs/response-caching.md +590 -0
  9. data/docs/subscriptions.md +261 -0
  10. data/docs/telemetry.md +39 -1
  11. data/docs/versioning.md +5 -0
  12. data/lib/graphql/armory/authorization/authorize.rb +3 -0
  13. data/lib/graphql/armory/authorization/scope.rb +2 -0
  14. data/lib/graphql/armory/caching/audit/command.rb +51 -0
  15. data/lib/graphql/armory/caching/audit/context_class.rb +117 -0
  16. data/lib/graphql/armory/caching/audit/declarations.rb +166 -0
  17. data/lib/graphql/armory/caching/audit/own_checks.rb +150 -0
  18. data/lib/graphql/armory/caching/audit/public_rules.rb +158 -0
  19. data/lib/graphql/armory/caching/audit/report.rb +113 -0
  20. data/lib/graphql/armory/caching/audit.rb +136 -0
  21. data/lib/graphql/armory/caching/base.rb +118 -0
  22. data/lib/graphql/armory/caching/canonical.rb +138 -0
  23. data/lib/graphql/armory/caching/charge.rb +86 -0
  24. data/lib/graphql/armory/caching/codec.rb +108 -0
  25. data/lib/graphql/armory/caching/context_methods.rb +88 -0
  26. data/lib/graphql/armory/caching/controls/app_schema.rb +70 -0
  27. data/lib/graphql/armory/caching/controls/context.rb +30 -0
  28. data/lib/graphql/armory/caching/controls/operations.rb +37 -0
  29. data/lib/graphql/armory/caching/controls/schema.rb +123 -0
  30. data/lib/graphql/armory/caching/controls/store.rb +16 -0
  31. data/lib/graphql/armory/caching/controls/walk_schema.rb +197 -0
  32. data/lib/graphql/armory/caching/controls.rb +19 -0
  33. data/lib/graphql/armory/caching/digester.rb +48 -0
  34. data/lib/graphql/armory/caching/errors.rb +16 -0
  35. data/lib/graphql/armory/caching/gate/admission.rb +67 -0
  36. data/lib/graphql/armory/caching/gate/signals.rb +65 -0
  37. data/lib/graphql/armory/caching/gate/store_check.rb +60 -0
  38. data/lib/graphql/armory/caching/gate/substitute.rb +61 -0
  39. data/lib/graphql/armory/caching/gate.rb +384 -0
  40. data/lib/graphql/armory/caching/integration/guard.rb +55 -0
  41. data/lib/graphql/armory/caching/integration/install.rb +262 -0
  42. data/lib/graphql/armory/caching/integration/query.rb +42 -0
  43. data/lib/graphql/armory/caching/integration.rb +141 -0
  44. data/lib/graphql/armory/caching/keyer.rb +208 -0
  45. data/lib/graphql/armory/caching/memory_store.rb +164 -0
  46. data/lib/graphql/armory/caching/nulls.rb +121 -0
  47. data/lib/graphql/armory/caching/purge.rb +98 -0
  48. data/lib/graphql/armory/caching/record_keys.rb +109 -0
  49. data/lib/graphql/armory/caching/recorder/context.rb +178 -0
  50. data/lib/graphql/armory/caching/recorder/log.rb +142 -0
  51. data/lib/graphql/armory/caching/recorder/null.rb +24 -0
  52. data/lib/graphql/armory/caching/recorder/shared_writes.rb +58 -0
  53. data/lib/graphql/armory/caching/recorder/substitute.rb +119 -0
  54. data/lib/graphql/armory/caching/recorder.rb +34 -0
  55. data/lib/graphql/armory/caching/redis_store/scripts.rb +159 -0
  56. data/lib/graphql/armory/caching/redis_store.rb +162 -0
  57. data/lib/graphql/armory/caching/rspec.rb +25 -0
  58. data/lib/graphql/armory/caching/schema_identity.rb +102 -0
  59. data/lib/graphql/armory/caching/store/null.rb +31 -0
  60. data/lib/graphql/armory/caching/store/substitute.rb +144 -0
  61. data/lib/graphql/armory/caching/store.rb +34 -0
  62. data/lib/graphql/armory/caching/tags.rb +39 -0
  63. data/lib/graphql/armory/caching/values/decision.rb +45 -0
  64. data/lib/graphql/armory/caching/values/declaration.rb +114 -0
  65. data/lib/graphql/armory/caching/values/entry.rb +108 -0
  66. data/lib/graphql/armory/caching/values/key.rb +39 -0
  67. data/lib/graphql/armory/caching/values/lookup.rb +35 -0
  68. data/lib/graphql/armory/caching/values/policy.rb +44 -0
  69. data/lib/graphql/armory/caching/viewer.rb +112 -0
  70. data/lib/graphql/armory/caching/walk/directives.rb +45 -0
  71. data/lib/graphql/armory/caching/walk/relay.rb +54 -0
  72. data/lib/graphql/armory/caching/walk/run.rb +130 -0
  73. data/lib/graphql/armory/caching/walk/tally.rb +38 -0
  74. data/lib/graphql/armory/caching/walk.rb +46 -0
  75. data/lib/graphql/armory/caching.rb +69 -0
  76. data/lib/graphql/armory/encoder/legacy.rb +14 -0
  77. data/lib/graphql/armory/encoder/null.rb +14 -1
  78. data/lib/graphql/armory/encoder/substitute.rb +54 -2
  79. data/lib/graphql/armory/encoder/versioned.rb +36 -0
  80. data/lib/graphql/armory/encoder.rb +80 -1
  81. data/lib/graphql/armory/persisted_queries.rb +8 -1
  82. data/lib/graphql/armory/rate_limits/cached_charge.rb +43 -0
  83. data/lib/graphql/armory/rate_limits/concurrency.rb +12 -5
  84. data/lib/graphql/armory/rate_limits/invalid_queries.rb +3 -2
  85. data/lib/graphql/armory/rate_limits/points.rb +21 -9
  86. data/lib/graphql/armory/rate_limits/time.rb +15 -8
  87. data/lib/graphql/armory/rate_limits.rb +9 -0
  88. data/lib/graphql/armory/subscriptions/ably/key.rb +49 -0
  89. data/lib/graphql/armory/subscriptions/ably/publisher.rb +132 -0
  90. data/lib/graphql/armory/subscriptions/ably/token_request.rb +42 -0
  91. data/lib/graphql/armory/subscriptions/ably/webhook.rb +96 -0
  92. data/lib/graphql/armory/subscriptions/ably.rb +126 -0
  93. data/lib/graphql/armory/subscriptions/audit.rb +92 -0
  94. data/lib/graphql/armory/subscriptions/controls/dependencies.rb +37 -0
  95. data/lib/graphql/armory/subscriptions/controls/event_log.rb +30 -0
  96. data/lib/graphql/armory/subscriptions/controls/id.rb +26 -0
  97. data/lib/graphql/armory/subscriptions/controls/record.rb +34 -0
  98. data/lib/graphql/armory/subscriptions/controls/schema.rb +187 -0
  99. data/lib/graphql/armory/subscriptions/controls/viewer.rb +99 -0
  100. data/lib/graphql/armory/subscriptions/controls.rb +19 -0
  101. data/lib/graphql/armory/subscriptions/core/channels.rb +93 -0
  102. data/lib/graphql/armory/subscriptions/core/clock.rb +40 -0
  103. data/lib/graphql/armory/subscriptions/core/controls.rb +29 -0
  104. data/lib/graphql/armory/subscriptions/core/delivery.rb +250 -0
  105. data/lib/graphql/armory/subscriptions/core/settings.rb +182 -0
  106. data/lib/graphql/armory/subscriptions/core/trigger.rb +63 -0
  107. data/lib/graphql/armory/subscriptions/core/viewer.rb +153 -0
  108. data/lib/graphql/armory/subscriptions/core/write.rb +172 -0
  109. data/lib/graphql/armory/subscriptions/core.rb +200 -0
  110. data/lib/graphql/armory/subscriptions/errors.rb +30 -0
  111. data/lib/graphql/armory/subscriptions/http/null.rb +17 -0
  112. data/lib/graphql/armory/subscriptions/http/substitute.rb +56 -0
  113. data/lib/graphql/armory/subscriptions/http.rb +93 -0
  114. data/lib/graphql/armory/subscriptions/memory_store.rb +115 -0
  115. data/lib/graphql/armory/subscriptions/pusher/signature.rb +45 -0
  116. data/lib/graphql/armory/subscriptions/pusher.rb +192 -0
  117. data/lib/graphql/armory/subscriptions/redis_store/scripts.rb +115 -0
  118. data/lib/graphql/armory/subscriptions/redis_store.rb +157 -0
  119. data/lib/graphql/armory/subscriptions/store/null.rb +23 -0
  120. data/lib/graphql/armory/subscriptions/store/substitute.rb +155 -0
  121. data/lib/graphql/armory/subscriptions/store.rb +38 -0
  122. data/lib/graphql/armory/subscriptions/topic.rb +253 -0
  123. data/lib/graphql/armory/subscriptions/transport/null.rb +28 -0
  124. data/lib/graphql/armory/subscriptions/transport/substitute.rb +150 -0
  125. data/lib/graphql/armory/subscriptions/transport.rb +41 -0
  126. data/lib/graphql/armory/subscriptions/values/capabilities.rb +50 -0
  127. data/lib/graphql/armory/subscriptions/values/message.rb +31 -0
  128. data/lib/graphql/armory/subscriptions/values/outcome.rb +42 -0
  129. data/lib/graphql/armory/subscriptions/values/record.rb +176 -0
  130. data/lib/graphql/armory/subscriptions/webhook_app.rb +31 -0
  131. data/lib/graphql/armory/subscriptions.rb +33 -0
  132. data/lib/graphql/armory/telemetry.rb +8 -4
  133. data/lib/graphql/armory/version.rb +1 -1
  134. data/lib/graphql/armory/versioning/audit.rb +1 -9
  135. data/lib/graphql/armory/versioning/integration.rb +60 -2
  136. data/lib/graphql/armory.rb +2 -0
  137. data/lib/tasks/graphql_armory.rake +9 -0
  138. metadata +110 -1
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Caching
8
+ class Audit
9
+ # Reads every declaration once: what it is (a type's `cache_fields`, a field's `cache:`, a directive's `cache_safe`),
10
+ # whether it can be used at all, whether the installed limits allow its TTL, and lists the public ones and the
11
+ # installed configuration's own declarations with their reasons. Answers the (serving type, field) pairs a
12
+ # `:public` declaration covers, for the checks of what stands on their path.
13
+ class Declarations
14
+ # graphql-ruby's names for the places a directive can stand in an operation.
15
+ EXECUTABLE_LOCATIONS = %i[QUERY MUTATION SUBSCRIPTION FIELD FRAGMENT_DEFINITION FRAGMENT_SPREAD INLINE_FRAGMENT VARIABLE_DEFINITION].freeze
16
+
17
+ # Whether anything of the schema includes `Caching::Base`: a type, a field or a directive.
18
+ def self.uses_base?(schema)
19
+ Audit.definitions(schema).any? do |type|
20
+ type.respond_to?(:armory_cache_fields) || Armory::Definitions.fields_at_every_level(type).any? { |field| field.respond_to?(:armory_cache_own) }
21
+ end || schema.directives.each_value.any? { |directive| directive.respond_to?(:armory_cache_safe?) }
22
+ end
23
+
24
+ # [[serving type, field, declaration]] for every field whose declaration is `:public`, on every type that serves it.
25
+ attr_reader :public_pairs
26
+
27
+ def initialize(schema, definitions, settings, findings)
28
+ @schema = schema
29
+ @definitions = definitions
30
+ @settings = settings
31
+ @findings = findings
32
+ @public_pairs = []
33
+ @seen = Set.new.compare_by_identity
34
+ @declarations = {}.compare_by_identity
35
+ end
36
+
37
+ def call
38
+ ignored_roots = [@schema.mutation, @schema.subscription].compact
39
+ @definitions.each do |type|
40
+ next ignored_input(type) if type.kind.input_object?
41
+ next unless type.respond_to?(:fields)
42
+ next ignored_root(type) if ignored_roots.include?(type)
43
+
44
+ type_default(type)
45
+ Armory::Definitions.fields_at_every_level(type).each { |field| field_declaration(type, field) }
46
+ end
47
+ directives
48
+ installed
49
+ end
50
+
51
+ private
52
+
53
+ def type_default(type)
54
+ declaration = type.armory_cache_fields if type.respond_to?(:armory_cache_fields)
55
+ return if declaration.nil?
56
+
57
+ check(declaration, type.graphql_name, type)
58
+ end
59
+
60
+ def field_declaration(type, field)
61
+ declaration = declaration_of(field)
62
+ return if declaration.nil? || declaration.forbidden?
63
+
64
+ @public_pairs << [type, field, declaration] if declaration.public?
65
+ return unless @seen.add?(field) && !field.armory_cache_own.nil?
66
+ # A field that keeps its type's TTL and scope is that type's declaration: it is checked and listed there.
67
+ return if inherits?(field, declaration) && !declaration.public?
68
+
69
+ check(declaration, path_of(field), field, limits: !inherits?(field, declaration))
70
+ end
71
+
72
+ # The declaration that applies to a field, worked out once. One its type cannot complete raises
73
+ # (`armory_cache`): it is reported, and the field counts as declaring nothing, as the walk refuses it.
74
+ def declaration_of(field)
75
+ return unless field.respond_to?(:armory_cache)
76
+ return @declarations[field] if @declarations.key?(field)
77
+
78
+ @declarations[field] = field.armory_cache
79
+ rescue ConfigurationError => error
80
+ @findings.problem(:incomplete, path_of(field), "#{error.message}: its type declares no cache_fields to complete it, so it is never cached. " \
81
+ "Give it ttl:, or give its type cache_fields", at: field)
82
+ @declarations[field] = nil
83
+ end
84
+
85
+ def inherits?(field, declaration)
86
+ default = field.owner.armory_cache_fields if field.owner.respond_to?(:armory_cache_fields)
87
+ !default.nil? && default.ttl == declaration.ttl && default.scope == declaration.scope
88
+ end
89
+
90
+ def check(declaration, path, at, limits: true)
91
+ @findings.declared(:public, path, declaration.reason, at: at) if declaration.public?
92
+ return unless limits
93
+
94
+ ttl(declaration, path, at)
95
+ viewer(declaration, path, at)
96
+ end
97
+
98
+ def ttl(declaration, path, at)
99
+ if declaration.ttl > @settings.max_ttl
100
+ @findings.problem(:ttl, path, "ttl: #{declaration.ttl} is longer than max_ttl: #{@settings.max_ttl} allows. Lower it", at: at)
101
+ elsif !declaration.public? && declaration.ttl > @settings.max_viewer_ttl
102
+ @findings.problem(:ttl, path, "a :viewer ttl: #{declaration.ttl} is longer than max_viewer_ttl: #{@settings.max_viewer_ttl} allows: " \
103
+ "a viewer keeps a revoked permission's answer that long. Lower it", at: at)
104
+ end
105
+ end
106
+
107
+ def viewer(declaration, path, at)
108
+ return if declaration.public? || !@settings.installed? || !@settings.viewer.nil?
109
+
110
+ @findings.problem(:viewer_unconfigured, path, "declared :viewer, but `use GraphQL::Armory::Caching` sets no viewer:, so operations that " \
111
+ "select it never cache. Set viewer:, or declare it :public if it really is the same for everyone", at: at)
112
+ end
113
+
114
+ # A mutation and a subscription are never cached: what their root declares is never read.
115
+ def ignored_root(type)
116
+ label = type.equal?(@schema.mutation) ? "mutation" : "subscription"
117
+ if type.respond_to?(:armory_cache_fields) && type.armory_cache_fields
118
+ @findings.problem(:ignored, type.graphql_name, "cache_fields on the #{label} root type is never used: a #{label} is never cached. Remove it", at: type)
119
+ end
120
+ Armory::Definitions.fields_at_every_level(type).each do |field|
121
+ next unless field.respond_to?(:armory_cache_own) && !field.armory_cache_own.nil?
122
+
123
+ @findings.problem(:ignored, "#{type.graphql_name}.#{field.graphql_name}", "cache: on a #{label} field is never used: a #{label} is never cached. Remove it", at: field)
124
+ end
125
+ end
126
+
127
+ def ignored_input(type)
128
+ return unless type.respond_to?(:armory_cache_fields) && type.armory_cache_fields
129
+
130
+ @findings.problem(:ignored, type.graphql_name, "cache_fields on an input type is never used: only output fields are cached. Remove it", at: type)
131
+ end
132
+
133
+ # A directive in an operation makes it uncacheable unless it declares `cache_safe`: that never serves a wrong
134
+ # answer, so it is information.
135
+ def directives
136
+ defaults = GraphQL::Schema.default_directives
137
+ @schema.directives.each do |name, directive|
138
+ next if defaults[name].equal?(directive)
139
+
140
+ path = "@#{name}"
141
+ if directive.respond_to?(:armory_cache_safe?) && directive.armory_cache_safe?
142
+ @findings.declared(:cache_safe, path, directive.armory_cache_safe_reason, at: directive)
143
+ elsif (Array(directive.locations).map(&:to_sym) & EXECUTABLE_LOCATIONS).any?
144
+ @findings.information(:directive, path, "an operation that uses it is never cached: declare `cache_safe reason: \"...\"` on it if it does not change the data", at: directive)
145
+ end
146
+ end
147
+ end
148
+
149
+ # What the installed configuration declares, with its reasons. `context_methods:` is listed by ContextClass,
150
+ # which knows which of them the context class defines.
151
+ def installed
152
+ return unless @settings.installed?
153
+
154
+ @findings.declared(:anonymous, "anonymous: :shared", @settings.anonymous_reason.to_s) if @settings.anonymous == :shared
155
+ @settings.neutral_keys.each { |key, reason| @findings.declared(:neutral_key, "neutral_keys: #{key.inspect}", reason.to_s) }
156
+ end
157
+
158
+ def path_of(field)
159
+ owner = field.owner
160
+ "#{owner.respond_to?(:graphql_name) ? owner.graphql_name : owner.inspect}.#{field.graphql_name}"
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Caching
8
+ class Audit
9
+ # The app's own `visible?`, `authorized?`, `scope_items`, `authorized_new` or `wrap` on a class a `:public`
10
+ # declaration reaches can answer per user, and the audit cannot read what it does: each is reported once, where it
11
+ # is defined, until `own_checks:` declares it with a reason. The classes reached are the type that serves the
12
+ # field and the one that defines it, every type it returns, its field and argument classes, the input objects of
13
+ # its arguments, and its resolver. Methods of graphql-ruby and of this gem are told apart with Authorization's
14
+ # audit's `LibraryMethods`, and the whole `super` chain is read: an override that calls super must not hide one
15
+ # below it.
16
+ class OwnChecks
17
+ TYPE_CHECKS = %i[visible? authorized? scope_items authorized_new wrap].freeze
18
+ MEMBER_CHECKS = %i[visible? authorized?].freeze
19
+ RESOLVER_CLASS_CHECKS = %i[visible?].freeze
20
+ LIBRARY_PATHS = [Authorization::Audit::GRAPHQL_RUBY_PATH, Authorization::Audit::ARMORY_PATH].freeze
21
+
22
+ def initialize(definitions, findings)
23
+ @definitions = definitions
24
+ @findings = findings
25
+ @found = {}
26
+ @reached = Set.new.compare_by_identity
27
+ end
28
+
29
+ def call(pairs, declared)
30
+ pairs.each do |type, field, _declaration|
31
+ reach_type(type)
32
+ reach_type(field.owner)
33
+ PublicRules.data_types(field, @definitions).each { |returned| reach_type(returned) }
34
+ reach_member(field.class)
35
+ reach_arguments(PublicRules.arguments_of(field))
36
+ reach_resolver(field.resolver) if field.resolver
37
+ end
38
+ report(declared)
39
+ end
40
+
41
+ private
42
+
43
+ def reach_type(type)
44
+ return unless type.is_a?(Module) && @reached.add?(type)
45
+
46
+ TYPE_CHECKS.each { |name| note(type, type.method(name), name, ".") if type.respond_to?(name, true) }
47
+ end
48
+
49
+ def reach_member(klass)
50
+ return unless @reached.add?(klass)
51
+
52
+ MEMBER_CHECKS.each { |name| note(klass, klass.instance_method(name), name, "#") if instance_check?(klass, name) }
53
+ end
54
+
55
+ def reach_resolver(resolver)
56
+ reach_member(resolver)
57
+ RESOLVER_CLASS_CHECKS.each { |name| note(resolver, resolver.method(name), name, ".") if resolver.respond_to?(name, true) }
58
+ end
59
+
60
+ def reach_arguments(arguments, seen = Set.new.compare_by_identity)
61
+ arguments.each do |argument|
62
+ reach_member(argument.class)
63
+ input = input_of(argument)
64
+ next unless input && seen.add?(input)
65
+
66
+ reach_type(input)
67
+ reach_arguments(input.all_argument_definitions, seen)
68
+ end
69
+ end
70
+
71
+ def input_of(argument)
72
+ type = argument.type.unwrap
73
+ type if type.respond_to?(:kind) && type.kind.input_object?
74
+ rescue GraphQL::Error, NameError
75
+ nil
76
+ end
77
+
78
+ def instance_check?(klass, name)
79
+ klass.method_defined?(name) || klass.private_method_defined?(name)
80
+ end
81
+
82
+ def note(klass, method, name, separator)
83
+ while method
84
+ holder = Authorization::Audit::LibraryMethods.holder_of(klass, method)
85
+ @found[[holder, name]] ||= ["#{label(holder)}#{separator}#{name}", method] if app?(method, holder)
86
+ method = method.super_method
87
+ end
88
+ end
89
+
90
+ # A method with no Ruby source is Ruby's own, not the app's.
91
+ def app?(method, holder)
92
+ !method.source_location.nil? && !Authorization::Audit::LibraryMethods.library_method?(method, holder, *LIBRARY_PATHS)
93
+ end
94
+
95
+ def report(declared)
96
+ @found.each do |(holder, name), (path, method)|
97
+ reason = declared.fetch(holder, {})[name]
98
+ next @findings.declared(:own_check, path, reason, at: method) if reason
99
+
100
+ @findings.problem(:own_check, path, "#{risk(name)}. Remove it, or declare it in own_checks: with a reason", at: method)
101
+ end
102
+ declared.each do |holder, methods|
103
+ methods.each_key do |name|
104
+ next if @found.key?([holder, name]) || defined_by_app?(holder, name)
105
+
106
+ @findings.problem(:own_check, "#{label(holder)} #{name}", "named in own_checks: but #{label(holder)} defines no #{name} of its own: remove it from the option", at: holder)
107
+ end
108
+ end
109
+ end
110
+
111
+ # A declared check a :public declaration does not reach is not a problem, as long as the class defines it.
112
+ def defined_by_app?(holder, name)
113
+ chain = []
114
+ chain << holder.method(name) if holder.respond_to?(name, true)
115
+ chain << holder.instance_method(name) if holder.is_a?(Module) && instance_check?(holder, name)
116
+ chain.any? do |method|
117
+ found = false
118
+ while method && !found
119
+ found = Authorization::Audit::LibraryMethods.holder_of(holder, method).equal?(holder) && app?(method, holder)
120
+ method = method.super_method
121
+ end
122
+ found
123
+ end
124
+ end
125
+
126
+ def label(holder)
127
+ return holder.name if holder.respond_to?(:name) && holder.name.is_a?(String)
128
+ return holder.graphql_name if holder.respond_to?(:graphql_name)
129
+
130
+ holder.inspect
131
+ rescue StandardError
132
+ holder.inspect # a class with no name graphql-ruby can give
133
+ end
134
+
135
+ def risk(name)
136
+ case name
137
+ when :visible? then "defined by the app, and the audit cannot read it: if it hides something from some users, a cached answer skips the " \
138
+ "validation that would refuse them, so they get what a viewer who could see it got"
139
+ when :scope_items then "defined by the app, and the audit cannot read it: if it narrows the list per user, a shared answer holds the list " \
140
+ "of whoever ran first"
141
+ when :authorized_new, :wrap then "defined by the app, and the audit cannot read it: it decides whether authorized? runs, or which object the " \
142
+ "fields see, so it can answer per user"
143
+ else "defined by the app, and the audit cannot read it: if it refuses some users, one it refuses gets the answer cached for one it allowed"
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module GraphQL
6
+ module Armory
7
+ module Caching
8
+ class Audit
9
+ # A `:public` answer is stored once and served to everyone, so nothing on its path may decide per user. Wherever
10
+ # Authorization consults a policy for it, the answer is not public: the type that serves the field or defines it,
11
+ # the field's own rule, `visible_when:` and `observe:`, its resolver's rule, an argument's rule, `loads:` and
12
+ # `find_by:`, and every type it returns (an interface's or a union's possible types, a connection's nodes and
13
+ # edges, so a scoped list too). Each is read with the same readers Authorization's audit uses: `armory_rule` and
14
+ # `Rule#none?`, `armory_declared`, `armory_visible_when`, `Integration.observed_reason`, `Argument#loads`.
15
+ class PublicRules
16
+ NO_RULE = [nil, :none].freeze
17
+
18
+ # Every type a field answers with: its unwrapped type, an interface's or a union's possible types among the
19
+ # definitions (hidden and orphan ones included), and a connection's nodes and edges.
20
+ def self.data_types(field, definitions)
21
+ pending = [element_of(field)].compact
22
+ seen = Set.new.compare_by_identity
23
+ found = []
24
+ until pending.empty?
25
+ type = pending.pop
26
+ next unless type.respond_to?(:kind) && seen.add?(type)
27
+
28
+ found << type
29
+ pending.concat(related(type, definitions))
30
+ end
31
+ found
32
+ end
33
+
34
+ def self.element_of(field)
35
+ field.type.unwrap
36
+ rescue GraphQL::Error, NameError
37
+ nil
38
+ end
39
+ private_class_method :element_of
40
+
41
+ def self.related(type, definitions)
42
+ related = []
43
+ if Armory::Definitions.connection?(type)
44
+ related << type.node_type
45
+ related.concat(Armory::Definitions.fields_at_every_level(type).select { |field| field.original_name == :edges }.filter_map { |field| element_of(field) })
46
+ end
47
+ related.concat(definitions.select { |member| member.is_a?(Class) && member.kind.object? && member <= type }) if type.kind.interface?
48
+ related.concat(type.type_memberships.map(&:object_type)) if type.kind.union?
49
+ related.concat(Armory::Definitions.fields_at_every_level(type).select { |field| field.original_name == :node }.filter_map { |field| element_of(field) }) if edge?(type)
50
+ related.compact
51
+ end
52
+ private_class_method :related
53
+
54
+ def self.edge?(type)
55
+ type.is_a?(Class) && type <= GraphQL::Types::Relay::BaseEdge
56
+ end
57
+ private_class_method :edge?
58
+
59
+ # The arguments a field takes: its own and its resolver's.
60
+ def self.arguments_of(field)
61
+ resolver = field.resolver
62
+ (field.all_argument_definitions + (resolver ? resolver.all_argument_definitions : [])).uniq
63
+ end
64
+
65
+ def initialize(definitions, findings)
66
+ @definitions = definitions
67
+ @findings = findings
68
+ end
69
+
70
+ def call(pairs)
71
+ pairs.each do |type, field, _declaration|
72
+ reasons = reasons_for(type, field).uniq
73
+ next if reasons.empty?
74
+
75
+ @findings.problem(:public_over_rule, "#{type.graphql_name}.#{field.graphql_name}",
76
+ "declared :public, but the policy decides per user here: #{reasons.join('; ')}. Make it :viewer, " \
77
+ "or bypass the rule (`authorize :none`) if the data really is the same for everyone", at: field)
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def reasons_for(type, field)
84
+ reasons = rule_reasons(type, "its type #{type.graphql_name}")
85
+ reasons.concat(rule_reasons(field.owner, "#{name_of(field.owner)}, which defines it,")) unless field.owner.equal?(type)
86
+ reasons.concat(field_reasons(field))
87
+ reasons.concat(argument_reasons(PublicRules.arguments_of(field), Set.new.compare_by_identity))
88
+ reasons << "find_by:, which looks the record up through the user's scope" if find_by?(field)
89
+ PublicRules.data_types(field, @definitions).each do |returned|
90
+ reasons.concat(rule_reasons(returned, "#{name_of(returned)}, which it returns#{' (a scoped list)' if list?(field)},"))
91
+ end
92
+ reasons
93
+ end
94
+
95
+ # A type's or a resolver's rule: an action is a policy, and `visible_when:` asks one even on a bypass.
96
+ def rule_reasons(holder, who)
97
+ rule = holder.armory_rule if holder.respond_to?(:armory_rule)
98
+ return [] if rule.nil?
99
+
100
+ reasons = []
101
+ reasons << "#{who} has the rule #{rule.action.inspect}#{" under observe: (#{rule.observe})" if rule.observed?}" unless rule.none?
102
+ reasons << "#{who} has visible_when: #{rule.visible_when.inspect}" if rule.visible_when
103
+ reasons
104
+ end
105
+
106
+ def field_reasons(field)
107
+ reasons = []
108
+ declared = field.armory_declared if field.respond_to?(:armory_declared)
109
+ reasons << "a field rule (authorize: #{declared.inspect})" unless NO_RULE.include?(declared)
110
+ reasons << "visible_when: #{field.armory_visible_when.inspect}" if field.respond_to?(:armory_visible_when) && field.armory_visible_when
111
+ observed = Authorization::Integration.observed_reason(field)
112
+ reasons << "observe: (#{observed})" if observed
113
+ reasons.concat(rule_reasons(field.resolver, "its resolver #{name_of(field.resolver)}")) if field.resolver
114
+ reasons
115
+ end
116
+
117
+ # An input object's arguments are loaded and checked too, nested to any depth.
118
+ def argument_reasons(arguments, seen, prefix = nil)
119
+ arguments.flat_map do |argument|
120
+ name = [prefix, argument.keyword].compact.join(".")
121
+ reasons = []
122
+ reasons << "loads: #{name_of(argument.loads)} through argument #{name}" if argument.loads
123
+ declared = argument.armory_declared if argument.respond_to?(:armory_declared)
124
+ reasons << "argument #{name} has the rule #{declared.inspect}" unless NO_RULE.include?(declared)
125
+ reasons << "argument #{name} has visible_when: #{argument.armory_visible_when.inspect}" if argument.respond_to?(:armory_visible_when) && argument.armory_visible_when
126
+ input = input_of(argument)
127
+ reasons.concat(argument_reasons(input.all_argument_definitions, seen, name)) if input && seen.add?(input)
128
+ reasons
129
+ end
130
+ end
131
+
132
+ def input_of(argument)
133
+ type = argument.type.unwrap
134
+ type if type.respond_to?(:kind) && type.kind.input_object?
135
+ rescue GraphQL::Error, NameError
136
+ nil
137
+ end
138
+
139
+ def find_by?(field)
140
+ field.extensions.any? { |extension| extension.is_a?(Authorization::Integration::FindBy) }
141
+ end
142
+
143
+ def list?(field)
144
+ field.type.list?
145
+ rescue GraphQL::Error, NameError
146
+ false
147
+ end
148
+
149
+ def name_of(member)
150
+ member.respond_to?(:graphql_name) ? member.graphql_name : member.inspect
151
+ rescue GraphQL::Error
152
+ member.inspect
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Caching
6
+ class Audit
7
+ # What a cache audit found: the problems, and every declaration someone made with its reason, listed apart.
8
+ class Report
9
+ # `location` is "path:line" where the app wrote what the problem is about (see Location), or nil. `severity` is
10
+ # nil for a problem, or :information for something that never serves a wrong answer (an operation that is never
11
+ # cached): `ok?` ignores it unless the audit was strict.
12
+ Problem = Struct.new(:kind, :path, :message, :location, :severity) do
13
+ def information? = severity == :information
14
+
15
+ def to_s
16
+ "#{path} (#{kind}): #{message}"
17
+ end
18
+ end
19
+
20
+ # A declaration the team made, and why: `kind` is :public, :anonymous, :neutral_key, :context_method, :own_check
21
+ # or :cache_safe.
22
+ Declared = Struct.new(:kind, :path, :reason, :location)
23
+
24
+ def self.line(text, location)
25
+ location ? "#{text} (at #{location})" : text
26
+ end
27
+
28
+ attr_reader :problems, :declarations
29
+
30
+ def initialize(problems: [], declarations: [], strict: false)
31
+ @problems = problems.freeze
32
+ @declarations = declarations.freeze
33
+ @strict = strict
34
+ freeze
35
+ end
36
+
37
+ def strict?
38
+ @strict
39
+ end
40
+
41
+ # The problems `ok?` counts: every one under `strict: true`, else all but information.
42
+ def failing
43
+ @strict ? problems : problems.reject(&:information?)
44
+ end
45
+
46
+ def information
47
+ @strict ? [] : problems.select(&:information?)
48
+ end
49
+
50
+ def ok?
51
+ failing.empty?
52
+ end
53
+
54
+ def to_s
55
+ [failing_text, information_text, declarations_text].compact.join("\n\n") << "\n"
56
+ end
57
+
58
+ private
59
+
60
+ def failing_text
61
+ return "No problems." if ok?
62
+
63
+ ["#{failing.size} #{failing.size == 1 ? 'problem' : 'problems'}:", *failing.map { |problem| " #{Report.line(problem.to_s, problem.location)}" }].join("\n")
64
+ end
65
+
66
+ def information_text
67
+ return if information.empty?
68
+
69
+ ["Information:", *information.map { |problem| " #{Report.line(problem.to_s, problem.location)}" }].join("\n")
70
+ end
71
+
72
+ def declarations_text
73
+ return if declarations.empty?
74
+
75
+ ["#{declarations.size} #{declarations.size == 1 ? 'declaration' : 'declarations'}:",
76
+ *declarations.map { |declared| " #{Report.line("#{declared.path} (#{declared.kind}): #{declared.reason}", declared.location)}" }].join("\n")
77
+ end
78
+ end
79
+
80
+ # Collects what one audit finds. A problem found twice (a field inherited by several types, a versioned twin of the
81
+ # same name) is listed once.
82
+ class Findings
83
+ def initialize
84
+ @problems = {}
85
+ @declarations = {}
86
+ end
87
+
88
+ def problem(kind, path, message, at: nil, severity: nil)
89
+ @problems[[kind, path, message]] ||= Report::Problem.new(kind, path, message, Location.of(at), severity)
90
+ end
91
+
92
+ def information(kind, path, message, at: nil)
93
+ problem(kind, path, message, at: at, severity: :information)
94
+ end
95
+
96
+ def problem?(kind, path = nil)
97
+ @problems.each_value.any? { |problem| problem.kind == kind && (path.nil? || problem.path == path) }
98
+ end
99
+
100
+ def declared(kind, path, reason, at: nil)
101
+ @declarations[[kind, path]] ||= Report::Declared.new(kind, path, reason, Location.of(at))
102
+ end
103
+
104
+ def report(strict: false)
105
+ Report.new(problems: @problems.values.sort_by { |problem| [problem.path, problem.kind.to_s, problem.message] },
106
+ declarations: @declarations.values.sort_by { |declared| [declared.kind.to_s, declared.path] },
107
+ strict: strict)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end