graphql-armory 0.1.0.pre7 → 0.1.0.pre8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -1
  3. data/README.md +5 -4
  4. data/docs/authorization.md +133 -6
  5. data/lib/graphql/armory/authorization/adapters/cancan.rb +10 -1
  6. data/lib/graphql/armory/authorization/adapters/pundit.rb +71 -13
  7. data/lib/graphql/armory/authorization/adapters/substitute.rb +4 -1
  8. data/lib/graphql/armory/authorization/audit/denial_hooks.rb +29 -0
  9. data/lib/graphql/armory/authorization/audit/edge_rules.rb +36 -0
  10. data/lib/graphql/armory/authorization/audit/findings.rb +34 -0
  11. data/lib/graphql/armory/authorization/audit/foreign_policies.rb +90 -0
  12. data/lib/graphql/armory/authorization/audit/input_loads.rb +26 -0
  13. data/lib/graphql/armory/authorization/audit/interface_rules.rb +39 -0
  14. data/lib/graphql/armory/authorization/audit/introspection_fields.rb +40 -0
  15. data/lib/graphql/armory/authorization/audit/library_methods.rb +24 -0
  16. data/lib/graphql/armory/authorization/audit/overrides.rb +81 -0
  17. data/lib/graphql/armory/authorization/audit/own_checks.rb +144 -0
  18. data/lib/graphql/armory/authorization/audit/type_rules.rb +265 -0
  19. data/lib/graphql/armory/authorization/audit/visibility_models.rb +83 -0
  20. data/lib/graphql/armory/authorization/audit.rb +50 -191
  21. data/lib/graphql/armory/authorization/authorize.rb +9 -3
  22. data/lib/graphql/armory/authorization/configuration.rb +30 -2
  23. data/lib/graphql/armory/authorization/definitions.rb +114 -0
  24. data/lib/graphql/armory/authorization/integration/argument.rb +24 -6
  25. data/lib/graphql/armory/authorization/integration/field.rb +13 -5
  26. data/lib/graphql/armory/authorization/integration/object.rb +1 -1
  27. data/lib/graphql/armory/authorization/integration/schema.rb +7 -0
  28. data/lib/graphql/armory/authorization/integration.rb +22 -1
  29. data/lib/graphql/armory/authorization/rule.rb +5 -0
  30. data/lib/graphql/armory/authorization.rb +4 -1
  31. data/lib/graphql/armory/version.rb +1 -1
  32. metadata +14 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29e27413e39d1f24ffcc5d1ebf7316170b0ebb935f7d07eb0fd17779358cdbd1
4
- data.tar.gz: 4db014feb0364b2f67773668f99b961a708a30a142141d96979db7da3fed8321
3
+ metadata.gz: f4d5727b97311ff099b347c1e06cfbf1ec425a03c6018c1385ec557d8a31fef5
4
+ data.tar.gz: 78015ec70e63fded090bfbebca21328792a0285f555f11f66e25e4c5b094f675
5
5
  SHA512:
6
- metadata.gz: 3c09fc9ec92e31c68fa01e82fafff46affc1cb2dba224bf0c513d4fb0786904a5bca96ace241a0099cff7b724a80e82738a0cb1361724f3f8c8edb6e2f16165d
7
- data.tar.gz: 691347bd3ad318eb25bbab16634bf05f03e3587bfc90b8c18e93f75913582aa03ec52435c70c315611f8dc6ea3375a829479b2d8bd84f73d8ac326b7466bffa0
6
+ metadata.gz: fcfd1948d727ccfd827c9acefdc76e78ee14e64e01d72fea83d3c367674af1cb3e302c2728b170a8561677d1c6b79ecd20723c639dde2ef9d46fa7b96c577927
7
+ data.tar.gz: a519a7cf57c9650c7d08965e51a3829fe9860ef31e1ad2bf78f29ca91810a704f7fdb662cfdf390f1efedea1932bd1bafbe1faced26c7deb6323e5eca009640f
data/CHANGELOG.md CHANGED
@@ -1,6 +1,40 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## 0.1.0.pre8 (2026-10-31)
4
+
5
+ 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.
6
+
7
+ ### Breaking
8
+
9
+ - 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)
10
+ - 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)
11
+ - 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)
12
+ - 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)
13
+ - 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).
14
+ - 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).
15
+
16
+ ### Changed
17
+
18
+ - `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).
19
+ - 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).
20
+
21
+ ### Fixed
22
+
23
+ - 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)
24
+
25
+ ### Security
26
+
27
+ - 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)
28
+ - 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)
29
+ - 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)
30
+ - 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)
31
+ - 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)
32
+ - 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).
33
+ - 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).
34
+ - 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).
35
+ - 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)
36
+ - 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)
37
+ - 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)
4
38
 
5
39
  ## 0.1.0.pre7 (2026-10-30)
6
40
 
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.pre7`). The API may change before 1.0.
9
+ > **Status: early development.** Prereleases on RubyGems (`0.1.0.pre8`). The API may change before 1.0.
10
10
 
