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.

Fix: a read-only agent reached three guards written for people

Damian Legawiec99611ef6f91e
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
index abe192f2ef..d087974e33 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
@@ -98,7 +98,17 @@ module Spree
# A `jwt_only` skip exempts the JWT/publishable principal we now know
# this is (no secret key resolved) — shell data the dashboard reads
# without a specific scope.
- return if scope_check_skipped_for_action? && self.class._scope_check_skip_jwt_only
+ #
+ # Not a delegated credential, though. The exemption exists because
+ # a signed-in staff member is already bounded by their role, and an
+ # OAuth grant is bounded by something narrower that this would skip
+ # — it carries no API key, so it was taking the human's exemption
+ # and reading settings its scopes never covered.
+ if scope_check_skipped_for_action? && self.class._scope_check_skip_jwt_only
+ return if current_oauth_token.blank?
+
+ return authorize_staff_permission!
+ end
# JWT staff pass through the same key gate as secret keys: their
# roles' catalog keys play the role scopes play for keys, so both
diff --git a/spree/api/app/controllers/spree/api/v3/admin/me_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/me_controller.rb
index aff7e6ff52..dab44bcc30 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/me_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/me_controller.rb
@@ -45,6 +45,8 @@ module Spree
# *other* users. `avatar_signed_id` takes a signed id from
# POST /files to set the photo, or `null` to remove it.
def update
+ return refuse_delegated_write if current_oauth_token.present?
+
if current_user.update(permitted_params)
render json: me_response
else
@@ -54,6 +56,20 @@ module Spree
private
+ # A grant acts *for* a staff member, not *as* one. This action is
+ # unguarded because it was only ever the person at the keyboard
+ # editing their own profile — but a delegated credential reaches it
+ # too, and the name it could rewrite is the one audit trails show
+ # as the actor. There is no catalog permission for "edit my own
+ # profile", so the answer is that an agent has no business here.
+ def refuse_delegated_write
+ render_error(
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
+ message: 'A connected agent cannot change the profile of the person who connected it.',
+ status: :forbidden
+ )
+ end
+
# A request authenticated by a secret API key has no Spree user to
# describe/update, so it gets a 404 pointing at the key endpoint
# rather than a 500 from a nil user — mirroring ApiKeysController#current.
@@ -82,11 +98,15 @@ module Spree
params.permit(*declared_permitted_attributes)
end
+ # What this credential may do, which is not always what its owner
+ # may do: a grant narrows the approver's role, so reporting the
+ # role would both disclose more than was handed over and mislead a
+ # client that reads /me to decide what to attempt.
def me_response
{
user: admin_user_serializer.new(current_user, params: serializer_params).to_h,
permissions: serialize_permissions(current_ability),
- permission_keys: serialize_permission_keys(current_ability)
+ permission_keys: staff_permission_keys
}
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
index 0abbc68177..66d6c0bb1e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
@@ -82,18 +82,21 @@ module Spree
end
# Per-type scope check for API-key principals: a key listing product
- # tags needs `read_products`, order tags `read_orders`, etc. JWT
- # admins are gated by store membership + CanCanCan, not scopes.
+ # tags needs `read_products`, order tags `read_orders`, etc.
+ #
+ # Asked of the credential, not of its kind: an OAuth grant carries
+ # no API key, so a check that began `return unless current_api_key`
+ # skipped itself entirely and handed an agent every order and
+ # customer tag the approver could see.
def authorize_taggable_scope!
- return unless current_api_key
return unless taggable_type
required = self.class.scope_for_taggable_type.fetch(taggable_type, 'read_all')
- return if current_api_key.has_scope?(required)
+ return if holds_permission?(required)
render_error(
code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
- message: "API key lacks scope: #{required}",
+ message: "Missing permission: #{required}",
status: :forbidden,
details: { required_scope: required }
)
diff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth_access_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth_access_spec.rb
index 041f233ac8..d84bfac6c6 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/oauth_access_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth_access_spec.rb
@@ -156,6 +156,61 @@ RSpec.describe 'Admin API OAuth access', type: :request do
# RFC 8707: a token names the resources it may be used against. A client
# that asked for one endpoint must not receive a credential good across the
# rest of the API.
+ # A grant authenticates AS the staff member who approved it, so every
+ # guard written for "a human at the dashboard" now answers for an agent
+ # too. These three asked what kind of credential had arrived instead of
+ # what it held, and a read-only grant walked through all of them.
+ describe 'a guard that was written before delegated credentials existed' do
+ let(:read_only) { token_for('read_products', resource: '/api/v3/admin') }
+
+ it 'refuses to let an agent rewrite the profile of the person who connected it' do
+ patch '/api/v3/admin/me',
+ params: { first_name: 'PWNED' }.to_json,
+ headers: { 'Authorization' => "Bearer #{read_only}", 'CONTENT_TYPE' => 'application/json' }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(admin.reload.first_name).not_to eq('PWNED')
+ end
+
+ # The tag list is per taggable type, and its check began
+ # `return unless current_api_key` — which an OAuth request never has.
+ it 'refuses order tags to a grant that holds no read_orders' do
+ get_with(read_only, '/api/v3/admin/tags?taggable_type=order')
+
+ expect(response).to have_http_status(:forbidden)
+ end
+
+ it 'allows them to a grant that does' do
+ get_with(token_for('read_orders', resource: '/api/v3/admin'),
+ '/api/v3/admin/tags?taggable_type=order')
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ # `jwt_only` exempts a signed-in staff member from the scope gate because
+ # their role already bounds them. A grant is bounded by something
+ # narrower, and was taking the exemption.
+ it 'refuses store settings to a grant that holds no read_settings' do
+ get_with(read_only, '/api/v3/admin/store')
+
+ expect(response).to have_http_status(:forbidden)
+ end
+
+ it 'allows them to a grant that does' do
+ get_with(token_for('read_settings', resource: '/api/v3/admin'), '/api/v3/admin/store')
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ # /me is what a client reads to decide what to attempt, so reporting the
+ # approver's whole role both oversells the grant and discloses the role.
+ it 'reports what the credential holds rather than what its owner does' do
+ get_with(read_only, '/api/v3/admin/me')
+
+ expect(json_response['permission_keys']).to eq(['read_products'])
+ end
+ end
+
describe 'a token issued for another resource' do
it 'is refused' do
get_with(token_for('read_products', resource: '/api/v3/admin/mcp'),