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

Fix spec not testing for response headers in selenium

Oliver Günther739860cc48f9
diff --git a/app/controllers/oauth/auth_base_controller.rb b/app/controllers/oauth/auth_base_controller.rb
index c8a2b6ac182..1e29dfde3e7 100644
--- a/app/controllers/oauth/auth_base_controller.rb
+++ b/app/controllers/oauth/auth_base_controller.rb
@@ -40,5 +40,11 @@ module OAuth
def extend_content_security_policy
use_content_security_policy_named_append(:oauth)
end
+
+ def allowed_forms
+ allowed_redirect_urls = pre_auth&.client&.application&.redirect_uri
+ urls = allowed_redirect_urls.to_s.split
+ urls.map { |url| URI.join(url, '/') }.map(&:to_s)
+ end
end
end
diff --git a/config/initializers/secure_headers.rb b/config/initializers/secure_headers.rb
index a93c09084b8..aeffb0ce845 100644
--- a/config/initializers/secure_headers.rb
+++ b/config/initializers/secure_headers.rb
@@ -75,12 +75,7 @@ SecureHeaders::Configuration.default do |config|
end
SecureHeaders::Configuration.named_append(:oauth) do |request|
- hosts = []
- if request.controller_instance.respond_to?(:pre_auth, true)
- allowed_redirect_urls = request.controller_instance.send(:pre_auth)&.client&.application&.redirect_uri
- urls = allowed_redirect_urls.to_s.split
- hosts = urls.map { |url| URI.join(url, '/') }.map(&:to_s)
- end
+ hosts = request.controller_instance.try(:allowed_forms) || []
{ form_action: hosts }
end
diff --git a/spec/features/oauth/authorization_code_flow_spec.rb b/spec/features/oauth/authorization_code_flow_spec.rb
index 3cf126209f4..9d0a69abb76 100644
--- a/spec/features/oauth/authorization_code_flow_spec.rb
+++ b/spec/features/oauth/authorization_code_flow_spec.rb
@@ -28,11 +28,13 @@
require 'spec_helper'
-describe 'OAuth authorization code flow', type: :feature, js: true do
+describe 'OAuth authorization code flow',
+ type: :feature,
+ js: true do
let!(:user) { FactoryBot.create(:user) }
let!(:redirect_uri) { 'urn:ietf:wg:oauth:2.0:oob' }
let!(:allowed_redirect_uri) { redirect_uri }
- let!(:app) { FactoryBot.create(:oauth_application, name: 'Cool API app!') }
+ let!(:app) { FactoryBot.create(:oauth_application, name: 'Cool API app!', redirect_uri: allowed_redirect_uri) }
let(:client_secret) { app.plaintext_secret }
def oauth_path(client_id, redirect_url)
@@ -40,7 +42,19 @@ describe 'OAuth authorization code flow', type: :feature, js: true do
end
before do
- # Do not login, will do that in the oauth flow
+ @record_requests = Billy.config.record_requests
+ @whitelist = Billy.config.whitelist
+ Billy.configure do |c|
+ c.record_requests = true
+ c.whitelist = []
+ end
+ end
+
+ after do
+ Billy.configure do |c|
+ c.record_requests = @record_requests
+ c.whitelist = @whitelist
+ end
end
it 'can authorize and manage an OAuth application grant' do
@@ -56,8 +70,19 @@ describe 'OAuth authorization code flow', type: :feature, js: true do
expect(page).to have_selector('li strong', text: I18n.t('oauth.scopes.api_v3'))
expect(page).to have_selector('li', text: I18n.t('oauth.scopes.api_v3_text'))
- # Check that the hosts of allowed redirection urls are present in the content security policy
- expect(page.response_headers['content-security-policy']).to include("form-action 'self' urn:ietf:wg:oauth:2.0:oob;")
+ first = true
+ allow_any_instance_of(::OAuth::AuthBaseController)
+ .to receive(:allowed_forms).and_wrap_original do |m|
+ forms = m.call
+
+ # Multiple requests end up here with one not containing the request url
+ if first
+ expect(forms).to include redirect_uri
+ first = false
+ end
+
+ forms
+ end
# Authorize
find('input.button[value="Authorize"]').click
@@ -126,19 +151,33 @@ describe 'OAuth authorization code flow', type: :feature, js: true do
expect(user.oauth_grants.count).to eq 0
end
- context 'with real urls as allowed redirect uris' do
- let!(:redirect_uri) { "https://foo.com/foo " }
- let!(:allowed_redirect_uri) { "#{redirect_uri} https://bar.com/bar" }
- it 'can authorize and manage an OAuth application grant' do
- visit oauth_path app.uid, redirect_uri
+ # Selenium can't return response headers
+ context 'in browser that can log response headers', js: false do
+ before do
+ login_as user
+ end
+
+ context 'with real urls as allowed redirect uris' do
+ let!(:redirect_uri) { "https://foo.com/foo " }
+ let!(:allowed_redirect_uri) { "#{redirect_uri} https://bar.com/bar" }
+ it 'can authorize and manage an OAuth application grant' do
+ visit oauth_path app.uid, redirect_uri
+
+ allow_any_instance_of(::OAuth::AuthBaseController)
+ .to receive(:allowed_forms).and_wrap_original do |m|
+ forms = m.call
+
+ expect(forms).to include 'https://foo.com/'
+ expect(forms).to include 'https://bar.com/'
- # Expect we're guided to the login screen
- login_with user.login, 'adminADMIN!', visit_signin_path: false
+ forms
+ end
- # Check that the hosts of allowed redirection urls are present in the content security policy
- expect(page.response_headers['content-security-policy']).to(
- include("form-action 'self' https://foo.com/ https://bar.com/;")
- )
+ # Check that the hosts of allowed redirection urls are present in the content security policy
+ expect(page.response_headers['content-security-policy']).to(
+ include("form-action 'self' https://foo.com/ https://bar.com/;")
+ )
+ end
end
end
end