graphql-armory 0.1.0.pre11 โ 0.1.0.pre12
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 +13 -0
- data/README.md +3 -3
- data/docs/authorization.md +7 -7
- data/docs/rate-limiting.md +1 -1
- data/lib/graphql/armory/authorization/adapters/pundit.rb +26 -1
- data/lib/graphql/armory/authorization/integration/object.rb +2 -2
- data/lib/graphql/armory/authorization/integration.rb +29 -2
- data/lib/graphql/armory/authorization/location.rb +26 -2
- data/lib/graphql/armory/authorization/rule.rb +3 -3
- data/lib/graphql/armory/rate_limits/analysis_guard.rb +9 -2
- data/lib/graphql/armory/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9c903e027989813481b053dbd0b920afdeaabdfab91ef33315000001264fe267
|
|
4
|
+
data.tar.gz: 30e1a989f7bb9180067dea9d58a0b3cd6be71b14d95f69225ee73b643abbce50
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4aab89e6ef2f28c43b63739db0a8014e13ead83a7fbfbc3ce50d412bf90d21217f4cd5f9eebb169544aaa7464a56db31cc36e00f9988601babada43d8c3d7aee
|
|
7
|
+
data.tar.gz: 9ca25dbac00ce73ea8072434e0e54b874f360c3f17440362a4b2f4301147b957c4ed8f5ac8f99f6723f9c36217f777ea0e49ea70244183a7be96539b3dcf6737
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.1.0.pre12 (2026-11-04)
|
|
6
|
+
|
|
7
|
+
This release makes [Authorization](docs/authorization.md) cheaper on lists and gives the audit a file and line for the types graphql-ruby generates, and it fixes a way an analysis your app runs itself could let [Rate limiting](docs/rate-limiting.md) undercount a query. Upgrade if you authorize long lists, read the audit's locations, or call `GraphQL::Analysis.analyze_query` before executing a `Query`. Nothing breaks.
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Authorizing a list costs a third to a half less per item for one field, and about a fifth less with three. Within one query the gem remembers each type's rule and, with the Pundit adapter (a subclass included), which policies answer for each record class; nothing is kept across queries, so code reloading never serves a stale class. An adapter that wraps Pundit is asked as configured and remembers nothing. [Performance](docs/authorization.md#performance). (#62)
|
|
12
|
+
- The audit gives a location to the types graphql-ruby generates with no constant: a mutation's payload points to its mutation, and a connection or an edge to its node type. Before, their problems had none. Limit: an anonymous subclass of a named type and a subscription's payload still have none. [The audit](docs/authorization.md#the-audit). (#63)
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- A `Query` your app analyzed itself (`GraphQL::Analysis.analyze_query`) before executing it no longer lets that analysis's answers decide the execution. When an analyzer raised during the execution's analysis, the guard read them as "already charged and visited" and ran the query undercounted. The execution now decides from its own analysis, on both engines: it charges `Points` again, as it already did when nothing raised, takes a second `Concurrency` slot, and charges `Time` only for itself. `Points.headers` no longer report what the app's analysis recorded. Limit: a trace module that runs an analysis inside `analyze_multiplex` must be added after `use`. [Rate limiting](docs/rate-limiting.md). (#61)
|
|
17
|
+
|
|
5
18
|
## 0.1.0.pre11 (2026-11-03)
|
|
6
19
|
|
|
7
20
|
This release closes the ways an [Authorization](docs/authorization.md) rule under `observe:` could still let a resolver or its arguments run for a user the rule would refuse, and gives every misconfigured observed root the same error on every engine. It also makes [the audit](docs/authorization.md#the-audit) say the file and line of each problem. Anyone using `observe:` or comparing audit problems as whole structs should read Breaking before upgrading.
|
data/README.md
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
Open-source extensions for [graphql-ruby](https://github.com/rmosolgo/graphql-ruby), built on its public extension points: no fork, no monkey patching. One gem, one module at a time; each module has its own page in [`docs/`](docs) with the problem it solves, how to use it, and its limits.
|
|
8
8
|
|
|
9
|
-
> **Status: early development.** Prereleases on RubyGems (`0.1.0.
|
|
9
|
+
> **Status: early development.** Prereleases on RubyGems (`0.1.0.pre12`). The API may change before 1.0.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
13
13
|
```ruby
|
|
14
|
-
gem "graphql-armory", "~> 0.1.0.
|
|
14
|
+
gem "graphql-armory", "~> 0.1.0.pre12" # a prerelease: `gem install graphql-armory --pre`
|
|
15
15
|
gem "pundit" # or "cancancan", for the authorization module
|
|
16
16
|
```
|
|
17
17
|
|
|
@@ -23,7 +23,7 @@ Status: โ
released, ๐ง in progress, โฌ not started. Update this table in th
|
|
|
23
23
|
|
|
24
24
|
| Order | Module | Status | Docs | Current limits |
|
|
25
25
|
|---|---|---|---|---|
|
|
26
|
-
| 1 | Authorization (Pundit, CanCanCan) | โ
| [docs/authorization.md](docs/authorization.md) | Released in 0.1.0.pre2 (prerelease). Pundit and CanCanCan (the audit checks less with CanCanCan). Global IDs in `loads:` decoded through `object_from_id`; `loads:` inside input objects scoped with `include Base`. `Execution::Next` needs graphql >= 2.6.2. The audit gives the file and line of each problem from 0.1.0.pre11. |
|
|
26
|
+
| 1 | Authorization (Pundit, CanCanCan) | โ
| [docs/authorization.md](docs/authorization.md) | Released in 0.1.0.pre2 (prerelease). Pundit and CanCanCan (the audit checks less with CanCanCan). Global IDs in `loads:` decoded through `object_from_id`; `loads:` inside input objects scoped with `include Base`. `Execution::Next` needs graphql >= 2.6.2. The audit gives the file and line of each problem from 0.1.0.pre11, and of generated payloads and connections from 0.1.0.pre12. |
|
|
27
27
|
| 2 | Encrypted, versioned IDs and cursors | โ
| [docs/encrypted-ids.md](docs/encrypted-ids.md) | Released in 0.1.0.pre4 (prerelease). `Encoder` (AES-256-GCM), versioned chains and `Legacy`, `Encoder.default` (ENV or Rails), `use GraphQL::Armory::IDs`. Composite primary keys not supported. |
|
|
28
28
|
| 3 | Persisted queries / operation allowlist | โ
| [docs/persisted-queries.md](docs/persisted-queries.md) | Released in 0.1.0.pre4 (prerelease). Operations can be published without a server deploy (`RedisStore`, unreleased). Allowlist from a build manifest (Apollo, Relay), `use GraphQL::Armory::PersistedQueries` with strict mode. A runtime store (database, sync endpoint) is planned for later. |
|
|
29
29
|
| 4 | Rate limiting | โ
| [docs/rate-limiting.md](docs/rate-limiting.md) | Released in 0.1.0.pre5 (prerelease); `RateLimits::Time` in 0.1.0.pre6. Points per client (query complexity) and operations at the same time, `MemoryStore` and `RedisStore`, the budget in response extensions and headers. `RateLimits::Time` charges the wall-clock milliseconds operations take. Invalid queries are charged `invalid_cost:` from 0.1.0.pre10. |
|
data/docs/authorization.md
CHANGED
|
@@ -225,7 +225,7 @@ load "tasks/graphql_armory.rake"
|
|
|
225
225
|
- A type, a mutation or a resolver gives the line of its own `authorize`, or else of its class.
|
|
226
226
|
- An own check or a denial hook gives the line of its method.
|
|
227
227
|
|
|
228
|
-
`location` is `nil` when there is no Ruby line to give: a schema built from SDL, or
|
|
228
|
+
A type graphql-ruby generates gives the line of the class it came from: a mutation's payload the line of its mutation, a connection or an edge the line of its node type. `location` is `nil` when there is no Ruby line to give: a schema built from SDL, or any other class with no name and no `authorize` of its own, such as an anonymous subclass of a named type or the payload of a subscription. A field declared by a helper method gives the helper's line, and a class reopened in several files gives the first one. The location is not part of a problem for the [baseline](#adopting-it-on-a-schema-with-many-problems): moving code never turns an accepted problem into a new one.
|
|
229
229
|
|
|
230
230
|
| Kind | What it means | What to do |
|
|
231
231
|
|---|---|---|
|
|
@@ -508,16 +508,16 @@ Both execution engines are tested: the default interpreter and `Execution::Next`
|
|
|
508
508
|
|
|
509
509
|
## Performance
|
|
510
510
|
|
|
511
|
-
`benchmark/authorization.rb`, 1000 invoices, graphql 2.6.
|
|
511
|
+
`benchmark/authorization.rb`, 1000 invoices, graphql 2.6.14, Ruby 3.2, Docker on Windows. Time per query; the cost of the gem is armory minus plain:
|
|
512
512
|
|
|
513
513
|
| Engine | Query | plain | armory (array) | armory (relation) |
|
|
514
514
|
|---|---|---|---|---|
|
|
515
|
-
| interpreter | `number` |
|
|
516
|
-
| interpreter | 3 fields |
|
|
517
|
-
| next | `number` |
|
|
518
|
-
| next | 3 fields |
|
|
515
|
+
| interpreter | `number` | 7.9 ms | 18.8 ms | 21.9 ms |
|
|
516
|
+
| interpreter | 3 fields | 14.8 ms | 54.9 ms | 59.6 ms |
|
|
517
|
+
| next | `number` | 1.6 ms | 13.8 ms | 16.1 ms |
|
|
518
|
+
| next | 3 fields | 2.4 ms | 37.5 ms | 38.7 ms |
|
|
519
519
|
|
|
520
|
-
|
|
520
|
+
That is about 11 to 14 ยตs per item for one field, a third to a half less than in 0.1.0.pre11. Within one query the gem remembers, for each type, its rule and, for each record class, which Pundit policies answer for it; it never remembers a record or an answer across queries, and the next query looks everything up again, so code reloading cannot serve stale classes. Run it yourself: `docker compose run --rm test bundle exec ruby benchmark/authorization.rb` (add `GRAPHQL_ARMORY_ENGINE=next` for Next, and `PROFILE=1` passed with `docker compose run -e` to see where the time goes).
|
|
521
521
|
|
|
522
522
|
## Migrating
|
|
523
523
|
|
data/docs/rate-limiting.md
CHANGED
|
@@ -317,7 +317,7 @@ Invalid settings raise `RateLimits::ConfigurationError` at boot.
|
|
|
317
317
|
- Each query in a multiplex is charged on its own.
|
|
318
318
|
- With several keys, `Points` reads every balance and then charges, so a refused query leaves the others as they were. Two queries racing between those steps can leave a refused one having charged other keys. `Concurrency` gives back the slots a refused query took at once. `Time` charges every key the whole time; a store failure on one key does not stop the others.
|
|
319
319
|
- An invalid query (a syntax error, a validation error including a variable of the wrong type or a missing required one, no query string, or a refusal from `PersistedQueries` in strict mode) is charged `invalid_cost:` points after it answers; its answer does not change. This penalises the client's next valid query: it does not stop a flood of invalid ones, which are parsed and validated all the same, and a bucket stops at zero. A query the analyzers refused is not invalid: it is charged as before, also when a multiplex analyzer refuses the whole multiplex. Keys are charged in list order, as if the queries had come one at a time: a key that cannot pay all of it pays what it holds, and the keys after it pay only that much, so sending invalid queries together never costs a client less than sending them apart. Put the narrowest key first: a client with nothing left cannot keep draining a bucket it shares. Keys must not depend on the document or the variables, which an invalid query may not have. Failures here, including a query with no key, go to `on_store_error` and never replace the answer, so a missing key on an invalid query does not raise. A trace does this after the multiplex, also when an exception ends it early: then queries that never reached validation are validated to find the invalid ones. A query run with its own trace (`context[:trace]`, or a class given to `trace_mode`) is not charged.
|
|
320
|
-
- An `ExecutionError` raised during analysis, which graphql-ruby would discard before running the query, does not skip a limiter. A refusal already given stands. A limiter not asked yet is asked on its own, so another analyzer's raise still charges the query. A limiter that cannot answer, because a `complexity:` lambda or its `on_store_error` hook raised, refuses the query in hard mode with the code `RATE_LIMIT_NOT_CHARGED`, and in soft mode lets it run and records `charged: false`. A client with no limit still runs, and a query with no key raises `ConfigurationError`. When a query takes this path it is visited once more per limiter not asked yet and per other analyzer (see below), so a `complexity:` lambda can run several times, `key:` and `limit:` are asked again, and a validation timeout can take longer. An analysis your app runs itself
|
|
320
|
+
- An `ExecutionError` raised during analysis, which graphql-ruby would discard before running the query, does not skip a limiter. A refusal already given stands. A limiter not asked yet is asked on its own, so another analyzer's raise still charges the query. A limiter that cannot answer, because a `complexity:` lambda or its `on_store_error` hook raised, refuses the query in hard mode with the code `RATE_LIMIT_NOT_CHARGED`, and in soft mode lets it run and records `charged: false`. A client with no limit still runs, and a query with no key raises `ConfigurationError`. When a query takes this path it is visited once more per limiter not asked yet and per other analyzer (see below), so a `complexity:` lambda can run several times, `key:` and `limit:` are asked again, and a validation timeout can take longer. An analysis your app runs itself (`GraphQL::Analysis.analyze_query`) is not guarded: `Points` charges it and `Concurrency` takes a slot for it, which an analysis the app never executes holds until `stale_after`. If the app then executes the same `Query`, the execution charges `Points` again, takes a second slot (so with a `Concurrency` limit of 1 the execution is refused), charges `Time` only for itself, and decides only from its own analysis, so what the app's analysis answered cannot hide a raise during the execution. An analysis the app runs inside `analyze_multiplex` from a trace module added before `use` is outside this too: it is taken for the execution's own, so a raise during the execution's analysis is not guarded. Add such a module after `use`. A trace that takes no modules from the schema (`context[:trace]` or a class given to `trace_mode`) is outside this: `Points` is not protected on such a trace.
|
|
321
321
|
- On the same path every query analyzer that is not a limiter is then asked on its own. A refusal it returns stands, such as a `max_depth` or `max_complexity` refusal, and so does an `AnalysisError` it raises from `result`, with its own message. One that raises anything else again cannot answer, and the query is refused with the code `QUERY_LIMIT_UNKNOWN`, in soft mode and for a client with no limit too, because otherwise a client could get past that analyzer by asking for the field that makes it raise. That covers `max_complexity` with a `complexity:` lambda that raises and your own maximums alike. So do not raise `ExecutionError` from a `complexity:` lambda or an analyzer: an authorization check belongs in the field. Multiplex analyzers, including a `max_complexity` for a whole multiplex (which `Schema.multiplex` takes from the schema by default), cannot be asked again. When a raise stopped the visit before its end, a query in a multiplex with any multiplex analyzer is refused the same way. A lambda raises after the visit, when the multiplex analyzers have seen the whole query, so it is not refused for that. Each analyzer is asked once more at most, each with the whole remaining validation timeout, so a query on this path can take one more visit per analyzer, and an analyzer with side effects repeats them. After a validation timeout no other analyzer is asked again, since the query is already refused; a limiter not asked yet still is. Limits: this needs a rate limiter on the schema, since a schema with none keeps graphql-ruby's behaviour and the refusal is lost. An analyzer's refusal stands only if it answers the same way when asked again: one that remembers in the context that it already refused is lost. A refusal an analyzer raises as `AnalysisError` during the visit, followed by an `ExecutionError` from the same analyzer, becomes `QUERY_LIMIT_UNKNOWN` without its message. The multiplex analyzers keep the part of a refused query they saw before its visit raised, though it does not run.
|
|
322
322
|
- On `Execution::Next`, `validate: false` ignores every refusal from analysis, the limiters' included. Never pass it for a client's query.
|
|
323
323
|
- A field with `max_page_size: nil` under a schema with `default_max_page_size` is sized by that default, while at runtime it has no maximum, so it is charged less than it returns. Give such a field a number.
|
|
@@ -17,8 +17,24 @@ module GraphQL
|
|
|
17
17
|
raise ArgumentError, "user_key must be a Symbol or a String, got #{user_key.class}" unless user_key.is_a?(Symbol) || user_key.is_a?(String)
|
|
18
18
|
|
|
19
19
|
@user_key = user_key
|
|
20
|
+
@found = nil
|
|
20
21
|
end
|
|
21
22
|
|
|
23
|
+
# A copy that remembers, for one query, which policies a record's class answers to: every item of a list asks the
|
|
24
|
+
# same few classes. Only the class is remembered, never a record, and a lookup that raises is asked again. A `clone`,
|
|
25
|
+
# so a subclass keeps what its `initialize` stored and an instance keeps the modules and methods added to it.
|
|
26
|
+
def per_query
|
|
27
|
+
copy = clone(freeze: false)
|
|
28
|
+
copy.found = {}
|
|
29
|
+
copy
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
protected
|
|
33
|
+
|
|
34
|
+
attr_writer :found
|
|
35
|
+
|
|
36
|
+
public
|
|
37
|
+
|
|
22
38
|
def actor(context)
|
|
23
39
|
context[@user_key]
|
|
24
40
|
end
|
|
@@ -71,8 +87,17 @@ module GraphQL
|
|
|
71
87
|
# policy, past the first, or a subclass past its own stricter policy.
|
|
72
88
|
def policies_for(rule, subject)
|
|
73
89
|
return [rule.policy] if rule.policy
|
|
90
|
+
# A Symbol's policies depend on the Symbol itself, not only on its class.
|
|
91
|
+
return find_policies(rule.model, subject) if @found.nil? || Symbol === subject
|
|
92
|
+
|
|
93
|
+
# For anything else they depend only on the declared model and the class Pundit is asked about.
|
|
94
|
+
by_target = (@found[rule.model] ||= {}.compare_by_identity)
|
|
95
|
+
target = record_target(subject)
|
|
96
|
+
by_target.fetch(target) { by_target[target] = find_policies(rule.model, subject) }
|
|
97
|
+
end
|
|
74
98
|
|
|
75
|
-
|
|
99
|
+
def find_policies(declared_model, subject)
|
|
100
|
+
model = normalize(declared_model) if declared_model
|
|
76
101
|
declared = finder(model).policy if model
|
|
77
102
|
target = model_named?(subject, model) ? subject : record_target(subject)
|
|
78
103
|
own = finder(target).policy
|
|
@@ -61,11 +61,11 @@ module GraphQL
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def authorized?(object, context)
|
|
64
|
-
rule =
|
|
64
|
+
rule = Integration.type_rule(self, context)
|
|
65
65
|
return false if rule.nil?
|
|
66
66
|
|
|
67
67
|
Integration.refuse_unobservable(self, rule, context)
|
|
68
|
-
Integration.allowed?(object,
|
|
68
|
+
Integration.allowed?(object, rule, context) && super
|
|
69
69
|
rescue GraphQL::UnauthorizedError
|
|
70
70
|
raise
|
|
71
71
|
rescue StandardError => error
|
|
@@ -42,7 +42,7 @@ module GraphQL
|
|
|
42
42
|
return Adapters::Null.new.scope(nil, items, rule) if rule.nil?
|
|
43
43
|
|
|
44
44
|
configuration = configuration(context)
|
|
45
|
-
Scope.(items, rule, context, adapter:
|
|
45
|
+
Scope.(items, rule, context, adapter: query_adapter(context), sinks: configuration.sinks, on_sink_error: configuration.on_sink_error)
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# Looks an object up inside the user's scope, by primary key unless a key is given.
|
|
@@ -110,6 +110,16 @@ module GraphQL
|
|
|
110
110
|
end
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
+
# The rule a type checks its objects with, model included, worked out once per type per query: every item of a list
|
|
114
|
+
# asks it. Nil when nothing covers the type. A rule declared while a query runs applies from the next query.
|
|
115
|
+
def self.type_rule(type, context)
|
|
116
|
+
rules = context.namespace(:armory)[:type_rules] ||= {}.compare_by_identity
|
|
117
|
+
rules.fetch(type) do
|
|
118
|
+
rule = type.armory_rule
|
|
119
|
+
rules[type] = rule && with_owner_model(rule, type)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
113
123
|
# The rule with the model its owner wraps, the one the audit reads: named by the rule, or named like the type.
|
|
114
124
|
def self.with_owner_model(rule, owner)
|
|
115
125
|
return rule if rule.none? || rule.policy || rule.model || !owner.respond_to?(:armory_model)
|
|
@@ -187,12 +197,29 @@ module GraphQL
|
|
|
187
197
|
store = context.namespace(:armory)
|
|
188
198
|
authorize = store[:authorize] ||= begin
|
|
189
199
|
configuration = configuration(context)
|
|
190
|
-
Authorize.build(context, adapter:
|
|
200
|
+
Authorize.build(context, adapter: query_adapter(context), sinks: configuration.sinks, on_sink_error: configuration.on_sink_error)
|
|
191
201
|
end
|
|
192
202
|
|
|
193
203
|
authorize.(subject, rule, context)
|
|
194
204
|
end
|
|
195
205
|
|
|
206
|
+
# The adapter this query asks, shared by the checks and the scopes. The Pundit adapter, a subclass included, gets a
|
|
207
|
+
# copy of its own that remembers lookups for this query. Any other adapter is asked as configured: a wrapper around
|
|
208
|
+
# Pundit keeps its own answers, it only remembers nothing.
|
|
209
|
+
def self.query_adapter(context)
|
|
210
|
+
store = context.namespace(:armory)
|
|
211
|
+
store[:adapter] ||= begin
|
|
212
|
+
adapter = configuration(context).adapter
|
|
213
|
+
pundit?(adapter) ? adapter.per_query : adapter
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Never loads Pundit: while its adapter is not loaded, no adapter can be one.
|
|
218
|
+
def self.pundit?(adapter)
|
|
219
|
+
!Adapters.autoload?(:Pundit) && Adapters::Pundit === adapter
|
|
220
|
+
end
|
|
221
|
+
private_class_method :pundit?
|
|
222
|
+
|
|
196
223
|
# Whether a type or field may appear in the schema this user sees. The question is asked of the
|
|
197
224
|
# model's class, with no record, so the rule's action must be one a policy can answer that way.
|
|
198
225
|
# `Schema.types` with no context sees the whole schema. A schema dump asks for it explicitly with
|
|
@@ -5,7 +5,8 @@ module GraphQL
|
|
|
5
5
|
module Authorization
|
|
6
6
|
# Where something of the app's schema was written, as "path:line", for the audit's report. graphql-ruby keeps no
|
|
7
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
|
|
8
|
+
# and a type records the line of its `authorize`. Anything else falls back to the line of its class, and a type
|
|
9
|
+
# graphql-ruby generated (a mutation's payload, a connection, an edge) to the class it was generated from.
|
|
9
10
|
#
|
|
10
11
|
# Limits: a schema built from SDL has no Ruby lines; a field declared by a helper method points to the helper; a class
|
|
11
12
|
# reopened in several files gives the first one.
|
|
@@ -45,11 +46,34 @@ module GraphQL
|
|
|
45
46
|
when GraphQL::Schema::Field, GraphQL::Schema::Argument
|
|
46
47
|
(thing.armory_location if thing.respond_to?(:armory_location)) || absolute(thing.owner)
|
|
47
48
|
when Module
|
|
48
|
-
(thing.armory_rule_location if thing.respond_to?(:armory_rule_location)) || constant(thing)
|
|
49
|
+
(thing.armory_rule_location if thing.respond_to?(:armory_rule_location)) || constant(thing) || generated_from(thing)
|
|
49
50
|
end
|
|
50
51
|
end
|
|
51
52
|
private_class_method :absolute
|
|
52
53
|
|
|
54
|
+
# graphql-ruby generates some types as classes with no constant: a mutation's payload, and a type's connection and
|
|
55
|
+
# edge. They point to the class they were generated from, which was written somewhere.
|
|
56
|
+
# Only an object type is followed: a schema also answers `mutation`, with its root type. A connection of connections
|
|
57
|
+
# can lead back to itself, so each class is visited once, and the walk stops after a few steps.
|
|
58
|
+
def self.generated_from(type)
|
|
59
|
+
seen = {}.compare_by_identity
|
|
60
|
+
while type.is_a?(Class) && type < GraphQL::Schema::Object && !seen.key?(type) && seen.size < 16
|
|
61
|
+
seen[type] = true
|
|
62
|
+
source = type.mutation if type.respond_to?(:mutation)
|
|
63
|
+
source = nil unless source.is_a?(Class) && source < GraphQL::Schema::Resolver
|
|
64
|
+
source ||= type.node_type if type.respond_to?(:node_type)
|
|
65
|
+
return unless source.is_a?(Module)
|
|
66
|
+
|
|
67
|
+
found = (source.armory_rule_location if source.respond_to?(:armory_rule_location)) || constant(source)
|
|
68
|
+
return found if found
|
|
69
|
+
|
|
70
|
+
type = source
|
|
71
|
+
end
|
|
72
|
+
rescue StandardError, NotImplementedError
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
private_class_method :generated_from
|
|
76
|
+
|
|
53
77
|
# Only when the name leads back to this class: a class may answer another constant's name. A location is never worth
|
|
54
78
|
# failing the audit, so a `name` that raises gives none.
|
|
55
79
|
def self.constant(mod)
|
|
@@ -32,6 +32,8 @@ module GraphQL
|
|
|
32
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
33
|
raise ArgumentError, "observe: does not apply to a bypass, which refuses nothing" if observe && none?
|
|
34
34
|
|
|
35
|
+
# Every answer is remembered under its rule, once per item: the hash is computed once.
|
|
36
|
+
@hash = members.hash
|
|
35
37
|
freeze
|
|
36
38
|
end
|
|
37
39
|
|
|
@@ -67,9 +69,7 @@ module GraphQL
|
|
|
67
69
|
end
|
|
68
70
|
alias eql? ==
|
|
69
71
|
|
|
70
|
-
|
|
71
|
-
members.hash
|
|
72
|
-
end
|
|
72
|
+
attr_reader :hash
|
|
73
73
|
|
|
74
74
|
protected
|
|
75
75
|
|
|
@@ -10,8 +10,10 @@ module GraphQL
|
|
|
10
10
|
# own: a refusal it returns or raises stands, and one that raises anything else cannot answer, so unless another refusal
|
|
11
11
|
# stands the query is refused with QUERY_LIMIT_UNKNOWN. A multiplex analyzer cannot be asked again, so when the raise
|
|
12
12
|
# stopped the visit before its end, a query of a multiplex with multiplex analyzers is refused the same way.
|
|
13
|
-
# Only the analysis of an execution
|
|
14
|
-
#
|
|
13
|
+
# Only the analysis of an execution decides here. An analysis the app runs itself is not guarded: its limiters still
|
|
14
|
+
# charge and take a slot, and the execution charges again; what it answered is dropped when the execution's analysis
|
|
15
|
+
# starts. It does not cover a trace that takes no modules from the schema (`context[:trace]`, or a class given to
|
|
16
|
+
# `trace_mode`).
|
|
15
17
|
module AnalysisGuard
|
|
16
18
|
ANSWERS = :limiter_answers
|
|
17
19
|
ASKED = :limiter_asked
|
|
@@ -21,6 +23,7 @@ module GraphQL
|
|
|
21
23
|
REASK = :limiter_guard_reask
|
|
22
24
|
RAISED = :limiter_guard_raised
|
|
23
25
|
VISITED = :limiter_guard_visited
|
|
26
|
+
STALE = [ASKED, ANSWERS, VISITED, RAISED, :points, :time_charges].freeze
|
|
24
27
|
NOT_CHARGED = "This query could not be charged to its rate limit."
|
|
25
28
|
LIMIT_UNKNOWN = "The limits of this query could not be computed."
|
|
26
29
|
|
|
@@ -147,6 +150,10 @@ module GraphQL
|
|
|
147
150
|
return reasked(state) { super } if state.delete(REASK)
|
|
148
151
|
return super unless state.delete(ARMED)
|
|
149
152
|
|
|
153
|
+
# An analysis the app ran itself on this Query left its answers and its visit: the execution decides from its own.
|
|
154
|
+
# Time's debt from that analysis goes too, so a query the execution refuses is not charged for it. A Concurrency
|
|
155
|
+
# slot it took stays recorded, so that the end of the execution releases it.
|
|
156
|
+
STALE.each { |key| state.delete(key) }
|
|
150
157
|
begin
|
|
151
158
|
super
|
|
152
159
|
rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => error
|