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.

Scope the media keys to the library alone

Damian Legawiec7adf9275e895
diff --git a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
index 98104ce111..ba22bc1939 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/media_controller.rb
@@ -30,6 +30,12 @@ module Spree
Spree.api.admin_media_serializer
end
+ # A product's gallery is part of the product: authorizing the product
+ # here is the whole check, and the per-record hook stays a no-op so a
+ # media key is never required to manage a product's own pictures.
+ # The media keys govern the library.
+ def authorize_resource!(resource = @resource, action = action_name.to_sym); end
+
def set_parent
@product = current_store.products.find_by_prefix_id!(params[:product_id])
authorize!(:update, @product)
@@ -94,8 +100,6 @@ module Spree
end
def create_from_url
- authorize!(:create, Spree::Media)
-
url = permitted_params[:url]
position = permitted_params[:position]
diff --git a/spree/api/app/controllers/spree/api/v3/admin/media_library_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/media_library_controller.rb
index 636ac1ad03..eb809727b8 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/media_library_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/media_library_controller.rb
@@ -95,8 +95,12 @@ module Spree
super + %w[usage]
end
+ # The library authorizes the media record itself; the product-nested
+ # controller authorizes its product instead, so this restores the
+ # per-record check its no-op skips. `usage` maps to `:show` because
+ # CanCanCan's `:read` alias covers only index and show.
def authorize_resource!(resource = @resource, action = action_name.to_sym)
- super(resource, action == :usage ? :show : action)
+ authorize!(action == :usage ? :show : action, resource || Spree::Media)
end
# No parent to resolve. The library is the store's own collection, so
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
index ccd14a54a1..92ee09888f 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/media_controller_spec.rb
@@ -351,6 +351,57 @@ RSpec.describe Spree::Api::V3::Admin::MediaController, type: :controller do
end
end
+ # A product's gallery is part of the product: the media keys govern the
+ # library, not this product's own pictures. Both directions matter — media
+ # permissions must not become a back door into editing products either.
+ describe 'authorization through the product' do
+ let(:user) { create(:admin_user, :without_admin_role) }
+ let(:jwt) do
+ Spree::Api::V3::TestingSupport.generate_jwt(
+ user, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_ADMIN
+ )
+ end
+ let(:headers) { { 'Authorization' => "Bearer #{jwt}" } }
+
+ before { store.add_user(user, role) }
+
+ def add_external_video
+ post :create, params: {
+ product_id: product.prefixed_id,
+ media_type: 'external_video',
+ external_video_url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
+ }, as: :json
+ end
+
+ context 'with product permissions' do
+ let(:role) { create(:role, name: 'Products', permissions: %w[read_products write_products]) }
+
+ it 'allows adding to the gallery' do
+ add_external_video
+
+ expect(response).to have_http_status(:created)
+ end
+
+ it 'allows fetching an image by URL' do
+ post :create, params: {
+ product_id: product.prefixed_id, url: 'https://example.com/a.jpg'
+ }, as: :json
+
+ expect(response).to have_http_status(:accepted)
+ end
+ end
+
+ context 'with media permissions only' do
+ let(:role) { create(:role, name: 'Media', permissions: %w[read_media write_media]) }
+
+ it 'refuses adding to the gallery' do
+ add_external_video
+
+ expect(response).to have_http_status(:forbidden)
+ end
+ end
+ end
+
describe 'placing a library file on a product' do
let!(:target) { create(:product) }