graphql-armory 0.1.0.pre7 → 0.1.0.pre9
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 +81 -0
- data/README.md +6 -5
- data/docs/authorization.md +220 -10
- data/docs/encrypted-ids.md +1 -1
- data/docs/persisted-queries.md +111 -19
- data/docs/rate-limiting.md +32 -11
- data/docs/telemetry.md +31 -8
- data/exe/graphql-armory-publish +6 -0
- data/lib/graphql/armory/authorization/adapters/cancan.rb +17 -4
- data/lib/graphql/armory/authorization/adapters/pundit.rb +79 -14
- data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -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/denial_hooks.rb +29 -0
- data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
- data/lib/graphql/armory/authorization/audit/findings.rb +35 -0
- data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
- data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
- data/lib/graphql/armory/authorization/audit/interface_rules.rb +42 -0
- data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
- data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
- data/lib/graphql/armory/authorization/audit/observed_rules.rb +58 -0
- data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
- data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -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 +282 -0
- data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
- data/lib/graphql/armory/authorization/audit.rb +59 -192
- data/lib/graphql/armory/authorization/authorize.rb +35 -9
- data/lib/graphql/armory/authorization/configuration.rb +32 -3
- data/lib/graphql/armory/authorization/definitions.rb +121 -0
- data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
- data/lib/graphql/armory/authorization/integration/field.rb +36 -6
- data/lib/graphql/armory/authorization/integration/object.rb +9 -1
- data/lib/graphql/armory/authorization/integration/resolver.rb +2 -0
- data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
- data/lib/graphql/armory/authorization/integration.rb +36 -4
- data/lib/graphql/armory/authorization/rspec.rb +10 -4
- data/lib/graphql/armory/authorization/rule.rb +24 -3
- data/lib/graphql/armory/authorization/scope.rb +6 -6
- data/lib/graphql/armory/authorization.rb +4 -1
- data/lib/graphql/armory/ids.rb +11 -11
- 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 +79 -0
- data/lib/graphql/armory/rate_limits/concurrency.rb +91 -14
- data/lib/graphql/armory/rate_limits/points.rb +91 -14
- data/lib/graphql/armory/rate_limits/time.rb +107 -19
- data/lib/graphql/armory/rate_limits.rb +29 -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 +28 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3200e9b549bc6bb3b7f77d1905fb79a9d41b59d4730452300437572b4c7cdfe3
|
|
4
|
+
data.tar.gz: 3653d86878c2395602ac78e48f70266cc89ea42d0f393eb122542e958466e36a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa7fbc795c731c4f11cd8e6a002b3178f377ef97caa1669771b8a641a7a2860a9fdf49525f49d25fcb0eaced66901cace0b74bb8acfe05918f456421bbc4d0e6
|
|
7
|
+
data.tar.gz: c828ed91f9bd66f7856d603e33f8e44ce875861be873061e4ae016b9b6b3714bb6daeb0001a8862fef1c9b21134a7c80476ec861f18dee25a015bf67218bcb7c
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,87 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.1.0.pre9 (2026-11-02)
|
|
6
|
+
|
|
7
|
+
This release is about adopting the gem on a real schema, and about limits that hold when a client tries to get around them. An existing app can now [observe a new rule](docs/authorization.md) before enforcing it and keep the audit green on known problems with a baseline; persisted queries can be published without a server deploy and from several manifests ([persisted queries](docs/persisted-queries.md)); and [rate limits](docs/rate-limiting.md) take several keys per query, against clients that rotate one identity. Anyone running the audit or rate limits in hard mode should read Breaking and Security before upgrading.
|
|
8
|
+
|
|
9
|
+
### Breaking
|
|
10
|
+
|
|
11
|
+
- An audit that passed can fail after upgrading, because it reports more: see the entries under Security and the audit entry under Fixed. Read each new problem instead of declaring it to get the build green; a baseline can hold them while you work through them. (#42, #44)
|
|
12
|
+
- In hard mode, a query whose rate limiter cannot answer, because a `complexity:` lambda or an `on_store_error` hook raised `ExecutionError`, is now refused with `RATE_LIMIT_NOT_CHARGED`. It used to run uncharged. Stop raising from those, or use soft mode while you do. (#46)
|
|
13
|
+
- In strict mode, a `GraphQL::Query` built by your own code, for example in `multiplex`, is now refused unless its context holds `armory_allow_raw_query: true`. It used to run any query, because it skips the plugin's query class. Add the flag where your code builds queries on purpose. Outside strict mode it now records `:raw_query_run`. (#32)
|
|
14
|
+
- A sink that raises no longer takes the request down: the gem's decision stands, the next sink runs, and the new `on_sink_error:` is called, which by default warns with the signal and the error's class. A monitoring failure used to become an API failure. If a record must exist before data is served, raise from `on_sink_error:` to fail closed as before. A sink's `Encoder::InvalidError` is no longer read as a forged ID. (#29)
|
|
15
|
+
- A sink that could never be called, such as `->(a) {}`, now raises `Telemetry::ConfigurationError` at boot. It used to fail on the first request. (#29)
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- Several keys per query, against clients that rotate one identity: `rate_limit_key` or `key:` may answer a list of up to 5 different Strings, for example `["user:1", "ip:1.2.3.4"]`, on `Points`, `Concurrency` and `Time`. A query runs only if every key allows it, and each is charged. It costs about two store round trips per key. Two queries racing between the read and the charge can leave a refused one having charged other keys. A key whose store fails is left out and `on_store_error` is called for it; the others still decide. A single String behaves as before. See [Clients that change identity](docs/rate-limiting.md#clients-that-change-identity). (#40)
|
|
20
|
+
- Observation mode, to learn who a new rule would refuse before enforcing it: `authorize :read, observe: "why"` on a type, or `authorize: :manage, observe: "why"` on a field. On a query, what the policy refuses is served and recorded as an `:observed` signal. It refuses as usual with no `sinks:`, with the null adapter, or in a mutation or subscription, and a type holding a field with a resolver or a mutation raises `ConfigurationError` instead of being observed; lists of object types and lookups still go through the scope, but a list with no scope (an interface, a union, `scope: false`, `nodes(ids:)`) serves its rows and records them. The audit reports each one as `:observed`, and a baseline cannot accept it. See [Observing a rule](docs/authorization.md#observing-a-rule-before-enforcing-it). (#35)
|
|
21
|
+
- Audit baseline, for adopting the gem on a schema with many problems: `rake "graphql_armory:audit_baseline[AppSchema,path]"` records them, then `rake "graphql_armory:audit[AppSchema,path]"` and `be_fully_authorized(baseline: path)` fail only on new problems, on an accepted one that grew (a field added to that type, a type added under that base class), or on an accepted one that is gone. The file only ever shrinks unless edited by hand, every accepted problem stays listed, and `report.ok?` never changes because of it. A baseline protects nothing: those problems are still open. (#34)
|
|
22
|
+
- `PersistedQueries::RedisStore` and `Publisher` publish operations without a server deploy, for frontends that deploy on their own: their CI runs `graphql-armory-publish manifest.json` before the deploy, and the server reads each operation when first asked. An operation it read keeps working when Redis fails; an id it cannot find or check is refused. A published operation never changes. There is no endpoint: whoever can write to those keys decides what strict mode allows, so give the CI its own credential. The app adds the `redis` gem. (#33)
|
|
23
|
+
- `PersistedQueries::Manifests` puts several manifests behind one `store:`, for several frontends and mobile versions still in use: `Manifests.build("config/persisted_queries/*.json")`. It raises at boot when a pattern matches nothing or two manifests disagree on an id. They are one allowlist, and a new manifest still needs a deploy. (#31)
|
|
24
|
+
- `PersistedQueries.operation_id` now reads Relay's `doc_id`, so Relay clients need no change to send their id. (#30)
|
|
25
|
+
- The Pundit and CanCanCan adapters take `user_key:`, for an app whose context holds the user under another key: `Adapters::Pundit.new(user_key: :viewer)`. It used to take a subclass. (#30)
|
|
26
|
+
- In Rails, `rake graphql_armory:audit[YourSchema]` now loads by itself, through a Railtie that loads only when Rails is there. The task now runs the app's `:environment` when it runs, not only when it was defined before the task, so clearing the task's prerequisites no longer skips it. An adapter subclass with its own `initialize` must call `super`: without it, nobody has a user and everyone is denied. (#30)
|
|
27
|
+
- A sink that requires three arguments now receives the query's context: `->(signal, data, context) { ... }`. It tells who was refused without a request-global. It reads through a view that cannot write to the context. Sinks of two arguments, `*arguments` or a defaulted third are unchanged and never receive it. (#29)
|
|
28
|
+
- A persisted queries refusal now carries `extensions.code`: `RAW_QUERY_REFUSED`, `UNKNOWN_OPERATION` or `OPERATION_MISMATCH`, so an app can match on it instead of on the message. (#29)
|
|
29
|
+
- Rate limiters take `sinks:` and record `:rate_limit_store_failed`, with the limiter and the error's class, when the store fails. A failing store used to reach only `on_store_error:`, or stderr. The query still runs. (#29)
|
|
30
|
+
|
|
31
|
+
### Fixed
|
|
32
|
+
|
|
33
|
+
- `Points` and `Time` on one store and one key shared a bucket, as the docs recommend: a query taking 300 ms spent the client's points, and each limiter capped and refilled the balance with its own limit. `Time` now keeps its milliseconds under `time:<key>`, read with `Time.bucket_key(key)`. On upgrading, `Time` balances start full once; `Points` keeps its key, so a balance the mix drained recovers as it refills, within one window. Tools or custom stores that read `Time`'s bucket by the client's key must use the new key. Two limiters of the same class on one store and key still share a bucket: give each its own store or keys. (#43)
|
|
34
|
+
- A payload field declared on a parent mutation with no rule, or on a mutation with `authorize :none` and no `covers_fields`, is denied while the mutation still runs, so the client read null after a write. It stays denied, and the audit now reports it as `:field_without_rule`. (#42)
|
|
35
|
+
- With `Points`, a connection asked for with no `first:` or `last:` on a schema with no page size was a server error any client could cause. It is now refused with `This connection needs first: or last:` (code `PAGE_SIZE_REQUIRED`), and charges nothing. Set `default_max_page_size`. A schema with `max_complexity` still gets graphql-ruby's error. (#41)
|
|
36
|
+
- A schema using `Concurrency` twice kept only the last limiter's slot for a query, so the other leaked until `stale_after`. (#40)
|
|
37
|
+
- When an object's check raised and a `rescue_from` answered true, graphql-ruby served the object. The handler still runs, and the object is denied. (#29)
|
|
38
|
+
- The rate limiters' default warning no longer prints the store error's message, which could hold a password. (#29)
|
|
39
|
+
|
|
40
|
+
### Security
|
|
41
|
+
|
|
42
|
+
- An `ExecutionError` raised during analysis, by another analyzer or by a `complexity:` lambda, made `Points`, `Time` and `Concurrency` skip the query or lose a refusal already given, and a client could choose that by field order. Each limiter now still decides. **Upgrading:** in hard mode, a query whose limiter cannot answer, because a `complexity:` lambda or an `on_store_error` hook raised, is refused with the code `RATE_LIMIT_NOT_CHARGED` instead of running. Soft mode runs it and records `charged: false` in the limiter's context record. Limit: a trace that takes no modules from the schema (`context[:trace]`, a class given to `trace_mode`) is not covered. (#46) Docs: [rate limiting](docs/rate-limiting.md#guarantees-and-limits).
|
|
43
|
+
- A resolver, mutation or subscription with no `Base`, on a type with `covers_fields`, was served to anyone while the audit said ok, because the declaration read as covering it and no rule ran. The audit now reports it as `:unprotected`. To fix it, include `Base` and declare `authorize`, or declare `authorize: :none, reason: "..."` on the field. A schema that had this shape fails the audit after upgrading; a plain read that answers a type with a rule is not reported, because that type refuses. Limit: a plain method field under `covers_fields` stays served and listed by its type. Runtime does not change. (#44)
|
|
44
|
+
- A field returning a mutation's payload, without being that mutation, served the payload's fields with no rule run while the audit said ok. The audit now reports it as `:payload_outside_mutation`, as well as a public mutation inheriting fields from a parent with a rule, so a schema that passed may now fail. The runtime does not change: return a type of its own, or give the parent's field `authorize: :none, reason: "..."`. (#42)
|
|
45
|
+
|
|
46
|
+
### Docs
|
|
47
|
+
|
|
48
|
+
- Limits named for rate limits (`max_depth` lost when an analyzer raises, `validate: false` on `Execution::Next`, `max_page_size: nil`, a query `Time` refuses still charged by `Points`, a multiplex analyzer raising), the audit (`authorize: false` with a resolver with no `Base`, another resolver at a path a baseline accepts) and persisted queries (graphql before 2.6 with json 3.1). (#47)
|
|
49
|
+
|
|
50
|
+
## 0.1.0.pre8 (2026-10-31)
|
|
51
|
+
|
|
52
|
+
This release is about [authorization](docs/authorization.md): the audit now follows every definition of the types it reaches from the schema's roots, `orphan_types` and what they reference, whether or not `visible?` hides them (the limits are in the module page), and Pundit judges a record by both the declared model's policy and its own class's. Read it if you run the audit in CI or rely on Pundit reading `policy_class` from an instance: rules that were silently skipped now show up, so an audit that passed can fail after upgrading, and each new problem names what to do.
|
|
53
|
+
|
|
54
|
+
### Breaking
|
|
55
|
+
|
|
56
|
+
- An audit that passed can fail after upgrading, because it reports more: see the entries under Security. Read each new problem instead of declaring it to get the build green. (#7, #8, #9, #11, #22, #24, #25, #26, #27)
|
|
57
|
+
- The Pundit adapter finds a record's policy from its class. Pundit asks the record for `policy_class` and `model_name`, so an object built from client input, such as an `OpenStruct` from JSON, could name a laxer policy and serve a refused user. An Array is no longer read as a namespace, so `[:admin, record]` asks `ArrayPolicy`. To upgrade, move an instance `policy_class` or `model_name` to the class (`def self.policy_class`), or name the policy with `policy:` on the type's `authorize`. A decorator asks the decorator's class. ActiveRecord relations still ask their model; a list that is neither an Array nor a relation, such as a Mongoid criteria, needs `model:` on the rule. (#18)
|
|
58
|
+
- With Pundit, a record is now judged by both the policy of the model its type declares and the policy of its own class. The audit reads the first and the runtime read only the second, so a resolver returning a record of another class, or a subclass with a laxer policy, served a refused user with the audit ok. Each policy that exists must allow it. To upgrade, make a laxer subclass's type name that subclass as `model:`, or name the policy with `policy:`. A field rule inherited from an interface or a parent cannot read the concrete type's model, so the audit now reports it as `:foreign_model`: redefine the field on the type, or name the same model on the parent. Types named like an unrelated top-level constant now ask its policy: declare `model:`. A subclass of the declared model with no policy of its own now runs under the declared one instead of raising `NotDefinedError`; anything else with no policy still raises it. (#21)
|
|
59
|
+
- A Symbol a type wraps no longer names its own policy or CanCanCan rule: only the Symbol written as `model:` does, so client text turned into a Symbol cannot pick one. To upgrade, write `model: :name` on a rule whose subject is that Symbol. A `model:` that is a Symbol or a String with no constant now works with `authorize:`, where it raised `TypeError`. An answer overriding `==` no longer keeps an item in an Array scope, on Pundit and CanCanCan, and telemetry no longer fails on a record whose `class` is a data member (on the interpreter, graphql-ruby itself still refuses one). (#23)
|
|
60
|
+
- A String a type wraps no longer names its own policy: the Pundit adapter read it as a model name, so a type wrapping the client's text let the client pick the policy that judged it. It now asks `StringPolicy`, or raises `Pundit::NotDefinedError`. A String in `model:` still names a model. An app that relied on the old reading must wrap the value in a class with a policy (#17).
|
|
61
|
+
- A field's or argument's `authorize:` and `visible_when:` now ask the `policy:` of the type that defines them, and the audit checks the action there. The `policy:` was dropped, so Pundit asked the policy found from the record: if that one was laxer, a user the type's policy refuses got the data, with the audit ok. An app that relied on this now gets denials, and an `AdapterError` at runtime when that policy lacks the action: add the method to the policy. The audit reports the missing action as an `:adapter` problem, except on a bypassed type and for a resolver's arguments. Under CanCanCan, which refuses `policy:`, such a rule now raises instead of serving. The `policy:` must be on the class that defines the rule (#14). A rule on an interface, on a parent whose subclass names the `policy:`, on an input object's argument or on a plain resolver's argument still asks another policy at runtime, because a field never sees its resolved type; the audit now reports each as a `:foreign_policy` problem until you move the rule or the `policy:`. There is no declaration for it. Only a type that names a `policy:` is checked (#15).
|
|
62
|
+
|
|
63
|
+
### Changed
|
|
64
|
+
|
|
65
|
+
- `AdapterError` for a policy that does not answer `true`, `false` or `nil` now names the answer's class instead of quoting `inspect`, which could put a record in a message a client sees and run a relation's query. An app matching that text must update it (#17).
|
|
66
|
+
- A record, list or relation with no policy still raises `Pundit::NotDefinedError`, but the message now names its class. Pundit's own message quotes `inspect`, so an app that shows exception text to clients could show the record, and on a relation it ran the query. An app matching that text must update it (#16).
|
|
67
|
+
|
|
68
|
+
### Fixed
|
|
69
|
+
|
|
70
|
+
- One `Audit` object can now serve several threads. It kept each run's state in instance variables, so concurrent runs overwrote each other and one could report no problems for a schema that had them. `Audit.(schema)` and `Audit.build(schema).(schema)` build what a run needs on each call. (#20)
|
|
71
|
+
|
|
72
|
+
### Security
|
|
73
|
+
|
|
74
|
+
- The audit reports two reads that skip the user's scope and were silent: a `loads:` owned by a field class or a resolver with no `Base` as `:unscoped_load`, where a user with an empty scope got the record, and a list or connection of records that skips the scope (`scope: false`, `connection: false` on a connection type) as the new `:unscoped_list`, where a refused user saw how many records it held. An audit that passed can now fail on either: include `Base` where the argument is defined, and remove the option. A hand-written `nodes` or `edges` with `scope: false` and a `resolve_*` option is not seen on `Execution::Next`: see the limits in the module page. (#27)
|
|
75
|
+
- The audit reports a direct subclass of `BaseConnection` or `BaseEdge` that adds a field, which was served with no check while the audit said ok. An audit that passed can now fail on a `total_count`: include `Base` and declare `authorize :none, reason: "...", covers_fields: true` on the class. Fields named like the generated ones (`edges`, `nodes`, `page_info`, `node`, `cursor`) are not covered by this check. (#26)
|
|
76
|
+
- The audit reaches more of what a client can reach: a `PageInfo` subclass with a field of its own, from a parent or from an interface, and a field added to or redefined on a custom `EntryPoints` or `DynamicFields`, with the types behind it. It also checks a Base resolver's arguments, a `RelayClassicMutation`'s included, reporting a missing action and listing `authorize: :none`. (#25)
|
|
77
|
+
- The audit no longer trusts `introspection(true)` on an app's type: graphql-ruby serves such a type like any other, so only a flagged type named with the reserved `__` prefix is exempt. It also reports a Base resolver whose own rule or argument asks another policy than the type's `policy:` as `:foreign_policy`: name the same `policy:` on the resolver. Documented limit: `use_visibility_profile: false` on one query, which the audit cannot see. (#24)
|
|
78
|
+
- A `visible_when:` that cannot name its model is now a `:model` problem wherever it is: on a bypassed type or its fields, on arguments, input objects and directives, and on an interface. It raises at request time; the audit only raised on its first run under `Schema::Visibility`, and said ok after that or with no Visibility at all. The audit also follows directive arguments, so an input object only a directive takes is audited. (#22)
|
|
79
|
+
- The audit's `:foreign_policy` also covers connection and edge subclasses that name a `policy:`. A field rule on their parent asked another policy, so a refused user could get the data with the audit silent (#16).
|
|
80
|
+
- The authorization audit follows every definition instead of `schema.types`, which is read with no request. A type or field hidden by `visible?`, or one of several definitions of a name, was not audited at all, so a user the rule refuses could get its records while the audit said ok. Such a schema can now report new problems: fix or declare them. A bypassed implementer hidden with no request now counts when an interface field runs, so that field is denied. With no `Schema::Visibility`, a hidden field falls back to another definition of its name, serving it with none of the override's rule, arguments, return type or extensions, so the audit reports every redefinition of a field or argument (on an input object, a resolver or one owner) until you remove one, use `Schema::Visibility` with `dynamic: true` and no `profiles:`, or declare `override_reason:` on the field or argument; a static profile still falls back and is reported, and so are two definitions on one owner under any profile. The definition a declared override shadows is audited too, own checks included, since the fallback serves it. A field a bypassed type inherits from a parent with a rule, or from a parent without `Base`, is served with no check, because a field runs under its owner's rule and the owner's type check never ran there; so is a field whose field class does not include `Base`, since graphql-ruby's own `authorized?` answers true: the audit said these were denied, and now says they are served, and asks you to give the field `authorize:` where it is defined or remove it, or to use a field class that includes `Base`. On a Relay schema without `Schema::Visibility` this reports `global_id_field :id` once per type that implements `Node`. A type listed nowhere, returned only by your own `resolve_type`, is not seen (#11).
|
|
81
|
+
- The authorization audit reports `reauthorize_scoped_objects` wherever it is off on an object, interface or union, and an own `self.scope_items`. With reauthorization off, records that came through the scope skip the rule, so a user the rule refuses got them while the audit said ok, and an own `scope_items` could add another account's records. The flag is reported even where it happens to skip nothing, because whether it does depends on the engine and the path. Remove them or declare them in `own_checks:` (#9).
|
|
82
|
+
- The authorization audit follows the whole `super` chain of an own check, and also reports an own `self.authorized_new` or `self.wrap`, and an own `visible?` where a `visible_when:` depends on it. Declare every level you keep in `own_checks:`. (#8)
|
|
83
|
+
- The authorization audit reports a rule on a class that includes `ConnectionBehaviors` or `EdgeBehaviors` as an `:ignored_rule` problem. Those modules answer `authorized?` with true, so the rule never ran. An audit that passed with such a rule now fails until it moves to the field. (#8)
|
|
84
|
+
- The authorization audit reports a schema that defines its own `unauthorized_field` or `unauthorized_object` as a `:hook` problem. Such a method wins over the plugin's, so `on_unauthorized:` did not apply to that denial and nothing said so. An app that keeps one on purpose declares it, with its reason: `own_denial_hooks: { unauthorized_field: "..." }`, which the audit lists as a bypass. A schema with its own hook that passed the audit now fails it. It also reports, as `:own_check` problems, what an app defines that wins over the plugin and can skip a rule or run code on a refused record: `authorized?` on a base field, argument, resolver, mutation, type or input object, `unauthorized_object` wherever graphql-ruby calls it, and `authorizes?`. Declare one with `own_checks: { Klass => { authorized?: "..." } }`. Docs: `docs/authorization.md`. (#7)
|
|
85
|
+
|
|
5
86
|
## 0.1.0.pre7 (2026-10-30)
|
|
6
87
|
|
|
7
88
|
Keyset connections (roadmap module 5). Docs: `docs/keyset-connections.md`.
|
data/README.md
CHANGED
|
@@ -6,16 +6,16 @@
|
|
|
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.pre9`). 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.pre9" # a prerelease: `gem install graphql-armory --pre`
|
|
15
15
|
gem "pundit" # or "cancancan", for the authorization module
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
Requires Ruby >= 3.1 and graphql >= 2.4.
|
|
18
|
+
Requires Ruby >= 3.1 and graphql >= 2.4. Tested on CRuby only: JRuby and TruffleRuby are not covered.
|
|
19
19
|
|
|
20
20
|
## Modules
|
|
21
21
|
|
|
@@ -25,7 +25,7 @@ Status: ✅ released, 🚧 in progress, ⬜ not started. Update this table in th
|
|
|
25
25
|
|---|---|---|---|---|
|
|
26
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. |
|
|
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
|
-
| 3 | Persisted queries / operation allowlist | ✅ | [docs/persisted-queries.md](docs/persisted-queries.md) | Released in 0.1.0.pre4 (prerelease). Allowlist from a build manifest (Apollo, Relay), `use GraphQL::Armory::PersistedQueries` with strict mode. A runtime store (database, sync endpoint) is planned for later. |
|
|
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. |
|
|
30
30
|
| 5 | Stable (keyset) connections | ✅ | [docs/keyset-connections.md](docs/keyset-connections.md) | Released in 0.1.0.pre7 (prerelease). `Keyset.build(relation)` per resolver, ActiveRecord only, plain non-null columns of the relation's own table. Base64 cursors refused unless `plain_cursors: true`. No total count. |
|
|
31
31
|
| 6 | Subscriptions over Pusher / Ably | ⬜ | | Research not started. |
|
|
@@ -55,7 +55,8 @@ GRAPHQL_VERSION="~> 2.4.0" docker compose run --rm test # another graphq
|
|
|
55
55
|
Releases are published from GitHub Actions with RubyGems Trusted Publishing (`.github/workflows/release.yml`): no API key is stored and no OTP is asked. Bump `GraphQL::Armory::VERSION` and the CHANGELOG on `main`, then push the matching tag:
|
|
56
56
|
|
|
57
57
|
```
|
|
58
|
-
git tag v0.1.0.
|
|
58
|
+
git tag -a v0.1.0.pre9 -F <message file> main
|
|
59
|
+
git push origin v0.1.0.pre9
|
|
59
60
|
```
|
|
60
61
|
|
|
61
62
|
The workflow checks that the tag matches the version, runs the specs and pushes the gem. It only publishes a tag on a commit of `main`. The `release` environment can require a maintainer's approval first (on a public repository, or a private one on a paid GitHub plan).
|
data/docs/authorization.md
CHANGED
|
@@ -46,7 +46,16 @@ class AppSchema < GraphQL::Schema
|
|
|
46
46
|
end
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
With graphql-ruby's install template, which includes `ConnectionBehaviors` and `EdgeBehaviors` in classes that inherit from your base object, also declare this in `BaseConnection` and `BaseEdge`, or the audit reports them as `:ignored_rule` ([why](#lists-connections-and-lookups)):
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
class Types::BaseConnection < Types::BaseObject
|
|
53
|
+
authorize :none, reason: "the field that returns it holds the rule; each node has its own", covers_fields: true
|
|
54
|
+
include GraphQL::Types::Relay::ConnectionBehaviors
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The actor is `context[:current_user]`; for another key, pass `adapter: GraphQL::Armory::Authorization::Adapters::Pundit.new(user_key: :viewer)`. A complete, tested version lives in [`examples/invoices.rb`](../examples/invoices.rb).
|
|
50
59
|
|
|
51
60
|
## Rules
|
|
52
61
|
|
|
@@ -55,10 +64,11 @@ The actor is `context[:current_user]`. A complete, tested version lives in [`exa
|
|
|
55
64
|
| `default_action :read` | Default rule for every type below this base class. |
|
|
56
65
|
| `authorize :manage` | This type is checked with `manage?` instead. |
|
|
57
66
|
| `authorize :read, model: Invoice` | The model when it is not named like the type. |
|
|
58
|
-
| `
|
|
67
|
+
| `authorize :read, policy: InvoicePolicy` | Names the policy, instead of finding it from the record. |
|
|
68
|
+
| `field ..., authorize: :manage` | The field asks another question of the same policy: the one the class that defines the field names, or else the one found from the record. |
|
|
59
69
|
| `authorize :none, reason: "..."` | Explicit bypass. A reason is required and appears in the audit. |
|
|
60
70
|
|
|
61
|
-
A bypass does not propagate: a field of a bypassed type still needs its own rule, or it is denied.
|
|
71
|
+
A bypass does not propagate: a field of a bypassed type still needs its own rule, or it is denied. One exception, which the audit names: a field runs under the rule of the class that defines it, so a field a bypassed subclass inherits from a parent with a rule, or from a parent that does not include `Base`, is **served with no check**, because the parent's type check never ran on that record. Give the parent's field its own `authorize:`, or remove it. Redeclaring it on the subclass with `authorize:` protects that field too, but the audit still reports the parent's definition, because a hidden child falls back to it. The same goes for a field whose field class does not include `Base`: graphql-ruby's own `authorized?` answers true, so on a bypassed type it is served.
|
|
62
72
|
|
|
63
73
|
## What happens on denial
|
|
64
74
|
|
|
@@ -74,6 +84,95 @@ use GraphQL::Armory::Authorization, adapter: :pundit, on_unauthorized: :null # d
|
|
|
74
84
|
|
|
75
85
|
Denied non-null fields raise the generic "Not found" error, because GraphQL cannot return `null` there (on Execution::Next a denied object in a non-null position answers graphql-ruby's own "Cannot return null for non-nullable field", and `:raise` messages are prefixed with the field path). `find_by:` lookups always answer `null` in every mode: a missing ID cannot raise, and a foreign ID must look the same.
|
|
76
86
|
|
|
87
|
+
### If your schema defines its own denial hooks
|
|
88
|
+
|
|
89
|
+
The modes above run in the plugin's `unauthorized_field` and `unauthorized_object`. A method your schema class defines on itself wins over the plugin's, wherever `use` is in the class, so that denial answers whatever your method answers and `on_unauthorized:` does not apply to it. If your method names the field or the type it refused, it confirms to the client that they exist.
|
|
90
|
+
|
|
91
|
+
The audit reports each one as a `:hook` problem. Remove the method, or call `super` from it and declare it, with your reason:
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
use GraphQL::Armory::Authorization, adapter: :pundit,
|
|
95
|
+
own_denial_hooks: { unauthorized_field: "logs the denial, then calls super" }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
A declared hook is listed in the audit as a bypass with that reason. The audit cannot read what your method does: the declaration is your word that it reveals nothing. Naming a hook that does not win over the plugin's is also a `:hook` problem. Your own `resolve_type` is not reported: an app with interfaces or unions must define it, and it decides types, not what a denial looks like.
|
|
99
|
+
|
|
100
|
+
A declared hook that returns `nil` for a non-null field behaves differently per engine: the interpreter answers "Cannot return null", and `Execution::Next` leaves the null with no error. Call `super`, or raise.
|
|
101
|
+
|
|
102
|
+
**Your own checks on base classes.** The same applies one level down: `Base` is included in your base classes, so a method you define there wins over the plugin's. The audit reports each as an `:own_check` problem, once, where it is defined:
|
|
103
|
+
|
|
104
|
+
- `authorized?` on a base field, a base argument, a resolver or a mutation, and `self.authorized?` on a type, a base object or an input object. If it does not call `super`, **it skips the rule**: a `BaseMutation#authorized?` that answers `true` runs a mutation the policy refuses.
|
|
105
|
+
- `unauthorized_object` on a resolver, a mutation (with or without `Base`), a base field, or `self.unauthorized_object` on an input object. graphql-ruby calls it on whatever loads a record, before the schema's hook. If it returns the object, the code **runs on the record the policy refused**. If it raises, the client sees your message. A foreign id outside the scope never reaches it.
|
|
106
|
+
- `authorizes?` on a base field, a resolver, a type or a base object (`self.authorizes?`). On `Execution::Next`, answering `false` means `authorized?` does not run, so the rule is skipped.
|
|
107
|
+
- `self.authorized_new` or `self.wrap` on a type. Each decides whether `authorized?` runs, so on the interpreter it can skip the rule.
|
|
108
|
+
- `self.scope_items` on a type, a connection, an interface or a union. It replaces the user's scope: a list can hold records the policy's scope excludes, and a connection counts them. Unless reauthorization is off, each record still goes through the rule.
|
|
109
|
+
- `reauthorize_scoped_objects(false)`, as a call or as your own method, wherever it is off on an object, interface or union in the schema, reported once on the class that sets it. Your own method is reported whatever it answers, at each level of a chain, because the audit does not run it. It is reported even where it skips nothing today, such as on a type with `authorize :none`, because a declaration costs one line and a wrong guess serves refused records. The interpreter asks the field's return type and `Execution::Next` the resolved object, so check both engines before you declare it: on `Execution::Next` a list of an interface or union applies no scope at all and still skips the rule. graphql-ruby then skips `authorized?` for records that came through the scope, so **a user the rule refuses gets them**, and with your own `scope_items` that includes another account's records. Keep it only when the scope is exactly as strict as the rule, and declare it: `own_checks: { Types::Invoice => { reauthorize_scoped_objects: "the scope is as strict as :read" } }`.
|
|
110
|
+
- `visible?` on a type, a field or an argument whose `visible_when:` depends on it. If it does not call `super`, `visible_when:` hides nothing. Elsewhere your own `visible?` is not reported.
|
|
111
|
+
|
|
112
|
+
Each level of a chain is reported: a method that calls `super` does not hide one below it that answers true, so declare every level you keep.
|
|
113
|
+
|
|
114
|
+
Methods that come from graphql-ruby or from this gem, in their files and on classes they name, are not reported, so the connection and edge classes of graphql-ruby's install template stay clean.
|
|
115
|
+
|
|
116
|
+
Remove the method, or make it call `super` and declare it, by the class that defines it and with your reason:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
use GraphQL::Armory::Authorization, adapter: :pundit,
|
|
120
|
+
own_checks: { Types::BaseMutation => { authorized?: "adds the OTP check, then calls super" } }
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
A declared check is listed as a bypass. Each method is declared on its own, and naming one the schema does not use is an `:own_check` problem. The audit cannot read what your method does: the declaration is your word.
|
|
124
|
+
|
|
125
|
+
**What the audit does not see.**
|
|
126
|
+
|
|
127
|
+
- `authorize: false` on a field with a resolver that has no `Base`: the audit says ok, and at runtime the field raises `NoMethodError` and serves nothing. Use `authorize: :none, reason: "..."`.
|
|
128
|
+
- It looks at the classes when it runs, so a method defined later is seen by the next audit.
|
|
129
|
+
- A type no definition leads to. The audit follows the schema's roots, `orphan_types` and what each definition references (fields, arguments, interfaces, union members, a connection's node), not filtering by `visible?`, so types hidden with no request and every definition of a duplicated name are audited. A type that only your own `resolve_type` returns, listed nowhere, is not seen.
|
|
130
|
+
- A resolver with no `Base` in a shared payload type is reported even when every mutation returning it has a rule that refuses first. The audit reads the type and not who returns it. Declare `authorize: :none, reason: "..."` on the field. A resolver with its own refusing `authorized?` is reported too: the audit cannot read that method.
|
|
131
|
+
- A plain field under a type with `covers_fields`, with a method or a `resolver_method:`. The declaration says its fields are public, so the field is served and only the type is listed. A resolver or mutation with no `Base` under that type is reported. A read one that answers a type with a rule, a list or a connection of one, or a union of them, is not, because that type refuses; on a write root it is reported, since it has already run.
|
|
132
|
+
- A missing action on an argument of an input object, when a plain field takes that input object. It raises at runtime instead of serving.
|
|
133
|
+
- A Base resolver on a field class with no `Base`, under a type with a rule: its rule's actions and its arguments are not read. It raises at runtime instead of serving.
|
|
134
|
+
- A missing policy action on a field of a bypassed type. The audit does not ask the policy there, so a misspelled action passes it and raises at runtime instead of serving.
|
|
135
|
+
- A shadowed definition whose argument names a type that cannot load makes the audit raise `ArgumentError`. A late-bound type that never resolves is skipped instead, with no problem reported.
|
|
136
|
+
- `authorize: true` on a field a bypassed type inherits is reported as denied, while the rule of the class that defines it does run: a user it allows gets the data, a user it refuses does not. Trust the rule, not that message.
|
|
137
|
+
- Code written to get past it. The audit reads the classes, it does not run your methods: a declared alias of a library method, a class of yours named under `GraphQL::`, or a forged `__FILE__` are not caught. The audit protects against mistakes, not against the app's own code.
|
|
138
|
+
- `rescue_from(NotAuthorizedError)`: with `on_unauthorized: :raise`, rescuing is what you are meant to do, so keep that handler's message generic. The interpreter catches field denials there and not object denials, which then raise out of the query; `Execution::Next` catches both.
|
|
139
|
+
|
|
140
|
+
## Observing a rule before enforcing it
|
|
141
|
+
|
|
142
|
+
To learn who a new rule would refuse on a schema already in production, name the rule and say why:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
class Types::Invoice < Types::BaseObject
|
|
146
|
+
authorize :read, observe: "rollout of invoices, ticket 123"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
field :notes, String, authorize: :manage, observe: "rollout of notes, ticket 124"
|
|
150
|
+
|
|
151
|
+
use GraphQL::Armory::Authorization, adapter: :pundit,
|
|
152
|
+
sinks: [->(signal, data) { Rails.logger.info("armory #{signal} #{data}") if signal == :observed }]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
When the policy answers `false` or `nil`, **the data is served to a user the rule would refuse**, and an `:observed` signal is recorded in place of `:denied`. Remove `observe:` to enforce the rule. There is no switch for a whole schema: each rule says it, with its reason, and the audit reports each one as `:observed` until it is gone, so `report.ok?` is false and a baseline cannot accept it.
|
|
156
|
+
|
|
157
|
+
| Case | What happens under `observe:` |
|
|
158
|
+
|---|---|
|
|
159
|
+
| A query, and the policy answers `false` or `nil` | Served, `:observed` recorded |
|
|
160
|
+
| No `sinks:` | Refused: observing with nobody listening is only an open door |
|
|
161
|
+
| `Adapters::Null` | Refused |
|
|
162
|
+
| A policy that raises, is missing, or answers anything else | Raises or refuses, as without `observe:` |
|
|
163
|
+
| A type or field with no rule | Denied: there is no rule to observe |
|
|
164
|
+
| A mutation or a subscription, including a read in its response | Refused: only a query runs |
|
|
165
|
+
| A mutation whose response returns a type that cannot be observed | The mutation's own body has already run when that type raises: its write stands |
|
|
166
|
+
| A type under `observe:` with a field that has a `resolver:` or a `mutation:`, its own, inherited or from an interface; a definition shadowed in a subclass does not count | Raises `ConfigurationError` on every query that reaches the type, and its resolvers do not run: observing the type would open the only check before them, and they can write. Remove `observe:`, or put it on the type's plain fields |
|
|
167
|
+
| The same, on the root type | Runs nothing, but the error depends on the engine: on the interpreter with graphql 2.6, graphql-ruby's own rescue reports a `NoMethodError` caused by the `ConfigurationError`. A `rescue_from` handler can turn it into an empty result, so run the audit, which names the type |
|
|
168
|
+
| `authorize: true` with a resolver, where the type's rule is observed | Raises `ConfigurationError`, after graphql-ruby has coerced the arguments: their `prepare:` has run, as it does before any field's rule |
|
|
169
|
+
| A list of an object type, a `loads:` or a `find_by:` | Still goes through the user's scope, and an in-memory lookup still asks the policy: rows are lost, and nothing is recorded about them |
|
|
170
|
+
| A list with no scope: of an interface or a union, `scope: false`, Relay `nodes(ids:)` | **Served row by row and recorded**: the check on each object is all that protects these lists, and it cannot tell it is inside one. A foreign row reaches the user |
|
|
171
|
+
| `visible_when:` | Still hides: the rule it builds is not observed |
|
|
172
|
+
| `observe:` on a mutation, a resolver, a field that holds either, an argument, a bypass, or a field with no action of its own | Raises `ArgumentError` when declared |
|
|
173
|
+
|
|
174
|
+
**Limits.** Only a `resolver:` or a `mutation:` counts as something that can write: a plain field's method that writes, or a `prepare:` on its arguments, runs for a refused user, so keep writes in mutations. A sink that raises loses that signal, reported to `on_sink_error:`, and the data is still served. `Telemetry::Notifications` counts as listening even when nothing subscribes to it. A rule on a type with lists can already lose rows while observed: declare its `scope` only when you trust it, because observing it would mean a second query per list and serving other users' rows. Any clear "no" counts: with CanCanCan that includes the guard that refuses a Symbol a type returns, and an adapter you wrap around `Adapters::Null` is not recognised as null. `load_through_scope: false` opts out of the scope, so a foreign record is served and recorded. While a rule is observed, `audit_baseline` refuses to write: remove `observe:` first.
|
|
175
|
+
|
|
77
176
|
## Lists, connections and lookups
|
|
78
177
|
|
|
79
178
|
Lists of a type are narrowed through `InvoicePolicy::Scope` (relations) or by calling the policy per item (arrays). Return a relation when you can: SQL filters before Ruby sees anything.
|
|
@@ -88,7 +187,11 @@ field :invoice_via_ref, InvoiceType, authorize: :none, reason: "loaded through t
|
|
|
88
187
|
end
|
|
89
188
|
```
|
|
90
189
|
|
|
91
|
-
|
|
190
|
+
**A rule on a connection or edge class does not run.** graphql-ruby's install template includes `ConnectionBehaviors` and `EdgeBehaviors` in a class that already has `Base`, and they bring an `authorized?` that always answers true. A rule there, declared or inherited, is ignored: a refused user still gets `pageInfo` and any field of the connection itself, while `nodes` stay narrowed. `visible_when:` there is ignored the same way. A direct `Relay::BaseEdge` subclass does run its rule, but only where an edge is built, and the `nodes` field never builds one. The audit reports all of these as `:ignored_rule`.
|
|
191
|
+
|
|
192
|
+
Put the rule where something answers for it, and declare `authorize :none, reason: "...", covers_fields: true` on the class. A field rule asks the policy of the parent object, so it works on a connection nested under a record. On a root field there is no parent to ask (Pundit raises, CanCanCan refuses everyone), so use a resolver with `authorize :manage, model: Invoice`.
|
|
193
|
+
|
|
194
|
+
`find_by:` and `loads:` (also inside input objects whose base class includes `Base`) look the record up **inside the user's scope**, so another tenant's ID answers exactly like a missing one. Opt out of the scope for a field's `loads:` only on purpose, with `load_through_scope: false`; the audit lists it. Whoever owns an argument loads it: a field class or a resolver with no `Base` never loads through the scope, whatever the other one includes, and the audit lists its `loads:` too. On `Execution::Next` the opt-out also reaches `loads:` nested in that field's input objects (the interpreter keeps those scoped), and the audit lists them too (`Query.field(filter.invoice_id)`). An input object that overrides `object_from_id` or defines `load_<argument>` itself is honoured on the interpreter and not seen by the audit.
|
|
92
195
|
|
|
93
196
|
**Global IDs.** When your schema defines `object_from_id` (Relay global IDs), `loads:` decodes the id with it, refuses a record of another model, and looks the record up again inside the scope (one extra query per load). Without `object_from_id`, the id is the primary key. `object_from_id` must return the record itself, not a lazy value: a lazy value is denied. A decoder that raises `ActiveRecord::RecordNotFound` (like `GlobalID.find`), `ArgumentError` (a malformed id) or `GraphQL::Armory::Encoder::InvalidError` (see [encrypted IDs](encrypted-ids.md)) is answered like a missing record; other errors propagate. `loads:` of an interface or union (for example `Types::Node`) raises `ConfigurationError`: load a concrete type. Composite primary keys are not supported.
|
|
94
197
|
|
|
@@ -105,12 +208,96 @@ expect(AppSchema).to be_fully_authorized
|
|
|
105
208
|
```
|
|
106
209
|
|
|
107
210
|
```ruby
|
|
108
|
-
#
|
|
211
|
+
# Exits 1 when anything is uncovered. In Rails the task loads by itself when the gem is required after Rails, as Bundler does.
|
|
212
|
+
# Elsewhere, in the Rakefile:
|
|
109
213
|
load "tasks/graphql_armory.rake"
|
|
110
214
|
# bundle exec rake "graphql_armory:audit[AppSchema]"
|
|
111
215
|
```
|
|
112
216
|
|
|
113
|
-
It
|
|
217
|
+
**When it runs.** Only when you call it, through one of the three forms above. It never runs when the schema loads or during a request, and it sends nothing to your API's users. Your rules are what protect each request, and they run with or without the audit. The audit is a static check for the team that owns the schema: it reads the classes and tells you where a rule will not run. Put the spec or the rake task in CI, so a change that opens a hole fails the build. Nothing enforces this for you: an app that never runs the audit gets no warning.
|
|
218
|
+
|
|
219
|
+
**What a pass means.** `report.ok?` means it found none of the problems below, not that every rule is correct: it checks that a rule exists and runs, not that your policy decides well. It also has limits, listed in [What the audit does not see](#if-your-schema-defines-its-own-denial-hooks).
|
|
220
|
+
|
|
221
|
+
**What it reports.** Each problem has a `kind`, a `path` and a `message`. Every declared bypass is listed apart, with its reason.
|
|
222
|
+
|
|
223
|
+
| Kind | What it means | What to do |
|
|
224
|
+
|---|---|---|
|
|
225
|
+
| `:plugin` | The schema never installed the plugin. | Add `use GraphQL::Armory::Authorization, adapter: ...`. |
|
|
226
|
+
| `:unprotected` | A base class, or the field class of an interface's field on a bypassed implementer, does not include `Base`, so nothing checks it. Also a resolver, mutation or subscription with no `Base` under a type with `covers_fields`: the declaration says a rule runs elsewhere, and none does. | Include `Base` and declare `authorize`, or declare `authorize: :none, reason: "..."` on the field, which lists it by its own path. |
|
|
227
|
+
| `:no_rule` | A type, mutation or resolver has no rule, so it is denied to everyone. | Add `authorize` or `default_action`. |
|
|
228
|
+
| `:field_without_rule` | A field of a bypassed type has no rule. The message says what really happens: denied, or served with no check (see [Rules](#rules)). | Give the field `authorize:` where it is defined (on the parent, if inherited), or remove it. A field class without `Base` needs one that includes it. |
|
|
229
|
+
| `:adapter` | The policy a rule needs does not exist, or lacks the method or the `Scope`. | Add what is missing. |
|
|
230
|
+
| `:model` | The model a rule needs cannot be worked out. | Pass `model:`. |
|
|
231
|
+
| `:unscoped_load` | A `loads:` goes through `object_from_id` instead of the user's scope. | Let the load use the scope. |
|
|
232
|
+
| `:unscoped_list` | A list or connection of records skips the user's scope, with `scope: false`, `connection: false` on a connection type, or a list type graphql-ruby does not detect such as `X.to_list_type`. A rule on each record hides its fields, not how many there are. | Remove the option, or write the type as `[Type]`. |
|
|
233
|
+
| `:payload_outside_mutation` | A field returns a mutation's payload without being a mutation with a rule, including a shared `payload_type` with no rule of its own. Its fields are covered by the mutation's rule, so here they are served with no check. | Return a type of its own. A public mutation that inherits fields from a parent with a rule serves them with no check, so give those fields `authorize: :none, reason: "..."` on the parent. |
|
|
234
|
+
| `:hook` | The schema's own `unauthorized_field` or `unauthorized_object` wins over the plugin's. | Remove it, or declare it in `own_denial_hooks:`. |
|
|
235
|
+
| `:own_check` | Your own method can skip the rule: `authorized?`, `wrap`, `visible?` and others, or `reauthorize_scoped_objects` turned off. | Remove it, or declare it in `own_checks:`. |
|
|
236
|
+
| `:ignored_rule` | A rule that does not run, or a field or argument defined more than once. | See [Redefined fields and arguments](#redefined-fields-and-arguments) and [Lists, connections and lookups](#lists-connections-and-lookups). |
|
|
237
|
+
| `:foreign_model` | A field rule is defined on an interface or a parent whose model is not this type's, so the policy of this type's model is never asked for that field. A record of another class or a laxer subclass gets past it. Redefine the field on this type with its `authorize:` and an `override_reason:`, or name the same model on the parent. |
|
|
238
|
+
| `:foreign_policy` | A type or a Base resolver names a `policy:`, but a rule it uses is defined on an interface, a parent, an input object or a resolver, so that rule asks another policy. A user this type's policy refuses can get the data. A field never sees its resolved type, so this cannot be fixed at runtime. | Move the rule to the class that names the policy and remove the original, or name the `policy:` on the class that defines the rule, if it can hold one: an interface or an input object cannot. There is no declaration for it. |
|
|
239
|
+
| `:observed` | A rule under `observe:` serves what it would refuse. A baseline cannot accept it. | Remove `observe:` to enforce the rule. |
|
|
240
|
+
|
|
241
|
+
A declaration (`own_checks:`, `own_denial_hooks:`, `override_reason:`, `authorize :none, reason:`) is your word: the audit stops reporting that problem and lists the reason, so a reviewer can read it. It does not read what your code does. A declaration that names nothing in use is itself a problem.
|
|
242
|
+
|
|
243
|
+
Upgrading can make an audit that used to pass fail, because the audit reports more: read each new problem instead of declaring it to get the build green.
|
|
244
|
+
|
|
245
|
+
### Adopting it on a schema with many problems
|
|
246
|
+
|
|
247
|
+
A first audit of an existing schema can report hundreds of problems. A baseline records them so the build fails only on new ones, while the team works through the list:
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
bundle exec rake "graphql_armory:audit_baseline[AppSchema,config/graphql_armory_baseline.json]" # once; commit the file
|
|
251
|
+
bundle exec rake "graphql_armory:audit[AppSchema,config/graphql_armory_baseline.json]" # in CI
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
```ruby
|
|
255
|
+
expect(AppSchema).to be_fully_authorized(baseline: "config/graphql_armory_baseline.json")
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
**A baseline does not protect anything.** The accepted problems are still open, and a refused user may still get that data. `report.ok?` stays false and the output lists every accepted problem under "still open".
|
|
259
|
+
|
|
260
|
+
| What happens | Result |
|
|
261
|
+
|---|---|
|
|
262
|
+
| A problem not in the file | Fails, named under "new" |
|
|
263
|
+
| An accepted problem whose message changed, for example after an upgrade | Fails as new: read it, then edit the file |
|
|
264
|
+
| An accepted problem that grew: a field or a `loads:` added to that type, or a type added under that base class or interface | Fails as new, naming what it now reaches |
|
|
265
|
+
| An accepted problem that now appears more times | The extra ones fail as new |
|
|
266
|
+
| An accepted problem that is gone | Fails until it leaves the file, so it cannot come back unseen |
|
|
267
|
+
| Another resolver put at a path whose problem is accepted | Passes: an entry names the path and the problem, not the code behind them. Review what changes at an accepted path |
|
|
268
|
+
| A file that is missing or invalid | Fails: it never runs as if there were no baseline |
|
|
269
|
+
| A schema without the plugin | Cannot be accepted: install the plugin first |
|
|
270
|
+
| An `:own_check` or a `:hook` problem | Cannot be accepted: whatever uses that method later would pass unseen. Remove it, or declare it in `own_checks:` or `own_denial_hooks:` with a reason, which the audit lists |
|
|
271
|
+
|
|
272
|
+
Run `audit_baseline` again after fixing problems: on a file that exists it only removes what is gone, and it never adds. To accept a new problem, edit the file by hand, so it shows in the pull request: add its kind, path and message as the output prints them; `count` and `covers` may be left out, and then the entry accepts one problem that reaches nothing, so run `audit_baseline` afterwards to fill them in. The file holds each problem's kind, path, message and count, and the names it reaches (`covers`), and no data of the app. Removing what a problem reaches is fine: running `audit_baseline` narrows the entry.
|
|
273
|
+
|
|
274
|
+
### Redefined fields and arguments
|
|
275
|
+
|
|
276
|
+
When a field is hidden from a user, by `visible?` or anything else, and the request carries no visibility profile, graphql-ruby does not answer "no such field": it looks for another definition of that name and serves it. So this rule does not run for a user the child's field is hidden from:
|
|
277
|
+
|
|
278
|
+
```ruby
|
|
279
|
+
class Types::Base < Types::BaseObject
|
|
280
|
+
field :notes, String
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
class Types::Doc < Types::Base
|
|
284
|
+
field :notes, String, authorize: :manage # hidden: the parent's field answers instead
|
|
285
|
+
end
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
A fallback also loses the field's arguments, return type and extensions. Arguments fall back the same way: one redefined on an input object's subclass, on a resolver's subclass, or twice on one owner.
|
|
289
|
+
|
|
290
|
+
The audit reports every redefinition of a field or argument, over an ancestor, over an interface or on the same owner (`:ignored_rule`), `global_id_field :id` over `Node`'s id included, even with the same rule, because only a request knows whether it gets hidden and what that loses. You have three ways out:
|
|
291
|
+
|
|
292
|
+
- Remove one of the definitions.
|
|
293
|
+
- Use `Schema::Visibility, dynamic: true`, with no `profiles:`. graphql-ruby skips the fallback to an ancestor only for a request that carries a visibility profile; a static profile runs with a plain Hash and still falls back, and the audit knows the difference. Two definitions on one owner fall back to each other under any profile, so they are always reported.
|
|
294
|
+
- Declare it, with `override_reason:`. It is listed as a bypass, and one with nothing to declare is a problem. A declared override still falls back, so the definition it shadows is audited like any other.
|
|
295
|
+
|
|
296
|
+
```ruby
|
|
297
|
+
field :notes, String, authorize: :manage, override_reason: "the parent's field is as strict"
|
|
298
|
+
argument :internal, Boolean, required: false, authorize: :manage, override_reason: "the parent's argument is as strict"
|
|
299
|
+
global_id_field :id, override_reason: "Node's id answers the same" # on a type that implements Node, once per type, on a field class that includes Base
|
|
300
|
+
```
|
|
114
301
|
|
|
115
302
|
## CanCanCan
|
|
116
303
|
|
|
@@ -120,7 +307,7 @@ Same schema, different adapter. Your types and fields do not change:
|
|
|
120
307
|
use GraphQL::Armory::Authorization, adapter: :cancan
|
|
121
308
|
```
|
|
122
309
|
|
|
123
|
-
The actor is `context[:current_ability]` (it wins over `current_user`), or `Ability.new(context[:current_user])` (pass `Adapters::CanCan.new(ability_class: MyAbility)` for another class). It is built once per query.
|
|
310
|
+
The actor is `context[:current_ability]` (it wins over `current_user`), or `Ability.new(context[:current_user])` (`user_key:` names another key; pass `Adapters::CanCan.new(ability_class: MyAbility)` for another class). It is built once per query.
|
|
124
311
|
|
|
125
312
|
| Question | CanCanCan call |
|
|
126
313
|
|---|---|
|
|
@@ -163,7 +350,7 @@ Things to know:
|
|
|
163
350
|
- **CanCanCan conditions are not applied to a class.** `can :export, Invoice, account_id: 1` makes the member visible to anyone who holds the rule. For a field, the attribute is passed: `can?(:export, Invoice, :internal_notes)`.
|
|
164
351
|
- **A subclass that redeclares `authorize` replaces the rule**, `visible_when:` included: repeat it.
|
|
165
352
|
- **`visible_when:` on a field needs a type that includes `Base`** (not an interface or a plain type); otherwise it raises `ConfigurationError` when a query selects it.
|
|
166
|
-
- **Static `Schema::Visibility` profiles are not supported
|
|
353
|
+
- **Static `Schema::Visibility` profiles are not supported**: `visible_when:` raises `ConfigurationError` under one, and a profile-only schema makes the audit's `possible_types` lookups raise `ArgumentError`. Plain `use Schema::Visibility`, `dynamic: true` and the default warden work.
|
|
167
354
|
|
|
168
355
|
Visibility is computed per query (and memoized inside it). Role-based profile caching across requests is graphql-ruby's `Schema::Visibility` feature and is not wrapped here.
|
|
169
356
|
|
|
@@ -219,6 +406,7 @@ end
|
|
|
219
406
|
- **`ready?` runs before authorization** (and, on the interpreter, before a failing `loads:`). Keep it free of side effects.
|
|
220
407
|
- **`loads:` inside an input object** goes through the scope only when your base input object includes `Base` (`class Types::BaseInputObject < GraphQL::Schema::InputObject; include GraphQL::Armory::Authorization::Base; end`); otherwise the audit reports it as `:unscoped_load`.
|
|
221
408
|
- **Do not put `authorize:` on a mutation field or on payload fields**; the mutation's own rule is the one that counts. A subclass that redeclares `authorize :none` is open to everyone, on purpose.
|
|
409
|
+
- **Fields on a parent mutation.** A payload field asks the class that defined it, so a field on a parent with no rule, or on a mutation with `authorize :none` and no `covers_fields`, is denied: the mutation runs and the field answers null. The audit reports it as `:field_without_rule`. Give that field `authorize: :none, reason: "..."`, or use a shared `payload_type`. Moving `authorize` to the parent also works, but every new child then inherits that rule instead of being denied, and the audit does not report it.
|
|
222
410
|
|
|
223
411
|
## Interfaces and unions
|
|
224
412
|
|
|
@@ -227,13 +415,15 @@ They are never authorized themselves: graphql-ruby only runs `authorized?` on th
|
|
|
227
415
|
What an interface adds is its fields, declared once and resolved on many types:
|
|
228
416
|
|
|
229
417
|
- A field with no rule is **covered** by the rule of the Base types that implement the interface (a plain implementer runs no check at all), unless one of them is a bypass (`authorize :none`). Then the field is **denied for every implementer**, because it cannot tell which type it runs on, and the audit reports it (`:field_without_rule`, for example `Document.number`). Declare it (`authorize: :none, reason:` or an action) to fix it.
|
|
230
|
-
- `visible_when:` is not supported on an interface field; put it on the concrete types' fields.
|
|
418
|
+
- `visible_when:` is not supported on an interface field; put it on the concrete types' fields, and use `Schema::Visibility`. Without it, a hidden field falls back to the interface's, so a user it hides this from gets that one and the concrete field's rule does not run. The same goes for a parent class's field, and for anything that hides the field: its resolver, its return type, the schema's `visible?`. See [Redefined fields and arguments](#redefined-fields-and-arguments).
|
|
231
419
|
- `authorize: true` is not valid on an interface field: it is always denied, and the audit reports it. Use an action or `:none`.
|
|
232
420
|
- An interface whose field class does not include `Base`, such as graphql-ruby's built-in Relay `Node`, runs no field check, so its `id` stays readable on a bypassed implementer. The audit reports it (`:unprotected`, `Node.id`); give your own `Node` interface a Base field class if that matters.
|
|
233
421
|
- The audit reports an interface field once, on the interface that declares it, not again on each implementer.
|
|
234
422
|
|
|
235
423
|
## Telemetry
|
|
236
424
|
|
|
425
|
+
**When something raises during an object's check.** If a policy raises and a `rescue_from` handles the error, the handler runs and the object is denied, whatever the handler answers. Without a handler the error reaches the request. This includes a `GraphQL::ExecutionError`: when a `rescue_from` catches it, its message does not reach the client, who gets the ordinary denial.
|
|
426
|
+
|
|
237
427
|
The decisions are visible to you and not to the client. Pass `sinks:` to hear each one:
|
|
238
428
|
|
|
239
429
|
```ruby
|
|
@@ -242,12 +432,32 @@ use GraphQL::Armory::Authorization, adapter: :pundit,
|
|
|
242
432
|
# armory denied {:type=>"Invoice", :action=>:read}
|
|
243
433
|
```
|
|
244
434
|
|
|
245
|
-
Signals are `:allowed`, `:denied`, `:bypassed` (an `authorize :none` rule ran) and `:scoped` (a list went through the user's scope). Data holds only `type`, `action` and `reason`, never record attributes. `GraphQL::Armory::Telemetry::Notifications` reports them to `ActiveSupport::Notifications`. Every signal, what to watch for and how to test it: [Telemetry](telemetry.md). A `sinks:` that is not an Array of callables raises `Telemetry::ConfigurationError` at boot.
|
|
435
|
+
Signals are `:allowed`, `:denied`, `:observed` (a rule under `observe:` would have refused; its data also holds the `reason`), `:bypassed` (an `authorize :none` rule ran) and `:scoped` (a list went through the user's scope). Data holds only `type`, `action` and `reason`, never record attributes. `GraphQL::Armory::Telemetry::Notifications` reports them to `ActiveSupport::Notifications`. Every signal, what to watch for and how to test it: [Telemetry](telemetry.md). A `sinks:` that is not an Array of callables raises `Telemetry::ConfigurationError` at boot.
|
|
246
436
|
|
|
247
437
|
## Guarantees and limits
|
|
248
438
|
|
|
249
439
|
Covered by the black-box suite in `spec/security`, on both engines: user A cannot read user B's objects by root field, nested association or `loads:`; lists never contain foreign objects; uncovered types fail closed and show in the audit; a field hidden with `visible?` is absent from introspection and error suggestions (that is graphql-ruby's behaviour, pinned by a test).
|
|
250
440
|
|
|
441
|
+
With Pundit, a record's policy comes from its class, never from the record: `self.policy_class` and `self.model_name` work, the same methods on an instance are not asked, and an object built from client input cannot name a policy. A decorator asks the decorator's class, so give it its own policy or name one with `policy:`. An ActiveRecord relation asks its model; an Array is checked item by item; any other list asks its own class, so give it `model:` on the rule.
|
|
442
|
+
|
|
443
|
+
`use_visibility_profile: false` on one query (`multiplex_next` on `Execution::Next`) brings back graphql-ruby's fallback to a hidden field's other definitions, and the audit reads only the schema: it cannot see that option in your code. Do not combine it with a field redefined under `Schema::Visibility`.
|
|
444
|
+
|
|
445
|
+
`reauthorize_scoped_objects(false)` skips the rule, which is why the audit reports it. On connections nested inside connections the engines differ: the interpreter skips it and `Execution::Next` may still refuse, so never rely on either.
|
|
446
|
+
|
|
447
|
+
A parent with `model: "Note"` and a child with `model: Note` keep the parent's spelling on the parent's field rules: a subclass with no policy of its own raises `Pundit::NotDefinedError` there, even for an allowed user. Name the model the same way on both.
|
|
448
|
+
|
|
449
|
+
A direct subclass of `BaseConnection` or `BaseEdge` that adds a field, itself, from a parent or from an interface, is audited like any other type, so with no `Base` it is reported as `:unprotected`. A `total_count` is the usual case: include `Base` and declare `authorize :none, reason: "...", covers_fields: true` on the class. That reason covers every field the class has now or gets later, and the report names only the class. To check a field, add `policy:` to the same declaration and give the field `authorize:`: without `policy:` the rule is asked about graphql-ruby's pagination object, which has no policy, so it raises `Pundit::NotDefinedError` for every user, and the audit does not say so. A field redefined under a name graphql-ruby generates there (`edges`, `nodes`, `page_info`, `node`, `cursor`) is not seen by this check: do not put data in one. A generated field renamed through `field_options` is reported as an added field, unless the class declares `covers_fields`, which covers it.
|
|
450
|
+
|
|
451
|
+
Do not put `scope: false` on a `nodes` or `edges` you write yourself on a connection. The audit reports one that reads another method, a resolver, another element type or another key, but not one with `resolve_static:`, `resolve_each:`, `resolve_batch:`, `resolve_legacy_instance_method:` or `dataload:`: on `Execution::Next` those run instead of the generated method, so a refused user sees how many records it holds while the audit says ok. `hash_key: :nodes` is not reported on the field either. A generated `edges` with `edge_type(edge, node_type: other)` or renamed through `field_options` gets a false `:unscoped_list`.
|
|
452
|
+
|
|
453
|
+
A field added to or redefined on a custom `EntryPoints` or `DynamicFields` is reported as `:unprotected`, whatever it declares: no type's rule runs on it, so move it to a type with a rule.
|
|
454
|
+
|
|
455
|
+
The audit exempts only types flagged `introspection` whose name starts with `__`, the prefix the GraphQL spec reserves. A subclass of a built-in such as `GraphQL::Introspection::TypeType` keeps that name, so a data field added to one, or to a `__Helper`, is not audited: do not put data there.
|
|
456
|
+
|
|
457
|
+
A Symbol names a policy only as the `model:` the schema author wrote: a Symbol a type wraps is a record, like a String, and has no policy of its own unless a `SymbolPolicy` exists. A client sending the model's own name reaches that policy with the Symbol as its record, so a headless policy must not read a record's fields. A Module as a subject still names its class's policy, so never turn client text into a constant with `Object.const_get`. An app's own `visible?` raising `ConfigurationError` while the audit lists the schema is reported on the first run only, because graphql-ruby does not list the schema twice.
|
|
458
|
+
|
|
459
|
+
The model a rule declares, or the constant named like the type, is the one the audit reads, so its policy answers too: with Pundit, both that policy and the policy of the record's class must allow it. A record of another class, or a subclass with a laxer policy, cannot get past the declared one, and a subclass keeps its own stricter policy. A subclass with no policy runs under the declared one; anything else with no policy of its own raises `Pundit::NotDefinedError`. A rule's `policy:` answers alone. A field never sees its resolved type, so a rule defined on an interface or a parent asks that owner's model, or only the record's class when the owner has none: the audit reports it as `:foreign_model`, and naming a subclass as `model:` on a child type does not change it. The name of a model is inferred from the last segment of the type's name, so `Types::Billing::Shared` reads a top-level `Shared` and its policy too: declare `model:` when that is not the record. Lists go through the scope of the declared model, or the record's when none is declared. CanCanCan's `can?` reads the record itself, so the declared model does not answer there; a Symbol record is refused unless it is the `model:` written on the rule.
|
|
460
|
+
|
|
251
461
|
Policies must return `true`/`false`/`nil`; anything else raises `AdapterError`. Do not use `dataloader.request` inside policies; use `dataloader.with(...).load`.
|
|
252
462
|
|
|
253
463
|
## Engines
|
data/docs/encrypted-ids.md
CHANGED
|
@@ -137,7 +137,7 @@ The signal never holds the ID, the key or the record. A rising `:invalid` is som
|
|
|
137
137
|
| `decode(id)` / `decode(cursor, nonce: true)` | The text, or raises `GraphQL::Armory::Encoder::InvalidError` (a `GraphQL::ExecutionError`, so clients see `Invalid ID or cursor`). |
|
|
138
138
|
| `Encoder.versioned(newest, ..., oldest)` | A chain with the same `encode` and `decode`; encodes with `newest`. |
|
|
139
139
|
| `chain.decode_versioned(text, nonce: false)` | `[data, encoder]`, or `[nil, nil]` when no encoder accepts it. |
|
|
140
|
-
| `use GraphQL::Armory::IDs, encoder: nil, find: nil, sinks: []` | Defines `id_from_object` and `object_from_id`; `encoder` defaults to `Encoder.default`; `find:` loads types without `Base`; `sinks:` hears `:id_resolved` and `:id_refused` ([Telemetry](telemetry.md)). |
|
|
140
|
+
| `use GraphQL::Armory::IDs, encoder: nil, find: nil, sinks: [], on_sink_error: nil` | Defines `id_from_object` and `object_from_id`; `encoder` defaults to `Encoder.default`; `find:` loads types without `Base`; `sinks:` hears `:id_resolved` and `:id_refused` ([Telemetry](telemetry.md)). |
|
|
141
141
|
| `Encoder.default` | An encoder keyed by `GRAPHQL_ARMORY_KEY`, else by a key derived with HKDF from Rails' `secret_key_base`; raises `ConfigurationError` without either. |
|
|
142
142
|
|
|
143
143
|
A subclass inherits `key`, `tag` and `codec`, and can override any of them. Passing `nil` to any of them raises `ConfigurationError`, so an unset environment variable fails at boot instead of being ignored.
|