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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: consent could mint a grant that carried no permissions
Damian Legawiec12bd8bd09217
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/oauth/authorize.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/oauth/authorize.tsxindex 8d40b0900f..47b2896c47 100644--- a/packages/dashboard/src/routes/_authenticated/$storeId/oauth/authorize.tsx+++ b/packages/dashboard/src/routes/_authenticated/$storeId/oauth/authorize.tsx@@ -184,7 +184,10 @@ function OauthAuthorizePage() {) : null}<div className="flex gap-3">- <Button disabled={pending} onClick={() => decide(approve)}>+ <Button+ disabled={pending || selected.length === 0}+ onClick={() => decide(approve)}+ >{t('admin.pages.oauth.authorize.approve')}</Button><Button variant="outline" disabled={pending} onClick={() => decide(deny)}>diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rbindex 3517a48f9a..aaa1722c05 100644--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb@@ -22,9 +22,24 @@ module Spree# meaningfully name.skip_scope_check!+ # Raised when Doorkeeper refuses the bounded pre-authorization.+ # An exception rather than a render, because the refusal happens+ # inside a helper the action reads a value from — returning there+ # would hand the action a rendered body to call methods on.+ class InvalidAuthorization < StandardError; end++ rescue_from InvalidAuthorization do |error|+ render_error(+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:resource_invalid],+ message: error.message,+ status: :unprocessable_content+ )+ end+before_action :require_signed_in_admin!before_action :require_client_of_this_store!before_action :load_pre_authorization+ before_action :require_something_to_grant!, only: :createdef showrender json: {@@ -153,6 +168,22 @@ module Spree# cut to what this person can actually hand over. Rebuilt rather# than mutated, because the scope is read in several places on# the way to the grant.+ # A grant that carries nothing is not a grant. It happens when the+ # merchant clears every box, or when the client asks only for+ # permissions this person does not hold, and it would otherwise+ # mint a token that authorizes nothing while the client reports a+ # successful connection and the merchant sees the application+ # listed as connected.+ def require_something_to_grant!+ return if grantable_scopes.any?++ render_error(+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:resource_invalid],+ message: 'Select at least one permission to grant.',+ status: :unprocessable_content+ )+ end+def granted_code_requestbounded = ::Doorkeeper::OAuth::PreAuthorization.new(::Doorkeeper.config,@@ -163,13 +194,8 @@ module Spree# Validation is what resolves `client`, and the grant is written# from `pre_auth.client.id` — an unvalidated pre-authorization# carries a nil client and fails deep inside the gem.- unless bounded.authorizable?- return render_error(- code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:resource_invalid],- message: bounded.error_response.body[:error_description].to_s,- status: :unprocessable_content- )- end+ raise InvalidAuthorization, bounded.error_response.body[:error_description].to_s unless+ bounded.authorizable?::Doorkeeper::OAuth::CodeRequest.new(bounded, current_actor)enddiff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rbindex 7e35c36455..0c1231c6c6 100644--- a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb@@ -53,6 +53,16 @@ RSpec.describe 'Admin OAuth consent', type: :request doexpect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to contain_exactly('read_products')end+ # Asking only for what this person cannot grant leaves nothing to grant.+ # Minting a token then would tell the client it had connected while the+ # merchant sees an application listed as connected that can do nothing.+ it 'leaves nothing to grant, and the request is refused' do+ expect { approve(staffer, scope: 'write_orders write_settings') }.+ not_to change(Spree::OauthAccessGrant, :count)++ expect(response).to have_http_status(:unprocessable_content)+ end+it 'is not offered on the consent screen either' doget '/api/v3/admin/oauth/authorize',params: authorize_params(scope: 'write_orders read_products'),