Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#50871] handle empty cookie error cases

Eric Schubert0b4f22d1956b
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index bcf67502fb4..f678ed817c8 100644
--- a/app/controllers/oauth_clients_controller.rb
+++ b/app/controllers/oauth_clients_controller.rb
@@ -132,12 +132,20 @@ class OAuthClientsController < ApplicationController
)
end
+ # rubocop:disable Metrics/AbcSize
def find_oauth_client
- # FIXME: This is a hack, fetching additional information of the storage to identify the oauth client.
- # This must be fixed in #50872.
- state_cookie = MultiJson.load(cookies["oauth_state_#{@oauth_state}"], symbolize_keys: true)
- @oauth_client = OAuthClient.find_by(client_id: params[:oauth_client_id],
- integration_id: state_cookie[:storageId])
+ oauth_client_from_cookie = -> do
+ cookie = cookies["oauth_state_#{@oauth_state}"]
+ return nil if cookie.blank?
+
+ # FIXME: This is a hack, fetching additional information of the storage to identify the oauth client.
+ # This must be fixed in #50872.
+ state_value = MultiJson.load(cookie, symbolize_keys: true)
+ @oauth_client = OAuthClient.find_by(client_id: params[:oauth_client_id],
+ integration_id: state_value[:storageId])
+ end
+
+ @oauth_client = oauth_client_from_cookie.call
if @oauth_client.nil?
# oauth_client can be nil if OAuthClient was not found.
# This happens during admin setup if the user forgot to update the return_uri
@@ -153,6 +161,8 @@ class OAuthClientsController < ApplicationController
end
end
+ # rubocop:enable Metrics/AbcSize
+
def redirect_user_or_admin(redirect_uri = nil)
# This needs to be modified as soon as we support more integration types.
if User.current.admin && redirect_uri && (nextcloud? || one_drive?)
diff --git a/app/services/oauth_clients/redirect_uri_from_state_service.rb b/app/services/oauth_clients/redirect_uri_from_state_service.rb
index ebfa198b7d2..def3aeba92d 100644
--- a/app/services/oauth_clients/redirect_uri_from_state_service.rb
+++ b/app/services/oauth_clients/redirect_uri_from_state_service.rb
@@ -51,8 +51,11 @@ module OAuthClients
def oauth_state_cookie
return nil if @state.blank?
- state_cookie = MultiJson.load(@cookies["oauth_state_#{@state}"], symbolize_keys: true)
- state_cookie[:href]
+ state_cookie = @cookies["oauth_state_#{@state}"]
+ return nil if state_cookie.blank?
+
+ state_value = MultiJson.load(@cookies["oauth_state_#{@state}"], symbolize_keys: true)
+ state_value[:href]
end
end
end
diff --git a/spec/requests/oauth_clients/callback_flow_spec.rb b/spec/requests/oauth_clients/callback_flow_spec.rb
index 42a9f8a0fea..223266b2fdb 100644
--- a/spec/requests/oauth_clients/callback_flow_spec.rb
+++ b/spec/requests/oauth_clients/callback_flow_spec.rb
@@ -51,12 +51,8 @@ RSpec.describe 'OAuthClient callback endpoint' do
client_id: 'kETWr2XsjPxhVbN7Q5jmPq83xribuUTRzgfXthpYT0vSqyJWm4dOnivKzHiZasf0',
client_secret: 'J1sg4L5PYbM2RZL3pUyxTnamvfpcP5eUcCPmeCQHJO60Gy6CJIdDaF4yXOeC8BPS')
end
- let(:rack_oauth2_client) do
- instance_double(Rack::OAuth2::Client)
- end
- let(:connection_manager) do
- instance_double(OAuthClients::ConnectionManager)
- end
+ let(:rack_oauth2_client) { instance_double(Rack::OAuth2::Client) }
+ let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }
let(:uri) { URI(File.join('oauth_clients', oauth_client.client_id, 'callback')) }
subject(:response) { last_response }
@@ -66,13 +62,13 @@ RSpec.describe 'OAuthClient callback endpoint' do
allow(Rack::OAuth2::Client).to receive(:new).and_return(rack_oauth2_client)
allow(rack_oauth2_client)
- .to receive(:access_token!).with(:body)
- .and_return(
- Rack::OAuth2::AccessToken::Bearer.new(access_token: 'xyzaccesstoken',
- refresh_token: 'xyzrefreshtoken')
- )
+ .to receive(:access_token!)
+ .with(:body)
+ .and_return(Rack::OAuth2::AccessToken::Bearer.new(access_token: 'xyzaccesstoken',
+ refresh_token: 'xyzrefreshtoken'))
allow(rack_oauth2_client).to receive(:authorization_code=)
- set_cookie "oauth_state_asdf1234=#{redirect_uri}"
+ state_cookie = CGI.escape({ href: redirect_uri, storageId: oauth_client.integration_id }.to_json)
+ set_cookie "oauth_state_asdf1234=#{state_cookie}"
end
# rubocop:disable RSpec/Rails/HaveHttpStatus
diff --git a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
index 9323d81bb26..360cb841928 100644
--- a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
+++ b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb
@@ -32,7 +32,7 @@ require 'webmock/rspec'
RSpec.describe OAuthClients::RedirectUriFromStateService, type: :model do
let(:state) { 'asdf123425' }
let(:redirect_uri) { File.join(API::V3::Utilities::PathHelper::ApiV3Path::root_url, 'foo/bar') }
- let(:cookies) { { "oauth_state_#{state}": redirect_uri }.with_indifferent_access }
+ let(:cookies) { { "oauth_state_#{state}": { href: redirect_uri }.to_json }.with_indifferent_access }
let(:instance) { described_class.new(state:, cookies:) }
describe '#call' do