decidim-dev 0.32.2 → 0.32.3

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 (81) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/decidim/dev/dummy_resources_controller.rb +1 -1
  3. data/app/packs/stylesheets/decidim/dev/_rack_profiler.scss +10 -0
  4. data/config/rubocop/decidim-linters/configuration.yml +2 -1
  5. data/decidim-dev.gemspec +1 -1
  6. data/lib/decidim/dev/admin_engine.rb +3 -0
  7. data/lib/decidim/dev/assets/malicious_svg_alt_glyph_external_reference.svg +1 -0
  8. data/lib/decidim/dev/assets/malicious_svg_animate_color_external_reference.svg +1 -0
  9. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_event_handler.svg +1 -0
  10. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_list.svg +1 -0
  11. data/lib/decidim/dev/assets/malicious_svg_animation_color_profile.svg +1 -0
  12. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri.svg +1 -0
  13. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri_parameter.svg +1 -0
  14. data/lib/decidim/dev/assets/malicious_svg_animation_gradient_reference.svg +1 -0
  15. data/lib/decidim/dev/assets/malicious_svg_base.svg +1 -0
  16. data/lib/decidim/dev/assets/malicious_svg_base_child_element.svg +1 -0
  17. data/lib/decidim/dev/assets/malicious_svg_base_protocol_relative.svg +1 -0
  18. data/lib/decidim/dev/assets/malicious_svg_clip_path_external_reference.svg +1 -0
  19. data/lib/decidim/dev/assets/malicious_svg_color_profile_element_external_reference.svg +1 -0
  20. data/lib/decidim/dev/assets/malicious_svg_color_profile_external_reference.svg +1 -0
  21. data/lib/decidim/dev/assets/malicious_svg_comment_first.svg +1 -0
  22. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_external_reference.svg +1 -0
  23. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_single_quote_external_reference.svg +1 -0
  24. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_external_reference.svg +1 -0
  25. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_single_quote_external_reference.svg +1 -0
  26. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_external_reference.svg +1 -0
  27. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_single_quote_external_reference.svg +1 -0
  28. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_external_reference.svg +2 -0
  29. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_single_quote_external_reference.svg +2 -0
  30. data/lib/decidim/dev/assets/malicious_svg_css_image_function.svg +1 -0
  31. data/lib/decidim/dev/assets/malicious_svg_css_image_set_escaped_quote_external_reference.svg +1 -0
  32. data/lib/decidim/dev/assets/malicious_svg_css_image_set_nested_url_external_reference.svg +1 -0
  33. data/lib/decidim/dev/assets/malicious_svg_css_image_set_quote_parentheses_external_reference.svg +1 -0
  34. data/lib/decidim/dev/assets/malicious_svg_css_string_newline_external_reference.svg +2 -0
  35. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_external_reference.svg +1 -0
  36. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_script.svg +1 -0
  37. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_not_last_parameter.svg +1 -0
  38. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_parameter_lookalike.svg +1 -0
  39. data/lib/decidim/dev/assets/malicious_svg_data_uri_fragment.svg +1 -0
  40. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_base64.svg +1 -0
  41. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_document.svg +1 -0
  42. data/lib/decidim/dev/assets/malicious_svg_data_uri_max_depth_exceeded.svg +1 -0
  43. data/lib/decidim/dev/assets/malicious_svg_data_uri_percent_decoded_base64.svg +5 -0
  44. data/lib/decidim/dev/assets/malicious_svg_filter_element_external_reference.svg +1 -0
  45. data/lib/decidim/dev/assets/malicious_svg_filter_external_reference.svg +1 -0
  46. data/lib/decidim/dev/assets/malicious_svg_font_face_uri_external_reference.svg +1 -0
  47. data/lib/decidim/dev/assets/malicious_svg_gradient_external_reference.svg +1 -0
  48. data/lib/decidim/dev/assets/malicious_svg_marker_start_external_reference.svg +1 -0
  49. data/lib/decidim/dev/assets/malicious_svg_mpath_external_reference.svg +1 -0
  50. data/lib/decidim/dev/assets/malicious_svg_pattern_external_reference.svg +1 -0
  51. data/lib/decidim/dev/assets/malicious_svg_protocol_relative_reference.svg +1 -0
  52. data/lib/decidim/dev/assets/malicious_svg_stroke_external_reference.svg +1 -0
  53. data/lib/decidim/dev/assets/malicious_svg_text_path_external_reference.svg +1 -0
  54. data/lib/decidim/dev/assets/malicious_svg_tref_external_reference.svg +1 -0
  55. data/lib/decidim/dev/assets/spoofed_svg_comment_first.svg +1 -0
  56. data/lib/decidim/dev/assets/test_animation_attribute_names_list.svg +7 -0
  57. data/lib/decidim/dev/assets/test_animation_data_uri_parameter.svg +7 -0
  58. data/lib/decidim/dev/assets/test_animation_percent_encoded_fragment.svg +7 -0
  59. data/lib/decidim/dev/assets/test_comment_first.svg +2 -0
  60. data/lib/decidim/dev/assets/test_css_image_set_nested_relative_url.svg +1 -0
  61. data/lib/decidim/dev/assets/test_css_unclosed_comment.svg +2 -0
  62. data/lib/decidim/dev/assets/test_css_url_opposite_quote.svg +12 -0
  63. data/lib/decidim/dev/assets/test_data_uri_fragment.svg +1 -0
  64. data/lib/decidim/dev/assets/test_data_uri_max_depth.svg +1 -0
  65. data/lib/decidim/dev/assets/test_data_uri_parameters.svg +1 -0
  66. data/lib/decidim/dev/assets/test_data_uri_percent_decoded_base64.svg +6 -0
  67. data/lib/decidim/dev/assets/test_doctype_name_on_next_line.svg +4 -0
  68. data/lib/decidim/dev/assets/test_link_data_document.svg +9 -0
  69. data/lib/decidim/dev/assets/test_link_non_web_scheme.svg +7 -0
  70. data/lib/decidim/dev/assets/test_long_comment_first.svg +2 -0
  71. data/lib/decidim/dev/assets/test_processing_instruction_first.svg +3 -0
  72. data/lib/decidim/dev/assets/test_reference_elements_resolved_in_document.svg +26 -0
  73. data/lib/decidim/dev/assets/test_relative_base.svg +2 -0
  74. data/lib/decidim/dev/assets/test_relative_image_function.svg +8 -0
  75. data/lib/decidim/dev/assets/test_root_relative_reference.svg +7 -0
  76. data/lib/decidim/dev/auth_engine.rb +3 -0
  77. data/lib/decidim/dev/engine.rb +3 -0
  78. data/lib/decidim/dev/rubocop/cop/decidim/organization_scoped_finder.rb +285 -24
  79. data/lib/decidim/dev/test/base_spec_helper.rb +4 -0
  80. data/lib/decidim/dev/version.rb +1 -1
  81. metadata +82 -13
