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
Use same Secure Context validation for OAuth applications
Wieland Lindenthal8230707bcf8f
diff --git a/app/validators/secure_context_uri_validator.rb b/app/validators/secure_context_uri_validator.rbindex e2e9802638d..377e6e710d8 100644--- a/app/validators/secure_context_uri_validator.rb+++ b/app/validators/secure_context_uri_validator.rb@@ -35,24 +35,22 @@ class SecureContextUriValidator < ActiveModel::EachValidatorbeginuri = URI.parse(value)rescue StandardError- contract.errors.add(attribute, :could_not_parse_host_uri)+ contract.errors.add(attribute, :invalid_url)returnend# The URI could be parsable but not contain a host nameif uri.host.nil?- contract.errors.add(attribute, :could_not_parse_host_uri)+ contract.errors.add(attribute, :invalid_url)returnend- unless secure_context_uri?(uri)- contract.errors.add(attribute, :uri_not_secure_context)+ unless self.class.secure_context_uri?(uri)+ contract.errors.add(attribute, :url_not_secure_context)endend- private-- def secure_context_uri?(uri)+ def self.secure_context_uri?(uri)return true if uri.scheme == 'https' # https is always safereturn true if uri.host == 'localhost' # Simple localhostreturn true if uri.host =~ /\.localhost\.?$/ # i.e. 'foo.localhost' or 'foo.localhost.'diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rbindex 12819f09402..910f484973c 100644--- a/config/initializers/doorkeeper.rb+++ b/config/initializers/doorkeeper.rb@@ -124,7 +124,7 @@ Doorkeeper.configure do## force_ssl_in_redirect_uri !Rails.env.development?#- force_ssl_in_redirect_uri { |uri| uri.host != 'localhost' }+ force_ssl_in_redirect_uri { |uri| !SecureContextUriValidator.secure_context_uri?(uri) }# Specify what redirect URI's you want to block during Application creation.# Any redirect URI is whitelisted by default.diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 004b32791e7..a020d5ab256 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -672,6 +672,8 @@ en:unknown_property: "is not a known property."unknown_property_nested: "has the unknown path '%{path}'."unremovable: "cannot be removed."+ url_not_secure_context: >+ is not providing a "Secure Context". Either use HTTPS or a loopback address, such as localhost.wrong_length: "is the wrong length (should be %{count} characters)."models:attachment:@@ -703,7 +705,7 @@ en:fragment_present: 'cannot contain a fragment.'invalid_uri: 'must be a valid URI.'relative_uri: 'must be an absolute URI.'- secured_uri: 'must be an HTTPS/SSL URI.'+ secured_uri: 'is not providing a "Secure Context". Either use HTTPS or a loopback address, such as localhost.'forbidden_uri: 'is forbidden by the server.'scopes:not_match_configured: "doesn't match available scopes."diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex 0383a06cc3c..2adbb48267b 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -30,8 +30,6 @@ en:is not fully set up. That Nextcloud instance still redirects to a URL containing 'index.php'. That is astrong indicator that the server does not have mod_rewrite, mod_headers and/or mod_env fully configured,which is necessary for a Bearer token based authorization of API requests.- could_not_parse_host_uri: "could not parse the host URI"- uri_not_secure_context: "is not a secure context (HTTPS or localhost)"storages/file_link:attributes:origin_id:diff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rbindex f723a37681f..39e560321fd 100644--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb@@ -119,13 +119,13 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true dcontext 'when host is an unsafe IP' dolet(:storage_host) { 'http://172.16.193.146' }- include_examples 'contract is invalid', host: :uri_not_secure_context+ include_examples 'contract is invalid', host: :url_not_secure_contextendcontext 'when host is an unsafe hostname' dolet(:storage_host) { 'http://nc.openproject.com' }- include_examples 'contract is invalid', host: :uri_not_secure_context+ include_examples 'contract is invalid', host: :url_not_secure_contextendcontext 'when provider_type is nextcloud' dodiff --git a/spec/features/admin/oauth/oauth_applications_management_spec.rb b/spec/features/admin/oauth/oauth_applications_management_spec.rbindex 2f864863892..b6c126e539d 100644--- a/spec/features/admin/oauth/oauth_applications_management_spec.rb+++ b/spec/features/admin/oauth/oauth_applications_management_spec.rb@@ -51,6 +51,14 @@ describe 'OAuth applications management', type: :feature, js: true doexpect(page).to have_selector('.errorExplanation', text: 'Redirect URI must be an absolute URI.')+ SeleniumHubWaiter.wait+ fill_in('application_redirect_uri', with: "")+ # Fill rediret_uri which does not provide a Secure Context+ fill_in 'application_redirect_uri', with: "http://example.org"+ click_on 'Create'++ expect(page).to have_selector('.errorExplanation', text: 'Redirect URI is not providing a "Secure Context"')+# Can create localhost without https (https://community.openproject.com/wp/34025)SeleniumHubWaiter.waitfill_in 'application_redirect_uri', with: "urn:ietf:wg:oauth:2.0:oob\nhttp://localhost/my/callback"diff --git a/spec/validator/secure_context_uri_validator_spec.rb b/spec/validator/secure_context_uri_validator_spec.rbindex 74a3c2aaac2..274ceff779f 100644--- a/spec/validator/secure_context_uri_validator_spec.rb+++ b/spec/validator/secure_context_uri_validator_spec.rb@@ -53,9 +53,9 @@ describe SecureContextUriValidator dodescribe "when URI is '#{uri}'" dolet(:host) { uri }- it "adds an :could_not_parse_host_uri error" do+ it "adds an :invalid_url error" doexpect(model_instance.errors).to include(:host)- expect(model_instance.errors.first.type).to be :could_not_parse_host_uri+ expect(model_instance.errors.first.type).to be :invalid_urlendendend@@ -66,9 +66,9 @@ describe SecureContextUriValidator dodescribe "when URI is '#{uri}'" dolet(:host) { uri }- it "adds an :could_not_parse_host_uri error" do+ it "adds an :invalid_url error" doexpect(model_instance.errors).to include(:host)- expect(model_instance.errors.first.type).to be :could_not_parse_host_uri+ expect(model_instance.errors.first.type).to be :invalid_urlendendend@@ -78,9 +78,9 @@ describe SecureContextUriValidator docontext 'when host is missing' dolet(:host) { 'https://' }- it "adds an :could_not_parse_host_uri error" do+ it "adds an :invalid_url error" doexpect(model_instance.errors).to include(:host)- expect(model_instance.errors.first.type).to be :could_not_parse_host_uri+ expect(model_instance.errors.first.type).to be :invalid_urlendend@@ -89,9 +89,9 @@ describe SecureContextUriValidator dodescribe "when URI is '#{uri}'" dolet(:host) { uri }- it "adds a :uri_not_secure_context error" do+ it "adds a :url_not_secure_context error" doexpect(model_instance.errors).to include(:host)- expect(model_instance.errors.first.type).to be :uri_not_secure_context+ expect(model_instance.errors.first.type).to be :url_not_secure_contextendendend