11
11
  ## Install
12
12
 
13
13
  ```ruby
14
- gem "graphql-armory", "~> 0.1.0.pre7" # a prerelease: `gem install graphql-armory --pre`
14
+ gem "graphql-armory", "~> 0.1.0.pre8" # 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
 
@@ -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.pre7 && git push origin v0.1.0.pre7
58
+ git tag -a v0.1.0.pre8 -F <message file> main
59
+ git push origin v0.1.0.pre8
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).
@@ -55,10 +55,11 @@ The actor is `context[:current_user]`. A complete, tested version lives in [`exa
55
55
  | `default_action :read` | Default rule for every type below this base class. |
56
56
  | `authorize :manage` | This type is checked with `manage?` instead. |
57
57
  | `authorize :read, model: Invoice` | The model when it is not named like the type. |
58
- | `field ..., authorize: :manage` | The field asks another question of the same policy. |
58
+ | `authorize :read, policy: InvoicePolicy` | Names the policy, instead of finding it from the record. |
59
+ | `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
60
  | `authorize :none, reason: "..."` | Explicit bypass. A reason is required and appears in the audit. |
60
61
 
61
- A bypass does not propagate: a field of a bypassed type still needs its own rule, or it is denied.
62
+ 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
63
 
63
64
  ## What happens on denial
64
65
 
@@ -74,6 +75,56 @@ use GraphQL::Armory::Authorization, adapter: :pundit, on_unauthorized: :null # d
74
75
 
75
76
  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
77
 
78
+ ### If your schema defines its own denial hooks
79
+
80
+ 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.
81
+
82
+ The audit reports each one as a `:hook` problem. Remove the method, or call `super` from it and declare it, with your reason:
83
+
84
+ ```ruby
85
+ use GraphQL::Armory::Authorization, adapter: :pundit,
86
+ own_denial_hooks: { unauthorized_field: "logs the denial, then calls super" }
87
+ ```
88
+
89
+ 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.
90
+
91
+ 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.
92
+
93
+ **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:
94
+
95
+ - `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.
96
+ - `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.
97
+ - `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.
98
+ - `self.authorized_new` or `self.wrap` on a type. Each decides whether `authorized?` runs, so on the interpreter it can skip the rule.
99
+ - `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.
100
+ - `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" } }`.
101
+ - `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.
102
+
103
+ 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.
104
+
105
+ 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.
106
+
107
+ Remove the method, or make it call `super` and declare it, by the class that defines it and with your reason:
108
+
109
+ ```ruby
110
+ use GraphQL::Armory::Authorization, adapter: :pundit,
111
+ own_checks: { Types::BaseMutation => { authorized?: "adds the OTP check, then calls super" } }
112
+ ```
113
+
114
+ 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.
115
+
116
+ **What the audit does not see.**
117
+
118
+ - It looks at the classes when it runs, so a method defined later is seen by the next audit.
119
+ - 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.
120
+ - 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.
121
+ - 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.
122
+ - 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.
123
+ - 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.
124
+ - `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.
125
+ - 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.
126
+ - `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.
127
+
77
128
  ## Lists, connections and lookups
78
129
 
79
130
  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 +139,11 @@ field :invoice_via_ref, InvoiceType, authorize: :none, reason: "loaded through t
88
139
  end
89
140
  ```