@@ -5,7 +5,7 @@ require "rubocop"
5
5
  module RuboCop
6
6
  module Cop
7
7
  module Decidim
8
- # Flags unscoped ActiveRecord finder calls in controllers.
8
+ # Flags unscoped ActiveRecord finder calls in controllers and commands.
9
9
  #
10
10
  # In a multi-tenant Decidim deployment, loading a record without scoping
11
11
  # it to the current organization can allow one tenant's users to access
@@ -14,21 +14,65 @@ module RuboCop
14
14
  # scoped `collection`.
15
15
  #
16
16
  # The cop targets chains that start with an ActiveRecord model constant
17
- # and end with a terminal finder (`find`, `find_by`, `find_by!`, `first`,
18
- # `take`, etc.) unless the chain includes a recognized scoping step.
17
+ # and end with a terminal query method (`find`, `find_by`, `first`,
18
+ # `take`, `exists?`, `find_or_create_by`, `destroy_all`, etc.) unless the
19
+ # chain includes a recognized scoping step.
19
20
  class OrganizationScopedFinder < RuboCop::Cop::Base
20
21
  MSG = "Unscoped ActiveRecord finder detected. Scope the query to the current organization, " \
21
22
  "e.g. `current_organization.<relation>.find_by(id: params[:id])` or use an already-scoped `collection`."
