active_scaffold 4.4.7 → 4.4.8

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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.rdoc +13 -0
  3. data/README.md +101 -0
  4. data/app/views/active_scaffold_overrides/_add_existing_form.html.erb +2 -2
  5. data/app/views/active_scaffold_overrides/_base_form.html.erb +2 -1
  6. data/app/views/active_scaffold_overrides/_field_search.html.erb +1 -0
  7. data/app/views/active_scaffold_overrides/_form_association_footer.html.erb +13 -11
  8. data/app/views/active_scaffold_overrides/_list.html.erb +1 -1
  9. data/app/views/active_scaffold_overrides/_new_record.js.erb +3 -3
  10. data/app/views/active_scaffold_overrides/_refresh_list.js.erb +1 -1
  11. data/app/views/active_scaffold_overrides/_search.html.erb +1 -0
  12. data/app/views/active_scaffold_overrides/_show.html.erb +1 -1
  13. data/app/views/active_scaffold_overrides/action_confirmation.html.erb +2 -2
  14. data/app/views/active_scaffold_overrides/delete.html.erb +2 -2
  15. data/app/views/active_scaffold_overrides/destroy.js.erb +5 -5
  16. data/app/views/active_scaffold_overrides/on_update.js.erb +1 -1
  17. data/lib/active_scaffold/actions/core.rb +114 -14
  18. data/lib/active_scaffold/actions/field_search.rb +8 -1
  19. data/lib/active_scaffold/actions/nested.rb +3 -1
  20. data/lib/active_scaffold/actions/subform.rb +56 -9
  21. data/lib/active_scaffold/actions/update.rb +2 -2
  22. data/lib/active_scaffold/api_association_serializer.rb +27 -0
  23. data/lib/active_scaffold/config/core.rb +25 -0
  24. data/lib/active_scaffold/extensions/action_view_rendering.rb +4 -9
  25. data/lib/active_scaffold/finder.rb +12 -4
  26. data/lib/active_scaffold/helpers/action_link_helpers.rb +18 -6
  27. data/lib/active_scaffold/helpers/association_helpers.rb +1 -0
  28. data/lib/active_scaffold/helpers/controller_helpers.rb +63 -1
  29. data/lib/active_scaffold/helpers/form_column_helpers.rb +1 -1
  30. data/lib/active_scaffold/helpers/form_ui_helpers.rb +1 -0
  31. data/lib/active_scaffold/helpers/list_column_helpers.rb +4 -4
  32. data/lib/active_scaffold/helpers/pagination_helpers.rb +1 -1
  33. data/lib/active_scaffold/helpers/tabs_helpers.rb +1 -1
  34. data/lib/active_scaffold/helpers/view_helpers.rb +1 -1
  35. data/lib/active_scaffold/parameter_signing.rb +34 -0
  36. data/lib/active_scaffold/version.rb +1 -1
  37. data/lib/active_scaffold.rb +1 -0
  38. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a456ae9c095faa9ee26c7634275b7517bc896af01f132882d44671b7479253d9
4
- data.tar.gz: 8330b0c66cbbe8ff6ee7b258c65aa70a40486ce99b8a5c24ca17b3fbe4b9cea2
3
+ metadata.gz: 93a50bab634923deec74bb0777798bbdb11e6a1892d2aecd85602defb7b6352e
4
+ data.tar.gz: 8efd5a4229407e194fbc0f7e901d4467ea1fcd1500c991ecc50d66c5911f9061
5
5
  SHA512:
6
- metadata.gz: 1be8db3d0d117c3ed52dbce11e75479e70e3b7c91769a8bdb0902d917d0ca8916dc4d8df4235abd205a643b1c783f04346d31b2e605aaa049142a30b3118ace9
7
- data.tar.gz: 7abf7b3d41488919571eb5ea182a4bbf8f8abd48bfe8cf9f5813de3401822f34dfe1219faf4c3a5bcaecd29912817c9d591d86b411ae50802d6465ef586144a6
6
+ metadata.gz: bb56102fc32fa3e594d666eec2446b553ed82ad8ad302d055fd8e5b4e6e1305fda0b54017f4f87ce86069a04a436e219423701831d9977b023f27ed08c444d96
7
+ data.tar.gz: 9072cd0c201e25156e01b9d757acca94f91bc365a8d5d6cb03492f6309b63063bc7f9259bdba491cdfa48ca1454dc31554ffca28add6aec9336cf1e959bacec3
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,16 @@
1
+ = 4.4.8
2
+ - Add authorized API association exports containing the primary key and +to_label+, with an overridable +api_association_options+ controller method. Associated record read authorization is checked automatically; applications can return Rails serialization options, including explicit nested +include:+ options, and must authorize the additional fields and nested records they request. In 4.4, +unsafe_api_associations = nil+ preserves legacy full-attribute exports with a deprecation warning, +true+ preserves them without warning, and +false+ enables safe exports. The unsafe behavior and compatibility setting will be removed in 4.5.
3
+ - Authenticate embedded scaffold parameters with a signature in +eid+, bound to the target controller. Reject unsigned or modified conditions and constraints before using them to query or assign records.
4
+ - Authenticate +return_to+ destinations before rendering links or redirecting, preventing request parameters from supplying unverified return URLs. Preserve the application's policy for redirects to external hosts.
5
+ - Authenticate tab selections before assigning associated records, binding the signature to the controller, parent form, association, and grouping value. Validate tab groups against their configured grouping field and resolve association classes according to polymorphism.
6
+ - Authorize existing records selected through add-existing and subform actions. Add +associated_record_scope+ so applications can restrict selectable records using the candidate class and parent association column.
7
+ - Require inline editing to be enabled for the requested column before rendering its editor or invoking update overrides, preventing inline endpoints from updating columns outside the configured inline-edit interface.
8
+ - Restrict grouped field searches to configured +group_options+ values, preventing requests from choosing arbitrary grouping columns or functions.
9
+ - Restrict date trend search units to supported date and time durations before invoking duration methods. Add the overridable +valid_datetime_trend_unit?+ finder method so applications can allow additional duration units when extending +active_scaffold_search_datetime_trend_units+.
10
+ - Add +conditions_from_params_allowed_columns+ and +conditions_from_params_excluded_columns+ to control filtering through URL parameters. Excluded columns take precedence; a nil allowed list preserves existing behavior, while an empty allowed list disables parameter filtering.
11
+ - Make +default_formats+ configurable globally and per controller so applications can restrict the response formats exposed by scaffold actions. Defaults remain HTML, JavaScript, JSON, and XML.
12
+ - Apply the selected tab grouping to existing subform records as well as newly built records. Require update authorization when changing an existing record's grouping and stage association changes until the parent form is saved.
13
+
1
14
  = 4.4.7
2
15
  - Apply per-record column read authorization to JSON and XML responses, preventing fields hidden by instance-level authorization from being disclosed through API formats.
3
16
 
data/README.md CHANGED
@@ -182,6 +182,22 @@ Configuration
182
182
  -------------
183
183
  See Wiki for instructions on customising ActiveScaffold and to find the full API details.
184
184
 
185
+ The default response formats are `html`, `js`, `json`, and `xml`. To change them for the application, configure the defaults in an initializer before controllers are loaded:
186
+
187
+ ```ruby
188
+ ActiveScaffold::Config::Core.default_formats = %i[html js]
189
+ ```
190
+
191
+ Override these defaults for an individual controller inside its `active_scaffold` block:
192
+
193
+ ```ruby
194
+ active_scaffold do |config|
195
+ config.default_formats = %i[html js]
196
+ end
197
+ ```
198
+
199
+ The existing `config.formats` and action settings such as `config.list.formats` add formats to these defaults. Removing a format from `default_formats` does not disable it if it is explicitly added elsewhere. Actions with a fixed set of formats, such as JavaScript subform rendering, keep their own formats.
200
+
185
201
  Credits
186
202
  -------
187
203
  ActiveScaffold grew out of a project named Ajaxscaffold dating back to 2006. It has had numerous contributors including:
@@ -219,3 +235,88 @@ Happy translating!
219
235
 
220
236
  ### Powered by