90
141
 
91
- `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. 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.
142
+ **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`.
143
+
144
+ 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`.
145
+
146
+ `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
147
 
93
148
  **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
149
 
@@ -110,7 +165,59 @@ load "tasks/graphql_armory.rake"
110
165
  # bundle exec rake "graphql_armory:audit[AppSchema]"
111
166
  ```
112
167
 
113
- It reports types nobody protects (a base class that never included `Base`), types and fields with no rule, policies missing the method or `Scope`, `loads:` that skip the scope, and lists every declared bypass with its reason.
168
+ **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.
169
+
170
+ **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).
171
+
172
+ **What it reports.** Each problem has a `kind`, a `path` and a `message`. Every declared bypass is listed apart, with its reason.
173
+
174
+ | Kind | What it means | What to do |
175
+ |---|---|---|
176
+ | `:plugin` | The schema never installed the plugin. | Add `use GraphQL::Armory::Authorization, adapter: ...`. |
177
+ | `: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. | Include `Base`. |
178
+ | `:no_rule` | A type, mutation or resolver has no rule, so it is denied to everyone. | Add `authorize` or `default_action`. |
179
+ | `: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. |
180
+ | `:adapter` | The policy a rule needs does not exist, or lacks the method or the `Scope`. | Add what is missing. |
181
+ | `:model` | The model a rule needs cannot be worked out. | Pass `model:`. |
182
+ | `:unscoped_load` | A `loads:` goes through `object_from_id` instead of the user's scope. | Let the load use the scope. |
183
+ | `: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]`. |
184
+ | `:hook` | The schema's own `unauthorized_field` or `unauthorized_object` wins over the plugin's. | Remove it, or declare it in `own_denial_hooks:`. |
185
+ | `: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:`. |
186
+ | `: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). |
187
+ | `: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. |
188
+ | `: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. |
189
+
190
+ 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.
191
+
192
+ 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.
193
+
194
+ ### Redefined fields and arguments
195
+
196
+ 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:
197
+
198
+ ```ruby
199
+ class Types::Base < Types::BaseObject
200
+ field :notes, String
201
+ end
202
+
203
+ class Types::Doc < Types::Base
204
+ field :notes, String, authorize: :manage # hidden: the parent's field answers instead
205
+ end
206
+ ```
207
+
208
+ 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.
209
+
210
+ 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:
211
+
212
+ - Remove one of the definitions.
213
+ - 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.
214
+ - 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.
215
+
216
+ ```ruby
217
+ field :notes, String, authorize: :manage, override_reason: "the parent's field is as strict"
218
+ argument :internal, Boolean, required: false, authorize: :manage, override_reason: "the parent's argument is as strict"
219
+ 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
220
+ ```
114
221
 
115
222
  ## CanCanCan
116
223
 
@@ -163,7 +270,7 @@ Things to know:
163
270
  - **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
271
  - **A subclass that redeclares `authorize` replaces the rule**, `visible_when:` included: repeat it.
165
272
  - **`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** and raise `ConfigurationError`; plain `use Schema::Visibility`, `dynamic: true` and the default warden work.
273
+ - **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
274
 
168
275
  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
276
 
@@ -227,7 +334,7 @@ They are never authorized themselves: graphql-ruby only runs `authorized?` on th
227
334
  What an interface adds is its fields, declared once and resolved on many types:
228
335
 
229
336
  - 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.
337
+ - `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
338
  - `authorize: true` is not valid on an interface field: it is always denied, and the audit reports it. Use an action or `:none`.
232
339
  - 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
340
  - The audit reports an interface field once, on the interface that declares it, not again on each implementer.
@@ -248,6 +355,26 @@ Signals are `:allowed`, `:denied`, `:bypassed` (an `authorize :none` rule ran) a
248
355
 
249
356
  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
357
 
