decidim-dev 0.32.1 → 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 (152) 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 +37 -0
  7. data/config/rubocop/rspec/configuration.yml +1 -0
  8. data/decidim-dev.gemspec +1 -1
  9. data/lib/decidim/dev/admin_engine.rb +3 -0
  10. data/lib/decidim/dev/assets/empty_file.png +0 -0
  11. data/lib/decidim/dev/assets/malformed_svg_unclosed_element.svg +7 -0
  12. data/lib/decidim/dev/assets/malicious_svg_alt_glyph_external_reference.svg +1 -0
  13. data/lib/decidim/dev/assets/malicious_svg_animate_color_external_reference.svg +1 -0
  14. data/lib/decidim/dev/assets/malicious_svg_animation.svg +1 -0
  15. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names.svg +1 -0
  16. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_event_handler.svg +1 -0
  17. data/lib/decidim/dev/assets/malicious_svg_animation_attribute_names_list.svg +1 -0
  18. data/lib/decidim/dev/assets/malicious_svg_animation_color_profile.svg +1 -0
  19. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri.svg +1 -0
  20. data/lib/decidim/dev/assets/malicious_svg_animation_data_uri_parameter.svg +1 -0
  21. data/lib/decidim/dev/assets/malicious_svg_animation_fill.svg +1 -0
  22. data/lib/decidim/dev/assets/malicious_svg_animation_gradient_reference.svg +1 -0
  23. data/lib/decidim/dev/assets/malicious_svg_animation_style.svg +1 -0
  24. data/lib/decidim/dev/assets/malicious_svg_animation_values.svg +1 -0
  25. data/lib/decidim/dev/assets/malicious_svg_attributename_case.svg +1 -0
  26. data/lib/decidim/dev/assets/malicious_svg_backslash_reference.svg +1 -0
  27. data/lib/decidim/dev/assets/malicious_svg_base.svg +1 -0
  28. data/lib/decidim/dev/assets/malicious_svg_base_child_element.svg +1 -0
  29. data/lib/decidim/dev/assets/malicious_svg_base_protocol_relative.svg +1 -0
  30. data/lib/decidim/dev/assets/malicious_svg_clip_path_external_reference.svg +1 -0
  31. data/lib/decidim/dev/assets/malicious_svg_color_profile_element_external_reference.svg +1 -0
  32. data/lib/decidim/dev/assets/malicious_svg_color_profile_external_reference.svg +1 -0
  33. data/lib/decidim/dev/assets/malicious_svg_comment_first.svg +1 -0
  34. data/lib/decidim/dev/assets/malicious_svg_css_backslash_url.svg +1 -0
  35. data/lib/decidim/dev/assets/malicious_svg_css_comment_in_url.svg +1 -0
  36. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_external_reference.svg +1 -0
  37. data/lib/decidim/dev/assets/malicious_svg_css_empty_string_single_quote_external_reference.svg +1 -0
  38. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_external_reference.svg +1 -0
  39. data/lib/decidim/dev/assets/malicious_svg_css_escaped_quote_unquoted_value_single_quote_external_reference.svg +1 -0
  40. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_external_reference.svg +1 -0
  41. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_crlf_single_quote_external_reference.svg +1 -0
  42. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_external_reference.svg +2 -0
  43. data/lib/decidim/dev/assets/malicious_svg_css_hex_escape_newline_single_quote_external_reference.svg +2 -0
  44. data/lib/decidim/dev/assets/malicious_svg_css_image_function.svg +1 -0
  45. data/lib/decidim/dev/assets/malicious_svg_css_image_set.svg +1 -0
  46. data/lib/decidim/dev/assets/malicious_svg_css_image_set_escaped_quote_external_reference.svg +1 -0
  47. data/lib/decidim/dev/assets/malicious_svg_css_image_set_nested_parentheses.svg +1 -0
  48. data/lib/decidim/dev/assets/malicious_svg_css_image_set_nested_url_external_reference.svg +1 -0
  49. data/lib/decidim/dev/assets/malicious_svg_css_image_set_quote_parentheses_external_reference.svg +1 -0
  50. data/lib/decidim/dev/assets/malicious_svg_css_string_newline_external_reference.svg +2 -0
  51. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_external_reference.svg +1 -0
  52. data/lib/decidim/dev/assets/malicious_svg_css_url_opposite_quote_nested_script.svg +1 -0
  53. data/lib/decidim/dev/assets/malicious_svg_cursor.svg +1 -0
  54. data/lib/decidim/dev/assets/malicious_svg_cursor_attribute.svg +1 -0
  55. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_not_last_parameter.svg +1 -0
  56. data/lib/decidim/dev/assets/malicious_svg_data_uri_base64_parameter_lookalike.svg +1 -0
  57. data/lib/decidim/dev/assets/malicious_svg_data_uri_declared_encoding.svg +1 -0
  58. data/lib/decidim/dev/assets/malicious_svg_data_uri_deep_nested.svg +1 -0
  59. data/lib/decidim/dev/assets/malicious_svg_data_uri_fragment.svg +1 -0
  60. data/lib/decidim/dev/assets/malicious_svg_data_uri_leading_space.svg +1 -0
  61. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_base64.svg +1 -0
  62. data/lib/decidim/dev/assets/malicious_svg_data_uri_malformed_document.svg +1 -0
  63. data/lib/decidim/dev/assets/malicious_svg_data_uri_max_depth_exceeded.svg +1 -0
  64. data/lib/decidim/dev/assets/malicious_svg_data_uri_nested.svg +1 -0
  65. data/lib/decidim/dev/assets/malicious_svg_data_uri_parameter.svg +1 -0
  66. data/lib/decidim/dev/assets/malicious_svg_data_uri_percent_decoded_base64.svg +5 -0
  67. data/lib/decidim/dev/assets/malicious_svg_entity.svg +5 -0
  68. data/lib/decidim/dev/assets/malicious_svg_entity_parameter.svg +7 -0
  69. data/lib/decidim/dev/assets/malicious_svg_external_animation.svg +1 -0
  70. data/lib/decidim/dev/assets/malicious_svg_external_animation_missing_target.svg +1 -0
  71. data/lib/decidim/dev/assets/malicious_svg_external_animation_target.svg +1 -0
  72. data/lib/decidim/dev/assets/malicious_svg_external_feimage.svg +1 -0
  73. data/lib/decidim/dev/assets/malicious_svg_external_image.svg +1 -0
  74. data/lib/decidim/dev/assets/malicious_svg_external_use.svg +1 -0
  75. data/lib/decidim/dev/assets/malicious_svg_fill_external_reference.svg +1 -0
  76. data/lib/decidim/dev/assets/malicious_svg_filter_element_external_reference.svg +1 -0
  77. data/lib/decidim/dev/assets/malicious_svg_filter_external_reference.svg +1 -0
  78. data/lib/decidim/dev/assets/malicious_svg_font_face_uri_external_reference.svg +1 -0
  79. data/lib/decidim/dev/assets/malicious_svg_foreign_object.svg +1 -0
  80. data/lib/decidim/dev/assets/malicious_svg_gradient_external_reference.svg +1 -0
  81. data/lib/decidim/dev/assets/malicious_svg_handler.svg +1 -0
  82. data/lib/decidim/dev/assets/malicious_svg_marker_external_reference.svg +1 -0
  83. data/lib/decidim/dev/assets/malicious_svg_marker_start_external_reference.svg +1 -0
  84. data/lib/decidim/dev/assets/malicious_svg_mask_attribute.svg +1 -0
  85. data/lib/decidim/dev/assets/malicious_svg_mpath_external_reference.svg +1 -0
  86. data/lib/decidim/dev/assets/malicious_svg_pattern_external_reference.svg +1 -0
  87. data/lib/decidim/dev/assets/malicious_svg_protocol_relative_reference.svg +1 -0
  88. data/lib/decidim/dev/assets/malicious_svg_script.svg +1 -0
  89. data/lib/decidim/dev/assets/malicious_svg_stroke_external_reference.svg +1 -0
  90. data/lib/decidim/dev/assets/malicious_svg_style_comment_string.svg +1 -0
  91. data/lib/decidim/dev/assets/malicious_svg_style_import.svg +1 -0
  92. data/lib/decidim/dev/assets/malicious_svg_style_import_string.svg +1 -0
  93. data/lib/decidim/dev/assets/malicious_svg_style_url_escape.svg +1 -0
  94. data/lib/decidim/dev/assets/malicious_svg_style_url_string.svg +1 -0
  95. data/lib/decidim/dev/assets/malicious_svg_stylesheet_pi.svg +1 -0
  96. data/lib/decidim/dev/assets/malicious_svg_text_path_external_reference.svg +1 -0
  97. data/lib/decidim/dev/assets/malicious_svg_tref_external_reference.svg +1 -0
  98. data/lib/decidim/dev/assets/malicious_svg_uri.svg +1 -0
  99. data/lib/decidim/dev/assets/spoofed_image.png +0 -0
  100. data/lib/decidim/dev/assets/spoofed_image_binary.png +1 -0
  101. data/lib/decidim/dev/assets/spoofed_image_html.png +5 -0
  102. data/lib/decidim/dev/assets/spoofed_svg_comment_first.svg +1 -0
  103. data/lib/decidim/dev/assets/spoofed_svg_xml.svg +2 -0
  104. data/lib/decidim/dev/assets/test.avif +0 -0
  105. data/lib/decidim/dev/assets/test.gif +0 -0
  106. data/lib/decidim/dev/assets/test.heic +0 -0
  107. data/lib/decidim/dev/assets/test.svg +1 -0
  108. data/lib/decidim/dev/assets/test_animation.svg +1 -0
  109. data/lib/decidim/dev/assets/test_animation_attribute_names_list.svg +7 -0
  110. data/lib/decidim/dev/assets/test_animation_data_uri_parameter.svg +7 -0
  111. data/lib/decidim/dev/assets/test_animation_link.svg +7 -0
  112. data/lib/decidim/dev/assets/test_animation_percent_encoded_fragment.svg +7 -0
  113. data/lib/decidim/dev/assets/test_animation_presentation_reference.svg +8 -0
  114. data/lib/decidim/dev/assets/test_comment_first.svg +2 -0
  115. data/lib/decidim/dev/assets/test_complex.svg +12 -0
  116. data/lib/decidim/dev/assets/test_css_image_set_nested_relative_url.svg +1 -0
  117. data/lib/decidim/dev/assets/test_css_references.svg +14 -0
  118. data/lib/decidim/dev/assets/test_css_unclosed_comment.svg +2 -0
  119. data/lib/decidim/dev/assets/test_css_url_opposite_quote.svg +12 -0
  120. data/lib/decidim/dev/assets/test_data_uri_declared_encoding.svg +5 -0
  121. data/lib/decidim/dev/assets/test_data_uri_fragment.svg +1 -0
  122. data/lib/decidim/dev/assets/test_data_uri_max_depth.svg +1 -0
  123. data/lib/decidim/dev/assets/test_data_uri_parameters.svg +1 -0
  124. data/lib/decidim/dev/assets/test_data_uri_percent_decoded_base64.svg +6 -0
  125. data/lib/decidim/dev/assets/test_doctype_name_on_next_line.svg +4 -0
  126. data/lib/decidim/dev/assets/test_entity_in_comment.svg +8 -0
  127. data/lib/decidim/dev/assets/test_entity_in_processing_instruction.svg +7 -0
  128. data/lib/decidim/dev/assets/test_link_data_document.svg +9 -0
  129. data/lib/decidim/dev/assets/test_link_non_web_scheme.svg +7 -0
  130. data/lib/decidim/dev/assets/test_long_comment_first.svg +2 -0
  131. data/lib/decidim/dev/assets/test_percent_encoded_fragment.svg +7 -0
  132. data/lib/decidim/dev/assets/test_percent_encoded_multibyte_reference.svg +7 -0
  133. data/lib/decidim/dev/assets/test_percent_encoded_multibyte_reference_mixed.svg +8 -0
  134. data/lib/decidim/dev/assets/test_presentation_attribute_references.svg +13 -0
  135. data/lib/decidim/dev/assets/test_processing_instruction_first.svg +3 -0
  136. data/lib/decidim/dev/assets/test_reference_elements_resolved_in_document.svg +26 -0
  137. data/lib/decidim/dev/assets/test_references.svg +12 -0
  138. data/lib/decidim/dev/assets/test_relative_base.svg +2 -0
  139. data/lib/decidim/dev/assets/test_relative_image_function.svg +8 -0
  140. data/lib/decidim/dev/assets/test_root_relative_reference.svg +7 -0
  141. data/lib/decidim/dev/assets/test_style_text.svg +9 -0
  142. data/lib/decidim/dev/assets/video.mp4 +0 -0
  143. data/lib/decidim/dev/auth_engine.rb +3 -0
  144. data/lib/decidim/dev/engine.rb +3 -0
  145. data/lib/decidim/dev/rubocop/cop/decidim/enforce_permission_to.rb +459 -0
  146. data/lib/decidim/dev/rubocop/cop/decidim/organization_scoped_finder.rb +461 -0
  147. data/lib/decidim/dev/test/base_spec_helper.rb +4 -0
  148. data/lib/decidim/dev/test/rspec_support/capybara.rb +54 -0
  149. data/lib/decidim/dev/test/rspec_support/editor_context.rb +1 -1
  150. data/lib/decidim/dev/test/rspec_support/helpers.rb +14 -1
  151. data/lib/decidim/dev/version.rb +1 -1
  152. metadata +148 -13
