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
Set default host at all times
Oliver Güntherc7286fb91faa
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 3310a9db849..1b8498c4d01 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -51,6 +51,7 @@ class ApplicationController < ActionController::Baseinclude ::OpenProject::Authentication::SessionExpiryinclude AdditionalUrlHelpersinclude OpenProjectErrorHelper+ include Security::DefaultUrlOptionslayout "base"@@ -148,13 +149,6 @@ class ApplicationController < ActionController::Baseinclude Redmine::MenuManager::MenuControllerhelper Redmine::MenuManager::MenuHelper- def default_url_options(_options = {})- {- layout: params["layout"],- protocol: Setting.protocol- }- end-# set http headers so that the browser does not store any# data (caches) of this site# see:diff --git a/app/controllers/concerns/security/default_url_options.rb b/app/controllers/concerns/security/default_url_options.rbnew file mode 100644index 00000000000..e648f54572d--- /dev/null+++ b/app/controllers/concerns/security/default_url_options.rb@@ -0,0 +1,41 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Security+ module DefaultUrlOptions+ # Determine host for links on our static host name+ def default_url_options(_options = {})+ {+ layout: params["layout"],+ protocol: Setting.protocol,+ host: Setting.host_without_protocol,+ port: Setting.optional_port_from_host_name+ }.compact+ end+ end+enddiff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rbindex aa7d59807d0..f7aa7a827eb 100644--- a/app/controllers/errors_controller.rb+++ b/app/controllers/errors_controller.rb@@ -1,8 +1,11 @@-class ErrorsController < ActionController::Base+class ErrorsController < ApplicationControllerinclude ErrorsHelperinclude OpenProjectErrorHelperinclude Accounts::CurrentUser+ no_authorization_required! :not_found, :unacceptable, :internal_error+ skip_before_action :check_if_login_required+def not_foundrender_404enddiff --git a/app/controllers/sys_controller.rb b/app/controllers/sys_controller.rbindex e709ef3b705..3d5b76cc753 100644--- a/app/controllers/sys_controller.rb+++ b/app/controllers/sys_controller.rb@@ -29,6 +29,8 @@require "open_project/repository_authentication"class SysController < ActionController::Base+ include Security::DefaultUrlOptions+before_action :check_enabledbefore_action :require_basic_auth, only: [:repo_auth]before_action :find_project, only: [:update_required_storage]diff --git a/app/models/setting/aliases.rb b/app/models/setting/aliases.rbindex cc008cf2833..363706189e6 100644--- a/app/models/setting/aliases.rb+++ b/app/models/setting/aliases.rb@@ -43,7 +43,13 @@ class Setting### Host name without protocoldef host_without_protocol- Setting.host_name.split(":").first+ Setting.host_name&.split(":")&.first+ end++ ##+ # Port from host_name if set+ def optional_port_from_host_name+ Setting.host_name&.split(":")&.[](1)endendenddiff --git a/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb b/modules/auth_saml/spec/requests/saml_provider_callback_spec.rbindex 628a636d18b..5b652a904ad 100644--- a/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb+++ b/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb@@ -88,7 +88,7 @@ RSpec.describe "SAML provider callback", with_ee: %i[openid_providers] doit "redirects user when no errors occured" doexpect(subject.status).to eq(302)- expect(subject.headers["Location"]).to eq("http://example.org/two_factor_authentication/request")+ expect(subject.headers["Location"]).to eq("http://#{Setting.host_name}/two_factor_authentication/request")endcontext "with an invalid timestamp" do@@ -114,7 +114,7 @@ RSpec.describe "SAML provider callback", with_ee: %i[openid_providers] doit "redirects the user to 2FA, but with back_url present in session" doexpect(subject.status).to eq(302)- expect(subject.headers["Location"]).to eq("http://example.org/two_factor_authentication/request")+ expect(subject.headers["Location"]).to eq("http://test.host/two_factor_authentication/request")session = Sessions::SqlBypass.lookup_data(subject.cookies["_open_project_session"].first)expect(session["back_url"]).to eq "/projects?some_param=true"@@ -123,7 +123,7 @@ RSpec.describe "SAML provider callback", with_ee: %i[openid_providers] docontext "when 2FA is disabled", :skip_2fa_stage doit "redirects directly" doexpect(subject.status).to eq(302)- expect(subject.headers["Location"]).to eq("http://example.org/projects?some_param=true")+ expect(subject.headers["Location"]).to eq("http://test.host/projects?some_param=true")endendenddiff --git a/modules/bim/spec/requests/api/bcf/v2_1/auth_api_spec.rb b/modules/bim/spec/requests/api/bcf/v2_1/auth_api_spec.rbindex cd66bac52cf..fd9e95736eb 100644--- a/modules/bim/spec/requests/api/bcf/v2_1/auth_api_spec.rb+++ b/modules/bim/spec/requests/api/bcf/v2_1/auth_api_spec.rb@@ -51,8 +51,8 @@ RSpec.describe "BCF 2.1 auth resource", content_type: :json doit_behaves_like "bcf api successful response" dolet(:expected_body) do{- oauth2_auth_url: "http://localhost:3000/oauth/authorize",- oauth2_token_url: "http://localhost:3000/oauth/token",+ oauth2_auth_url: "http://#{Setting.host_name}/oauth/authorize",+ oauth2_token_url: "http://#{Setting.host_name}/oauth/token",http_basic_supported: false,supported_oauth2_flows: %w(authorization_code_grant client_credentials)}diff --git a/modules/openid_connect/app/session/session_controller.rb b/modules/openid_connect/app/session/session_controller.rbindex c86ec40f506..7e29c1add93 100644--- a/modules/openid_connect/app/session/session_controller.rb+++ b/modules/openid_connect/app/session/session_controller.rb@@ -1,4 +1,6 @@-class SessionController < ActionController::Base+class SessionController < ApplicationController+ no_authorization_required! :logout_warning+def logout_warningurl = signin_url back_url: params[:back_url]diff --git a/modules/storages/spec/requests/project_storages_open_spec.rb b/modules/storages/spec/requests/project_storages_open_spec.rbindex d5f61a9cc25..d1b45a8b209 100644--- a/modules/storages/spec/requests/project_storages_open_spec.rb+++ b/modules/storages/spec/requests/project_storages_open_spec.rb@@ -36,7 +36,7 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" dolet(:project_storage) { create(:project_storage, project:, storage:) }let(:route) { "projects/#{project.identifier}/project_storages/#{project_storage.id}/open" }let(:expected_redirect_url) do- "#{last_request.scheme}://#{last_request.host}#{expected_redirect_path}"+ "#{Setting.protocol}://#{Setting.host_name}#{expected_redirect_path}"endshared_let(:project) { create(:project) }@@ -101,7 +101,7 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" doexpect(last_response).to have_http_status(:found)expect(last_response.headers["Location"]).to eq (- "http://example.org/oauth_clients/#{storage.oauth_client.client_id}/ensure_connection?destination_url=http%3A%2F%2Fexample.org%2Fprojects%2F#{project.identifier}%2Fproject_storages%2F#{project_storage.id}%2Fopen&storage_id=#{storage.id}"+ "http://#{Setting.host_name}/oauth_clients/#{storage.oauth_client.client_id}/ensure_connection?destination_url=http%3A%2F%2F#{CGI.escape(Setting.host_name)}%2Fprojects%2F#{project.identifier}%2Fproject_storages%2F#{project_storage.id}%2Fopen&storage_id=#{storage.id}")endend@@ -111,7 +111,7 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" doget route, {}, { "HTTP_ACCEPT" => "text/html" }expect(last_response).to have_http_status(:found)- expect(last_response.headers["Location"]).to eq ("http://example.org/projects/#{project.identifier}")+ expect(last_response.headers["Location"]).to eq ("http://#{Setting.host_name}/projects/#{project.identifier}")expect(last_request.session["flash"]["flashes"]).to eq({"modal" => {@@ -142,7 +142,7 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" doget route, {}, { "HTTP_ACCEPT" => "text/html" }expect(last_response).to have_http_status(:found)- expect(last_response.headers["Location"]).to eq ("http://example.org/projects/#{project.identifier}")+ expect(last_response.headers["Location"]).to eq ("http://#{Setting.host_name}/projects/#{project.identifier}")expect(last_request.session["flash"]["flashes"]).to eq({"modal" => {diff --git a/modules/storages/spec/requests/storages/project_settings/oauth_access_grant_flow_spec.rb b/modules/storages/spec/requests/storages/project_settings/oauth_access_grant_flow_spec.rbindex eb2def019f9..53aeca18b20 100644--- a/modules/storages/spec/requests/storages/project_settings/oauth_access_grant_flow_spec.rb+++ b/modules/storages/spec/requests/storages/project_settings/oauth_access_grant_flow_spec.rb@@ -85,7 +85,7 @@ RSpec.describe "GET /projects/:project_id/settings/project_storages/:id/oauth_ac)expect(last_response.cookies["oauth_state_#{nonce}"])- .to eq([CGI.escape({ href: "http://example.org/projects/#{project.id}/settings/project_storages/external_file_storages",+ .to eq([CGI.escape({ href: "http://#{Setting.host_name}/projects/#{project.id}/settings/project_storages/external_file_storages",storageId: project_storage.storage_id }.to_json)])endend@@ -105,7 +105,7 @@ RSpec.describe "GET /projects/:project_id/settings/project_storages/:id/oauth_acstorage.oauth_clientexpect(last_response).to have_http_status(:found)- expect(last_response.location).to eq("http://example.org/projects/#{project.id}/settings/project_storages/external_file_storages")+ expect(last_response.location).to eq("http://#{Setting.host_name}/projects/#{project.id}/settings/project_storages/external_file_storages")expect(last_response.cookies.keys).to eq(["_open_project_session"])endenddiff --git a/spec/features/auth/omniauth_spec.rb b/spec/features/auth/omniauth_spec.rbindex 3957086fbd9..b1069314a56 100644--- a/spec/features/auth/omniauth_spec.rb+++ b/spec/features/auth/omniauth_spec.rb@@ -32,20 +32,6 @@ RSpec.describe "Omniauth authentication" do# Load ViewAccountLoginAuthProvider to have this spec passingOpenProject::Hooks::ViewAccountLoginAuthProvider- # Running the tests inside docker changes the hostname. To accommodate that we changed- # the Capybara app_host, however this change was not being reflected in the Rails host,- # causing the redirect checks to fail below.- def self.default_url_options- host =- if Capybara.app_host- Capybara.app_host.sub(/https?\/\//, "")- else- "www.example.com"- end-- { host: }- end-let(:user) docreate(:user,force_password_change: false,diff --git a/spec/requests/auth/token_based_access_spec.rb b/spec/requests/auth/token_based_access_spec.rbindex 32c5476df93..87612f321fe 100644--- a/spec/requests/auth/token_based_access_spec.rb+++ b/spec/requests/auth/token_based_access_spec.rb@@ -40,7 +40,7 @@ RSpec.describe "Token based access", type: :rails_request, with_settings: { logi# work_packages of a private projectget "/work_packages/#{work_package.id}.atom"expect(response)- .to redirect_to(signin_path(back_url: "http://www.example.com/work_packages/#{work_package.id}"))+ .to redirect_to(signin_path(back_url: "http://#{Setting.host_name}/work_packages/#{work_package.id}"))# access is possible with a tokenget "/work_packages/#{work_package.id}.atom?key=#{rss_key}"@@ -50,6 +50,6 @@ RSpec.describe "Token based access", type: :rails_request, with_settings: { logi# but for the next request, the user is not logged inget "/work_packages/#{work_package.id}"expect(response)- .to redirect_to(signin_path(back_url: "http://www.example.com/work_packages/#{work_package.id}"))+ .to redirect_to(signin_path(back_url: "http://#{Setting.host_name}/work_packages/#{work_package.id}"))endenddiff --git a/spec/requests/oauth_clients/ensure_connection_flow_spec.rb b/spec/requests/oauth_clients/ensure_connection_flow_spec.rbindex e2799dfb9df..0b871edcb61 100644--- a/spec/requests/oauth_clients/ensure_connection_flow_spec.rb+++ b/spec/requests/oauth_clients/ensure_connection_flow_spec.rb@@ -85,7 +85,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :we"callback&response_type=code&state=#{nonce}")expect(last_response.cookies["oauth_state_#{nonce}"])- .to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com" \+ .to eq(["%7B%22href%22%3A%22http%3A%2F%2F#{CGI.escape(Setting.host_name)}" \"%2F%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])endend@@ -106,7 +106,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :we"callback&response_type=code&state=#{nonce}")expect(last_response.cookies["oauth_state_#{nonce}"])- .to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com" \+ .to eq(["%7B%22href%22%3A%22http%3A%2F%2F#{CGI.escape(Setting.host_name)}" \"%2F123%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])endend@@ -126,7 +126,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :we"callback&response_type=code&state=#{nonce}")expect(last_response.cookies["oauth_state_#{nonce}"])- .to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com" \+ .to eq(["%7B%22href%22%3A%22http%3A%2F%2F#{CGI.escape(Setting.host_name)}" \"%2F%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])endend@@ -154,7 +154,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :weget oauth_clients_ensure_connection_url(oauth_client_id: oauth_client.client_id, storage_id: storage.id)expect(last_response).to have_http_status(:found)- expect(last_response.location).to eq("http://www.example.com/")+ expect(last_response.location).to eq("http://#{Setting.host_name}/")expect(last_response.cookies.keys).to eq(["_open_project_session"])endend@@ -168,7 +168,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :westorage.oauth_clientexpect(last_response).to have_http_status(:found)- expect(last_response.location).to eq("http://www.example.com/123")+ expect(last_response.location).to eq("http://#{Setting.host_name}/123")expect(last_response.cookies.keys).to eq(["_open_project_session"])endend@@ -181,7 +181,7 @@ RSpec.describe "/oauth_clients/:oauth_client_id/ensure_connection endpoint", :westorage.oauth_clientexpect(last_response).to have_http_status(:found)- expect(last_response.location).to eq("http://www.example.com/")+ expect(last_response.location).to eq("http://#{Setting.host_name}/")expect(last_response.cookies.keys).to eq(["_open_project_session"])endenddiff --git a/spec/requests/openid_google_provider_callback_spec.rb b/spec/requests/openid_google_provider_callback_spec.rbindex 43906e46995..1045b75dd88 100644--- a/spec/requests/openid_google_provider_callback_spec.rb+++ b/spec/requests/openid_google_provider_callback_spec.rb@@ -97,6 +97,6 @@ RSpec.describe "OpenID Google provider callback", with_ee: %i[openid_providers]} doresponse = get uri.to_sexpect(response).to have_http_status(:found)- expect(response.location).to eq("http://example.org/two_factor_authentication/request")+ expect(response.location).to eq("http://#{Setting.host_name}/two_factor_authentication/request")endenddiff --git a/spec/support/capybara.rb b/spec/support/capybara.rbindex 146c1a299fd..48a9d74b818 100644--- a/spec/support/capybara.rb+++ b/spec/support/capybara.rb@@ -21,6 +21,16 @@ RSpec.shared_context "with default_url_options and host name set to Capybara tesendend+RSpec.shared_context "with host name set to test.host" do+ around do |example|+ original_host_setting = Setting.host_name+ Setting.host_name = "test.host"+ example.run+ ensure+ Setting.host_name = original_host_setting+ end+end+RSpec.configure do |config|Capybara.default_max_wait_time = 4Capybara.javascript_driver = :chrome_en@@ -31,17 +41,37 @@ RSpec.configure do |config|endCapybara.always_include_port = true- if ENV["CAPYBARA_DYNAMIC_BIND_IP"]- ip_address = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address- hostname = ENV.fetch("CAPYBARA_APP_HOSTNAME", ip_address)- Capybara.server_host = ip_address- Capybara.app_host = "http://#{hostname}"- else- Capybara.server_host = ENV.fetch("CAPYBARA_APP_HOSTNAME", "localhost")- end+ hostname =+ if ENV["CAPYBARA_DYNAMIC_BIND_IP"]+ ip_address = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address+ Capybara.server_host = ip_address++ ENV.fetch("CAPYBARA_APP_HOSTNAME", ip_address)+ else+ ENV.fetch("CAPYBARA_APP_HOSTNAME", "localhost")+ end++ Capybara.server_host = hostname+ Capybara.app_host = "http://#{hostname}:#{Capybara.server_port}"+ Capybara.default_host = Capybara.app_host# Set the default optionsconfig.include_context "with default_url_options and host name set to Capybara test server", type: :feature+ config.include_context "with host name set to test.host", type: :controller++ # Fix the host name for rails_request specs+ %i[request rails_request].each do |type|+ config.include_context("with host name set to test.host", type:)+ config.before(:each, type:) do+ host! "test.host"++ # Rack::Test uses a respond_to?(:default_host) to check for overwritten host+ # and there appears to be no other option+ def default_host+ "test.host"+ end+ end+ end# Make it possible to match on value attribute.#