358
+ 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.
359
+
360
+ `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`.
361
+
362
+ `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.
363
+
364
+ 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.
365
+
366
+ 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.
367
+
368
+ 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`.
369
+
370
+ 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.
371
+
372
+ 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.
373
+
374
+ 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.
375
+
376
+ 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.
377
+
251
378
  Policies must return `true`/`false`/`nil`; anything else raises `AdapterError`. Do not use `dataloader.request` inside policies; use `dataloader.with(...).load`.
252
379
 
253
380
  ## Engines
@@ -26,6 +26,8 @@ module GraphQL
26
26
 
27
27
  def authorized?(ability, subject, rule)
28
28
  options!(rule)
29
+ return false if client_symbol?(subject, rule)
30
+
29
31
  return ability.can?(rule.action, subject, rule.attribute) if rule.attribute
30
32
 
31
33
  ability.can?(rule.action, subject)
@@ -34,6 +36,13 @@ module GraphQL
34
36
  # A relation is narrowed in SQL with `accessible_by`, so it stays paginable. A rule defined only
35
37
  # with a block cannot be turned into SQL: it fails, unless the type declared `scope: :in_memory`,
36
38
  # which filters with `can?` and gives up efficient pagination.
39
+ # A Symbol names a rule (`can :read, :dashboard`) only as the `model:` the schema author wrote: any other is a
40
+ # record, maybe the client's text.
41
+ def client_symbol?(subject, rule)
42
+ Symbol === subject && subject.to_s != rule.model.to_s
43
+ end
44
+ private :client_symbol?
45
+
37
46
  def scope(ability, items, rule)
38
47
  options!(rule)
39
48
  return in_memory(ability, items, rule) if items.is_a?(Array) || rule.scope == :in_memory
@@ -72,7 +81,7 @@ module GraphQL
72
81
  end
73
82
 
74
83
  def in_memory(ability, items, rule)
75
- items.select { |item| authorized?(ability, item, rule) == true }
84
+ items.select { |item| true.equal?(authorized?(ability, item, rule)) }
76
85
  end
77
86
 
78
87
  def ability_for(context)
@@ -8,7 +8,7 @@ module GraphQL
8
8
  module Adapters
9
9
  # Answers the core's four questions with Pundit's public API.
10
10
  #
11
- # The policy is found from the model through Pundit::PolicyFinder, so `policy_class`
11
+ # The policy is found from the model's class through Pundit::PolicyFinder, so `self.policy_class`
12
12
  # overrides and namespaced models work. A policy must answer `true`, `false` or `nil`:
13
13
  # anything else (a lazy, a record) is handed back as is and the core refuses it.
14
14
  class Pundit
@@ -17,19 +17,21 @@ module GraphQL
17
17
  end
18
18
 
19
19
  def authorized?(user, subject, rule)
20
- policy = policy_class(rule, subject).new(user, subject)
21
20
  query = query_for(rule)
22
- unless policy.respond_to?(query)
23
- raise AdapterError, "#{policy.class} does not define #{query}"
24
- end
21
+ policies_for(rule, subject).each do |policy_class|
22
+ policy = policy_class.new(user, subject)
23
+ raise AdapterError, "#{policy.class} does not define #{query}" unless policy.respond_to?(query)
25
24
 
26
- policy.public_send(query)
25
+ answer = policy.public_send(query)
26
+ return answer unless true.equal?(answer)
27
+ end
28
+ true
27
29
  end
28
30
 
29
31
  def scope(user, items, rule)
30
- return items.select { |item| authorized?(user, item, rule) == true } if items.is_a?(Array)
32
+ return items.select { |item| true.equal?(authorized?(user, item, rule)) } if items.is_a?(Array)
31
33
 
32
- policy = policy_class(rule, rule.model || items)
34
+ policy = policy_class(rule, normalize(rule.model) || record_target(items))
33
35
  scope_class = scope_constant(policy)
34
36
  raise MissingScopeError, "#{policy} has no Scope class" if scope_class.nil?
35
37
 