22
23
 
24
+ # Terminal methods that execute a database query. This is the subset of
25
+ # `ActiveRecord::Querying::QUERYING_METHODS` that loads, creates,
26
+ # mutates, or deletes records, so an unscoped call can cross
27
+ # organization boundaries. Relation-building methods (`order`, `joins`,
28
+ # etc.) are not listed here because they do not run the query by
29
+ # themselves.
23
30
  FINDER_METHODS = [
24
31
  :find, :find_by, :find_by!, :find_sole_by, :first, :first!, :last, :last!,
25
32
  :sole, :sole!, :take, :take!,
26
- :find_or_initialize_by, :find_or_create_by, :find_or_create_by!
33
+ :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!,
34
+ :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!,
35
+ :exists?, :any?, :many?, :none?, :one?
27
36
  ].freeze
28
- RESTRICT_ON_SEND = [*FINDER_METHODS, :where].freeze
37
+
38
+ CREATION_FINDER_METHODS = [
39
+ :first_or_create, :first_or_create!, :first_or_initialize,
40
+ :find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
41
+ :create_or_find_by, :create_or_find_by!
42
+ ].freeze
43
+
44
+ MUTATION_METHODS = [
45
+ :destroy, :destroy_all, :delete, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by
46
+ ].freeze
47
+
48
+ # Query methods whose argument hash filters the records they read,
49
+ # create, or delete. Only these can establish organization scope.
50
+ # Methods such as `first_or_create` or `update_all` use their arguments
51
+ # as values to write, so they do not filter out records from other
52
+ # organizations.
53
+ FILTERING_ARGUMENT_METHODS = [
54
+ :where,
55
+ :find_by, :find_by!, :find_sole_by,
56
+ :exists?, :any?, :many?, :none?, :one?,
57
+ :find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
58
+ :create_or_find_by, :create_or_find_by!,
59
+ :destroy_by, :delete_by
60
+ ].freeze
61
+
62
+ QUERY_METHODS = [*FINDER_METHODS, *CREATION_FINDER_METHODS, *MUTATION_METHODS].freeze
63
+ RESTRICT_ON_SEND = [*QUERY_METHODS, :where, :or].freeze
64
+
65
+ RECORD_LOADING_METHODS = [:load, :load_async].freeze
29
66
 
30
67
  SCOPE_ROOT_METHODS = [:current_organization, :collection].freeze
31
68
 
69
+ # Creation finders that use their argument hash to look up the existing
70
+ # record, so an organization in that hash really scopes the lookup.
71
+ SCOPED_CREATION_FINDER_METHODS = [
72
+ :find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
73
+ :create_or_find_by, :create_or_find_by!
74
+ ].freeze
75
+
32
76
  SCOPED_WHERE_KEYS = [:current_organization, :current_component].freeze
33
77
 
34
78
  ORGANIZATION_SCOPED_KEYS = [:decidim_organization_id, :organization_id].freeze
@@ -37,30 +81,84 @@ module RuboCop
37
81
 
38
82
  SCOPED_HELPER_METHODS = [:current_organization, :current_component, :current_participatory_space].freeze
39
83
 
