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.

Tighten RANSACK_ID_PREDICATE_RE to require a predicate suffix

Damian Legawiec9cc5910c12e3
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb b/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
index 2285e6f2dd..2a3add7530 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
@@ -10,7 +10,10 @@ module Spree
# `ResourceController` — duplicated here because `FiltersController`
# extends `Store::BaseController`, not `ResourceController`, and
# would otherwise NoMethodError on `decode_prefixed_id_predicates`.
- RANSACK_ID_PREDICATE_RE = /(?:\A|_)id(?:s)?(?:_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq))?\z/.freeze
+ # Requires a Ransack-predicate suffix so we don't match scope
+ # names like `with_option_value_ids` (which handle their own
+ # prefix decoding).
+ RANSACK_ID_PREDICATE_RE = /(?:\A|_)id(?:s)?_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq)\z/.freeze
private
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
index b7071a90ae..8f1e95bd87 100644
--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -189,7 +189,13 @@ module Spree
# and the bare-`id` predicates (`id_in`, `id_eq`) on the resource's
# primary key. Without the bare-id branch, `q[id_in][]=prod_x` would
# be passed to Ransack verbatim and never match any row.
- RANSACK_ID_PREDICATE_RE = /(?:\A|_)id(?:s)?(?:_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq))?\z/.freeze
+ #
+ # Requires a Ransack-predicate suffix (`_eq`, `_in`, ...) — bare
+ # `_id`/`_ids` keys without a suffix are scope names, not predicates
+ # (e.g. `with_option_value_ids` is a custom scope that handles its
+ # own decoding). Decoding those would double-strip prefixes and
+ # break downstream filter code.
+ RANSACK_ID_PREDICATE_RE = /(?:\A|_)id(?:s)?_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq)\z/.freeze
def ransack_id_predicate?(key)
RANSACK_ID_PREDICATE_RE.match?(key.to_s)
end