model_driven_api 3.18.0 → 3.19.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 +2 -28
- 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: 758e0e3a9837dd6f424ab2c36d77e51204bfa357ea27fe7d667d4889a3e0e0a7
|
|
4
|
+
data.tar.gz: 86a1e32e866e87ef080601458a0c1347da2c592faeca0e8e2e8c4d6f124cc047
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 30e10a13688e1d8aefa04a7072e2516e58ddb53c34db778c745271f3ae48e6f6056f4b818faab5b1e184c11e785baf45f18f67317a73e0714790ae4b351fd5f8
|
|
7
|
+
data.tar.gz: 6acdee803752c29121791d732b4e4a5688b80a6cc37c245e092afc3cead0cd97a3abfcd39ed10ba5c44f78b68f48ce546230a9bd0768ff888cb5c036b1dcf4ba
|
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 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
|
|
|
@@ -51,7 +51,7 @@ module Api
|
|
|
51
51
|
# The options Rails should serialize a record of +klass+ with, and the methods to evaluate
|
|
52
52
|
# under the read-only guard.
|
|
53
53
|
def for_class(klass, options)
|
|
54
|
-
hidden =
|
|
54
|
+
hidden = Api::HiddenSecrets.for(klass)
|
|
55
55
|
rewritten = options.except(MARK)
|
|
56
56
|
rewritten[:only] = Array(options[:only]).map(&:to_s) - hidden if options.key?(:only)
|
|
57
57
|
rewritten[:except] = Array(options[:except]).map(&:to_s) | hidden
|
|
@@ -85,7 +85,7 @@ module Api
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
def classify(klass, name)
|
|
88
|
-
return "hidden attribute" if
|
|
88
|
+
return "hidden attribute" if Api::HiddenSecrets.for(klass).include?(name)
|
|
89
89
|
return :declared if declared_methods(klass).include?(name)
|
|
90
90
|
return :reader if reader?(klass, name)
|
|
91
91
|
return "ends in ! or =" if name.end_with?("!", "=")
|
|
@@ -114,32 +114,6 @@ module Api
|
|
|
114
114
|
klass.reflect_on_all_associations.any? { |r| r.collection? && "#{r.name.to_s.singularize}_ids" == name }
|
|
115
115
|
end
|
|
116
116
|
|
|
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
117
|
# Written in the host app or in a thecore gem. The gem that owns the file decides first:
|
|
144
118
|
# deploy images install the bundle inside the app (/app/vendor/bundle), so "under Rails.root"
|
|
145
119
|
# 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.19.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
|