40
- IGNORED_CONSTANT_PREFIXES = %w(Bundler ActiveRecord).freeze
84
+ # Query objects are commonly built from a positional argument, e.g.
85
+ # `AssembliesWithUserRole.for(current_user)`. The current user is bound
86
+ # to the current organization, so it also establishes the tenant scope.
87
+ SCOPED_QUERY_ARGUMENT_METHODS = [*SCOPED_HELPER_METHODS, :current_user].freeze
88
+
89
+ # Only these methods build a query object from a positional argument.
90
+ # Other methods called on a model constant may receive the current user
91
+ # without scoping the query, as in
92
+ # `Decidim::User.excluding(current_user)`, which only removes that user
93
+ # and still returns users from other organizations.
94
+ SCOPED_QUERY_CONSTRUCTOR_METHODS = [:new, :for].freeze
95
+
96
+ # Framework and standard library constants expose query-like methods
97
+ # (`File.delete`, `Rails.cache.delete`, etc.) without being
98
+ # ActiveRecord models. Namespaced entries are matched as prefixes; the
99
+ # remaining ones are only ignored when they are the full constant name,
100
+ # so `Decidim` is ignored but `Decidim::Proposal` is not.
101
+ IGNORED_CONSTANT_PREFIXES = %w(
102
+ ActionController ActionDispatch ActionMailer ActionView ActiveModel ActiveSupport
103
+ ActiveRecord Arel Bundler Devise Rack Rails
104
+ ).freeze
105
+ IGNORED_CONSTANTS = %w(
106
+ CGI Date DateTime Decidim Dir Doorkeeper ENV File I18n JSON OpenSSL Pathname Process
107
+ SecureRandom Thread Time URI
108
+ ).freeze
109
+
110
+ # SCREAMING_SNAKE_CASE constants hold plain values (e.g.
111
+ # `OPTIONS = [25, 50, 100]`), not ActiveRecord model classes, which
112
+ # follow the CamelCase convention.
113
+ VALUE_CONSTANT_PATTERN = /\A[A-Z][A-Z0-9_]*\z/
41
114
 
42
115
  def on_send(node)
43
- return unless RESTRICT_ON_SEND.include?(node.method_name)
44
- return if node.block_node
45
- return if scoped_finder_arguments?(node)
116
+ return unless relevant_node?(node)
46
117
  return unless node.receiver
47
- return if organization_scoped?(node.receiver)
48
- return unless unscoped_model_class?(node.receiver)
49
- return if constructed_scope_root?(node.receiver)
50
118
 
51
- return if consumed_by_finder?(node)
119
+ # Check `or` relations before the model-constant gate: either branch
120
+ # may be an unscoped relation whose receiver is not a model constant
121
+ # (for example a delegated `taxonomies` relation).
122
+ return check_or_relation(node) if node.method_name == :or
123
+ return unless offense_candidate?(node)
124
+ return if consumed_by_outer_relation?(node)
52
125
 
53
126
  add_offense(node)
54
127
  end
55
128
 
56
129
  private
57
130
 
131
+ def relevant_node?(node)
132
+ RESTRICT_ON_SEND.include?(node.method_name) && !node.block_node && !scoped_finder_arguments?(node)
133
+ end
134
+
135
+ # Whether a relevant call is itself an unscoped lookup this cop should
136
+ # report, ignoring whether an outer call consumes it. `on_send` uses
137
+ # this for the offense, and `reportable_outer_relation?` uses it to
138
+ # decide if an outer call will actually produce a warning.
139
+ def offense_candidate?(node)
140
+ return false if organization_scoped?(node.receiver)
141
+ return false unless unscoped_model_class?(node.receiver) || unscoped_or_relation?(node.receiver)
142
+ return false if constructed_scope_root?(node.receiver)
143
+
144
+ true
145
+ end
146
+
147
+ def check_or_relation(node)
148
+ return if node.ancestors.any? { |ancestor| ancestor.send_type? && ancestor.method_name == :or }
149
+ return if scoped_or_branches?(node)
150
+ return if consumed_by_outer_relation?(node)
151
+
152
+ add_offense(node)
153
+ end
154
+
58
155
  def organization_scoped?(node)
59
156
  return false unless node
60
157
  return true if scoped_node?(node)
61
- return organization_scoped?(node.receiver) if node.send_type?
158
+ return false unless node.send_type?
159
+ return scoped_or_branches?(node) if node.method_name == :or
62
160
 
