Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed admin brakeman issues

Damian Legawieca7511ceca368
diff --git a/admin/app/controllers/spree/admin/page_blocks_controller.rb b/admin/app/controllers/spree/admin/page_blocks_controller.rb
index f97c1ca6ee..bc58e1b7ea 100644
--- a/admin/app/controllers/spree/admin/page_blocks_controller.rb
+++ b/admin/app/controllers/spree/admin/page_blocks_controller.rb
@@ -27,9 +27,10 @@ module Spree
def create
page_block_type = params.dig(:page_block, :type)
+ allowed_type = allowed_types.find { |type| type.to_s == page_block_type }
- if allowed_types.map(&:to_s).include?(page_block_type) && page_block_type.safe_constantize.present?
- @page_block = page_block_type.constantize.new
+ if allowed_type
+ @page_block = allowed_type.new
@page_block.section = @page_section
@page_block.save!
end
diff --git a/admin/app/controllers/spree/admin/page_sections_controller.rb b/admin/app/controllers/spree/admin/page_sections_controller.rb
index b35d5158b4..21a8ee3de8 100644
--- a/admin/app/controllers/spree/admin/page_sections_controller.rb
+++ b/admin/app/controllers/spree/admin/page_sections_controller.rb
@@ -9,10 +9,15 @@ module Spree
page_section_type = params.dig(:page_section, :type)
allowed_types = available_page_section_types.map(&:to_s)
- if allowed_types.include?(page_section_type) && page_section_type.safe_constantize.present?
- @page_section = page_section_type.constantize.new(permitted_resource_params)
- @page_section.pageable = @pageable
- @page_section.save!
+ if allowed_types.include?(page_section_type)
+ # Find the actual class from our allowed types rather than using constantize
+ section_class = available_page_section_types.find { |type| type.to_s == page_section_type }
+
+ if section_class
+ @page_section = section_class.new(permitted_resource_params)
+ @page_section.pageable = @pageable
+ @page_section.save!
+ end
end
end
diff --git a/admin/app/controllers/spree/admin/payment_methods_controller.rb b/admin/app/controllers/spree/admin/payment_methods_controller.rb
index 184e1227c0..92d7dc104a 100644
--- a/admin/app/controllers/spree/admin/payment_methods_controller.rb
+++ b/admin/app/controllers/spree/admin/payment_methods_controller.rb
@@ -11,7 +11,15 @@ module Spree
private
def build_resource
- @object = params[:payment_method].delete(:type).constantize.new if params[:payment_method].present?
+ if params[:payment_method].present?
+ payment_type = params[:payment_method].delete(:type)
+ # Find the actual class from our allowed types rather than using constantize
+ payment_class = allowed_payment_types.find { |type| type.to_s == payment_type }
+
+ if payment_class
+ @object = payment_class.new
+ end
+ end
end
def collection
@@ -35,6 +43,10 @@ module Spree
def set_breadcrumb
add_breadcrumb @payment_method.name, spree.edit_admin_payment_method_path(@payment_method)
end
+
+ def allowed_payment_types
+ Rails.application.config.spree.payment_methods
+ end
end
end
end
diff --git a/admin/app/controllers/spree/admin/promotion_actions_controller.rb b/admin/app/controllers/spree/admin/promotion_actions_controller.rb
index 852c4e8a38..d70c8c165f 100644
--- a/admin/app/controllers/spree/admin/promotion_actions_controller.rb
+++ b/admin/app/controllers/spree/admin/promotion_actions_controller.rb
@@ -11,8 +11,12 @@ module Spree
def model_class
@model_class = if params.dig(:promotion_action, :type).present?
- if allowed_action_types.map(&:to_s).include?(params.dig(:promotion_action, :type))
- params.dig(:promotion_action, :type).safe_constantize
+ # Find the actual class from allowed types rather than using constantize
+ action_type = params.dig(:promotion_action, :type)
+ action_class = allowed_action_types.find { |type| type.to_s == action_type }
+
+ if action_class
+ action_class
else
raise 'Unknown promotion action type'
end
diff --git a/admin/app/controllers/spree/admin/promotion_rules_controller.rb b/admin/app/controllers/spree/admin/promotion_rules_controller.rb
index f81aa62e07..9a5c8778a7 100644
--- a/admin/app/controllers/spree/admin/promotion_rules_controller.rb
+++ b/admin/app/controllers/spree/admin/promotion_rules_controller.rb
@@ -9,8 +9,12 @@ module Spree
def model_class
@model_class = if params.dig(:promotion_rule, :type).present?
- if allowed_rule_types.map(&:to_s).include?(params.dig(:promotion_rule, :type))
- params.dig(:promotion_rule, :type).safe_constantize
+ # Find the actual class from allowed types rather than using constantize
+ rule_type = params.dig(:promotion_rule, :type)
+ rule_class = allowed_rule_types.find { |type| type.to_s == rule_type }
+
+ if rule_class
+ rule_class
else
raise 'Unknown promotion rule type'
end
diff --git a/admin/app/controllers/spree/admin/reports_controller.rb b/admin/app/controllers/spree/admin/reports_controller.rb
index 99d9de40e9..83251e8c1d 100644
--- a/admin/app/controllers/spree/admin/reports_controller.rb
+++ b/admin/app/controllers/spree/admin/reports_controller.rb
@@ -36,9 +36,12 @@ module Spree
def model_class
@model_class = if params[:type].present?
+ # Find the actual class from allowed types rather than using constantize
report_type = "Spree::Reports::#{params[:type].classify}"
- if allowed_report_types.include?(report_type)
- report_type.constantize
+ report_class = allowed_report_types.find { |type| type == report_type }
+
+ if report_class
+ Object.const_get(report_class)
else
raise 'Unknown report type'
end
diff --git a/admin/app/controllers/spree/admin/translations_controller.rb b/admin/app/controllers/spree/admin/translations_controller.rb
index 200eb93ef1..f60fa44f1d 100644
--- a/admin/app/controllers/spree/admin/translations_controller.rb
+++ b/admin/app/controllers/spree/admin/translations_controller.rb
@@ -36,7 +36,15 @@ module Spree
end
def resource_class
- @resource_class ||= params[:resource_type].constantize
+ @resource_class ||= begin
+ klass = params[:resource_type]
+ allowed_resource_class.find { |allowed_class| allowed_class.to_s == klass } ||
+ raise(ActiveRecord::RecordNotFound, "Resource type not found")
+ end
+ end
+
+ def allowed_resource_class
+ Rails.application.config.spree.translatable_resources
end
def set_resource
diff --git a/admin/app/helpers/spree/admin/tags_helper.rb b/admin/app/helpers/spree/admin/tags_helper.rb
index 920479ec98..d82061fedc 100644
--- a/admin/app/helpers/spree/admin/tags_helper.rb
+++ b/admin/app/helpers/spree/admin/tags_helper.rb
@@ -12,7 +12,7 @@ module Spree
def post_tags_scope
@post_tags_scope ||= ActsAsTaggableOn::Tag.
joins(:taggings).
- where("#{ActsAsTaggableOn.taggings_table}.taggable_type = ?", 'Spree::Post').
+ where(ActsAsTaggableOn.taggings_table => { taggable_type: 'Spree::Post' }).
for_context(:tags).for_tenant(current_store.id)
end
@@ -23,7 +23,7 @@ module Spree
def user_tags_scope
@user_tags_scope ||= ActsAsTaggableOn::Tag.
joins(:taggings).
- where("#{ActsAsTaggableOn.taggings_table}.taggable_type = ?", Spree.user_class.to_s).
+ where(ActsAsTaggableOn.taggings_table => { taggable_type: Spree.user_class.to_s }).
for_context(:tags)
end
diff --git a/admin/app/views/spree/admin/coupon_codes/index.html.erb b/admin/app/views/spree/admin/coupon_codes/index.html.erb
index 75e3e05bba..be8c7082bb 100644
--- a/admin/app/views/spree/admin/coupon_codes/index.html.erb
+++ b/admin/app/views/spree/admin/coupon_codes/index.html.erb
@@ -19,14 +19,14 @@
</button>
<div class="dropdown-menu w-100 font-size-sm" style="min-width: 200px">
<%= active_link_to Spree.t(:all),
- spree.admin_promotion_coupon_codes_path,
+ spree.admin_promotion_coupon_codes_path(@promotion),
class: 'dropdown-item',
active: (params[:q] || {}).values_at(:state_eq).all?(&:blank?) %>
<%= link_to Spree.t('admin.coupon_codes.used'),
- params.merge({q: {state_eq: :used}}).permit!,
+ spree.admin_promotion_coupon_codes_path(@promotion, q: {state_eq: :used}),
class: "dropdown-item #{'active' if params.dig(:q, :state_eq) == 'used'}" %>
<%= link_to Spree.t('admin.coupon_codes.unused'),
- params.merge({q: {state_eq: :unused}}).permit!,
+ spree.admin_promotion_coupon_codes_path(@promotion, q: {state_eq: :unused}),
class: "dropdown-item #{'active' if params.dig(:q, :state_eq) == 'unused'}" %>
</div>
</div>
diff --git a/admin/app/views/spree/admin/orders/_shipment.html.erb b/admin/app/views/spree/admin/orders/_shipment.html.erb
index 561c535bc3..2149008ae4 100644
--- a/admin/app/views/spree/admin/orders/_shipment.html.erb
+++ b/admin/app/views/spree/admin/orders/_shipment.html.erb
@@ -65,7 +65,7 @@
<strong><%= Spree.t(:tracking) %>: </strong>
<% if shipment.tracked? %>
<% if shipment.tracking_url.present? %>
- <span><%= link_to shipment.tracking, shipment.tracking_url, target: '_blank', rel: "noopener noreferrer" %></span>
+ <span><%= link_to ERB::Util.html_escape(shipment.tracking), ERB::Util.html_escape(shipment.tracking_url), target: '_blank', rel: "noopener noreferrer" %></span>
<% elsif shipment.tracking.present? %>
<span><%= shipment.tracking %></span>
<% end %>
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 7e3902d28c..42360b8196 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -21,6 +21,7 @@ module Spree
:page_sections,
:page_blocks,
:reports,
+ :translatable_resources,
:analytics_events,
:analytics_event_handlers,
:integrations)
@@ -235,6 +236,13 @@ module Spree
Spree::Reports::SalesTotal
]
+ Rails.application.config.spree.translatable_resources = [
+ Spree::Product,
+ Spree::Taxon,
+ Spree::Taxonomy,
+ Spree::Store
+ ]
+
Rails.application.config.spree.analytics_events = {
product_viewed: 'Product Viewed',
product_list_viewed: 'Product List Viewed',