decidim-dev 0.31.7 → 0.31.9

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 (151) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/decidim/dev/dummy_resources_controller.rb +3 -1
  3. data/app/packs/stylesheets/decidim/dev/_rack_profiler.scss +10 -0
  4. data/config/locales/eu.yml +6 -6
  5. data/config/locales/gl.yml +15 -1
  6. data/config/rubocop/decidim-linters/configuration.yml +41 -0
  7. data/config/rubocop/rspec/configuration.yml +1 -0
  8. data/lib/decidim/dev/admin_engine.rb +3 -0
  9. data/lib/decidim/dev/assets/empty_file.png +0 -0
  10. data/lib/decidim/dev/assets/malformed_svg_unclosed_element.svg +7 -0
  11. data/lib/decidim/dev/assets/malicious_svg_alt_glyph_external_reference.svg +1 -0
  12. data/lib/decidim/dev/assets/malicious_svg_animate_color_external_reference.svg +1 -0
  13. data/lib/decidim/dev/assets/malicious_svg_animation.svg +1 -0
  14. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names.svg +1 -0
  15. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_event_handler.svg +1 -0
  16. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_list.svg +1 -0
  17. data/lib/decidim/dev/assets/malicious_svg_animation_color_profile.svg +1 -0
  18. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri.svg +1 -0
  19. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri_parameter.svg +1 -0
  20. data/lib/decidim/dev/assets/malicious_svg_animation_fill.svg +1 -0
  21. data/lib/decidim/dev/assets/malicious_svg_animation_gradient_reference.svg +1 -0
  22. data/lib/decidim/dev/assets/malicious_svg_animation_style.svg +1 -0
  23. data/lib/decidim/dev/assets/malicious_svg_animation_values.svg +1 -0
  24. data/lib/decidim/dev/assets/malicious_svg_attributename_case.svg +1 -0
  25. data/lib/decidim/dev/assets/malicious_svg_backslash_reference.svg +1 -0
  26. data/lib/decidim/dev/assets/malicious_svg_base.svg +1 -0
  27. data/lib/decidim/dev/assets/malicious_svg_base_child_element.svg +1 -0
  28. data/lib/decidim/dev/assets/malicious_svg_base_protocol_relative.svg +1 -0
  29. data/lib/decidim/dev/assets/malicious_svg_clip_path_external_reference.svg +1 -0
  30. data/lib/decidim/dev/assets/malicious_svg_color_profile_element_external_reference.svg +1 -0
  31. data/lib/decidim/dev/assets/malicious_svg_color_profile_external_reference.svg +1 -0
  32. data/lib/decidim/dev/assets/malicious_svg_comment_first.svg +1 -0
  33. data/lib/decidim/dev/assets/malicious_svg_css_backslash_url.svg +1 -0
  34. data/lib/decidim/dev/assets/malicious_svg_css_comment_in_url.svg +1 -0
  35. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_external_reference.svg +1 -0
  36. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_single_quote_external_reference.svg +1 -0
  37. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_external_reference.svg +1 -0
  38. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_single_quote_external_reference.svg +1 -0
  39. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_external_reference.svg +1 -0
  40. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_single_quote_external_reference.svg +1 -0
  41. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_external_reference.svg +2 -0
  42. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_single_quote_external_reference.svg +2 -0
  43. data/lib/decidim/dev/assets/malicious_svg_css_image_function.svg +1 -0
  44. data/lib/decidim/dev/assets/malicious_svg_css_image_set.svg +1 -0
  45. data/lib/decidim/dev/assets/malicious_svg_css_image_set_escaped_quote_external_reference.svg +1 -0
  46. data/lib/decidim/dev/assets/malicious_svg_css_image_set_nested_parentheses.svg +1 -0
  47. data/lib/decidim/dev/assets/malicious_svg_css_image_set_nested_url_external_reference.svg +1 -0
  48. data/lib/decidim/dev/assets/malicious_svg_css_image_set_quote_parentheses_external_reference.svg +1 -0
  49. data/lib/decidim/dev/assets/malicious_svg_css_string_newline_external_reference.svg +2 -0
  50. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_external_reference.svg +1 -0
  51. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_script.svg +1 -0
  52. data/lib/decidim/dev/assets/malicious_svg_cursor.svg +1 -0
  53. data/lib/decidim/dev/assets/malicious_svg_cursor_attribute.svg +1 -0
  54. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_not_last_parameter.svg +1 -0
  55. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_parameter_lookalike.svg +1 -0
  56. data/lib/decidim/dev/assets/malicious_svg_data_uri_declared_encoding.svg +1 -0
  57. data/lib/decidim/dev/assets/malicious_svg_data_uri_deep_nested.svg +1 -0
  58. data/lib/decidim/dev/assets/malicious_svg_data_uri_fragment.svg +1 -0
  59. data/lib/decidim/dev/assets/malicious_svg_data_uri_leading_space.svg +1 -0
  60. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_base64.svg +1 -0
  61. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_document.svg +1 -0
  62. data/lib/decidim/dev/assets/malicious_svg_data_uri_max_depth_exceeded.svg +1 -0
  63. data/lib/decidim/dev/assets/malicious_svg_data_uri_nested.svg +1 -0
  64. data/lib/decidim/dev/assets/malicious_svg_data_uri_parameter.svg +1 -0
  65. data/lib/decidim/dev/assets/malicious_svg_data_uri_percent_decoded_base64.svg +5 -0
  66. data/lib/decidim/dev/assets/malicious_svg_entity.svg +5 -0
  67. data/lib/decidim/dev/assets/malicious_svg_entity_parameter.svg +7 -0
  68. data/lib/decidim/dev/assets/malicious_svg_external_animation.svg +1 -0
  69. data/lib/decidim/dev/assets/malicious_svg_external_animation_missing_target.svg +1 -0
  70. data/lib/decidim/dev/assets/malicious_svg_external_animation_target.svg +1 -0
  71. data/lib/decidim/dev/assets/malicious_svg_external_feimage.svg +1 -0
  72. data/lib/decidim/dev/assets/malicious_svg_external_image.svg +1 -0
  73. data/lib/decidim/dev/assets/malicious_svg_external_use.svg +1 -0
  74. data/lib/decidim/dev/assets/malicious_svg_fill_external_reference.svg +1 -0
  75. data/lib/decidim/dev/assets/malicious_svg_filter_element_external_reference.svg +1 -0
  76. data/lib/decidim/dev/assets/malicious_svg_filter_external_reference.svg +1 -0
  77. data/lib/decidim/dev/assets/malicious_svg_font_face_uri_external_reference.svg +1 -0
  78. data/lib/decidim/dev/assets/malicious_svg_foreign_object.svg +1 -0
  79. data/lib/decidim/dev/assets/malicious_svg_gradient_external_reference.svg +1 -0
  80. data/lib/decidim/dev/assets/malicious_svg_handler.svg +1 -0
  81. data/lib/decidim/dev/assets/malicious_svg_marker_external_reference.svg +1 -0
  82. data/lib/decidim/dev/assets/malicious_svg_marker_start_external_reference.svg +1 -0
  83. data/lib/decidim/dev/assets/malicious_svg_mask_attribute.svg +1 -0
  84. data/lib/decidim/dev/assets/malicious_svg_mpath_external_reference.svg +1 -0
  85. data/lib/decidim/dev/assets/malicious_svg_pattern_external_reference.svg +1 -0
  86. data/lib/decidim/dev/assets/malicious_svg_protocol_relative_reference.svg +1 -0
  87. data/lib/decidim/dev/assets/malicious_svg_script.svg +1 -0
  88. data/lib/decidim/dev/assets/malicious_svg_stroke_external_reference.svg +1 -0
  89. data/lib/decidim/dev/assets/malicious_svg_style_comment_string.svg +1 -0
  90. data/lib/decidim/dev/assets/malicious_svg_style_import.svg +1 -0
  91. data/lib/decidim/dev/assets/malicious_svg_style_import_string.svg +1 -0
  92. data/lib/decidim/dev/assets/malicious_svg_style_url_escape.svg +1 -0
  93. data/lib/decidim/dev/assets/malicious_svg_style_url_string.svg +1 -0
  94. data/lib/decidim/dev/assets/malicious_svg_stylesheet_pi.svg +1 -0
  95. data/lib/decidim/dev/assets/malicious_svg_text_path_external_reference.svg +1 -0
  96. data/lib/decidim/dev/assets/malicious_svg_tref_external_reference.svg +1 -0
  97. data/lib/decidim/dev/assets/malicious_svg_uri.svg +1 -0
  98. data/lib/decidim/dev/assets/spoofed_image.png +0 -0
  99. data/lib/decidim/dev/assets/spoofed_image_binary.png +1 -0
  100. data/lib/decidim/dev/assets/spoofed_image_html.png +5 -0
  101. data/lib/decidim/dev/assets/spoofed_svg_comment_first.svg +1 -0
  102. data/lib/decidim/dev/assets/spoofed_svg_xml.svg +2 -0
  103. data/lib/decidim/dev/assets/test.avif +0 -0
  104. data/lib/decidim/dev/assets/test.gif +0 -0
  105. data/lib/decidim/dev/assets/test.heic +0 -0
  106. data/lib/decidim/dev/assets/test.svg +1 -0
  107. data/lib/decidim/dev/assets/test_animation.svg +1 -0
  108. data/lib/decidim/dev/assets/test_animation_attribute_names_list.svg +7 -0
  109. data/lib/decidim/dev/assets/test_animation_data_uri_parameter.svg +7 -0
  110. data/lib/decidim/dev/assets/test_animation_link.svg +7 -0
  111. data/lib/decidim/dev/assets/test_animation_percent_encoded_fragment.svg +7 -0
  112. data/lib/decidim/dev/assets/test_animation_presentation_reference.svg +8 -0
  113. data/lib/decidim/dev/assets/test_comment_first.svg +2 -0
  114. data/lib/decidim/dev/assets/test_complex.svg +12 -0
  115. data/lib/decidim/dev/assets/test_css_image_set_nested_relative_url.svg +1 -0
  116. data/lib/decidim/dev/assets/test_css_references.svg +14 -0
  117. data/lib/decidim/dev/assets/test_css_unclosed_comment.svg +2 -0
  118. data/lib/decidim/dev/assets/test_css_url_opposite_quote.svg +12 -0
  119. data/lib/decidim/dev/assets/test_data_uri_declared_encoding.svg +5 -0
  120. data/lib/decidim/dev/assets/test_data_uri_fragment.svg +1 -0
  121. data/lib/decidim/dev/assets/test_data_uri_max_depth.svg +1 -0
  122. data/lib/decidim/dev/assets/test_data_uri_parameters.svg +1 -0
  123. data/lib/decidim/dev/assets/test_data_uri_percent_decoded_base64.svg +6 -0
  124. data/lib/decidim/dev/assets/test_doctype_name_on_next_line.svg +4 -0
  125. data/lib/decidim/dev/assets/test_entity_in_comment.svg +8 -0
  126. data/lib/decidim/dev/assets/test_entity_in_processing_instruction.svg +7 -0
  127. data/lib/decidim/dev/assets/test_link_data_document.svg +9 -0
  128. data/lib/decidim/dev/assets/test_link_non_web_scheme.svg +7 -0
  129. data/lib/decidim/dev/assets/test_long_comment_first.svg +2 -0
  130. data/lib/decidim/dev/assets/test_percent_encoded_fragment.svg +7 -0
  131. data/lib/decidim/dev/assets/test_percent_encoded_multibyte_reference.svg +7 -0
  132. data/lib/decidim/dev/assets/test_percent_encoded_multibyte_reference_mixed.svg +8 -0
  133. data/lib/decidim/dev/assets/test_presentation_attribute_references.svg +13 -0
  134. data/lib/decidim/dev/assets/test_processing_instruction_first.svg +3 -0
  135. data/lib/decidim/dev/assets/test_reference_elements_resolved_in_document.svg +26 -0
  136. data/lib/decidim/dev/assets/test_references.svg +12 -0
  137. data/lib/decidim/dev/assets/test_relative_base.svg +2 -0
  138. data/lib/decidim/dev/assets/test_relative_image_function.svg +8 -0
  139. data/lib/decidim/dev/assets/test_root_relative_reference.svg +7 -0
  140. data/lib/decidim/dev/assets/test_style_text.svg +9 -0
  141. data/lib/decidim/dev/assets/video.mp4 +0 -0
  142. data/lib/decidim/dev/auth_engine.rb +3 -0
  143. data/lib/decidim/dev/engine.rb +3 -0
  144. data/lib/decidim/dev/rubocop/cop/decidim/enforce_permission_to.rb +459 -0
  145. data/lib/decidim/dev/rubocop/cop/decidim/organization_scoped_finder.rb +461 -0
  146. data/lib/decidim/dev/test/base_spec_helper.rb +4 -0
  147. data/lib/decidim/dev/test/rspec_support/capybara.rb +54 -0
  148. data/lib/decidim/dev/test/rspec_support/helpers.rb +14 -1
  149. data/lib/decidim/dev/version.rb +1 -1
  150. data/rubocop-decidim.yml +1 -0
  151. metadata +150 -14