221
237
  [![RubyMine logo](https://resources.jetbrains.com/storage/products/company/brand/logos/RubyMine.png)](https://jb.gg/OpenSource)
238
+
239
+ ### API association exports
240
+
241
+ In 4.4, associations in JSON and XML responses retain their legacy serialization by default.
242
+ This exports all database attributes on associated records without checking their record or
243
+ column read authorization, even when the associated scaffold hides those attributes.
244
+ A deprecation warning is emitted when associations are exported.
245
+
246
+ Enable the safe output globally:
247
+
248
+ ```ruby
249
+ ActiveScaffold::Config::Core.unsafe_api_associations = false
250
+ ```
251
+
252
+ Or configure an individual scaffold:
253
+
254
+ ```ruby
255
+ active_scaffold do |config|
256
+ config.unsafe_api_associations = false
257
+ end
258
+ ```
259
+
260
+ Safe output exports each readable associated record as a hash containing its primary key
261
+ and `to_label`. Unreadable singular records become `null`; unreadable records in collections
262
+ are omitted. The primary key and label do not require column authorization checks. By default,
263
+ associations are not expanded recursively, and no associated scaffold configuration is loaded.
264
+
265
+ Override `api_association_options(column, record)` to return the options passed directly to
266
+ Rails' `serializable_hash`, including `only:`, `except:`, `methods:`, and `include:`. For example:
267
+
268
+ ```ruby
269
+ protected
270
+
271
+ def api_association_options(column, record)
272
+ if column.name == :company
273
+ {only: [record.class.primary_key, :name], methods: [:to_label]}
274
+ else
275
+ super
276
+ end
277
+ end
278
+ ```
279
+
280
+ The serializer checks associated record read authorization but does not authorize the fields
281
+ selected by these options. If additional fields need column read authorization, filter them
282
+ explicitly in the override:
283
+
284
+ ```ruby
285
+ protected
286
+
287
+ def api_association_options(column, record)
288
+ options = super
289
+ if column.name == :company
290
+ columns = %i[name email].select do |name|
291
+ record.authorized_for?(crud_type: :read, column: name)
292
+ end
293
+ options = options.merge(only: Array(options[:only]) + columns)
294
+ end
295
+ options
296
+ end
297
+ ```
298
+
299
+ This preserves the default primary key and label and checks permissions only for the extra
300
+ fields. Choose label and virtual methods that expose data appropriate for your API.
301
+
302
+ Applications can explicitly request nested data using `include:` (singular):
303
+
304
+ ```ruby
305
+ def api_association_options(column, record)
306
+ options = super
307
+ if column.name == :company
308
+ options = options.merge(include: {address: {only: [:city, :country]}})
309
+ end
310
+ options
311
+ end
312
+ ```
313
+
314
+ Nested includes use Rails' normal serialization. Applications are responsible for authorizing
315
+ all nested records and fields they request. `serializable_hash` does not check those permissions;
316
+ using an unrestricted `include:` can export every database attribute of nested records.
317
+ For JSON, the parent uses `as_json` to preserve its model root, while associated records use
318
+ `serializable_hash`, matching Rails' nested `include:` behavior and avoiding extra model roots.
319
+
320
+ Setting `unsafe_api_associations = true` explicitly retains the legacy unsafe output without
321
+ warnings in 4.4. Leaving it `nil` retains the output with a warning. Both the unsafe behavior
322
+ and this compatibility setting will be removed in 4.5; configure `false` now to migrate.
@@ -6,7 +6,7 @@ options = {id: element_form_id(action: :add_existing),
6
6
  method: :post,
7
7
  'data-loading' => true}
8
8
  options[:remote] = true if xhr
9
- form_tag url_options, options -%>
9
+ form_tag signed_url_options(url_options), options -%>
10
10
 
11
11
  <%= as_element :form_title, active_scaffold_config.nested.label -%>
12
12
 
@@ -27,7 +27,7 @@ options = {id: element_form_id(action: :add_existing),
27
27
 
28
28
  <%= as_element :form_footer, class: 'form-footer' do %>
29
29
  <%= submit_tag as_(:add), as_element_attributes(:form_submit) %>
30
- <%= link_to as_(:cancel), main_path_to_return, as_element_attributes(:form_cancel, class: 'as_cancel', remote: true) %>
30
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), as_element_attributes(:form_cancel, class: 'as_cancel', remote: true) %>
31
31
  <%= loading_indicator_tag(action: :add_existing, id: params[:id]) %>
32
32
  <% end %>
33
33
 
@@ -46,6 +46,7 @@ options = as_element_attributes(
46
46
  data: {loading: defined?(loading) ? loading : true}
47
47
  )
48
48
 
49
+ url_options = signed_url_options(url_options)
49
50
  if xhr && multipart # file_uploads
50
51
  form_remote_upload_tag url_options.merge(iframe: true), options
51
52
  else
@@ -71,7 +72,7 @@ end
71
72
  <%= as_element :form_footer, class: 'form-footer' do %>
72
73
  <%= submit_tag as_(submit_text), as_element_attributes(:form_submit) if !persistent || persistent == :optional %>
73
74
  <%= submit_tag as_(apply_text), as_element_attributes(:form_apply, name: 'dont_close') if persistent %>
74
- <%= link_to as_(cancel_text), cancel_url, as_element_attributes(:form_cancel, **cancel_options) if cancel_link %>
75
+ <%= link_to as_(cancel_text), signed_url_options(cancel_url), as_element_attributes(:form_cancel, **cancel_options) if cancel_link %>
75
76
  <%= loading_indicator_tag action: form_action, id: @record&.id %>
76
77
  <%= render footer_extension, form_action: form_action if footer_extension %>
77
78
  <% end %>
@@ -11,6 +11,7 @@
11
11
  method: :get
12
12
  )
13
13
 
14
+ url_options = signed_url_options(url_options)
14
15
  hidden_params = url_options.except(:controller, :action, :id, :search).to_query.split(Rack::Utils::DEFAULT_SEP)
15
16
  -%>
16
17
 
@@ -14,17 +14,19 @@ temporary_id = generated_id(parent_record) if parent_record.new_record?
14
14
  controller_path = active_scaffold_controller_for(parent_record.class).controller_path
15
15
  parent_controller = (main_form_controller || controller).controller_path
16
16
  url_options = {controller: controller_path, action: 'edit_associated', child_association: column.name, scope: scope, id: parent_record.to_param, generated_id: temporary_id, parent_controller: parent_controller}
17
- edit_associated_url = params_for(url_options.merge(associated_id: '--ID--')) if show_add_existing
18
- if show_add_new
19
- if local_assigns[:tabbed_by]
20
- url_options.merge! tabbed_by: tabbed_by, value: tab_value
21
- if (tabbed_association = tabbed_by_association(column, tabbed_by))
22
- url_options[:value] = tab_value.id
23
- url_options[:value_type] = tab_value.class.base_class.sti_name if tabbed_association.polymorphic?
24
- end
17
+ # Tab selection parameters are one-use context. Only the values constructed
18
+ # below from the rendered tab may be signed, never inherited request values.
19
+ tab_parameters = ActiveScaffold::ParameterSigning::TAB_SELECTION_PARAMETERS
20
+ if local_assigns[:tabbed_by]
21
+ url_options.merge! tabbed_by: tabbed_by, value: tab_value
22
+ if (tabbed_association = tabbed_by_association(column, tabbed_by))
23
+ url_options[:value] = tab_value.id
24
+ url_options[:value_type] = tab_value.class.base_class.sti_name if tabbed_association.polymorphic?
25
25
  end
26
- add_new_url = params_for(url_options)
27
26
  end
27
+ tab_options = url_options.slice(:tabbed_by, :value, :value_type)
28
+ edit_associated_url = params_for(url_options.merge(associated_id: '--ID--')).except(*tab_parameters).merge(tab_options) if show_add_existing
29
+ add_new_url = params_for(url_options).except(*tab_parameters).merge(tab_options) if show_add_new
28
30
 
29
31
  -%>
30
32
  <div class="footer-wrapper">
@@ -40,7 +42,7 @@ end
40
42
  end
41
43
  add_button_attrs = {id: "#{subform_div_id}-create-another", remote: true, class: "as-js-button as_#{add_class}", style: 'display: none;'}
42
44
  %>
43
- <%= link_to add_label, add_new_url, as_element_attributes(:"subform_#{add_class}", **add_button_attrs) %>
45
+ <%= link_to add_label, signed_tab_selection_options(signed_url_options(add_new_url)), as_element_attributes(:"subform_#{add_class}", **add_button_attrs) %>
44
46
  <% end -%>
45
47
 
46
48
  <%= '|' if show_add_new && show_add_existing %>
@@ -56,7 +58,7 @@ end
56
58
  add_existing_attrs = {id: "#{subform_div_id}-add-existing", remote: true, class: "as-js-button as_#{add_existing_label}", style: 'display: none;'}
57
59
  %>
58
60
  <%= select_tag 'associated_id', content_tag(:option, as_(:_select_), value: '') + select_options %>
59
- <%= link_to as_(add_existing_label), edit_associated_url, as_element_attributes(:subform_add_existing, **add_existing_attrs) %>
61
+ <%= link_to as_(add_existing_label), signed_tab_selection_options(signed_url_options(edit_associated_url)), as_element_attributes(:subform_add_existing, **add_existing_attrs) %>
60
62
  <% end -%>
61
63
  <% end -%>
62
64
  <% end %>
@@ -18,7 +18,7 @@
18
18
  </tr>
19
19
  </thead>
20
20
  <%= render 'list_messages', columns: columns %>
21
- <tbody <%= as_element_attributes :list_records, class: 'records', id: active_scaffold_tbody_id, data: {refresh_record: url_for(params_for(action: :index, id: '--ID--'))} %>>
21
+ <tbody <%= as_element_attributes :list_records, class: 'records', id: active_scaffold_tbody_id, data: {refresh_record: url_for(signed_url_options(params_for(action: :index, id: '--ID--')))} %>>
22
22
  <% if !@page.empty? -%>
23
23
  <%= render partial: list_record_view, collection: @page.items, locals: {hidden: false, columns: columns, action_links: active_scaffold_config.action_links.member} %>
24
24
  <% end -%>
@@ -2,9 +2,9 @@
2
2
  <% if nested_singular_association? %>
3
3
  if (action_link) action_link.close(true);
4
4
  <% elsif params[:parent_sti] && render_parent_action == :row %>
5
- ActiveScaffold.create_record_row_from_url(action_link, '<%=raw url_for(render_parent_options) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
5
+ ActiveScaffold.create_record_row_from_url(action_link, '<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
6
6
  <% else %>
7
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
7
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
8
8
  <% end %>
9
9
  <% elsif active_scaffold_config.create.refresh_list %>
10
10
  <%= render 'refresh_list', no_history: true %>
@@ -14,4 +14,4 @@
14
14
  <% new_row = render list_record_view, record: @saved_record || @record %>
15
15
  ActiveScaffold.create_record_row(action_link ? action_link.scaffold() : '<%= active_scaffold_id %>', '<%= escape_javascript(new_row) %>', <%= {insert_at: insert_at}.to_json.html_safe %>);
16
16
  <%= render 'update_calculations' %>
17
- <% end %>
17
+ <% end %>
@@ -3,7 +3,7 @@
3
3
  ActiveScaffold.append('<%= active_scaffold_tbody_id %>', '<%= escape_javascript(render(partial: list_record_view, collection: @page.items, locals: {hidden: false, columns: list_columns, action_links: active_scaffold_config.action_links.member})) %>');
4
4
 
5
5
  <% if @page.pager.infinite? || @page.number < @page.pager.number_of_pages -%>
6
- ActiveScaffold.auto_load_page('<%=raw url_for main_path_to_return %>', {auto_pagination: true, page: <%= @page.number + 1 %>});
6
+ ActiveScaffold.auto_load_page('<%= escape_javascript(url_for(signed_url_options(main_path_to_return))) %>', {auto_pagination: true, page: <%= @page.number + 1 %>});
7
7
  <% else %>
8
8
  ActiveScaffold.hide('<%= loading_indicator_id(action: :pagination) %>');
9
9
  <% end # @page.pager.infinite?... %>
@@ -12,6 +12,7 @@
12
12
  method: :get
13
13
  )
14
14
 
15
+ url_options = signed_url_options(url_options)
15
16
  hidden_params = url_options.except(:controller, :action, :id, :search).to_query.split(Rack::Utils::DEFAULT_SEP)
16
17
  -%>
17
18
 
@@ -3,6 +3,6 @@
3
3
  <%= render 'show_columns', columns: active_scaffold_config.show.columns, record: @record -%>
4
4
 
5
5
  <%= as_element :show_footer do %>
6
- <%= link_to as_(:close), main_path_to_return, as_element_attributes(:show_cancel, class: 'as_cancel', remote: request.xhr?) %>
6
+ <%= link_to as_(:close), signed_url_options(main_path_to_return), as_element_attributes(:show_cancel, class: 'as_cancel', remote: request.xhr?) %>
7
7
  <%= loading_indicator_tag(action: :create, id: params[:id]) %>
8
8
  <% end %>
@@ -1,11 +1,11 @@
1
1
  <div class="active-scaffold">
2
2
  <div class="delete-view view">
3
- <%= form_tag params_for(action: link.action, id: params[:id]), {method: link.method} %>
3
+ <%= form_tag signed_url_options(params_for(action: link.action, id: params[:id])), {method: link.method} %>
4
4
  <h4><%= link.confirm(h(record&.to_label)) -%></h4>
5
5
 
6
6
  <p class="form-footer">
7
7
  <%= submit_tag as_(link.label(record)), class: 'submit' %>
8
- <%= link_to as_(:cancel), main_path_to_return, class: 'cancel' %>
8
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), class: 'cancel' %>
9
9
  </p>
10
10
 
11
11
  </form>
@@ -1,11 +1,11 @@
1
1
  <div class="active-scaffold">
2
2
  <div class="delete-view view">
3
- <%= form_tag params_for(action: :destroy, id: params[:id]), {method: :delete} %>
3
+ <%= form_tag signed_url_options(params_for(action: :destroy, id: params[:id])), {method: :delete} %>
4
4
  <h4><%= as_(:are_you_sure_to_delete, label: h(@record&.to_label)) -%></h4>
5
5
 
6
6
  <p class="form-footer">
7
7
  <%= submit_tag as_(:delete), class: 'submit' %>
8
- <%= link_to as_(:cancel), main_path_to_return, class: 'cancel' %>
8
+ <%= link_to as_(:cancel), signed_url_options(main_path_to_return), class: 'cancel' %>
9
9
  </p>
10
10
 
11
11
  </form>
@@ -3,14 +3,14 @@
3
3
  <% if render_parent? %>
4
4
  <% if render_parent_action == :row %>
5
5
  <% current_id = controller_id(params[:eid] || params[:parent_sti]) -%>
6
- ActiveScaffold.delete_record_row('<%= element_row_id(controller_id: current_id, action: 'list', id: params[:id]) %>', '<%=raw url_for params_for(render_parent_options.merge(id: nil, page: [active_scaffold_config.list.user.page.to_i - 1, 1].max)) %>');
6
+ ActiveScaffold.delete_record_row('<%= element_row_id(controller_id: current_id, action: 'list', id: params[:id]) %>', '<%= escape_javascript(url_for(signed_url_options(params_for(render_parent_options.merge(id: nil, page: [active_scaffold_config.list.user.page.to_i - 1, 1].max))))) %>');
7
7
  <% messages_id = active_scaffold_messages_id(controller_id: current_id) %>
8
8
  <%= render 'update_calculations', calculations_id: active_scaffold_calculations_id(controller_id: current_id) %>
9
9
  <% elsif render_parent_action == :index %>
10
10
  <% if controller.respond_to?(:render_component_into_view, true) %>
11
- <%= escape_javascript(controller.send(:render_component_into_view, render_parent_options)) %>
11
+ <%= escape_javascript(controller.send(:render_component_into_view, signed_url_options(render_parent_options))) %>
12
12
  <% else %>
13
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
13
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
14
14
  <% end %>
15
15
  <% end %>
16
16
  <% elsif active_scaffold_config.delete.refresh_list %>
@@ -20,10 +20,10 @@
20
20
  if active_scaffold_config.actions.exclude?(:list) || active_scaffold_config.list.pagination
21
21
  url = main_path_to_return
22
22
  url[:page] = [active_scaffold_config.list.user.page.to_i - 1, 1].max if url.is_a?(Hash) && active_scaffold_config.actions.include?(:list)
23
- url = url_for(url)
23
+ url = url_for(signed_url_options(url))
24
24
  end
25
25
  %>
26
- ActiveScaffold.delete_record_row('<%= element_row_id(action: 'list', id: params[:id]) %>', '<%= url %>');
26
+ ActiveScaffold.delete_record_row('<%= element_row_id(action: 'list', id: params[:id]) %>', '<%= escape_javascript(url) %>');
27
27
  <%= render 'update_calculations' %>
28
28
  <% end %>
29
29
  <% else %>
@@ -16,7 +16,7 @@ if (action_link) action_link.update_flash_messages('<%= escape_javascript(render
16
16
  <% if nested_singular_association? || render_parent_action == :row %>
17
17
  if (action_link) action_link.close(true);
18
18
  <% else %>
19
- ActiveScaffold.reload('<%=raw url_for render_parent_options %>');
19
+ ActiveScaffold.reload('<%= escape_javascript(url_for(signed_url_options(render_parent_options))) %>');
20
20
  <% end %>
21
21
  <% elsif update_refresh_list? %>
22
22
  <%= render 'refresh_list', no_history: true %>
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_scaffold/api_association_serializer'
4
+
3
5
  module ActiveScaffold::Actions
4
6
  module Core
5
7
  class AuthorizedApiResponse
6
- def initialize(response_object, columns_names, config)
8
+ def initialize(response_object, columns_names, config, association_options: nil)
7
9
  @response_object = response_object
8
10
  @columns_names = columns_names
9
11
  @config = config
12
+ @association_serializer = ActiveScaffold::ApiAssociationSerializer.new(association_options)
10
13
  end
11
14
 
12
15
  def to_json(options = {})
@@ -14,13 +17,17 @@ module ActiveScaffold::Actions
14
17
  end
15
18
 
16
19
  def to_xml(options = {})
17
- return serialized_response(:to_xml, options) unless collection?
20
+ return serialized_response(:to_xml, options) if !collection? && unsafe_associations?
18
21
 
19
22
  xml_options = options.dup
20
- records = response_records
21
- if records.present? && records.all?(records.first.class)
22
- record_name = ActiveSupport::Inflector.underscore(records.first.class.name).tr('/', '_')
23
- xml_options[:root] ||= ActiveSupport::Inflector.pluralize(record_name)
23
+ if collection?
24
+ records = response_records
25
+ if records.present? && records.all?(records.first.class)
26
+ record_name = ActiveSupport::Inflector.underscore(records.first.class.name).tr('/', '_')
27
+ xml_options[:root] ||= ActiveSupport::Inflector.pluralize(record_name)
28
+ end
29
+ else
30
+ xml_options.reverse_merge!(root: @response_object.class.model_name.singular)
24
31
  end
25
32
  serialized_response(:serializable_hash).to_xml(xml_options)
26
33
  end
@@ -30,11 +37,54 @@ module ActiveScaffold::Actions
30
37
  def serialized_response(method, options = {})
31
38
  if collection?
32
39
  response_records.map do |record|
33
- record.public_send(method, options.merge(serialization_options(record)))
40
+ serialized_record(record, method, options)
34
41
  end
35
42
  else
36
- @response_object.public_send(method, options.merge(serialization_options(@response_object)))
43
+ serialized_record(@response_object, method, options)
44
+ end
45
+ end
46
+
47
+ def serialized_record(record, method, options)
48
+ options = options.merge(serialization_options(record))
49
+ associations = options[:include]
50
+ if unsafe_associations?
51
+ warn_unsafe_associations if associations.any?
52
+ return record.public_send(method, options)
53
+ end
54
+
55
+ options[:include] = []
56
+ result = record.public_send(method, options)
57
+ attributes = serialized_attributes(result, record, method, options)
58
+ associations.each do |name|
59
+ column = @config.columns[name]
60
+ attributes[name.to_s] = @association_serializer.serialize(record.public_send(name), column)
37
61
  end
62
+ result
63
+ end
64
+
65
+ def serialized_attributes(result, record, method, options)
66
+ return result unless method == :as_json
67
+
68
+ root = options.key?(:root) ? options[:root] : record.include_root_in_json
69
+ root = record.model_name.element if root == true
70
+ root ? result.fetch(root) : result
71
+ end
72
+
73
+ def unsafe_associations?
74
+ @config.unsafe_api_associations != false
75
+ end
76
+
77
+ def warn_unsafe_associations
78
+ return if @config.unsafe_api_associations || @warned_unsafe_associations
79
+
80
+ @warned_unsafe_associations = true
81
+ ActiveScaffold.deprecator.warn(
82
+ 'API association serialization currently exports all associated database attributes without ' \
83
+ 'checking associated record or column read authorization. This unsafe behavior and the ' \
84
+ 'unsafe_api_associations setting will be removed in ActiveScaffold 4.5. Set ' \
85
+ 'unsafe_api_associations = false to export authorized association IDs and labels, or true ' \
86
+ 'to explicitly retain unsafe serialization without this warning until 4.5.'
87
+ )
38
88
  end
39
89
 
40
90
  def collection?
@@ -74,6 +124,7 @@ module ActiveScaffold::Actions
74
124
 
75
125
  def self.included(base)
76
126
  base.class_eval do
127
+ before_action :verify_return_to_parameter
77
128
  before_action :set_vary_accept_header
78
129
  before_action :check_input_device
79
130
  before_action :register_constraints_with_action_columns, unless: :nested?
@@ -131,13 +182,25 @@ module ActiveScaffold::Actions
131
182
  end
132
183
 
133
184
  def render_field_for_inplace_editing
134
- @column = active_scaffold_config.columns[params[:update_column]]
185
+ @column = inplace_edit_column(params[:update_column])
135
186
  @record = find_if_allowed(params[:id], crud_type: :update, column: params[:update_column])
136
187
  end
137
188
 
189
+ def inplace_edit_column(name)
190
+ column = active_scaffold_config.columns[name] if name.present?
191
+ raise ActiveScaffold::ActionNotAllowed unless column&.inplace_edit
192
+
193
+ column
194
+ end
195
+
138
196
  def add_tab
139
197
  process_render_field_params
140
198
  @column = @main_columns.find_by_name(params[:column])
199
+ unless @column.is_a?(ActiveScaffold::DataStructures::ActionColumns) && @column.tabbed_by.present? &&
200
+ @column.tabbed_by.to_s == params[:tabbed_by]
201
+ raise ::ActionController::BadRequest, 'Invalid tab group'
202
+ end
203
+
141
204
  @record = updated_record_with_form(@main_columns, {}, @scope)
142
205
  end
143
206
 
@@ -367,7 +430,7 @@ module ActiveScaffold::Actions
367
430
  end
368
431
 
369
432
  def default_formats
370
- %i[html js json xml]
433
+ active_scaffold_config.default_formats
371
434
  end
372
435
 
373
436
  # Returns true if the client accepts one of the MIME types passed to it
@@ -397,10 +460,19 @@ module ActiveScaffold::Actions
397
460
  @response_object ||= successful? ? (@record || @records) : @record.errors
398
461
  end
399
462
 
463
+ # Override the options passed to serializable_hash for an associated record.
464
+ # Applications must authorize any additional fields or nested includes they select.
465
+ def api_association_options(column, record)
466
+ {only: [record.class.primary_key], methods: [:to_label]}
467
+ end
468
+
400
469
  def response_to_api(format, columns_names, options = {})
401
470
  response =
402
471
  if successful?
403
- AuthorizedApiResponse.new(response_object, columns_names, active_scaffold_config)
472
+ AuthorizedApiResponse.new(
473
+ response_object, columns_names, active_scaffold_config,
474
+ association_options: method(:api_association_options)
475
+ )
404
476
  else
405
477
  response_object
406
478
  end
@@ -422,7 +494,7 @@ module ActiveScaffold::Actions
422
494
  options = main_path_to_return
423
495
  # use url_for in case main_path_to_return returns Hash with status param,
424
496
  # which would be interpreted as status option to redirect_to instead of url param
425
- redirect_to options.is_a?(Hash) ? url_for(options) : options
497
+ redirect_to options.is_a?(Hash) ? url_for(signed_url_options(options)) : options
426
498
  end
427
499
 
428
500
  def filtered_query
@@ -434,6 +506,12 @@ module ActiveScaffold::Actions
434
506
  active_scaffold_config.model
435
507
  end
436
508
 
509
+ # Override to scope existing records that can be selected for an association.
510
+ # The column belongs to the parent scaffold, which may use a different model.
511
+ def associated_record_scope(klass, column: nil)
512
+ klass
513
+ end
514
+
437
515
  # Builds search conditions from query parameters whose names match columns.
438
516
  # For example, `contacts/list?company_id=5` matches records with company_id 5.
439
517
  # Append `!` to a column name to negate the condition, as in `company_id!=5`.
@@ -443,11 +521,19 @@ module ActiveScaffold::Actions
443
521
  # Array parameters use the `column[]=value` syntax, for example `company_id[]=5&company_id[]=10`.
444
522
  # Negation may also be combined with ranges or arrays, using `not_between` or `not_in`.
445
523
  def conditions_from_params
524
+ return [{}] if active_scaffold_config.conditions_from_params_allowed_columns&.empty? # rubocop:disable Lint/SafeNavigationWithEmpty -- nil allows all columns
525
+
446
526
  @conditions_from_params ||= begin
447
527
  conditions = [{}]
528
+ allowed_columns = active_scaffold_config.conditions_from_params_allowed_columns&.map(&:to_s)
529
+ excluded_columns = active_scaffold_config.conditions_from_params_excluded_columns.map(&:to_s)
448
530
  params.except(:controller, :action, :page, :sort, :sort_direction, :format, :id).each do |key, value|
449
531
  distinct = key.match?(/!$/)
450
- column = active_scaffold_config._columns_hash[key.to_s[0..(distinct ? -2 : -1)]]
532
+ column_name = key.to_s[0..(distinct ? -2 : -1)]
533
+ next if allowed_columns&.exclude?(column_name)
534
+ next if excluded_columns.include?(column_name)
535
+
536
+ column = active_scaffold_config._columns_hash[column_name]
451
537
  next unless column
452
538
 
453
539
  key = column.name.to_sym
@@ -550,7 +636,21 @@ module ActiveScaffold::Actions
550
636
  end
551
637
 
552
638
  def active_scaffold_embedded_params
553
- params[:embedded] || {}
639
+ @active_scaffold_embedded_params ||= begin
640
+ embedded = params[:embedded]
641
+ if embedded.nil?
642
+ {}
643
+ else
644
+ raise ActionController::BadRequest, 'Invalid embedded parameters' unless embedded.is_a?(ActionController::Parameters)
645
+
646
+ payload = ActiveScaffold::ParameterSigning.embedded_payload(embedded, controller: controller_path)
647
+ unless ActiveScaffold::ParameterSigning.valid_signature?(payload, params[:eid], purpose: :embedded)
648
+ raise ActionController::BadRequest, 'Invalid embedded signature'
649
+ end
650
+
651
+ embedded
652
+ end
653
+ end
554
654
  end
555
655
 
556
656
  def clear_storage
@@ -41,10 +41,17 @@ module ActiveScaffold::Actions
41
41
  end
42
42
 
43
43
  def grouped_search?
44
- field_search_params.present? && field_search_params['active_scaffold_group'].present?
44
+ group = field_search_params['active_scaffold_group']
45
+ return false if group.blank?
46
+
47
+ Array(active_scaffold_config.field_search.group_options).any? do |text, value|
48
+ (value || text).to_s == group.to_s
49
+ end
45
50
  end
46
51
 
47
52
  def setup_search_group
53
+ return unless grouped_search?
54
+
48
55
  @_search_group_name, @_search_group_function = field_search_params['active_scaffold_group'].to_s.split('#')
49
56
  end
50
57
 
@@ -251,7 +251,9 @@ module ActiveScaffold::Actions::Nested
251
251
  # The actual "add_existing" algorithm
252
252
  def do_add_existing
253
253
  parent_record = nested_parent_record(:update)
254
- @record = active_scaffold_config.model.find(params[:associated_id])
254
+ column = nested.parent_scaffold.active_scaffold_config.columns[nested.association.name]
255
+ scope = associated_record_scope(active_scaffold_config.model, column: column)
256
+ @record = find_if_allowed(params[:associated_id], :read, scope)
255
257
  return false unless parent_record && @record
256
258
 
257
259
  association = parent_record.send(nested.association.name)
@@ -21,28 +21,75 @@ module ActiveScaffold::Actions
21
21
  end
22
22
 
23
23
  def do_edit_associated
24
+ verify_tab_selection_parameters
24
25
  @scope = params[:scope]
25
26
  @parent_record = params[:id].nil? ? new_parent_record : find_if_allowed(params[:id], :update)
26
27
  @column = active_scaffold_config.columns[params[:child_association]]
27
28
 
28
29
  @record = (find_associated_record if params[:associated_id]) ||
29
- build_associated(@column.association, @parent_record) do |blank_record|
30
- if params[:tabbed_by] && params[:value]
31
- @tab_id = params.delete(:value)
32
- assign_tabbed_by(blank_record, @column, params.delete(:tabbed_by), @tab_id, params.delete(:value_type))
33
- end
34
- end
30
+ build_associated(@column.association, @parent_record)
31
+ if params[:tabbed_by] && params[:value]
32
+ @tab_id = params.delete(:value)
33
+ assign_tabbed_by(@record, @column, params.delete(:tabbed_by), @tab_id, params.delete(:value_type))
34
+ end
35
+ @record
36
+ end
37
+
38
+ def verify_tab_selection_parameters
39
+ return unless params.key?(:tabbed_by) || params.key?(:value_type) || params.key?(:tab_signature)
40
+
41
+ payload = ActiveScaffold::ParameterSigning.tab_selection_payload(params, controller: controller_path)
42
+ unless params[:tabbed_by].is_a?(String) && params[:value].is_a?(String) &&
43
+ ActiveScaffold::ParameterSigning.valid_signature?(payload, params[:tab_signature], purpose: :tab_selection)
44
+ raise ::ActionController::BadRequest, 'Invalid tab selection signature'
45
+ end
46
+
47
+ params.delete(:tab_signature)
35
48
  end
36
49
 
37
50
  def assign_tabbed_by(record, parent_column, tabbed_by, value, value_type)
38
51
  if (association = tabbed_by_association(parent_column, tabbed_by))
39
- klass = value_type&.constantize || association.klass
52
+ klass =
53
+ if association.polymorphic?
54
+ raise ::ActionController::BadRequest if value_type.blank?
55
+
56
+ value_type.constantize
57
+ else
58
+ association.klass
59
+ end
40
60
  end
41
- record.send :"#{tabbed_by}=", klass&.find(value) || value
61
+ value =
62
+ if klass
63
+ klass.find(value)
64
+ else
65
+ field = ActiveScaffold::OrmChecks.columns_hash(record.class)[tabbed_by.to_s]
66
+ ActiveScaffold::Core.column_type_cast(value, field, record.class)
67
+ end
68
+ assign_tabbed_value(record, association, tabbed_by, value)
69
+ end
70
+
71
+ def assign_tabbed_value(record, association, tabbed_by, value)
72
+ return if record.public_send(tabbed_by) == value
73
+
74
+ if record.persisted?
75
+ unless record.authorized_for?(crud_type: :update, column: tabbed_by.to_sym)
76
+ raise ActiveScaffold::RecordNotAllowed, 'Not authorized to change the tab grouping'
77
+ end
78
+
79
+ if association && association.macro != :belongs_to
80
+ # Association setters could save records immediately. Only stage
81
+ # their form values until the parent form is submitted.
82
+ record.association(association.name).target = association.collection? ? Array(value) : value
83
+ return
84
+ end
85
+ end
86
+
87
+ record.public_send :"#{tabbed_by}=", value
42
88
  end
43
89
 
44
90
  def find_associated_record
45
- @column.association.klass.find(params[:associated_id]).tap do |record|
91
+ scope = associated_record_scope(@column.association.klass, column: @column)
92
+ find_if_allowed(params[:associated_id], :read, scope).tap do |record|
46
93
  save_record_to_association(record, @column.association.reverse_association, @parent_record, @column.association)
47
94
  end
48
95
  end
@@ -23,6 +23,7 @@ module ActiveScaffold::Actions
23
23
 
24
24
  # for inline (inlist) editing
25
25
  def update_column
26
+ @column = inplace_edit_column(params[:column])
26
27
  do_update_column
27
28
  @column_span_id = params.delete(:editor_id) || params.delete(:editorId)
28
29
  end
@@ -153,10 +154,9 @@ module ActiveScaffold::Actions
153
154
  def do_update_column
154
155
  # delete from params so update :table won't break urls, also they shouldn't be used in sort links too
155
156
  value = params.delete(:value)
156
- column = params.delete(:column)
157
+ params.delete(:column)
157
158
  params.delete(:original_html)
158
159
  params.delete(:original_value)
159
- @column = active_scaffold_config.columns[column]
160
160
 
161
161
  value_record = record_for_update_column
162
162
  value = value_for_update_column(value, @column, value_record)
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveScaffold
4
+ class ApiAssociationSerializer
5
+ def initialize(options = nil)
6
+ @options = options
7
+ end
8
+
9
+ def serialize(value, column)
10
+ if column.association.collection?
11
+ value.filter_map { |record| serialize_record(record, column) }
12
+ else
13
+ serialize_record(value, column)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def serialize_record(record, column)
20
+ return unless record
21
+ return if record.respond_to?(:authorized_for?) && !record.authorized_for?(crud_type: :read)
22
+
23
+ options = @options ? @options.call(column, record) : {only: [record.class.primary_key], methods: [:to_label]}
24
+ record.serializable_hash(options)
25
+ end
26
+ end
27
+ end
@@ -43,6 +43,22 @@ module ActiveScaffold::Config
43
43
  cattr_accessor :store_user_settings, instance_accessor: false
44
44
  @@store_user_settings = true
45
45
 
46
+ # nil preserves legacy association exports with a warning; false enables safe exports.
47
+ cattr_accessor :unsafe_api_associations, instance_accessor: false
48
+ @@unsafe_api_associations = nil
49
+
50
+ # response formats enabled by default, before additional core and action formats
51
+ cattr_accessor :default_formats, instance_accessor: false
52
+ @@default_formats = %i[html js json xml]
53
+
54
+ # nil allows filtering on all database columns; an empty array disables it
55
+ cattr_accessor :conditions_from_params_allowed_columns, instance_accessor: false
56
+ @@conditions_from_params_allowed_columns = nil
57
+
58
+ # columns excluded from parameter filtering, even when explicitly allowed
59
+ cattr_accessor :conditions_from_params_excluded_columns, instance_accessor: false
60
+ @@conditions_from_params_excluded_columns = []
61
+
46
62
  # action links are used by actions to tie together. you can use them, too! this is a collection of ActiveScaffold::DataStructures::ActionLink objects.
47
63
  cattr_reader :action_links, instance_reader: false
48
64
  @@action_links = ActiveScaffold::DataStructures::ActionLinks.new
@@ -138,6 +154,11 @@ module ActiveScaffold::Config
138
154
  # enable saving user settings in session (per_page, limit, page, sort, search params)
139
155
  attr_accessor :store_user_settings
140
156
 
157
+ # override the default response formats for a specific controller
158
+ attr_accessor :default_formats
159
+
160
+ attr_accessor :unsafe_api_associations, :conditions_from_params_allowed_columns, :conditions_from_params_excluded_columns
161
+
141
162
  # lets you specify whether add a create link for each sti child for a specific controller
142
163
  attr_accessor :sti_create_links
143
164
 
@@ -197,6 +218,10 @@ module ActiveScaffold::Config
197
218
  @cache_association_options = self.class.cache_association_options
198
219
  @conditional_get_support = self.class.conditional_get_support
199
220
  @store_user_settings = self.class.store_user_settings
221
+ @default_formats = self.class.default_formats.dup
222
+ @unsafe_api_associations = self.class.unsafe_api_associations
223
+ @conditions_from_params_allowed_columns = self.class.conditions_from_params_allowed_columns&.dup
224
+ @conditions_from_params_excluded_columns = self.class.conditions_from_params_excluded_columns.dup
200
225
  @sti_create_links = self.class.sti_create_links
201
226
 
202
227
  # inherit from the global set of action links
@@ -135,24 +135,19 @@ module ActiveScaffold # :nodoc:
135
135
  end
136
136
 
137
137
  def render_embedded(options)
138
- require 'digest/md5'
139
-
140
138
  remote_controller = options[:active_scaffold]
141
- # It is important that the EID hash remains short as to not contribute
142
- # to a large session size and thus a possible cookie overflow exception
143
- # when using rails CookieStore or EncryptedCookieStore. For example,
144
- # when rendering many embedded scaffolds with constraints or conditions
145
- # on a single page.
146
- eid = Digest::MD5.hexdigest(params[:controller] + options.to_s)
147
139
  eid_info = {loading: true}
148
140
  eid_info[:constraints] = options[:constraints] if options[:constraints]
149
141
  eid_info[:conditions] = options[:conditions] if options[:conditions]
150
142
  eid_info[:label] = options[:label] if options[:label]
151
143
  options[:params] ||= {}
144
+ url_options = {controller: remote_controller.to_s, action: 'index', id: nil}.merge(options[:params])
145
+ url_options[:embedded] = eid_info
146
+ url_options = signed_url_options(url_options)
147
+ eid = url_options[:eid]
152
148
  options[:params].merge! eid: eid, embedded: eid_info
153
149
 
154
150
  id = "as_#{eid}-embedded"
155
- url_options = {controller: remote_controller.to_s, action: 'index', id: nil}.merge(options[:params])
156
151
 
157
152
  if controller.respond_to?(:render_component_into_view, true) && !options[:xhr]
158
153
  controller.send(:render_component_into_view, url_options)
@@ -442,16 +442,24 @@ module ActiveScaffold
442
442
  Time.zone.now
443
443
  end
444
444
 
445
+ # Override to allow additional normalized duration units offered by the trend dropdown.
446
+ def valid_datetime_trend_unit?(unit)
447
+ DATE_UNITS.include?(unit.upcase) || TIME_UNITS.include?(unit.upcase)
448
+ end
449
+
445
450
  def datetime_from_to_for_trend(column, value)
451
+ unit = value['unit'].to_s.downcase.pluralize
452
+ return [nil, nil] unless valid_datetime_trend_unit?(unit)
453
+
446
454
  case value['opt']
447
455
  when 'PAST'
448
456
  trend_number = [value['number'].to_i, 1].max
449
457
  now = datetime_now
450
458
  if datetime_column_date?(column)
451
- from = now.beginning_of_day.ago(trend_number.send(value['unit'].downcase.singularize.to_sym))
459
+ from = now.beginning_of_day.ago(trend_number.public_send(unit))
452
460
  to = now.end_of_day
453
461
  else
454
- from = now.ago(trend_number.send(value['unit'].downcase.singularize.to_sym))
462
+ from = now.ago(trend_number.public_send(unit))
455
463
  to = now
456
464
  end
457
465
  [from, to]
@@ -460,10 +468,10 @@ module ActiveScaffold
460
468
  now = datetime_now
461
469
  if datetime_column_date?(column)
462
470
  from = now.beginning_of_day
463
- to = now.end_of_day.in(trend_number.send(value['unit'].downcase.singularize.to_sym))
471
+ to = now.end_of_day.in(trend_number.public_send(unit))
464
472
  else
465
473
  from = now
466
- to = now.in(trend_number.send(value['unit'].downcase.singularize.to_sym))
474
+ to = now.in(trend_number.public_send(unit))
467
475
  end
468
476
  [from, to]
469
477
  end
@@ -276,10 +276,10 @@ module ActiveScaffold
276
276
  @action_links_urls[link.name_to_cache.to_s] || begin
277
277
  url_options = cached_action_link_url_options(link, record)
278
278
  if cache_action_link_url?(link, record)
279
- @action_links_urls[link.name_to_cache.to_s] = url_for(url_options)
279
+ @action_links_urls[link.name_to_cache.to_s] = url_for(signed_url_options(url_options))
280
280
  else
281
281
  url_options.merge! eid: nil, embedded: nil if link.nested_link?
282
- url_for(params_for(url_options))
282
+ url_for(signed_url_options(params_for(url_options)))
283
283
  end
284
284
  end
285
285
  end
@@ -302,11 +302,17 @@ module ActiveScaffold
302
302
 
303
303
  def add_query_string_to_cached_url(link, url)
304
304
  query_string, non_nested_query_string = query_string_for_action_links(link)
305
- nested_params = !link.nested_link? && non_nested_query_string
306
- if query_string || nested_params
305
+ parts = [query_string]
306
+ parts << non_nested_query_string&.delete_prefix('&') unless link.nested_link?
307
+ query = parts.compact.join('&')
308
+ # Explicit options from an application override take precedence over
309
+ # return destinations propagated from the current request.
310
+ if url.match?(/[?&]return_to=/)
311
+ query = query.split('&').reject { |part| part.start_with?('return_to=', 'return_to_signature=') }.join('&')
312
+ end
313
+ if query.present?
307
314
  url << (url.include?('?') ? '&' : '?')
308
- url << query_string if query_string
309
- url << non_nested_query_string if nested_params
315
+ url << query
310
316
  end
311
317
  url
312
318
  end
@@ -362,9 +368,15 @@ module ActiveScaffold
362
368
  if nested_singular_association? && action_name == 'index'
363
369
  # pass the current path as return_to, for nested listing on singular association, so forms don't return to parent listing
364
370
  @query_string_params << :return_to
371
+ @query_string_params << :return_to_signature
372
+ query_string_options.except!(:return_to, :return_to_signature)
365
373
  non_nested_query_string_options[:return_to] = request.fullpath
374
+ non_nested_query_string_options = signed_url_options(non_nested_query_string_options)
366
375
  end
367
376
 
377
+ query_string_options.delete(:return_to_signature) unless query_string_options[:return_to]
378
+ non_nested_query_string_options.delete(:return_to_signature) unless non_nested_query_string_options[:return_to]
379
+
368
380
  query_string = query_string_options.to_query if query_string_options.present?
369
381
  if non_nested_query_string_options.present?
370
382
  non_nested_query_string = "#{'&' if query_string}#{non_nested_query_string_options.to_query}"
@@ -128,6 +128,7 @@ module ActiveScaffold
128
128
  end
129
129
 
130
130
  def record_select_params_for_add_existing(association, edit_associated_url_options, record)
131
+ edit_associated_url_options = signed_tab_selection_options(signed_url_options(edit_associated_url_options))
131
132
  if ActiveScaffold::Bridges::RecordSelect.callback_registry?
132
133
  {
133
134
  onselect: 'active_scaffold.add_existing',
@@ -12,7 +12,9 @@ module ActiveScaffold
12
12
  :render_parent_action, :nested_singular_association?,
13
13
  :main_form_controller, :build_associated,
14
14
  :generate_temporary_id, :generated_id,
15
- :active_scaffold_config_for, :tabbed_by_association
15
+ :active_scaffold_config_for, :tabbed_by_association,
16
+ :signed_embedded_options, :signed_return_to_options, :signed_url_options,
17
+ :signed_tab_selection_options
16
18
  end
17
19
  end
18
20
 
@@ -23,6 +25,62 @@ module ActiveScaffold
23
25
  config.user || config.new_user_settings({}, {})
24
26
  end
25
27
 
28
+ # Sign only embedded options supplied by application code, before URL generation.
29
+ def signed_embedded_options(options)
30
+ if options.key?('embedded') || (options[:embedded] && options.key?('controller'))
31
+ options = options.symbolize_keys
32
+ end
33
+ return options unless options[:embedded]
34
+
35
+ target = options[:controller] ? controller_requested(options[:controller]) : controller_path
36
+ payload = ActiveScaffold::ParameterSigning.embedded_payload(options[:embedded], controller: target)
37
+ options.merge(eid: ActiveScaffold::ParameterSigning.signature(payload, purpose: :embedded))
38
+ end
39
+
40
+ def signed_url_options(options)
41
+ return options unless options.is_a?(Hash)
42
+
43
+ signed_return_to_options(signed_embedded_options(options))
44
+ end
45
+
46
+ # Only sign tab selections constructed by application code, not parameters
47
+ # inherited from the current request through params_for.
48
+ def signed_tab_selection_options(options)
49
+ options = options.symbolize_keys if options.key?('tabbed_by') || options.key?('controller')
50
+ return options unless options[:tabbed_by]
51
+
52
+ target = options[:controller] ? controller_requested(options[:controller]) : controller_path
53
+ payload = ActiveScaffold::ParameterSigning.tab_selection_payload(options, controller: target)
54
+ options.merge(tab_signature: ActiveScaffold::ParameterSigning.signature(payload, purpose: :tab_selection))
55
+ end
56
+
57
+ # Call after overridable URL-option builders, immediately before generating a URL.
58
+ def signed_return_to_options(options)
59
+ return options unless options.is_a?(Hash)
60
+
61
+ options = options.symbolize_keys if options.key?('return_to') || options.key?('return_to_signature')
62
+ if options[:return_to]
63
+ unless options[:return_to].is_a?(String)
64
+ raise ArgumentError, 'return_to must be a URL string'
65
+ end
66
+
67
+ options.merge(return_to_signature: ActiveScaffold::ParameterSigning.signature(options[:return_to], purpose: :return_to))
68
+ elsif options.key?(:return_to_signature)
69
+ options.except(:return_to_signature)
70
+ else
71
+ options
72
+ end
73
+ end
74
+
75
+ def verify_return_to_parameter
76
+ return if params[:return_to].nil?
77
+
78
+ unless params[:return_to].is_a?(String) &&
79
+ ActiveScaffold::ParameterSigning.valid_signature?(params[:return_to], params[:return_to_signature], purpose: :return_to)
80
+ raise ::ActionController::BadRequest, 'Invalid return_to signature'
81
+ end
82
+ end
83
+
26
84
  def generate_temporary_id(record = nil, generated_id = nil)
27
85
  unless generated_id
28
86
  temp_id = (Time.now.to_f * 1_000_000).to_i.to_s
@@ -61,6 +119,8 @@ module ActiveScaffold
61
119
  iframe associated_id authenticity_token _added _removed _popstate _].freeze
62
120
 
63
121
  def params_for(options = {})
122
+ verify_return_to_parameter
123
+ active_scaffold_embedded_params if params[:embedded]
64
124
  unless @params_for
65
125
  @params_for = {}
66
126
  params.except(*BLACKLIST_PARAMS).each do |key, value|
@@ -80,6 +140,7 @@ module ActiveScaffold
80
140
  url_options[:sort_direction] ||= direction
81
141
  end
82
142
  end
143
+ url_options.delete(:return_to_signature) unless url_options[:return_to]
83
144
  url_options
84
145
  end
85
146
 
@@ -103,6 +164,7 @@ module ActiveScaffold
103
164
 
104
165
  # Parameters to generate url to the main page (override if the ActiveScaffold is used as a component on another controllers page)
105
166
  def main_path_to_return
167
+ verify_return_to_parameter
106
168
  if params[:return_to]
107
169
  params[:return_to]
108
170
  else
@@ -185,7 +185,7 @@ module ActiveScaffold
185
185
  if force || (form_columns && update_columns&.intersect?(form_columns))
186
186
  url_params = url_options_for_render_field(column, record, scope, subform_controller, url_params)
187
187
  options[:class] = "#{options[:class]} update_form".strip
188
- options['data-update_url'] = url_for(url_params)
188
+ options['data-update_url'] = url_for(signed_url_options(url_params))
189
189
  options['data-update_send_form'] = column.update_columns&.any?(Hash) || column.send_form_on_update_column
190
190
  options['data-update_send_form_selector'] = column.options[:send_form_selector] if column.options[:send_form_selector]
191
191
  options['data-skip-disable-form'] = !column.disable_on_update_column
@@ -106,6 +106,7 @@ module ActiveScaffold
106
106
  url_options[:parent_model] ||= record.class.name
107
107
  url_options[:parent_column] ||= column.name
108
108
  url_options.reverse_merge! options[:url_options] if options[:url_options]
109
+ url_options = signed_url_options(url_options)
109
110
  link_to(link_text, url_options, as_element_attributes(:field_add_new_popup, remote: true, data: {position: :popup}, class: 'as_action'))
110
111
  end
111
112
 
@@ -407,7 +407,7 @@ module ActiveScaffold
407
407
  INPLACE_EDIT_PLURAL_FORM_UI = %i[select record_select].freeze
408
408
  def inplace_edit_data(column)
409
409
  data = {}
410
- data[:ie_url] = url_for(params_for(action: 'update_column', column: column.name, id: '__id__'))
410
+ data[:ie_url] = url_for(signed_url_options(params_for(action: 'update_column', column: column.name, id: '__id__')))
411
411
  data[:ie_cancel_text] = column.options[:cancel_text] || as_(:cancel)
412
412
  data[:ie_loading_text] = column.options[:loading_text] || as_(:loading)
413
413
  data[:ie_save_text] = column.options[:save_text] || as_(:update)
@@ -422,7 +422,7 @@ module ActiveScaffold
422
422
  elsif inplace_edit_cloning?(column)
423
423
  data[:ie_mode] = :clone
424
424
  elsif column.inplace_edit == :ajax
425
- url = url_for(params_for(controller: params_for[:controller], action: 'render_field', id: '__id__', update_column: column.name))
425
+ url = url_for(signed_url_options(params_for(controller: params_for[:controller], action: 'render_field', id: '__id__', update_column: column.name)))
426
426
  plural = column.association&.collection? && !override_form_field?(column) && INPLACE_EDIT_PLURAL_FORM_UI.include?(column.form_ui)
427
427
  data[:ie_render_url] = url
428
428
  data[:ie_mode] = :ajax
@@ -478,7 +478,7 @@ module ActiveScaffold
478
478
  if column.name == :as_marked
479
479
  tag_options[:data] = {
480
480
  ie_mode: :inline_checkbox,
481
- ie_url: url_for(params_for(action: 'mark', id: '__id__'))
481
+ ie_url: url_for(signed_url_options(params_for(action: 'mark', id: '__id__')))
482
482
  }
483
483
  elsif column.inplace_edit
484
484
  tag_options[:data] = inplace_edit_data(column)
@@ -501,7 +501,7 @@ module ActiveScaffold
501
501
  if !active_scaffold_config.store_user_settings && respond_to?(:search_params) && search_params.present?
502
502
  url_options[:search] = search_params
503
503
  end
504
- link_to column_heading_label(column), url_options, options
504
+ link_to column_heading_label(column), signed_url_options(url_options), options
505
505
  else
506
506
  content_tag(:p, column_heading_label(column))
507
507
  end
@@ -9,7 +9,7 @@ module ActiveScaffold
9
9
  proc_options: {page_number: page_number, text: text},
10
10
  **options.smart_merge(class: 'as_paginate')
11
11
  )
12
- link_to text ? as_(text) : page_number, url_options.merge(page: page_number), attributes
12
+ link_to text ? as_(text) : page_number, signed_url_options(url_options.merge(page: page_number)), attributes
13
13
  end
14
14
 
15
15
  def pagination_url_options(url_options = nil)
@@ -12,7 +12,7 @@ module ActiveScaffold
12
12
  input_helper = override_helper_per_model(:active_scaffold_input_for_tabbed, record.class)
13
13
  safe_join([
14
14
  send(input_helper, column, record, subsection_id, tab_options, used_tabs.map { |v, _| v }),
15
- active_scaffold_refresh_link(nil, {'data-update_url' => url_for(add_tab_url)}, record, refresh_opts),
15
+ active_scaffold_refresh_link(nil, {'data-update_url' => url_for(signed_url_options(add_tab_url))}, record, refresh_opts),
16
16
  active_scaffold_tabs_for(column, record, subsection_id, tab_options, used_tabs, &)
17
17
  ])
18
18
  end
@@ -77,7 +77,7 @@ module ActiveScaffold
77
77
  options[:multipart] ||= true
78
78
  options[:class] = "#{options[:class]} as_remote_upload".strip
79
79
  output = []
80
- output << form_tag(url_for_options, options)
80
+ output << form_tag(signed_url_options(url_for_options), options)
81
81
  output << content_tag(:iframe, '', id: action_iframe_id(url_for_options), name: action_iframe_id(url_for_options), style: 'display:none')
82
82
  safe_join output
83
83
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'openssl'
5
+
6
+ module ActiveScaffold
7
+ module ParameterSigning
8
+ TAB_SELECTION_PARAMETERS = %i[tabbed_by value value_type tab_signature].freeze
9
+ TAB_SELECTION_FIELDS = %i[id generated_id child_association scope parent_controller eid tabbed_by value value_type].freeze
10
+
11
+ def self.signature(data, purpose:)
12
+ key = Rails.application.key_generator.generate_key("active_scaffold/#{purpose}/v1", 32)
13
+ Base64.urlsafe_encode64(OpenSSL::HMAC.digest('SHA256', key, data), padding: false)
14
+ end
15
+
16
+ def self.valid_signature?(data, signature, purpose:)
17
+ return false unless signature.is_a?(String) && signature.bytesize == 43
18
+
19
+ ActiveSupport::SecurityUtils.secure_compare(self.signature(data, purpose: purpose), signature)
20
+ end
21
+
22
+ def self.embedded_payload(embedded, controller:)
23
+ embedded = embedded.to_unsafe_h if embedded.respond_to?(:to_unsafe_h)
24
+ # to_query normalizes values exactly as URL generation does. Loading is a
25
+ # one-use rendering flag, removed after the initial embedded request.
26
+ [controller.to_s.delete_prefix('/'), embedded.except(:loading, 'loading').to_query].to_json
27
+ end
28
+
29
+ def self.tab_selection_payload(options, controller:)
30
+ fields = TAB_SELECTION_FIELDS.index_with { |key| options[key] || options[key.to_s] }
31
+ [controller.to_s.delete_prefix('/'), fields.to_query].to_json
32
+ end
33
+ end
34
+ end
@@ -4,7 +4,7 @@ module ActiveScaffold
4
4
  module Version
5
5
  MAJOR = 4
6
6
  MINOR = 4
7
- PATCH = 7
7
+ PATCH = 8
8
8
  FIX = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, FIX].compact.join('.')
