model_driven_api 3.18.0 → 3.20.0
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/README.md +3 -1
- data/app/controllers/api/v3/application_controller.rb +6 -5
- data/lib/api/hidden_secrets.rb +44 -0
- data/lib/api/open_api/v3.rb +2 -2
- data/lib/api/resource_attribute_set.rb +3 -3
- data/lib/api/v2/request_json_attrs.rb +15 -29
- data/lib/concerns/model_driven_api_application_record.rb +3 -1
- data/lib/model_driven_api/version.rb +1 -1
- data/lib/model_driven_api.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7ab2b6a7c7c7384c9decd7ab6491ba6daa6f31a0d3fbfc70c5aa65f61ee609d9
|
|
4
|
+
data.tar.gz: 2516e2a385ac48e62dff7e5a4c5d7f9863bbd8fb474ebcc3af98fae1b4d99d6c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bdfab13321344419715fd92b297353803c527b7f99280c461280cf8f97cb25f6e263feda93c3ca40805874fb884d5805f40a55909971c7434424fcbf663c22e
|
|
7
|
+
data.tar.gz: a5f9525d01d41c1cec3353bbc651f014a3c4612108a8b27a15da8dc61a43b58a76c177c3a48713e4119f2b0031792d661341eed2e1204fb64b5db5a26c987529
|
data/README.md
CHANGED
|
@@ -195,7 +195,7 @@ Parameters work identically in query string (GET) and JSON body (POST search).
|
|
|
195
195
|
}
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
The client's options follow conventions (ADR 0006): `include` takes real associations only; secrets the model hides
|
|
198
|
+
The client's options follow conventions (ADR 0006): `include` takes real associations only; secrets the model hides (a secret-named column left out of its default JSON, its `json_attrs[:except]` or `write_only_attributes`, e.g. Devise's `encrypted_password`) never come back, in v2, v3 or Ransack filters and sorts; `methods` serves what the model declares in `json_attrs[:methods]`, columns, associations, `<association>_ids`, and public methods written in the app or a thecore gem (not a model callback, not ending in `!`/`=`, no required arguments), which run read-only. Anything else is dropped from the response and logged; the response stays 200.
|
|
199
199
|
|
|
200
200
|
#### Ransack filtering (`q`)
|
|
201
201
|
|
|
@@ -271,6 +271,8 @@ GET /api/v3/articles?sort=-created_at # descending
|
|
|
271
271
|
GET /api/v3/articles?sort=status,-title # multi-field
|
|
272
272
|
```
|
|
273
273
|
|
|
274
|
+
Fields are validated against the model's `ransortable_attributes` (by default its `ransackable_attributes`, which leave out its hidden secrets). Unknown fields are silently ignored.
|
|
275
|
+
|
|
274
276
|
### Pagination
|
|
275
277
|
|
|
276
278
|
```
|
|
@@ -87,14 +87,15 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
87
87
|
scope
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
+
# Sorts only by fields the model lets Ransack sort by, like `filter`: ordering by a hidden secret
|
|
91
|
+
# would reveal it (host Odoo #6313). Other fields are ignored.
|
|
90
92
|
def apply_sorting(scope)
|
|
91
93
|
return scope if params[:sort].blank?
|
|
94
|
+
sortable = @model.ransortable_attributes & @model.column_names # a ransacker is no column to order by
|
|
92
95
|
params[:sort].to_s.split(",").each do |field|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
scope = scope.order(field => :asc)
|
|
97
|
-
end
|
|
96
|
+
name, direction = field.start_with?("-") ? [field[1..], :desc] : [field, :asc]
|
|
97
|
+
next unless sortable.include?(name)
|
|
98
|
+
scope = scope.order(name => direction)
|
|
98
99
|
end
|
|
99
100
|
scope
|
|
100
101
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Api
|
|
2
|
+
# The API-hidden secrets of a model (host Odoo #6313, ADR 0006): its columns named like a secret
|
|
3
|
+
# (Rails' filter_parameters convention) that the model already hides in some way: left out of its
|
|
4
|
+
# default serializable_hash (Devise's encrypted_password), listed in its own json_attrs[:except]
|
|
5
|
+
# (thecore_spot_overrides' encrypted_access_token), or declared in write_only_attributes. They are
|
|
6
|
+
# never readable through the API: not in v3 attributes, not through v2 request options, not
|
|
7
|
+
# through Ransack filters or sorts. A secret-named column the model doesn't hide stays readable.
|
|
8
|
+
module HiddenSecrets
|
|
9
|
+
# Names Rails treats as secrets, on top of the host's own filter_parameters.
|
|
10
|
+
SECRET_NAMES = %i[passw secret token _key crypt salt certificate otp ssn cvv cvc].freeze
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# Column names (Strings), memoized per class.
|
|
15
|
+
def for(klass)
|
|
16
|
+
return [] unless klass.is_a?(Class) && klass < ActiveRecord::Base && !klass.abstract_class?
|
|
17
|
+
|
|
18
|
+
(@hidden ||= Concurrent::Map.new).compute_if_absent(klass) do
|
|
19
|
+
write_only = klass.respond_to?(:write_only_attributes) ? Array(klass.write_only_attributes).map(&:to_s) : []
|
|
20
|
+
hidden_columns = left_out_by_default(klass) | excepted_by_json_attrs(klass)
|
|
21
|
+
# write_only_attributes are secrets by declaration, whatever their name.
|
|
22
|
+
(hidden_columns.select { |column| secret_name?(column) } | write_only).freeze
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def secret_name?(name)
|
|
27
|
+
@secret_filter ||= ActiveSupport::ParameterFilter.new(SECRET_NAMES + Rails.application.config.filter_parameters)
|
|
28
|
+
@secret_filter.filter(name.to_s => "x")[name.to_s] != "x"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def left_out_by_default(klass)
|
|
32
|
+
klass.column_names - klass.new.serializable_hash.keys
|
|
33
|
+
rescue StandardError => e
|
|
34
|
+
Rails.logger.warn("[model_driven_api] HiddenSecrets: #{klass.name}.new failed (#{e.class}), " \
|
|
35
|
+
"secrets hidden by its serializable_hash are not detected")
|
|
36
|
+
[]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def excepted_by_json_attrs(klass)
|
|
40
|
+
attrs = klass.respond_to?(:json_attrs) ? klass.json_attrs : nil
|
|
41
|
+
attrs.is_a?(Hash) ? Array(attrs[:except] || attrs["except"]).map(&:to_s) : []
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/api/open_api/v3.rb
CHANGED
|
@@ -232,7 +232,7 @@ module Api
|
|
|
232
232
|
end
|
|
233
233
|
|
|
234
234
|
id_param = { "name" => "id", "in" => "path", "required" => true, "schema" => { "type" => "integer" } }
|
|
235
|
-
sort_param = { "name" => "sort", "in" => "query", "schema" => { "type" => "string" }, "description" => "field or -field (descending), comma-separated" }
|
|
235
|
+
sort_param = { "name" => "sort", "in" => "query", "schema" => { "type" => "string" }, "description" => "field or -field (descending), comma-separated; only the model's ransortable attributes, others are ignored" }
|
|
236
236
|
page_params = [
|
|
237
237
|
{ "name" => "page[number]", "in" => "query", "schema" => { "type" => "integer" } },
|
|
238
238
|
{ "name" => "page[size]", "in" => "query", "schema" => { "type" => "integer" } },
|
|
@@ -336,7 +336,7 @@ module Api
|
|
|
336
336
|
|
|
337
337
|
### Sorting
|
|
338
338
|
|
|
339
|
-
`?sort=field` (asc) · `?sort=-field` (desc) · `?sort=field1,-field2` (multi-field).
|
|
339
|
+
`?sort=field` (asc) · `?sort=-field` (desc) · `?sort=field1,-field2` (multi-field) — validated against each model's `ransortable_attributes`; other fields are ignored.
|
|
340
340
|
|
|
341
341
|
### Pagination
|
|
342
342
|
|
|
@@ -7,9 +7,9 @@ module Api
|
|
|
7
7
|
except = Array(jattrs[:except]).map(&:to_sym)
|
|
8
8
|
only = model_class.column_names.map(&:to_sym) - except
|
|
9
9
|
end
|
|
10
|
-
# A model's
|
|
11
|
-
# json_attrs asks (host Odoo #6084).
|
|
12
|
-
only -=
|
|
10
|
+
# A model's API-hidden secrets (Devise's encrypted_password, write_only_attributes such as
|
|
11
|
+
# LdapServer#admin_password) are never serialized, whatever json_attrs asks (host Odoo #6084, #6313).
|
|
12
|
+
only -= Api::HiddenSecrets.for(model_class).map(&:to_sym)
|
|
13
13
|
new(only.reject { |a| a == :id }, Array(jattrs[:methods]).map(&:to_sym), jattrs[:include])
|
|
14
14
|
end
|
|
15
15
|
|
|
@@ -9,7 +9,8 @@ module Api
|
|
|
9
9
|
# serializable_hash, e.g. Devise's encrypted_password, LdapServer#admin_password) never come back;
|
|
10
10
|
# - a `methods` name declared in the class's json_attrs[:methods] runs as before (a developer
|
|
11
11
|
# exposed it); a column or association reader (or `<assoc>_ids`) is a plain read; any other
|
|
12
|
-
# name must be public,
|
|
12
|
+
# name must be public, not a callback of the class (host Odoo #6314), end in neither `!` nor
|
|
13
|
+
# `=`, take no required argument and be written in
|
|
13
14
|
# the host or in a thecore gem (a gem named thecore* or depending on one), and it runs under
|
|
14
15
|
# the read-only guard: if it writes, or raises, only that field is dropped.
|
|
15
16
|
#
|
|
@@ -51,7 +52,7 @@ module Api
|
|
|
51
52
|
# The options Rails should serialize a record of +klass+ with, and the methods to evaluate
|
|
52
53
|
# under the read-only guard.
|
|
53
54
|
def for_class(klass, options)
|
|
54
|
-
hidden =
|
|
55
|
+
hidden = Api::HiddenSecrets.for(klass)
|
|
55
56
|
rewritten = options.except(MARK)
|
|
56
57
|
rewritten[:only] = Array(options[:only]).map(&:to_s) - hidden if options.key?(:only)
|
|
57
58
|
rewritten[:except] = Array(options[:except]).map(&:to_s) | hidden
|
|
@@ -85,11 +86,12 @@ module Api
|
|
|
85
86
|
end
|
|
86
87
|
|
|
87
88
|
def classify(klass, name)
|
|
88
|
-
return "hidden attribute" if
|
|
89
|
+
return "hidden attribute" if Api::HiddenSecrets.for(klass).include?(name)
|
|
89
90
|
return :declared if declared_methods(klass).include?(name)
|
|
90
91
|
return :reader if reader?(klass, name)
|
|
91
92
|
return "ends in ! or =" if name.end_with?("!", "=")
|
|
92
93
|
return "not a public method" unless klass.public_method_defined?(name)
|
|
94
|
+
return "a callback" if callback?(klass, name)
|
|
93
95
|
|
|
94
96
|
method = klass.instance_method(name)
|
|
95
97
|
return "takes required arguments" if method.parameters.any? { |type, _| %i[req keyreq].include?(type) }
|
|
@@ -106,6 +108,16 @@ module Api
|
|
|
106
108
|
|
|
107
109
|
def active_record?(klass) = klass < ActiveRecord::Base
|
|
108
110
|
|
|
111
|
+
# Registered as a Symbol filter in any of the class's callback chains (validation, validate,
|
|
112
|
+
# save, create, update, destroy, commit, rollback, initialize, find, touch, and any custom
|
|
113
|
+
# chain). A callback belongs to the record's lifecycle, never to its API, and its side
|
|
114
|
+
# effects may live outside the database, where the read-only guard can't see them.
|
|
115
|
+
def callback?(klass, name)
|
|
116
|
+
return false unless klass.respond_to?(:__callbacks)
|
|
117
|
+
|
|
118
|
+
klass.__callbacks.each_value.any? { |chain| chain.any? { |cb| cb.filter.is_a?(Symbol) && cb.filter.to_s == name } }
|
|
119
|
+
end
|
|
120
|
+
|
|
109
121
|
def reader?(klass, name)
|
|
110
122
|
return false unless active_record?(klass)
|
|
111
123
|
return true if klass.column_names.include?(name)
|
|
@@ -114,32 +126,6 @@ module Api
|
|
|
114
126
|
klass.reflect_on_all_associations.any? { |r| r.collection? && "#{r.name.to_s.singularize}_ids" == name }
|
|
115
127
|
end
|
|
116
128
|
|
|
117
|
-
# Names Rails treats as secrets (filter_parameters), on top of the host's own list.
|
|
118
|
-
SECRET_NAMES = %i[passw secret token _key crypt salt certificate otp ssn cvv cvc].freeze
|
|
119
|
-
|
|
120
|
-
# The secrets the class leaves out of its own default serialization: columns missing from a
|
|
121
|
-
# new record's default serializable_hash whose name is a secret's (Devise's encrypted_password,
|
|
122
|
-
# not its sign-in tracking, which frontends read through `only`), plus write_only_attributes.
|
|
123
|
-
# Memoized per class.
|
|
124
|
-
def hidden_attributes(klass)
|
|
125
|
-
return [] unless active_record?(klass)
|
|
126
|
-
|
|
127
|
-
(@hidden ||= Concurrent::Map.new).compute_if_absent(klass) do
|
|
128
|
-
write_only = klass.respond_to?(:write_only_attributes) ? Array(klass.write_only_attributes).map(&:to_s) : []
|
|
129
|
-
left_out = begin
|
|
130
|
-
klass.column_names - klass.new.serializable_hash.keys
|
|
131
|
-
rescue StandardError
|
|
132
|
-
[]
|
|
133
|
-
end
|
|
134
|
-
(left_out.select { |column| secret_name?(column) } | write_only).freeze
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
def secret_name?(name)
|
|
139
|
-
@secret_filter ||= ActiveSupport::ParameterFilter.new(SECRET_NAMES + Rails.application.config.filter_parameters)
|
|
140
|
-
@secret_filter.filter(name => "x")[name] != "x"
|
|
141
|
-
end
|
|
142
|
-
|
|
143
129
|
# Written in the host app or in a thecore gem. The gem that owns the file decides first:
|
|
144
130
|
# deploy images install the bundle inside the app (/app/vendor/bundle), so "under Rails.root"
|
|
145
131
|
# alone would make Rails, Devise and every other gem ours.
|
|
@@ -3,8 +3,10 @@ module ModelDrivenApiApplicationRecord
|
|
|
3
3
|
|
|
4
4
|
# Fixes: https://stackoverflow.com/a/76781489
|
|
5
5
|
included do
|
|
6
|
+
# Every column but the API-hidden secrets (Api::HiddenSecrets, host Odoo #6313): filtering or
|
|
7
|
+
# sorting on a hash would reveal it a character at a time.
|
|
6
8
|
def self.ransackable_attributes(auth_object = nil)
|
|
7
|
-
column_names + _ransackers.keys
|
|
9
|
+
column_names + _ransackers.keys - Api::HiddenSecrets.for(self)
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def self.ransackable_associations(auth_object = nil)
|
data/lib/model_driven_api.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: model_driven_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.20.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
@@ -246,6 +246,7 @@ files:
|
|
|
246
246
|
- db/migrate/20210528111450_rename_valid_to_is_valid_in_used_token.rb
|
|
247
247
|
- db/migrate/20261125120000_apply_thecore_raw_grants.rb
|
|
248
248
|
- lib/api/custom_action_dispatcher.rb
|
|
249
|
+
- lib/api/hidden_secrets.rb
|
|
249
250
|
- lib/api/model_resolver.rb
|
|
250
251
|
- lib/api/open_api/base.rb
|
|
251
252
|
- lib/api/open_api/v2.rb
|