63
- false
161
+ organization_scoped?(node.receiver)
64
162
  end
65
163
 
66
164
  def unscoped_model_class?(node)
@@ -72,9 +170,29 @@ module RuboCop
72
170
  unscoped_model_class?(node.receiver)
73
171
  end
74
172
 
173
+ # An `or` relation whose branches are not all scoped can leak records
174
+ # from another organization even when its receiver chain has no model
175
+ # constant, as in `current_organization.taxonomies.or(Taxonomy.all)`.
176
+ # Walk the whole receiver chain so intermediate calls between the `or`
177
+ # and the terminal finder (e.g. `.order(:id)`) do not hide it.
178
+ # `organization_scoped?` has already rejected the chain at this point.
179
+ def unscoped_or_relation?(node)
180
+ return false unless node&.send_type?
181
+ return !scoped_or_branches?(node) if node.method_name == :or
182
+
183
+ unscoped_or_relation?(node.receiver)
184
+ end
185
+
75
186
  def ignored_constant?(node)
76
- node.const_type? &&
77
- IGNORED_CONSTANT_PREFIXES.any? { |prefix| node.source.start_with?(prefix) }
187
+ return false unless node.const_type?
188
+
189
+ IGNORED_CONSTANT_PREFIXES.any? { |prefix| node.source.start_with?(prefix) } ||
190
+ IGNORED_CONSTANTS.include?(node.source) ||
191
+ value_constant?(node)
192
+ end
193
+
194
+ def value_constant?(node)
195
+ VALUE_CONSTANT_PATTERN.match?(node.short_name.to_s)
78
196
  end
79
197
 
80
198
  def scoped_node?(node)
@@ -89,10 +207,21 @@ module RuboCop
89
207
  node.receiver.nil? || node.receiver.self_type?
90
208
  end
91
209
 
210
+ # Also recognizes helpers that filter by a scoped association, such as
211
+ # `MeetingLink.find_meetings(component: current_component)`.
212
+ # Known query methods that write their arguments instead of filtering
213
+ # with them (`first_or_create`, `update_all`, ...) cannot establish
214
+ # scope, even when the hash contains the current organization.
92
215
  def scoped_finder_arguments?(node)
216
+ return false if QUERY_METHODS.include?(node.method_name) && !filtering_argument_method?(node)
217
+
93
218
  node.arguments.any? { |arg| scoped_argument?(arg) }
94
219
  end
95
220
 
221
+ def filtering_argument_method?(node)
222
+ FILTERING_ARGUMENT_METHODS.any? { |method| method == node.method_name }
223
+ end
224
+
96
225
  def scoped_where?(node)
97
226
  return false unless node.method_name == :where
98
227
 
@@ -128,18 +257,66 @@ module RuboCop
128
257
 
129
258
  def scoped_where_pair?(key, value)
130
259
  return false unless key.sym_type?
260
+
261
+ return organization_value?(value) if key.value == :organization
131
262
  return false unless scoped_where_key?(key)
132
263
 
133
264
  scoped_trusted_value?(value)
134
265
  end
135
266
 
267
+ # `where(organization: organization)` and `where(organization: record.organization)`
268
+ # restrict the relation to a single organization only when the value is
269
+ # the current organization or a record's own organization. Reject
270
+ # parameterized lookups such as
271
+ # `tenant_resolver.organization(params[:organization_id])`, because the
272
+ # resolver may select another tenant from untrusted input.
273
+ def organization_value?(node)
274
+ # In `find_template(organization)`, Ruby parses the argument as a
275
+ # local variable rather than a send, but it is still the intended
276
+ # organization.
277
+ return true if organization_local_variable?(node)
278
+ return false unless node&.send_type? && node.method_name == :organization
279
+ return false unless node.arguments.empty?
280
+ return true unless node.receiver
281
+
282
+ trusted_organization_receiver?(node.receiver)
283
+ end
284
+
285
+ def organization_local_variable?(node)
286
+ node&.lvar_type? && node.name == :organization
287
+ end
288
+
289
+ # Records held in variables (`@user.organization`) and current-context
290
+ # helpers (`current_user.organization`, `form.organization`) are bound
291
+ # to the organization being queried. Arbitrary method calls are not,
292
+ # as they may resolve a tenant from request parameters.
293
+ def trusted_organization_receiver?(node)
294
+ return true if node.lvar_type? || node.ivar_type?
295
+ return false unless node.send_type?
296
+ return true if scoped_query_argument_method?(node)
297
+ return true if node.receiver&.self_type? && SCOPED_QUERY_ARGUMENT_METHODS.include?(node.method_name)
298
+
299
+ form_context_receiver?(node)
300
+ end
301
+
136
302
  def scoped_association_pair?(key, value)