@@ -0,0 +1,461 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Decidim
8
+ # Flags unscoped ActiveRecord finder calls in controllers and commands.
9
+ #
10
+ # In a multi-tenant Decidim deployment, loading a record without scoping
11
+ # it to the current organization can allow one tenant's users to access
12
+ # another tenant's resources. This cop promotes safe patterns such as
13
+ # `current_organization.templates.find_by(id: ...)` or using an already
14
+ # scoped `collection`.
15
+ #
16
+ # The cop targets chains that start with an ActiveRecord model constant
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.
20
+ class OrganizationScopedFinder < RuboCop::Cop::Base
21
+ MSG = "Unscoped ActiveRecord finder detected. Scope the query to the current organization, " \
22
+ "e.g. `current_organization.<relation>.find_by(id: params[:id])` or use an already-scoped `collection`."
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.
30
+ FINDER_METHODS = [
31
+ :find, :find_by, :find_by!, :find_sole_by, :first, :first!, :last, :last!,
32
+ :sole, :sole!, :take, :take!,
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?
36
+ ].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
66
+
67
+ SCOPE_ROOT_METHODS = [:current_organization, :collection].freeze
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
+
76
+ SCOPED_WHERE_KEYS = [:current_organization, :current_component].freeze
77
+
78
+ ORGANIZATION_SCOPED_KEYS = [:decidim_organization_id, :organization_id].freeze
79
+
80
+ SCOPED_ASSOCIATION_KEYS = [:component, :organization, :participatory_space].freeze
81
+
82
+ SCOPED_HELPER_METHODS = [:current_organization, :current_component, :current_participatory_space].freeze
83
+
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/
114
+
115
+ def on_send(node)
116
+ return unless relevant_node?(node)
117
+ return unless node.receiver
118
+
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)
125
+
126
+ add_offense(node)
127
+ end
128
+
129
+ private
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
+
155
+ def organization_scoped?(node)
156
+ return false unless node
157
+ return true if scoped_node?(node)
158
+ return false unless node.send_type?
159
+ return scoped_or_branches?(node) if node.method_name == :or
160
+
161
+ organization_scoped?(node.receiver)
162
+ end
163
+
164
+ def unscoped_model_class?(node)
165
+ return false unless node
166
+ return false if ignored_constant?(node)
167
+ return true if node.const_type?
168
+ return false unless node.send_type?
169
+
170
+ unscoped_model_class?(node.receiver)
171
+ end
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
+
186
+ def ignored_constant?(node)
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)
196
+ end
197
+
198
+ def scoped_node?(node)
199
+ return false unless node.send_type?
200
+
201
+ scope_root?(node) || scoped_where?(node) || scoped_finder_arguments?(node)
202
+ end
203
+
204
+ def scope_root?(node)
205
+ return false unless SCOPE_ROOT_METHODS.include?(node.method_name)
206
+
207
+ node.receiver.nil? || node.receiver.self_type?
208
+ end
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.
215
+ def scoped_finder_arguments?(node)
216
+ return false if QUERY_METHODS.include?(node.method_name) && !filtering_argument_method?(node)
217
+
218
+ node.arguments.any? { |arg| scoped_argument?(arg) }
219
+ end
220
+
221
+ def filtering_argument_method?(node)
222
+ FILTERING_ARGUMENT_METHODS.any? { |method| method == node.method_name }
223
+ end
224
+
225
+ def scoped_where?(node)
226
+ return false unless node.method_name == :where
227
+
228
+ node.arguments.any? { |arg| scoped_argument?(arg) }
229
+ end
230
+
231
+ def scoped_argument?(node)
232
+ return false unless node.hash_type?
233
+
234
+ node.children.any? { |pair| scoped_pair?(pair) }
235
+ end
236
+
237
+ def scoped_pair?(pair_node)
238
+ return false unless pair_node.pair_type?
239
+
240
+ key = pair_node.children[0]
241
+ value = pair_node.children[1]
242
+
243
+ scoped_where_pair?(key, value) ||
244
+ scoped_association_pair?(key, value) ||
245
+ (value.hash_type? && value.children.any? { |inner| ownership_scoped_pair?(inner) })
246
+ end
247
+
248
+ def ownership_scoped_pair?(pair_node)
249
+ return false unless pair_node.pair_type?
250
+
251
+ key = pair_node.children[0]
252
+ value = pair_node.children[1]
253
+
254
+ scoped_association_pair?(key, value) ||
255
+ (value.hash_type? && value.children.any? { |inner| ownership_scoped_pair?(inner) })
256
+ end
257
+
258
+ def scoped_where_pair?(key, value)
259
+ return false unless key.sym_type?
260
+
261
+ return organization_value?(value) if key.value == :organization
262
+ return false unless scoped_where_key?(key)
263
+
264
+ scoped_trusted_value?(value)
265
+ end
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
+
302
+ def scoped_association_pair?(key, value)
303
+ return false unless key.sym_type?
304
+ return false unless SCOPED_ASSOCIATION_KEYS.include?(key.value)
305
+ return false unless value.send_type?
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?
318
+
319
+ node.ivar_type? && node.name == :@form
320
+ end
321
+
322
+ def scoped_trusted_value?(node)
323
+ return false unless node
324
+
325
+ case node.type
326
+ when :send
327
+ scoped_helper_method?(node) || (node.receiver && scoped_trusted_value?(node.receiver))
328
+ when :hash
329
+ node.children.any? { |pair| scoped_pair?(pair) }
330
+ else
331
+ false
332
+ end
333
+ end
334
+
335
+ def scoped_helper_method?(node)
336
+ return false unless node.send_type?
337
+ return false if node.receiver
338
+
339
+ SCOPED_HELPER_METHODS.include?(node.method_name)
340
+ end
341
+
342
+ def scoped_where_key?(key)
343
+ key.sym_type? &&
344
+ (SCOPED_WHERE_KEYS.include?(key.value) ||
345
+ ORGANIZATION_SCOPED_KEYS.include?(key.value))
346
+ end
347
+
348
+ def constructed_scope_root?(node)
349
+ return false unless node.send_type?
350
+
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) }
354
+
355
+ return scoped_finder_arguments?(node)
356
+ end
357
+
358
+ return scoped_constructor_argument?(node) if node.method_name == :or
359
+
360
+ constructed_scope_root?(node.receiver) if node.receiver
361
+ end
362
+
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
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)
397
+ ancestor = node.parent
398
+ while ancestor&.send_type?
399
+ break unless ancestor.receiver == node
400
+ return true if reportable_outer_relation?(ancestor, loads_records)
401
+
402
+ loads_records ||= loads_records?(ancestor)
403
+ node = ancestor
404
+ ancestor = node.parent
405
+ end
406
+
407
+ false
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
458
+ end
459
+ end
460
+ end
461
+ 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"]
@@ -27,6 +27,60 @@ module Decidim
27
27
 