@@ -50,10 +52,65 @@ module GraphQL
50
52
  problems
51
53
  end
52
54
 
55
+ # The policy a rule that names none asks about this model.
56
+ def found_policy(model)
57
+ finder(normalize(model)).policy
58
+ end
59
+
53
60
  private
54
61
 
62
+ # A rule's `policy:` answers alone. Otherwise the declared model's policy, which is the one the audit reads, and
63
+ # the record's class's must both allow it: one alone lets a record of another class, or a subclass with a laxer
64
+ # policy, past the first, or a subclass past its own stricter policy.
65
+ def policies_for(rule, subject)
66
+ return [rule.policy] if rule.policy
67
+
68
+ model = normalize(rule.model) if rule.model
69
+ declared = finder(model).policy if model
70
+ target = model_named?(subject, model) ? subject : record_target(subject)
71
+ own = finder(target).policy
72
+ return [declared, own].compact.uniq if own
73
+ return [declared] if declared && kind_of_model?(subject, model)
74
+
75
+ raise ::Pundit::NotDefinedError, "no policy found for #{subject_name(target)}"
76
+ end
77
+
78
+ # A Symbol names a policy only when it is the `model:` the schema author wrote: any other is a record, maybe the
79
+ # client's text, as a String is.
80
+ def model_named?(subject, model)
81
+ Symbol === subject && subject.equal?(model)
82
+ end
83
+
84
+ def record_target(subject)
85
+ Symbol === subject ? ::Symbol : policy_subject(subject)
86
+ end
87
+
88
+ # Only a record of the model inherits its policy: anything else with no policy of its own raises, as it always did.
89
+ def kind_of_model?(subject, model)
90
+ return false unless model.is_a?(Module)
91
+
92
+ target = policy_subject(subject)
93
+ target.is_a?(Module) ? target <= model : model === target
94
+ end
95
+
55
96
  def policy_class(rule, subject)
56
- rule.policy || finder(subject).policy!
97
+ rule.policy || finder(policy_subject(subject)).policy || raise(::Pundit::NotDefinedError, "no policy found for #{subject_name(subject)}")
98
+ end
99
+
100
+ # Pundit asks the object for `policy_class` and `model_name`, and reads an Array as a namespace. A record's
101
+ # answers can be the client's data (an OpenStruct from JSON), so only its class is asked. A relation's class
102
+ # names no model, so its `klass` is read, but only on a real relation: `klass` on anything else is an answer too.
103
+ def policy_subject(subject)
104
+ return subject if Module === subject || Symbol === subject
105
+ return subject.klass if defined?(::ActiveRecord::Relation) && ::ActiveRecord::Relation === subject
106
+
107
+ ::Kernel.instance_method(:class).bind_call(subject)
108
+ end
109
+
110
+ # Never `inspect`: Pundit's own message quotes the record, and on a relation it runs the query.
111
+ # Nor the record's own `class` or `is_a?`: on a Struct or OpenStruct, `class` can be a data member.
112
+ def subject_name(subject)
113
+ Module === subject || Symbol === subject ? subject : ::Kernel.instance_method(:class).bind_call(subject)
57
114
  end
58
115
 
59
116
  def query_for(rule)
@@ -61,12 +118,13 @@ module GraphQL
61
118
  end
62
119
 
63
120
  def finder(subject)
64
- ::Pundit::PolicyFinder.new(normalize(subject))
121
+ ::Pundit::PolicyFinder.new(subject)
65
122
  end
66
123
 
67
- # Pundit reads a Symbol as a model name but a String as a plain object.
68
- def normalize(subject)
69
- subject.is_a?(String) ? subject.to_sym : subject
124
+ # Pundit reads a Symbol as a model name but a String as a plain object. Only for a model the schema author
125
+ # wrote: a String a type wraps is a record, maybe the client's text, and must not name its own policy.
126
+ def normalize(model)
127
+ model.is_a?(String) ? model.to_sym : model
70
128
  end
71
129
 
72
130
  # Looks the Scope up through the policy's ancestors but never in Object, so a
