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

[#56334] moved url builder to higher context

Eric Schubertccebe4e63c08
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
index 764c4b4b0e3..aa8a038b827 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
@@ -52,13 +52,10 @@ module Storages
end
def to_httpx_oauth_config
- issuer = URI(StorageInteraction::RequestUrlBuilder.build(@storage,
- "/index.php/apps/oauth2/api/v1")).normalize
-
StorageInteraction::AuthenticationStrategies::OAuthConfiguration.new(
client_id: @oauth_client.client_id,
client_secret: @oauth_client.client_secret,
- issuer:,
+ issuer: URI(UrlBuilder.url(@storage.uri, "/index.php/apps/oauth2/api/v1")).normalize,
scope: []
)
end
@@ -77,8 +74,8 @@ module Storages
scheme: uri.scheme,
host: uri.host,
port: uri.port,
- authorization_endpoint: StorageInteraction::RequestUrlBuilder.path(uri.path, "/index.php/apps/oauth2/authorize"),
- token_endpoint: StorageInteraction::RequestUrlBuilder.path(uri.path, "/index.php/apps/oauth2/api/v1/token")
+ authorization_endpoint: UrlBuilder.path(uri.path, "/index.php/apps/oauth2/authorize"),
+ token_endpoint: UrlBuilder.path(uri.path, "/index.php/apps/oauth2/api/v1/token")
)
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
index 5ea1cc9bccc..276a0b6f8d3 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
@@ -51,10 +51,7 @@ module Storages
.basic_auth(@username, @password)
.with(headers: { "OCS-APIRequest" => "true" })
.post(
- RequestUrlBuilder.build(@storage,
- "ocs/v1.php/cloud/users",
- user,
- "groups"),
+ UrlBuilder.url(@storage.uri, "ocs/v1.php/cloud/users", user, "groups"),
form: { "groupid" => CGI.escapeURIComponent(group) }
)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb
index 2ff3d4358db..69330d91b27 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb
@@ -45,7 +45,7 @@ module Storages
def call(auth_strategy:)
Authentication[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|
- handle_response http.get(RequestUrlBuilder.build(@storage, "/ocs/v1.php/cloud/user"))
+ handle_response http.get(UrlBuilder.url(@storage.uri, "/ocs/v1.php/cloud/user"))
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index d8e0e50a791..f16d41e5997 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -67,15 +67,8 @@ module Storages
end
def build_origin_urls(source_path:, destination_path:)
- source_url = RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- @storage.username,
- source_path)
-
- destination_url = RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- @storage.username,
- destination_path)
+ source_url = UrlBuilder.url(@storage.uri, "remote.php/dav/files", @storage.username, source_path)
+ destination_url = UrlBuilder.url(@storage.uri, "remote.php/dav/files", @storage.username, destination_path)
{ source_url:, destination_url: }
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
index 6cd8cd90fcf..772bee9f1b5 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
@@ -48,14 +48,12 @@ module Storages
.on_failure { |error| return error }
.result
- path_prefix = RequestUrlBuilder.path(@storage.uri.path,
- "remote.php/dav/files",
- origin_user_id)
- request_url = RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- origin_user_id,
- parent_location.path,
- folder_name)
+ path_prefix = UrlBuilder.path(@storage.uri.path, "remote.php/dav/files", origin_user_id)
+ request_url = UrlBuilder.url(@storage.uri,
+ "remote.php/dav/files",
+ origin_user_id,
+ parent_location.path,
+ folder_name)
create_folder_request(auth_strategy, request_url, path_prefix)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index ab0490f32d3..f74c78fccf4 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -61,8 +61,7 @@ module Storages
def direct_download_request(auth_strategy:, file_link:)
Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|
- result = handle_response http.post(RequestUrlBuilder.build(@storage,
- "/ocs/v2.php/apps/dav/api/v1/direct"),
+ result = handle_response http.post(UrlBuilder.url(@storage.uri, "/ocs/v2.php/apps/dav/api/v1/direct"),
json: { fileId: file_link.origin_id })
result.bind do |resp|
@@ -96,7 +95,7 @@ module Storages
end
def download_link(token, origin_name)
- RequestUrlBuilder.build(@storage, "index.php/apps/integration_openproject/direct", token, origin_name)
+ UrlBuilder.url(@storage.uri, "index.php/apps/integration_openproject/direct", token, origin_name)
end
def direct_download_token(body:)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
index 83279f0f208..7671b70ac87 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
@@ -71,7 +71,7 @@ module Storages
end
def file_info(http, file_id)
- response = http.get(RequestUrlBuilder.build(@storage, FILE_INFO_PATH, file_id))
+ response = http.get(UrlBuilder.url(@storage.uri, FILE_INFO_PATH, file_id))
error_data = StorageErrorData.new(source: self.class, payload: response)
case response
@@ -140,7 +140,7 @@ module Storages
idx += prefix.length - 1
- RequestUrlBuilder.path(file_path[idx..])
+ UrlBuilder.path(file_path[idx..])
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
index 43e081719df..779c3d8e4c6 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -63,7 +63,7 @@ module Storages
private
def files_info(http, file_ids)
- response = http.post(RequestUrlBuilder.build(@storage, FILES_INFO_PATH), json: { fileIds: file_ids })
+ response = http.post(UrlBuilder.url(@storage.uri, FILES_INFO_PATH), json: { fileIds: file_ids })
error_data = StorageErrorData.new(source: self.class, payload: response)
case response
@@ -143,7 +143,7 @@ module Storages
# in all supported versions of OpenProjectIntegation Nextcloud App.
file_path << "/" if mimetype == "application/x-op-directory" && file_path[-1] != "/"
- RequestUrlBuilder.path(file_path[idx..])
+ UrlBuilder.path(file_path[idx..])
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index 8a47b60059f..1b9ba3e9f2f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -46,9 +46,7 @@ module Storages
end
origin_user = origin_user_id(auth_strategy.user)
- @location_prefix = RequestUrlBuilder.path(@storage.uri.path,
- "remote.php/dav/files",
- origin_user)
+ @location_prefix = UrlBuilder.path(@storage.uri.path, "remote.php/dav/files", origin_user)
result = make_request(auth_strategy:, folder:, user: auth_strategy.user)
storage_files(result)
@@ -62,10 +60,10 @@ module Storages
Authentication[auth_strategy].call(storage: @storage,
http_options: Util.webdav_request_with_depth(1)) do |http|
response = http.request("PROPFIND",
- RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- origin_user,
- folder.path),
+ UrlBuilder.url(@storage.uri,
+ "remote.php/dav/files",
+ origin_user,
+ folder.path),
xml: requested_properties)
handle_response(response)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
index 55c319bb6cb..4249b21f643 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
@@ -49,8 +49,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.basic_auth(@username, @password)
.with(headers: { "OCS-APIRequest" => "true" })
.get(
- Storages::Peripherals::StorageInteraction::RequestUrlBuilder.build(
- @storage,
+ Storages::UrlBuilder.url(
+ @storage.uri,
"ocs/v1.php/cloud/groups",
CGI.escapeURIComponent(group)
)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
index f09231b839e..c6021441f8f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
@@ -47,7 +47,7 @@ module Storages
Authentication[auth_strategy].call(storage: @storage) do |http|
handle_response http.delete(
- RequestUrlBuilder.build(@storage, "remote.php/dav/files", origin_user_id, location)
+ UrlBuilder.url(@storage.uri, "remote.php/dav/files", origin_user_id, location)
)
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
index 311d2d29564..ad5a22c36db 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
@@ -73,10 +73,7 @@ module Storages
end
def call(http:, username:, path:, props:)
- request_uri = RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- username,
- path)
+ request_uri = UrlBuilder.url(@storage.uri, "remote.php/dav/files", username, path)
response = http.request(:propfind, request_uri, xml: request_body(props))
handle_response(response, username)
@@ -130,7 +127,7 @@ module Storages
def resource_path(section, username)
path = CGI.unescape(section.xpath("d:href").text.strip)
- .gsub!(RequestUrlBuilder.path(@storage.uri.path, "remote.php/dav/files", username), "")
+ .gsub!(UrlBuilder.path(@storage.uri.path, "remote.php/dav/files", username), "")
path.end_with?("/") && path.length > 1 ? path.chop : path
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query_legacy.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query_legacy.rb
index 014e6cf16f6..a080512170f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query_legacy.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query_legacy.rb
@@ -98,10 +98,7 @@ module Storages
.with(headers: { "Depth" => depth })
.request(
"PROPFIND",
- RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- @username,
- path),
+ UrlBuilder.url(@storage.uri, "remote.php/dav/files", @username, path),
xml: body
)
@@ -112,7 +109,7 @@ module Storages
doc = Nokogiri::XML(response.body.to_s)
result = {}
doc.xpath("/d:multistatus/d:response").each do |resource_section|
- source_path = RequestUrlBuilder.path(@storage.uri.path, "/remote.php/dav/files", @username)
+ source_path = UrlBuilder.path(@storage.uri.path, "/remote.php/dav/files", @username)
resource = CGI.unescape(resource_section.xpath("d:href").text.strip).gsub!(source_path, "")
result[resource] = {}
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb
index 5ad857f0a8c..1f9bfe317b9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb
@@ -44,7 +44,7 @@ module Storages
# rubocop:disable Lint/UnusedMethodArgument
def call(auth_strategy:, file_id:, open_location: false)
location_flag = open_location ? 0 : 1
- url = RequestUrlBuilder.build(@storage, "index.php/f/#{file_id}") + "?openfile=#{location_flag}"
+ url = UrlBuilder.url(@storage.uri, "index.php/f/#{file_id}") + "?openfile=#{location_flag}"
ServiceResult.success(result: url)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_storage_query.rb
index 79c97bcd059..5e63aac427a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_storage_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_storage_query.rb
@@ -43,7 +43,7 @@ module Storages
# rubocop:disable Lint/UnusedMethodArgument
def call(auth_strategy:)
- ServiceResult.success(result: RequestUrlBuilder.build(@storage, "index.php/apps/files"))
+ ServiceResult.success(result: UrlBuilder.url(@storage.uri, "index.php/apps/files"))
end
# rubocop:enable Lint/UnusedMethodArgument
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
index ad865088ba8..f2799d62428 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
@@ -48,8 +48,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.basic_auth(@username, @password)
.with(headers: { "OCS-APIRequest" => "true" })
.delete(
- Storages::Peripherals::StorageInteraction::RequestUrlBuilder.build(
- @storage,
+ Storages::UrlBuilder.url(
+ @storage.uri,
"ocs/v1.php/cloud/users",
user,
"groups"
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
index 2b025793110..865c0ad18ed 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
@@ -61,14 +61,15 @@ module Storages
private
def make_request(auth_strategy, user, file_info, name)
- source_path = RequestUrlBuilder.build(@storage,
- "remote.php/dav/files",
- user,
- CGI.unescape(file_info.location))
- destination = RequestUrlBuilder.path(@storage.uri.path,
- "remote.php/dav/files",
- user,
- CGI.unescape(target_path(file_info, name)))
+ source_path = UrlBuilder.url(@storage.uri,
+ "remote.php/dav/files",
+ user,
+ CGI.unescape(file_info.location))
+
+ destination = UrlBuilder.path(@storage.uri.path,
+ "remote.php/dav/files",
+ user,
+ CGI.unescape(target_path(file_info, name)))
Authentication[auth_strategy].call(storage: @storage) do |http|
handle_response http.request("MOVE", source_path, headers: { "Destination" => destination })
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
index 6f09f7ed991..eed0d4b2855 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -85,8 +85,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.basic_auth(@username, @password)
.request(
"PROPPATCH",
- Storages::Peripherals::StorageInteraction::RequestUrlBuilder
- .build(@storage, "remote.php/dav/files", @username, path),
+ Storages::UrlBuilder.url(@storage.uri, "remote.php/dav/files", @username, path),
xml: body
)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
index bf66441be32..2a8fd6f3fcc 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -58,11 +58,11 @@ module Storages
private
def base_uri
- RequestUrlBuilder.build(@storage, "index.php/apps/integration_openproject/direct-upload-token")
+ UrlBuilder.url(@storage.uri, "index.php/apps/integration_openproject/direct-upload-token")
end
def upload_base_uri
- RequestUrlBuilder.build(@storage, "index.php/apps/integration_openproject/direct-upload")
+ UrlBuilder.url(@storage.uri, "index.php/apps/integration_openproject/direct-upload")
end
def upload_data_failure
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/request_url_builder.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/request_url_builder.rb
deleted file mode 100644
index e13c98f33ff..00000000000
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/request_url_builder.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-# frozen_string_literal:true
-
-#-- 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 Storages
- module Peripherals
- module StorageInteraction
- class RequestUrlBuilder
- class << self
- def build(storage, *path_fragments)
- host_uri = storage.uri
-
- URI.join(host_uri.origin,
- ensure_sub_path(host_uri.path),
- *split_and_escape(path_fragments))
- .to_s
- end
-
- def path(*path_fragments)
- URI.join(URI("https://drop.me"), *split_and_escape(path_fragments)).path
- end
-
- private
-
- def ensure_sub_path(fragment)
- fragment.ends_with?("/") ? fragment : "#{fragment}/"
- end
-
- def split_and_escape(fragments)
- return fragments if fragments.empty?
-
- single_fragments = fragments
- .map { |f| f.split("/") }
- .flatten
- .reject(&:empty?)
- .each { |f| ensure_unescaped_fragments(f) }
- .map { |f| CGI.escapeURIComponent(f) }
-
- single_fragments[..-2]
- .map { |f| "#{f}/" }
- .push(single_fragments.last)
- end
-
- def ensure_unescaped_fragments(fragment)
- raise ArgumentError, "URL-escaped character found: #{fragment}" if CGI.unescape(fragment) != fragment
- end
- end
- end
- end
- end
-end
diff --git a/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb b/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
index ce7b1fee7b1..cd1eda0c8e2 100644
--- a/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
+++ b/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
@@ -86,19 +86,5 @@ module Storages::Admin::Forms
def default_form_url
admin_settings_storage_automatically_managed_project_folders_path(storage)
end
-
- def storage_provider_credentials_copy_instructions
- "#{I18n.t('storages.instructions.copy_from')}: #{provider_credentials_instructions_link}".html_safe
- end
-
- def provider_credentials_instructions_link
- render(
- Primer::Beta::Link.new(
- href: Storages::Peripherals::StorageInteraction::RequestUrlBuilder.build(storage,
- "settings/admin/openproject"),
- target: "_blank"
- )
- ) { I18n.t("storages.instructions.#{storage.short_provider_type}.integration") }
- end
end
end
diff --git a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
index 57be26c54ef..e7ee84b98c4 100644
--- a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
+++ b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
@@ -64,8 +64,7 @@ module Storages::Admin::Forms
end
def nextcloud_integration_link(target: "_blank")
- href = Storages::Peripherals::StorageInteraction::RequestUrlBuilder
- .build(storage, "settings/admin/openproject")
+ href = Storages::UrlBuilder.url(storage.uri, "settings/admin/openproject")
render(Primer::Beta::Link.new(href:, target:)) { I18n.t("storages.instructions.nextcloud.integration") }
end
diff --git a/modules/storages/app/components/storages/admin/oauth_application_info_copy_component.rb b/modules/storages/app/components/storages/admin/oauth_application_info_copy_component.rb
index 5b37b6e92a4..d5be8c22042 100644
--- a/modules/storages/app/components/storages/admin/oauth_application_info_copy_component.rb
+++ b/modules/storages/app/components/storages/admin/oauth_application_info_copy_component.rb
@@ -46,10 +46,7 @@ module Storages::Admin
def oauth_application_details_link
render(
Primer::Beta::Link.new(
- href: ::Storages::Peripherals::StorageInteraction::RequestUrlBuilder.build(
- storage,
- "settings/admin/openproject"
- ),
+ href: ::Storages::UrlBuilder.url(storage.uri, "settings/admin/openproject"),
target: "_blank"
)
) { I18n.t("storages.instructions.oauth_application_details_link_text") }
diff --git a/modules/storages/app/forms/storages/admin/managed_project_folders/application_password_input.rb b/modules/storages/app/forms/storages/admin/managed_project_folders/application_password_input.rb
index 78b72512b35..fecc2dc137b 100644
--- a/modules/storages/app/forms/storages/admin/managed_project_folders/application_password_input.rb
+++ b/modules/storages/app/forms/storages/admin/managed_project_folders/application_password_input.rb
@@ -55,8 +55,7 @@ module Storages::Admin::ManagedProjectFolders
def provider_type_link
render(
Primer::Beta::Link.new(
- href: Storages::Peripherals::StorageInteraction::RequestUrlBuilder.build(@storage,
- "settings/admin/openproject"),
+ href: Storages::UrlBuilder.url(@storage.uri, "settings/admin/openproject"),
target: "_blank"
)
) { I18n.t("storages.instructions.#{@storage.short_provider_type}.integration") }
diff --git a/modules/storages/app/helpers/storages/url_builder.rb b/modules/storages/app/helpers/storages/url_builder.rb
new file mode 100644
index 00000000000..41e1d861558
--- /dev/null
+++ b/modules/storages/app/helpers/storages/url_builder.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal:true
+
+#-- 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 Storages
+ class UrlBuilder
+ class << self
+ def url(uri, *path_fragments)
+ URI.join(uri.origin,
+ ensure_sub_path(uri.path),
+ *split_and_escape(path_fragments))
+ .to_s
+ end
+
+ def path(*path_fragments)
+ URI.join(URI("https://drop.me"), *split_and_escape(path_fragments)).path
+ end
+
+ private
+
+ def ensure_sub_path(fragment)
+ fragment.ends_with?("/") ? fragment : "#{fragment}/"
+ end
+
+ def split_and_escape(fragments)
+ return fragments if fragments.empty?
+
+ single_fragments = fragments
+ .map { |f| f.split("/") }
+ .flatten
+ .reject(&:empty?)
+ .each { |f| ensure_unescaped_fragments(f) }
+ .map { |f| CGI.escapeURIComponent(f) }
+
+ single_fragments[..-2]
+ .map { |f| "#{f}/" }
+ .push(single_fragments.last)
+ end
+
+ def ensure_unescaped_fragments(fragment)
+ raise ArgumentError, "URL-escaped character found: #{fragment}" if CGI.unescape(fragment) != fragment
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rb
index aa1420da726..3575337a0d3 100644
--- a/modules/storages/app/models/storages/project_storage.rb
+++ b/modules/storages/app/models/storages/project_storage.rb
@@ -70,7 +70,7 @@ module Storages
end
def project_folder_path_escaped
- escape_path(managed_project_folder_path)
+ UrlBuilder.path(managed_project_folder_path)
end
def file_inside_project_folder?(escaped_file_path)
@@ -122,9 +122,5 @@ module Storages
(project_folder_automatic? && !user.allowed_in_project?(:read_files, project)) ||
project_folder_id.blank?
end
-
- def escape_path(path)
- Peripherals::StorageInteraction::RequestUrlBuilder.path(path)
- end
end
end
diff --git a/modules/storages/app/validator/nextcloud_application_credentials_validator.rb b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
index 7deae539322..74cf924e09c 100644
--- a/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
+++ b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
@@ -26,8 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
class NextcloudApplicationCredentialsValidator
- BUILDER = Storages::Peripherals::StorageInteraction::RequestUrlBuilder
-
attr_reader :contract, :uri
def initialize(contract)
@@ -41,7 +39,7 @@ class NextcloudApplicationCredentialsValidator
response = OpenProject
.httpx
.basic_auth(contract.username, contract.password)
- .head(BUILDER.build(contract.model, "remote.php/dav"))
+ .head(Storages::UrlBuilder.url(contract.model.uri, "remote.php/dav"))
case response
in { status: 200..299 }
true
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/request_url_builder_spec.rb b/modules/storages/spec/common/storages/url_builder_spec.rb
similarity index 65%
rename from modules/storages/spec/common/storages/peripherals/storage_interaction/request_url_builder_spec.rb
rename to modules/storages/spec/common/storages/url_builder_spec.rb
index 657213f6c27..4a4291194ce 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/request_url_builder_spec.rb
+++ b/modules/storages/spec/common/storages/url_builder_spec.rb
@@ -30,31 +30,31 @@
require "spec_helper"
-RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
- describe "build" do
+RSpec.describe Storages::UrlBuilder do
+ describe "url" do
context "if storage is of Nextcloud provider type" do
context "with a standard host url" do
let(:storage) { create(:nextcloud_storage, host: "https://example.com/") }
it "returns the correct request URL" do
- expect(described_class.build(storage, "/path/")).to eq("https://example.com/path")
- expect(described_class.build(storage, "/path")).to eq("https://example.com/path")
- expect(described_class.build(storage, "path/")).to eq("https://example.com/path")
- expect(described_class.build(storage, "path")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "/path/")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "/path")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "path/")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "path")).to eq("https://example.com/path")
- expect(described_class.build(storage, "path", "/another/fragment/"))
+ expect(described_class.url(storage.uri, "path", "/another/fragment/"))
.to eq("https://example.com/path/another/fragment")
- expect(described_class.build(storage, "/path/", "/another/fragment/"))
+ expect(described_class.url(storage.uri, "/path/", "/another/fragment/"))
.to eq("https://example.com/path/another/fragment")
- expect(described_class.build(storage, "/path/", "another/fragment"))
+ expect(described_class.url(storage.uri, "/path/", "another/fragment"))
.to eq("https://example.com/path/another/fragment")
- expect(described_class.build(storage, "/path/", "/another/", "/fragment/"))
+ expect(described_class.url(storage.uri, "/path/", "/another/", "/fragment/"))
.to eq("https://example.com/path/another/fragment")
- expect(described_class.build(storage, "path", "//with-double-slash/"))
+ expect(described_class.url(storage.uri, "path", "//with-double-slash/"))
.to eq("https://example.com/path/with-double-slash")
- expect(described_class.build(storage, "path", "new folder/stærforge"))
+ expect(described_class.url(storage.uri, "path", "new folder/stærforge"))
.to eq("https://example.com/path/new%20folder/st%C3%A6rforge")
end
end
@@ -63,8 +63,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:nextcloud_storage, host: "https://example.com") }
it "returns the correct request URL" do
- expect(described_class.build(storage, "/path/")).to eq("https://example.com/path")
- expect(described_class.build(storage, "path")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "/path/")).to eq("https://example.com/path")
+ expect(described_class.url(storage.uri, "path")).to eq("https://example.com/path")
end
end
@@ -72,8 +72,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:nextcloud_storage, host: "https://example.com/html/") }
it "returns the correct request URL" do
- expect(described_class.build(storage, "/path/")).to eq("https://example.com/html/path")
- expect(described_class.build(storage, "path")).to eq("https://example.com/html/path")
+ expect(described_class.url(storage.uri, "/path/")).to eq("https://example.com/html/path")
+ expect(described_class.url(storage.uri, "path")).to eq("https://example.com/html/path")
end
end
@@ -81,8 +81,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:nextcloud_storage, host: "https://example.com/html") }
it "returns the correct request URL" do
- expect(described_class.build(storage, "/path/")).to eq("https://example.com/html/path")
- expect(described_class.build(storage, "path")).to eq("https://example.com/html/path")
+ expect(described_class.url(storage.uri, "/path/")).to eq("https://example.com/html/path")
+ expect(described_class.url(storage.uri, "path")).to eq("https://example.com/html/path")
end
end
end
@@ -91,12 +91,12 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:one_drive_storage) }
it "returns the correct request URL" do
- expect(described_class.build(storage, "/path/")).to eq("https://graph.microsoft.com/path")
- expect(described_class.build(storage, "/path")).to eq("https://graph.microsoft.com/path")
- expect(described_class.build(storage, "path/")).to eq("https://graph.microsoft.com/path")
- expect(described_class.build(storage, "path")).to eq("https://graph.microsoft.com/path")
+ expect(described_class.url(storage.uri, "/path/")).to eq("https://graph.microsoft.com/path")
+ expect(described_class.url(storage.uri, "/path")).to eq("https://graph.microsoft.com/path")
+ expect(described_class.url(storage.uri, "path/")).to eq("https://graph.microsoft.com/path")
+ expect(described_class.url(storage.uri, "path")).to eq("https://graph.microsoft.com/path")
- expect(described_class.build(storage, "path", "/to/", "/request"))
+ expect(described_class.url(storage.uri, "path", "/to/", "/request"))
.to eq("https://graph.microsoft.com/path/to/request")
end
end
@@ -105,7 +105,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:nextcloud_storage) }
it "raises an exception" do
- expect { described_class.build(storage, "/path/", "/i%20am%20escaped/") }.to raise_error(ArgumentError)
+ expect { described_class.url(storage.uri, "/path/", "/i%20am%20escaped/") }.to raise_error(ArgumentError)
end
end
@@ -113,7 +113,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
let(:storage) { create(:nextcloud_storage, host: "https://example.com/html/") }
it "returns only the storage uri" do
- expect(described_class.build(storage)).to eq("https://example.com/html/")
+ expect(described_class.url(storage.uri)).to eq("https://example.com/html/")
end
end
end
@@ -132,5 +132,11 @@ RSpec.describe Storages::Peripherals::StorageInteraction::RequestUrlBuilder do
expect(described_class.path("/path/", "/new folder/stærforge/")).to eq("/path/new%20folder/st%C3%A6rforge")
end
+
+ context "if a fragment with an url escaped character is given" do
+ it "raises an error" do
+ expect { described_class.path("/path/", "/i%20am%20escaped/") }.to raise_error(ArgumentError)
+ end
+ end
end
end