28
28
  "http"
29
29
  end
30
+
31
+ def expire_browser_session(wait: Capybara.default_max_wait_time)
32
+ wait_pending_requests(wait)
33
+
34
+ travel(Decidim.config.expire_session_after + 1.second) do
35
+ yield
36
+ end
37
+ end
38
+
39
+ # Clear the buffers and background connections by taking the browser to
40
+ # offline state for a tiny moment. If a block is given, any further commands
41
+ # can be run during the offline state.
42
+ def force_browser_offline_state
43
+ page.driver.browser.execute_cdp(
44
+ "Network.emulateNetworkConditions",
45
+ offline: true,
46
+ latency: 0,
47
+ downloadThroughput: 0,
48
+ uploadThroughput: 0
49
+ )
50
+ sleep 0.05
51
+
52
+ yield if block_given?
53
+ ensure
54
+ page.driver.browser.execute_cdp(
55
+ "Network.emulateNetworkConditions",
56
+ offline: false,
57
+ latency: 0,
58
+ downloadThroughput: -1,
59
+ uploadThroughput: -1
60
+ )
61
+ end
62
+
63
+ # Waits for any pending requests to finish and clears any active browser
64
+ # background connections after that.
65
+ def wait_pending_requests(max_wait_time = Capybara.default_max_wait_time, idle_time: 1)
66
+ wait_time = 0
67
+ loop do
68
+ time_since_last_asset = page.evaluate_script(
69
+ <<~JS
70
+ performance.now() - Math.max(...window.performance.getEntriesByType("resource").map((r) => r.responseEnd), 0)
71
+ JS
72
+ )
73
+ break if time_since_last_asset > (idle_time * 1000)
74
+
75
+ remaining_wait_time = max_wait_time - wait_time
76
+ raise StandardError, "Requests still pending." if remaining_wait_time <= 0
77
+
78
+ sleep_time = [idle_time, remaining_wait_time].min
79
+ sleep sleep_time
80
+ wait_time += sleep_time
81
+ end
82
+ force_browser_offline_state
83
+ end
30
84
  end