@@ -8,12 +8,13 @@ module GraphQL
8
8
  class Substitute
9
9
  attr_reader :asked, :scoped_for
10
10
 
11
- def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil)
11
+ def initialize(allow: true, actor: nil, scoped: :itself.to_proc, raises: nil, raises_on_scope: nil, raises_on_check: nil)
12
12
  @allow = allow
13
13
  @actor = actor
14
14
  @scoped = scoped
15
15
  @raises = raises
16
16
  @raises_on_scope = raises_on_scope
17
+ @raises_on_check = raises_on_check
17
18
  @asked = []
18
19
  @scoped_for = []
19
20
  end
@@ -37,6 +38,8 @@ module GraphQL
37
38
  end
38
39
 
39
40
  def check(_type, _rule)
41
+ raise @raises_on_check if @raises_on_check
42
+
40
43
  []
41
44
  end
42
45
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A method the schema defines on itself wins over the plugin's, so `on_unauthorized:` no longer decides
8
+ # what that denial looks like. The check runs here because `use` usually comes before those methods.
9
+ module DenialHooks
10
+ def self.call(schema, findings)
11
+ declared = schema.armory_configuration.own_denial_hooks
12
+ Configuration::DENIAL_HOOKS.each do |hook|
13
+ path = "Schema.#{hook}"
14
+ own = schema.method(hook).owner != Integration::Schema
15
+ if own && declared.key?(hook)
16
+ findings.bypass(path, declared[hook])
17
+ elsif own
18
+ findings.problem(:hook, path, "the schema's own method runs instead of the plugin's, so on_unauthorized: does not apply and a denial " \
19
+ "may reveal what it hides: remove it, or declare it in own_denial_hooks: with a reason")
20
+ elsif declared.key?(hook)
21
+ findings.problem(:hook, path, "named in own_denial_hooks: but the plugin's hook is the one that runs: remove it from the option")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # A direct edge subclass runs the plugin's check, but only where an edge is built: `nodes` never builds one.
8
+ module EdgeRules
9
+ def self.call(definitions, findings)
10
+ definitions.each do |type|
11
+ next unless type.kind.object? && type <= GraphQL::Types::Relay::BaseEdge
12
+
13
+ rule = type.armory_rule if type.respond_to?(:armory_rule)
14
+ next if rule.nil? || rule.none? || !nodes_field_beside?(type, definitions)
15
+
16
+ findings.problem(:ignored_rule, type.graphql_name, "the connection's nodes field returns the nodes without building an edge, so this rule does not " \
17
+ "cover it: put the rule on the field that returns the connection (on a root field, on a resolver: " \
18
+ "a field rule asks the parent's policy), or remove the nodes field with has_nodes_field(false)")
19
+ end
20
+ end
21
+
22
+ def self.nodes_field_beside?(edge, definitions)
23
+ definitions.any? do |type|
24
+ next false unless type.kind.object? && !Definitions.introspection?(type)
25
+
26
+ fields = type.all_field_definitions
27
+ fields.any? { |field| field.graphql_name == "nodes" } &&
28
+ fields.any? { |field| field.graphql_name == "edges" && field.type.unwrap == edge }
29
+ end
30
+ end
31
+ private_class_method :nodes_field_beside?
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Armory
5
+ module Authorization
6
+ class Audit
7
+ # Collects what one audit finds.
8
+ class Findings
9
+ def initialize
10
+ @problems = []
11
+ @bypasses = []
12
+ end
13
+
14
+ def problem(kind, path, message)
15
+ @problems << Report::Problem.new(kind, path, message)
16
+ end
17
+
18
+ def problem?(kind, path = nil)
19
+ @problems.any? { |problem| problem.kind == kind && (path.nil? || problem.path == path) }
20
+ end
21
+
22
+ def bypass(path, reason)
23
+ @bypasses << Report::Bypass.new(path, reason)
24
+ end
25
+
26
+ def report
27
+ Report.new(problems: @problems.sort_by { |problem| [problem.path, problem.kind.to_s] },
28
+ bypasses: @bypasses.sort_by(&:path))
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end