@@ -0,0 +1,459 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Decidim
8
+ # Flags controller actions that lack authorization checks.
9
+ #
10
+ # In Decidim admin controllers, every action must explicitly call
11
+ # `enforce_permission_to` or `action_authorized_to` to verify the
12
+ # current user has permission to perform that action. Missing
13
+ # authorization checks can allow unauthorized access to resources.
14
+ #
15
+ # This cop checks all public method definitions in admin controllers
16
+ # and flags any that do not contain an authorization call.
17
+ #
18
+ # Actions are exempt when the controller uses a `before_action` that
19
+ # performs authorization (either by referencing a method that calls
20
+ # `enforce_permission_to`, or by using a name containing
21
+ # "permission", "authorize", or "enforce"). When such `before_action`
22
+ # is restricted with `only:` or `except:`, only the actions it covers
23
+ # are exempt.
24
+ #
25
+ # Methods referenced by framework lifecycle callbacks (`before_action`
26
+ # and its variants, `around_action`, `after_action`, `helper_method`,
27
+ # `rescue_from`, ...) are treated as lifecycle/helper methods and are
28
+ # never flagged, as they themselves are not controller actions.
29
+ #
30
+ # Calls to methods starting with `enforce_permission_to_` or
31
+ # `action_authorized_to_` (authorization helper wrappers) are also
32
+ # accepted as authorization checks.
33
+ #
34
+ # @example
35
+ # # bad
36
+ # class Admin::ResourcesController < Admin::ApplicationController
37
+ # def edit
38
+ # @resource = Resource.find(params[:id])
39
+ # end
40
+ # end
41
+ #
42
+ # # good
43
+ # class Admin::ResourcesController < Admin::ApplicationController
44
+ # def edit
45
+ # enforce_permission_to :update, :resource, resource:
46
+ # @resource = Resource.find(params[:id])
47
+ # end
48
+ # end
49
+ #
50
+ # # good - authorization via before_action
51
+ # class Admin::ResourcesController < Admin::ApplicationController
52
+ # before_action :ensure_permissions
53
+ #
54
+ # def edit
55
+ # @resource = Resource.find(params[:id])
56
+ # end
57
+ #
58
+ # private
59
+ #
60
+ # def ensure_permissions
61
+ # enforce_permission_to :update, :resource
62
+ # end
63
+ # end
64
+ class EnforcePermissionTo < RuboCop::Cop::Base
65
+ MSG = "Action `%{action}` is missing an authorization check. " \
66
+ "Add `enforce_permission_to` or `action_authorized_to` at the start of the action, " \
67
+ "or use `# rubocop:disable Decidim/EnforcePermissionTo` if authorization is handled elsewhere."
68
+
69
+ AUTHORIZATION_METHODS = [:enforce_permission_to, :action_authorized_to].freeze
70
+
71
+ # Prefixes accepted for authorization helper wrappers, such as
72
+ # `enforce_permission_to_update_resource`.
73
+ AUTHORIZATION_PREFIXES = %w(enforce_permission_to_ action_authorized_to_).freeze
74
+
75
+ BEFORE_ACTION_AUTH_KEYWORDS = %w(permission authorize enforce).freeze
76
+
77
+ # Visibility switch methods recognized when declared with no arguments.
78
+ VISIBILITY_METHODS = [:public, :protected, :private].freeze
79
+
80
+ # Framework declarations whose referenced methods are lifecycle or
81
+ # helper methods rather than controller actions.
82
+ CALLBACK_METHODS = [
83
+ :before_action, :prepend_before_action, :append_before_action, :skip_before_action,
84
+ :around_action, :prepend_around_action, :append_around_action, :skip_around_action,
85
+ :after_action, :prepend_after_action, :append_after_action, :skip_after_action,
86
+ :helper_method, :rescue_from
87
+ ].freeze
88
+
89
+ def on_new_investigation
90
+ @scope_callback_methods = {}
91
+ reset_scope_state
92
+ end
93
+
94
+ def on_class(node)
95
+ reset_scope_state
96
+ collect_callback_methods(node)
97
+ check_before_actions(node)
98
+ check_before_action_blocks(node)
99
+ end
100
+
101
+ def on_module(node)
102
+ reset_scope_state
103
+ collect_callback_methods(node)
104
+ check_before_actions(node)
105
+ check_before_action_blocks(node)
106
+ end
107
+
108
+ def on_def(node)
109
+ method_name = node.method_name
110
+
111
+ inline_visibility = @inline_visibility
112
+ @inline_visibility = nil
113
+
114
+ return if class_methods_block?(node)
115
+ return unless method_public?(node, method_name, inline_visibility)
116
+ return if action_exempted_by_before_action?(method_name)
117
+ return if callback_method?(node, method_name)
118
+ return if contains_authorization_check?(node)
119
+
120
+ action_name = method_name.to_s
121
+ add_offense(node, message: format(MSG, action: action_name))
122
+ end
123
+
124
+ def on_send(node)
125
+ return unless VISIBILITY_METHODS.include?(node.method_name)
126
+
127
+ if node.arguments.empty?
128
+ @in_private_section = node.method_name != :public
129
+ elsif visibility_def_argument?(node)
130
+ # `private def foo` only makes that single method private, it does
131
+ # not change the default visibility for the following methods.
132
+ @inline_visibility = node.method_name
133
+ end
134
+ end
135
+
136
+ private
137
+
138
+ # Resets the state that is scoped to a single class or module body.
139
+ # Callback methods are intentionally not part of this state: they are
140
+ # collected per scope in `collect_callback_methods`, so sibling scopes
141
+ # do not see each other's callbacks. A definition only inherits the
142
+ # callbacks of its own scope and of enclosing modules (never across a
143
+ # class boundary, as nested classes are not subclasses).
144
+ def reset_scope_state
145
+ @in_private_section = false
146
+ @inline_visibility = nil
147
+ @all_actions_auth = false
148
+ @only_actions = Set.new
149
+ @except_sets = []
150
+ end
151
+
152
+ # Definitions inside an `ActiveSupport::Concern` `class_methods do`
153
+ # block are class-level DSL methods, not controller actions, so they
154
+ # must never be flagged as missing an authorization check.
155
+ def class_methods_block?(node)
156
+ node.each_ancestor(:block).any? do |block|
157
+ block.send_node.method_name == :class_methods
158
+ end
159
+ end
160
+
161
+ def visibility_def_argument?(node)
162
+ node.arguments.any?(&:def_type?)
163
+ end
164
+
165
+ def method_public?(node, method_name, inline_visibility)
166
+ named_visibility = named_visibility_for(node, method_name)
167
+ return named_visibility == :public if named_visibility
168
+
169
+ visibility = inline_visibility || (@in_private_section ? :private : :public)
170
+ visibility == :public
171
+ end
172
+
173
+ # Resolves named visibility modifiers such as `public :publish` or
174
+ # `private :resource` that appear after the method definition. In Ruby
175
+ # these modifiers only apply to methods already defined, so a modifier
176
+ # placed before a later `def` does not change that definition's
177
+ # visibility. Among the applicable modifiers, the last one wins.
178
+ def named_visibility_for(node, method_name)
179
+ scope = node.each_ancestor(:class, :module).first
180
+ return nil unless scope
181
+
182
+ named_visibility_overrides(scope, node)[method_name]
183
+ end
184
+
185
+ def named_visibility_overrides(scope, node)
186
+ def_end = node.source_range.end_pos
187
+
188
+ scope.body&.each_child_node&.with_object({}) do |child, overrides|
189
+ next unless named_visibility_after?(child, def_end)
190
+
191
+ child.arguments.each do |arg|
192
+ covered_actions(arg).each do |name|
193
+ overrides[name] = child.method_name
194
+ end
195
+ end
196
+ end || {}
197
+ end
198
+
199
+ def named_visibility_after?(child, def_end)
200
+ named_visibility_modifier?(child) && child.source_range.begin_pos > def_end
201
+ end
202
+
203
+ def named_visibility_modifier?(node)
204
+ node.send_type? && VISIBILITY_METHODS.include?(node.method_name) &&
205
+ !node.arguments.empty? && !visibility_def_argument?(node)
206
+ end
207
+
208
+ # Collects the lifecycle/helper methods owned by a scope (a class or
209
+ # module). Only declarations and definitions belonging to the scope
210
+ # itself are considered; nested classes and modules are excluded so
211
+ # callbacks cannot leak between sibling scopes. The result is stored per
212
+ # scope so a definition can inherit the callbacks of its enclosing
213
+ # scopes without seeing those of its siblings.
214
+ def collect_callback_methods(node)
215
+ return unless node.body
216
+
217
+ definitions = own_scope_definitions(node)
218
+ declared = own_scope_callback_names(node)
219
+ @scope_callback_methods[node] = expand_callback_dependencies(declared, definitions)
220
+ end
221
+
222
+ # The methods referenced by callback declarations on the scope itself.
223
+ def own_scope_callback_names(node)
224
+ own_scope_nodes(node).each_with_object(Set.new) do |descendant, names|
225
+ next unless descendant.send_type?
226
+ next unless CALLBACK_METHODS.include?(descendant.method_name)
227
+
228
+ callback_method_names(descendant).each { |name| names << name }
229
+ end
230
+ end
231
+
232
+ # The method definitions belonging to the scope itself, indexed by name.
233
+ def own_scope_definitions(node)
234
+ own_scope_nodes(node).each_with_object({}) do |descendant, definitions|
235
+ next unless descendant.def_type?
236
+
237
+ definitions[descendant.method_name] ||= descendant
238
+ end
239
+ end
240
+
241
+ # Walks a scope body without descending into nested classes or modules,
242
+ # so each scope only sees its own declarations and definitions.
243
+ def own_scope_nodes(node)
244
+ nodes = []
245
+ queue = [node.body].compact
246
+
247
+ until queue.empty?
248
+ current = queue.shift
249
+ next if current.class_type? || current.module_type?
250
+
251
+ nodes << current
252
+ current.each_child_node { |child| queue << child }
253
+ end
254
+
255
+ nodes
256
+ end
257
+
258
+ # Methods called by a lifecycle callback are helper methods as well,
259
+ # e.g. a callback delegating to a public helper method.
260
+ def expand_callback_dependencies(declared, definitions)
261
+ names = Set.new(declared)
262
+ queue = declared.to_a
263
+
264
+ until queue.empty?
265
+ definition = definitions[queue.shift]
266
+ next unless definition&.body
267
+
268
+ sends = [definition.body]
269
+ sends.concat(definition.body.each_descendant(:send).to_a)
270
+
271
+ sends.each do |send_node|
272
+ next unless send_node.send_type?
273
+ next unless controller_call?(send_node)
274
+
275
+ callee = send_node.method_name
276
+ next unless definitions.has_key?(callee)
277
+ next unless names.add?(callee)
278
+
279
+ queue << callee
280
+ end
281
+ end
282
+
283
+ names
284
+ end
285
+
286
+ # Whether the method is a lifecycle/helper method of the def's own scope
287
+ # or of an enclosing module. Callbacks declared in a class are not
288
+ # inherited by definitions inside a nested class: Ruby does not treat a
289
+ # nested class as a subclass, so an outer controller's `helper_method`
290
+ # must not exempt an unrelated nested controller's action with the same
291
+ # name. Enclosing modules are still followed (a common concern pattern),
292
+ # but the walk stops at the first class boundary.
293
+ def callback_method?(node, method_name)
294
+ scopes = node.each_ancestor(:class, :module).to_a
295
+ return false if scopes.empty?
296
+
297
+ own_scope = scopes.shift
298
+ return true if scope_callback?(own_scope, method_name)
299
+
300
+ scopes.each do |scope|
301
+ break if scope.class_type?
302
+
303
+ return true if scope_callback?(scope, method_name)
304
+ end
305
+
306
+ false
307
+ end
308
+
309
+ def scope_callback?(scope, method_name)
310
+ @scope_callback_methods.fetch(scope, Set.new).include?(method_name)
311
+ end
312
+
313
+ # A call is made on the controller itself when the receiver is implicit
314
+ # or an explicit `self`. Calls with any other receiver, such as
315
+ # `Resource.find`, must not be followed because a same-named controller
316
+ # action could then be mistaken for a lifecycle/helper method, hiding a
317
+ # missing authorization check.
318
+ def controller_call?(send_node)
319
+ send_node.receiver.nil? || send_node.receiver.self_type?
320
+ end
321
+
322
+ def callback_method_names(send_node)
323
+ send_node.arguments.flat_map do |arg|
324
+ case arg.type
325
+ when :sym, :str
326
+ arg.value.to_sym
327
+ when :hash
328
+ rescue_handler_names(arg)
329
+ else
330
+ []
331
+ end
332
+ end
333
+ end
334
+
335
+ # `rescue_from SomeError, with: :handler` references the handler
336
+ # method through the `with:` option.
337
+ def rescue_handler_names(hash_node)
338
+ hash_node.pairs.filter_map do |pair|
339
+ next unless pair.key.sym_type? && pair.key.value == :with
340
+
341
+ value = pair.value
342
+ value.value.to_sym if value.sym_type? || value.str_type?
343
+ end
344
+ end
345
+
346
+ def check_before_actions(class_node)
347
+ class_node.body&.each_child_node(:send) do |send_node|
348
+ next unless send_node.method_name == :before_action
349
+
350
+ register_auth_before_action(send_node) if before_action_handles_auth?(send_node)
351
+ end
352
+ end
353
+
354
+ def check_before_action_blocks(class_node)
355
+ class_node.body&.each_descendant(:block) do |block_node|
356
+ next unless block_node.send_node.method_name == :before_action
357
+
358
+ register_auth_before_action(block_node.send_node) if block_contains_auth?(block_node)
359
+ end
360
+ end
361
+
362
+ def before_action_handles_auth?(node)
363
+ node.arguments.any? do |arg|
364
+ auth_symbol_argument?(arg)
365
+ end
366
+ end
367
+
368
+ def register_auth_before_action(node)
369
+ only, except = action_restrictions(node)
370
+
371
+ if only
372
+ only -= except if except
373
+ @only_actions.merge(only)
374
+ elsif except
375
+ @except_sets << except
376
+ else
377
+ @all_actions_auth = true
378
+ end
379
+ end
380
+
381
+ def action_exempted_by_before_action?(method_name)
382
+ return true if @all_actions_auth
383
+ return true if @only_actions.include?(method_name)
384
+ return true if @except_sets.any? { |actions| actions.none?(method_name) }
385
+
386
+ false
387
+ end
388
+
389
+ def action_restrictions(node)
390
+ only = nil
391
+ except = nil
392
+
393
+ node.arguments.each do |arg|
394
+ next unless arg.hash_type?
395
+
396
+ arg.pairs.each do |pair|
397
+ next unless pair.key.sym_type?
398
+
399
+ case pair.key.value
400
+ when :only
401
+ only = covered_actions(pair.value)
402
+ when :except
403
+ except = covered_actions(pair.value)
404
+ end
405
+ end
406
+ end
407
+
408
+ [only, except]
409
+ end
410
+
411
+ def covered_actions(value)
412
+ nodes = value.array_type? ? value.children : [value]
413
+
414
+ nodes.select { |node| node.sym_type? || node.str_type? }
415
+ .to_set { |node| node.value.to_sym }
416
+ end
417
+
418
+ def block_contains_auth?(block_node)
419
+ body = block_node.body
420
+ return false unless body
421
+
422
+ return true if authorization_call?(body)
423
+
424
+ body.each_descendant(:send).any? do |send_node|
425
+ authorization_call?(send_node)
426
+ end
427
+ end
428
+
429
+ def auth_symbol_argument?(arg)
430
+ return false unless arg.sym_type?
431
+
432
+ name = arg.value.to_s
433
+ BEFORE_ACTION_AUTH_KEYWORDS.any? { |keyword| name.include?(keyword) }
434
+ end
435
+
436
+ def contains_authorization_check?(node)
437
+ return false unless node.body
438
+
439
+ body = node.body
440
+
441
+ return true if authorization_call?(body)
442
+
443
+ body.each_descendant(:send).any? do |send_node|
444
+ authorization_call?(send_node)
445
+ end
446
+ end
447
+
448
+ def authorization_call?(send_node)
449
+ return false unless send_node.send_type?
450
+
451
+ return true if AUTHORIZATION_METHODS.include?(send_node.method_name)
452
+
453
+ method_name = send_node.method_name.to_s
454
+ AUTHORIZATION_PREFIXES.any? { |prefix| method_name.start_with?(prefix) }
455
+ end
456
+ end
457
+ end
458
+ end
459
+ end