Class: Spree::StoreScopeGuard
- Inherits:
-
Object
- Object
- Spree::StoreScopeGuard
- Defined in:
- lib/spree/store_scope_guard.rb
Overview
Development/test tripwire for cross-store data leaks (docs/plans/6.0-store-context-and-first-run-setup.md): while a guarded request runs, a SELECT against a store-owned table (any spree_* table carrying a store_id column) that is neither store-scoped nor id-filtered is reported — that is the shape of an IDOR/cross-store leak, since every controller lookup must go through a current_store association. Open-core isolation rests on that discipline (row-level enforcement is not a core feature), so this is the net under it.
Never active in production — it is a correctness net for development and
CI, not an enforcement layer. Mode comes from
Spree::Config[:store_scope_guard] / SPREE_STORE_SCOPE_GUARD:
'log' (default), 'raise', or 'off'.
A deliberately store-less lookup (a global credential search, a cross-store listing for the store switcher) opts out explicitly:
Spree::StoreScopeGuard.skip { Spree::ApiKey.find_by(token: token) }
Flags live in RequestStore — like Spree::Events' disable flags — so a request that leaks mid-flight cannot leave a reused server thread permanently skipped or watched.
Defined Under Namespace
Classes: UnscopedQueryError
Constant Summary collapse
- ACTIVE_KEY =
:spree_store_scope_guard_active- SKIP_KEY =
:spree_store_scope_guard_skip- IGNORED_TABLES =
Tables that answer store-less queries by design. spree_stores is the tenant root itself; the *_stores join tables are the legacy multi-store bridges, filtered by their other foreign key.
%w[ spree_stores spree_products_stores spree_promotions_stores spree_payment_methods_stores ].freeze
Class Method Summary collapse
- .active? ⇒ Boolean
-
.arm! ⇒ Object
Opens the window for whatever declared a store context — a job, a webhook, a console script — and leaves it open until Spree::Current resets, which Rails does per request and per job.
- .enabled? ⇒ Boolean
-
.mode ⇒ String
'log', 'raise' or 'off'.
-
.reset! ⇒ Object
Forgets memoized environment/schema state (specs, migrations).
-
.skip ⇒ Object
Suppresses the guard for a deliberately store-less lookup.
-
.watch ⇒ Object
Wraps a unit of work (an API request) in the guarded window.
-
.watchable_environment? ⇒ Boolean
Overridable: OSS keeps the guard out of production, but a multi-tenant enforcement layer may run it there as a log-mode canary.
Class Method Details
.active? ⇒ Boolean
97 98 99 100 101 |
# File 'lib/spree/store_scope_guard.rb', line 97 def active? return false if RequestStore.store[SKIP_KEY] RequestStore.store[ACTIVE_KEY] || Spree::Current.store_scope_guard_armed.present? end |
.arm! ⇒ Object
Opens the window for whatever declared a store context — a job, a
webhook, a console script — and leaves it open until Spree::Current
resets, which Rails does per request and per job. Called from
Spree::Current#store=; work that never declares a store stays
unguarded, since reading the default store through the fallback is
not a scoping claim.
81 82 83 84 85 86 |
# File 'lib/spree/store_scope_guard.rb', line 81 def arm! return unless enabled? install! Spree::Current.store_scope_guard_armed = true end |
.enabled? ⇒ Boolean
43 44 45 |
# File 'lib/spree/store_scope_guard.rb', line 43 def enabled? watchable_environment? && mode != 'off' end |
.mode ⇒ String
Returns 'log', 'raise' or 'off'.
58 59 60 |
# File 'lib/spree/store_scope_guard.rb', line 58 def mode Spree::Config[:store_scope_guard] end |
.reset! ⇒ Object
Forgets memoized environment/schema state (specs, migrations).
104 105 106 107 |
# File 'lib/spree/store_scope_guard.rb', line 104 def reset! remove_instance_variable(:@watchable_environment) if defined?(@watchable_environment) @watched_tables = nil end |
.skip ⇒ Object
Suppresses the guard for a deliberately store-less lookup.
89 90 91 92 93 94 95 |
# File 'lib/spree/store_scope_guard.rb', line 89 def skip previous = RequestStore.store[SKIP_KEY] RequestStore.store[SKIP_KEY] = true yield ensure RequestStore.store[SKIP_KEY] = previous end |
.watch ⇒ Object
Wraps a unit of work (an API request) in the guarded window. No-op when the guard is disabled.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/spree/store_scope_guard.rb', line 64 def watch return yield unless enabled? install! previous = RequestStore.store[ACTIVE_KEY] RequestStore.store[ACTIVE_KEY] = true yield ensure RequestStore.store[ACTIVE_KEY] = previous end |
.watchable_environment? ⇒ Boolean
Overridable: OSS keeps the guard out of production, but a multi-tenant enforcement layer may run it there as a log-mode canary. Memoized — this sits on the production request path, where it must answer "no" for free.
51 52 53 54 55 |
# File 'lib/spree/store_scope_guard.rb', line 51 def watchable_environment? return @watchable_environment if defined?(@watchable_environment) @watchable_environment = Rails.env.development? || Rails.env.test? end |