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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex c8a2b6ac182..1e29dfde3e7 100644--- a/app/controllers/oauth/auth_base_controller.rb+++ b/app/controllers/oauth/auth_base_controller.rb@@ -40,5 +40,11 @@ module OAuthdef extend_content_security_policyuse_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)+ endendenddiff --git a/config/initializers/secure_headers.rb b/config/initializers/secure_headers.rbindex a93c09084b8..aeffb0ce845 100644--- a/config/initializers/secure_headers.rb+++ b/config/initializers/secure_headers.rb@@ -75,12 +75,7 @@ SecureHeaders::Configuration.default do |config|endSecureHeaders::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 }enddiff --git a/spec/features/oauth/authorization_code_flow_spec.rb b/spec/features/oauth/authorization_code_flow_spec.rbindex 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 dolet!(: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 doendbefore 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+ endendit 'can authorize and manage an OAuth application grant' do@@ -56,8 +70,19 @@ describe 'OAuth authorization code flow', type: :feature, js: true doexpect(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# Authorizefind('input.button[value="Authorize"]').click@@ -126,19 +151,33 @@ describe 'OAuth authorization code flow', type: :feature, js: true doexpect(user.oauth_grants.count).to eq 0end- 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/;")+ )+ endendendend