137
303
  return false unless key.sym_type?
138
304
  return false unless SCOPED_ASSOCIATION_KEYS.include?(key.value)
139
305
  return false unless value.send_type?
140
- return false if value.receiver
306
+ return false unless value.method_name == :"current_#{key.value}"
307
+ return true unless value.receiver
308
+
309
+ # Accept form-provided context helpers such as
310
+ # `form.current_component` or `@form.current_organization`, but not
311
+ # arbitrary receivers that merely expose a similarly named method.
312
+ form_context_receiver?(value.receiver)
313
+ end
314
+
315
+ def form_context_receiver?(node)
316
+ return node.method_name == :form if node.send_type? && node.receiver.nil?
317
+ return node.name == :form if node.lvar_type?
141
318
 
142
- value.method_name == :"current_#{key.value}"
319
+ node.ivar_type? && node.name == :@form
143
320
  end
144
321
 
145
322
  def scoped_trusted_value?(node)
@@ -171,29 +348,113 @@ module RuboCop
171
348
  def constructed_scope_root?(node)
172
349
  return false unless node.send_type?
173
350
 
174
- if node.method_name == :new
175
- return false unless node.receiver&.const_type?
351
+ if node.receiver&.const_type?
352
+ return true if SCOPED_QUERY_CONSTRUCTOR_METHODS.include?(node.method_name) &&
353
+ node.arguments.any? { |arg| scoped_constructor_argument?(arg) }
176
354
 
177
355
  return scoped_finder_arguments?(node)
178
356
  end
179
357
 
358
+ return scoped_constructor_argument?(node) if node.method_name == :or
359
+
180
360
  constructed_scope_root?(node.receiver) if node.receiver
181
361
  end
182
362
 
183
- def consumed_by_finder?(node)
184
- return false unless node.method_name == :where
363
+ # Query objects in Decidim commonly receive the organization or the
364
+ # current user as a positional argument, e.g.
365
+ # `OrganizationAssemblies.new(current_organization)` or
366
+ # `AssembliesWithUserRole.for(current_user)`.
367
+ # For `or` relations, every branch must either already be
368
+ # organization-scoped or be constructed from an organization-scoped
369
+ # query object.
370
+ def scoped_constructor_argument?(node)
371
+ return scoped_query_argument_method?(node) unless node.send_type? && node.method_name == :or
372
+
373
+ scoped_or_branches?(node)
374
+ end
375
+
376
+ def scoped_query_argument_method?(node)
377
+ return false unless node.send_type?
378
+ return false if node.receiver
185
379
 
380
+ SCOPED_QUERY_ARGUMENT_METHODS.include?(node.method_name)
381
+ end
382
+
383
+ # An `or` relation is only safe when every nested branch is scoped,
384
+ # not just the receiver of the `or` call.
385
+ def scoped_or_branches?(node)
386
+ return false unless node.receiver
387
+
388
+ ([node.receiver] + node.arguments).all? do |branch|
389
+ organization_scoped?(branch) || constructed_scope_root?(branch)
390
+ end
391
+ end
392
+
393
+ # Report only the outermost relevant call of a receiver chain, so a
394
+ # single query cannot produce several offenses.
395
+ def consumed_by_outer_relation?(node)
396
+ loads_records = loads_records?(node)
186
397
  ancestor = node.parent