@@ -14,6 +14,7 @@ module ActiveScaffold
14
14
  autoload :Finder, 'active_scaffold/finder'
15
15
  autoload :MarkedModel, 'active_scaffold/marked_model'
16
16
  autoload :OrmChecks, 'active_scaffold/orm_checks'
17
+ autoload :ParameterSigning, 'active_scaffold/parameter_signing'
17
18
  autoload :Registry, 'active_scaffold/registry'
18
19
  autoload :RespondsToParent, 'active_scaffold/responds_to_parent'
19
20
  autoload :Routing, 'active_scaffold/extensions/routing_mapper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.7
4
+ version: 4.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Many, see README
@@ -223,6 +223,7 @@ files:
223
223
  - lib/active_scaffold/actions/subform.rb
224
224
  - lib/active_scaffold/actions/update.rb
225
225
  - lib/active_scaffold/active_record_permissions.rb
226
+ - lib/active_scaffold/api_association_serializer.rb
226
227
  - lib/active_scaffold/assets.rb
227
228
  - lib/active_scaffold/assets/css_deps_generator.rb
228
229
  - lib/active_scaffold/assets/jquery_ui_manifest.rb
@@ -357,6 +358,7 @@ files:
357
358
  - lib/active_scaffold/marked_model.rb
358
359
  - lib/active_scaffold/orm_checks.rb
359
360
  - lib/active_scaffold/paginator.rb
361
+ - lib/active_scaffold/parameter_signing.rb
360
362
  - lib/active_scaffold/railties/tasks.rake
361
363
  - lib/active_scaffold/registry.rb
362
364
  - lib/active_scaffold/responds_to_parent.rb