31
85
  end
32
86
 
@@ -68,7 +68,20 @@ module Decidim
68
68
  "User-Agent" => "Ruby"
69
69
  }
70
70
  )
71
- .to_return(status: 200, body: "", headers: { content_type: rs_format })
71
+ .to_return(status: 200, body: stubbed_file_body(rs_format), headers: { content_type: rs_format })
72
+ end
73
+
74
+ # Real content for the stubbed formats. Content validation rejects empty
75
+ # uploads declared as images, so the stubbed downloads must return bytes
76
+ # matching their content type.
77
+ def stubbed_file_body(rs_format)
78
+ asset = {
79
+ "image/jpeg" => "city.jpeg",
80
+ "application/pdf" => "Exampledocument.pdf"
81
+ }[rs_format]
82
+ return "" if asset.nil?
83
+
84
+ File.binread(Decidim::Dev.asset(asset))
72
85
  end
73
86
  end
74
87
 
@@ -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.31.7"
7
+ "0.31.9"
8
8
  end
9
9
  end
10
10
  end
data/rubocop-decidim.yml CHANGED
@@ -26,3 +26,4 @@ inherit_from:
26
26
  - config/rubocop/factory_bot/configuration.yml
27
27
  - config/rubocop/factory_bot/disabled.yml
28
28
  - config/rubocop/disabled.yml
29
+ - config/rubocop/decidim-linters/configuration.yml