187
398
  while ancestor&.send_type?
188
399
  break unless ancestor.receiver == node
189
- return true if FINDER_METHODS.include?(ancestor.method_name)
400
+ return true if reportable_outer_relation?(ancestor, loads_records)
190
401
 
402
+ loads_records ||= loads_records?(ancestor)
191
403
  node = ancestor
192
404
  ancestor = node.parent
193
405
  end
194
406
 
195
407
  false
196
408
  end
409
+
410
+ # A call with a block is usually an Enumerable operation rather than a
411
+ # relation continuation, so the cop skips it and it cannot report the
412
+ # offense for the query it receives. Scoped creation finders are the
413
+ # exception: ActiveRecord applies their finder attributes before
414
+ # loading records, and their block only initializes values on a new
415
+ # record.
416
+ #
417
+ # Calls that already scope their own arguments are also skipped by
418
+ # `relevant_node?`, so they cannot report the offense for an inner
419
+ # unscoped finder. They can still consume the unscoped calls that build
420
+ # their relation, but only while those calls have not loaded records
421
+ # yet.
422
+ #
423
+ # A scoped call must not hide a lookup that already loaded records
424
+ # before it ran. It can only consume inner calls that build the query.
425
+ # Nor can an outer call hide an inner lookup when the outer call itself
426
+ # reports nothing: in
427
+ # `Model.find(id).children.where(organization: current_organization).first`
428
+ # the final `first` is scoped through its receiver, so the earlier
429
+ # `find` must stay reportable.
430
+ def reportable_outer_relation?(node, loads_records)
431
+ return false unless RESTRICT_ON_SEND.include?(node.method_name)
432
+ return false if node.block_node && !scoped_creation_finder?(node)
433
+ return !scoped_or_branches?(node) if node.method_name == :or
434
+
435
+ if scoped_finder_arguments?(node)
436
+ return false if loads_records
437
+
438
+ return true
439
+ end
440
+
441
+ offense_candidate?(node)
442
+ end
443
+
444
+ # Records may have been loaded earlier in the receiver chain. In
445
+ # particular, an `or` relation must inherit that state from its
446
+ # receiver, so an outer scoped call cannot hide the warning for
447
+ # `Template.where(name: "foo").load.or(current_organization.templates)`.
448
+ def loads_records?(node)
449
+ return false unless node&.send_type?
450
+ return true if QUERY_METHODS.include?(node.method_name) || RECORD_LOADING_METHODS.include?(node.method_name)
451
+
452
+ loads_records?(node.receiver)
453
+ end
454
+
455
+ def scoped_creation_finder?(node)
456
+ SCOPED_CREATION_FINDER_METHODS.include?(node.method_name) && scoped_finder_arguments?(node)
457
+ end
197
458
  end
198
459
  end
199
460
  end
@@ -14,6 +14,10 @@ ENV["DECIDIM_TIMESTAMP_SERVICE"] ||= "Decidim::Initiatives::DummyTimestamp"
14
14
  ENV["DECIDIM_PDF_SIGNATURE_SERVICE"] ||= "Decidim::PdfSignatureExample"
15
15
  ENV["DECIDIM_MACHINE_TRANSLATION_SERVICE"] ||= "Decidim::Dev::DummyTranslator"
16
16
 
17
+ # The env has to be manually set because "decidim/dev" already loads
18
+ # engines/railties which call the "paths" class method that initiates Rails.env.
19
+ Rails.env = ENV.fetch("RAILS_ENV", "test")
20
+
17
21
  engine_spec_dir = File.join(Dir.pwd, "spec")
18
22
 
19
23
  require "simplecov" if ENV["SIMPLECOV"]
@@ -4,7 +4,7 @@ module Decidim
4
4
  # This holds the decidim-dev version.
5
5
  module Dev
6
6
  def self.version
7
- "0.32.2"
7
+ "0.32.3"
8
8
  end
9
9
  end
10
10
  end