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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b18e677036c14852dba06410da79ef19389baa005b05581cbe7e7666744622b1
4
- data.tar.gz: bbf43b381479c8de9dc7c755074ba1b8291305bbd57c6f1196307fe9b34f8b3f
3
+ metadata.gz: 758e0e3a9837dd6f424ab2c36d77e51204bfa357ea27fe7d667d4889a3e0e0a7
4
+ data.tar.gz: 86a1e32e866e87ef080601458a0c1347da2c592faeca0e8e2e8c4d6f124cc047
5
5
  SHA512:
6
- metadata.gz: 6a1ed69d37339dadd60a593d907ecd2ddac9e819dee7fb9f5959762e3ecc9571fccc0bb60e673aca149f6966fc6bedbde9d406169f5be739dd752784cb058a42
7
- data.tar.gz: 571015de06d02774a28a5efb46ed0cc5a1ec322df33da33383d5883de307f79db2d996a485330282d79e330caf9b4cc94983929b0fe81572e3e7209b199589fb
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 by default (e.g. Devise's `encrypted_password`) never come back; `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.
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
- if field.start_with?("-")
94
- scope = scope.order(field[1..] => :desc)
95
- else
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
@@ -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 write-only attributes (LdapServer#admin_password) are never serialized, whatever
11
- # json_attrs asks (host Odoo #6084).
12
- only -= Array(model_class.write_only_attributes).map(&:to_sym) if model_class.respond_to?(:write_only_attributes)
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 = hidden_attributes(klass)
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 hidden_attributes(klass).include?(name)
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)
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.18.0".freeze
2
+ VERSION = "3.19.0".freeze
3
3
  end
@@ -26,6 +26,7 @@ require "model_driven_api/current"
26
26
 
27
27
  require "safe_sql_executor"
28
28
  require "raw_sql_grants"
29
+ require "api/hidden_secrets"
29
30
  require "api/resource_attribute_set"
30
31
  require "api/v2/request_json_attrs"
31
32
  require "api/model_resolver"
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.18.0
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