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

Add native / custom schemes to form-action CSP

Oliver Günther2d975cd9ad61
diff --git a/app/controllers/oauth/auth_base_controller.rb b/app/controllers/oauth/auth_base_controller.rb
index 560873907fb..ef8653f3906 100644
--- a/app/controllers/oauth/auth_base_controller.rb
+++ b/app/controllers/oauth/auth_base_controller.rb
@@ -42,19 +42,39 @@ module OAuth
def extend_content_security_policy
return unless pre_auth&.authorizable?
- additional_form_actions = application_redirect_uris
+ additional_form_actions = application_http_redirect_uris + application_native_redirect_uris
return if additional_form_actions.empty?
flash[:_csp_appends] = { form_action: additional_form_actions }
append_content_security_policy_directives flash[:_csp_appends]
end
- def application_redirect_uris
- pre_auth&.client&.application&.redirect_uri
- .to_s
- .split
+ def application_http_redirect_uris
+ registered_redirect_uris
.select { |url| url.start_with?('http') }
.map { |url| URI.join(url, '/').to_s }
end
+
+ def application_native_redirect_uris
+ registered_redirect_uris
+ .reject { |url| url.start_with?('http') || url.start_with?('urn') }
+ .map(&method(:parse_native_redirect_uri))
+ .compact
+ end
+
+ def parse_native_redirect_uri(value)
+ uri = URI.parse(value)
+ "#{uri.scheme}:"
+ rescue StandardError => e
+ Rails.logger.error "Failed to parse native URL '#{value}' for allowing CSP redirects: #{e.message} "
+ nil
+ end
+
+ def registered_redirect_uris
+ @registered_redirect_uris ||=
+ pre_auth&.client&.application&.redirect_uri
+ .to_s
+ .split
+ end
end
end
diff --git a/spec/requests/auth/oauth_login_csp_spec.rb b/spec/requests/auth/oauth_login_csp_spec.rb
index 8a893663fe8..c7aaf658e60 100644
--- a/spec/requests/auth/oauth_login_csp_spec.rb
+++ b/spec/requests/auth/oauth_login_csp_spec.rb
@@ -47,4 +47,15 @@ describe 'CSP appends on login form from oauth',
expect(location).to include("/login?back_url=#{CGI.escape(oauth_path)}")
end
end
+
+ context 'with redirect-uri being a custom scheme' do
+ let(:redirect_uri) { 'myscheme://custom-foobar' }
+
+ it 'appends given CSP appends from flash' do
+ get oauth_path
+
+ csp = response.headers['Content-Security-Policy']
+ expect(csp).to include "form-action 'self' myscheme:"
+ end
+ end
end