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 sanitization of customer search query

Damian Legawiece89def8e0916
diff --git a/spree/core/app/models/concerns/spree/searchable.rb b/spree/core/app/models/concerns/spree/searchable.rb
index 253493772f..6401ca0137 100644
--- a/spree/core/app/models/concerns/spree/searchable.rb
+++ b/spree/core/app/models/concerns/spree/searchable.rb
@@ -11,13 +11,18 @@ module Spree
encrypted_attributes = model_class.encrypted_attributes.presence || []
if encrypted_attributes.include?(attribute.to_sym)
- model_class.arel_table[attribute.to_sym].eq(query)
+ # Encrypted columns can only be compared by equality — wildcard
+ # LIKE escapes would prevent the row from matching itself, so pass
+ # the raw query straight through.
+ model_class.arel_table[attribute.to_sym].eq(query.to_s.strip)
else
- # `sanitize_query_for_search` escapes LIKE wildcards (`_`, `%`) with a
- # backslash. Pass `\` as the ESCAPE character so SQLite/MySQL honor the
- # escaping instead of treating the backslash as a literal — otherwise a
- # query like `[email protected]` never matches its own row.
- model_class.arel_table[attribute.to_sym].lower.matches("%#{query}%", '\\')
+ # Plain columns use case-insensitive LIKE. `sanitize_sql_like`
+ # escapes `_` and `%` so a query like `[email protected]`
+ # doesn't have its underscore treated as a wildcard matching
+ # `[email protected]`. Pass `\` as the ESCAPE character so
+ # SQLite/MySQL honor the escaping.
+ escaped = sanitize_query_for_search(query)
+ model_class.arel_table[attribute.to_sym].lower.matches("%#{escaped}%", '\\')
end
end
diff --git a/spree/core/app/models/concerns/spree/user_methods.rb b/spree/core/app/models/concerns/spree/user_methods.rb
index 818c41bbcb..c38b4a3203 100644
--- a/spree/core/app/models/concerns/spree/user_methods.rb
+++ b/spree/core/app/models/concerns/spree/user_methods.rb
@@ -107,17 +107,15 @@ module Spree
def self.search(query)
return none if query.blank?
- sanitized_query = sanitize_query_for_search(query)
-
+ # `search_condition` handles sanitization itself — it escapes LIKE
+ # wildcards for plain columns and compares encrypted columns by
+ # equality. Pass the raw query so it can branch correctly.
conditions = []
- # Email may be stored encrypted (equality lookups only), so we pass the
- # raw query — escaped LIKE wildcards would break encrypted comparisons.
- # Name columns are plain text and use sanitized input for LIKE safety.
- conditions << search_condition(self, :email, query.strip)
- conditions << search_condition(self, :first_name, sanitized_query)
- conditions << search_condition(self, :last_name, sanitized_query)
-
- full_name = NameOfPerson::PersonName.full(sanitized_query)
+ conditions << search_condition(self, :email, query)
+ conditions << search_condition(self, :first_name, query)
+ conditions << search_condition(self, :last_name, query)
+
+ full_name = NameOfPerson::PersonName.full(query.to_s.strip)
if full_name.first.present? && full_name.last.present?
conditions << search_condition(self, :first_name, full_name.first)