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

feat[OP#48569] Validate application password against Nextcloud (#13141)

Kabiru Mwenjad8e3ee88e1fb
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index 2ea7b6f3658..f76c92c6652 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -64,6 +64,12 @@ module Storages::Storages
validates :password, presence: true, if: :nextcloud_storage_automatically_managed?
validates :password, absence: true, unless: :nextcloud_storage_automatically_managed?
+ validate do
+ if nextcloud_storage_automatically_managed? && errors.exclude?(:password)
+ NextcloudApplicationCredentialsValidator.new(self).call
+ end
+ end
+
private
def nextcloud_storage_automatically_managed?
diff --git a/modules/storages/app/validator/nextcloud_application_credentials_validator.rb b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
new file mode 100644
index 00000000000..8d784c055fa
--- /dev/null
+++ b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
@@ -0,0 +1,64 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+class NextcloudApplicationCredentialsValidator
+ attr_reader :contract, :uri
+
+ def initialize(contract)
+ @contract = contract
+ @uri = URI(contract.host).normalize
+ end
+
+ def call
+ return unless contract.model.password_changed?
+
+ case make_http_head_request_from(build_http_head_request)
+ when Net::HTTPSuccess
+ true
+ when Net::HTTPUnauthorized
+ contract.errors.add(:password, :invalid_password)
+ else
+ contract.errors.add(:password, :unknown_error)
+ end
+ end
+
+ private
+
+ def build_http_head_request
+ request = Net::HTTP::Head.new Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .join_uri_path(uri, "remote.php/dav")
+ request.initialize_http_header Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .basic_auth_header(contract.username, contract.password)
+ request
+ end
+
+ def make_http_head_request_from(request)
+ Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .http(uri)
+ .request(request)
+ end
+end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index ff6865d1ed0..d6db727f8bd 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -38,6 +38,9 @@ en:
is not fully set up. The Nextcloud instance does not receive the "Authorization" header, which is
necessary for a Bearer token based authorization of API requests. Please double check your HTTP
server configuration.
+ password:
+ invalid_password: "is not valid."
+ unknown_error: "could not be validated. Please check your storage connection and try again."
storages/file_link:
attributes:
origin_id:
diff --git a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb
index f1413fcc15b..bf7802cabc7 100644
--- a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb
@@ -47,4 +47,76 @@ RSpec.describe Storages::Storages::BaseContract, :storage_server_helpers, webmoc
expect(capabilities_request).not_to have_been_made
expect(host_request).not_to have_been_made
end
+
+ describe 'Nextcloud application credentials validation' do
+ before do
+ mock_server_capabilities_response(storage.host)
+ mock_server_config_check_response(storage.host)
+ end
+
+ context 'with valid credentials' do
+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }
+
+ it 'passes validation' do
+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host)
+ contract = described_class.new(storage, current_user)
+
+ expect(contract).to be_valid
+ expect(credentials_request).to have_been_made.once
+ end
+ end
+
+ context 'with invalid credentials' do
+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }
+
+ it 'fails validation' do
+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 401)
+ contract = described_class.new(storage, current_user)
+
+ expect(contract).not_to be_valid
+ expect(contract.errors.to_hash).to eq({ password: ["is not valid."] })
+
+ expect(credentials_request).to have_been_made.once
+ end
+ end
+
+ context 'with unknown error' do
+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }
+
+ it 'fails validation' do
+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 500)
+ contract = described_class.new(storage, current_user)
+
+ expect(contract).not_to be_valid
+ expect(contract.errors.to_hash)
+ .to eq({ password: ["could not be validated. Please check your storage connection and try again."] })
+
+ expect(credentials_request).to have_been_made.once
+ end
+ end
+
+ context 'when the storage is not automatically managed' do
+ let(:storage) { build(:nextcloud_storage, :as_not_automatically_managed) }
+
+ it 'skips credentials validation' do
+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host)
+ contract = described_class.new(storage, current_user)
+
+ expect(contract).to be_valid
+ expect(credentials_request).not_to have_been_made
+ end
+ end
+
+ context 'when the storage host has a subpath' do
+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: 'https://host1.example.com/api') }
+
+ it 'passes validation' do
+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host)
+ contract = described_class.new(storage, current_user)
+
+ expect(contract).to be_valid
+ expect(credentials_request).to have_been_made.once
+ end
+ end
+ end
end
diff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
index 42d8766dc6b..8630b37c5b9 100644
--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
@@ -41,6 +41,7 @@ RSpec.shared_examples_for 'storage contract', :storage_server_helpers, webmock:
if storage_host.present?
mock_server_capabilities_response(storage_host)
mock_server_config_check_response(storage_host)
+ mock_nextcloud_application_credentials_validation(storage_host)
end
end
diff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rb
index 4ce9b408018..d86e58ce7f1 100644
--- a/modules/storages/spec/features/admin_storages_spec.rb
+++ b/modules/storages/spec/features/admin_storages_spec.rb
@@ -112,7 +112,22 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true, with_flag: {
expect(page).to have_title("Automatically managed project folders")
expect(page).to have_content("Password can't be blank.")
+ # Test the error path for an invalid storage password.
+ # Mock a valid response (=401) for example.com, so the password validation should fail
+ mock_nextcloud_application_credentials_validation("https://example.com", password: "1234567890", response_code: 401)
+ automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
+ expect(automatically_managed_switch).to be_checked
+ page.fill_in 'storages_nextcloud_storage_password', with: "1234567890"
+ # Clicking submit with application password empty should show an error
+ page.click_button('Save')
+ # Check that we're still on the same page
+ expect(page).to have_title("Automatically managed project folders")
+ expect(page).to have_content("Password is not valid.")
+
+ # Test the happy path for a valid storage password.
+ # Mock a valid response (=200) for example.com, so the password validation should succeed
# Fill in application password and submit
+ mock_nextcloud_application_credentials_validation("https://example.com", password: "1234567890")
automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
expect(automatically_managed_switch).to be_checked
page.fill_in 'storages_nextcloud_storage_password', with: "1234567890"
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 76e976265a8..5df88170d56 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -94,6 +94,7 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
before do
mock_server_capabilities_response(host)
mock_server_config_check_response(host)
+ mock_nextcloud_application_credentials_validation(host)
end
subject(:last_response) do
@@ -287,6 +288,10 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
)
end
+ before do
+ mock_nextcloud_application_credentials_validation(storage.host, password: 'myappsecret')
+ end
+
subject { last_response.body }
it_behaves_like 'successful response'
@@ -307,6 +312,22 @@ RSpec.describe 'API v3 storages resource', content_type: :json, webmock: true do
it { is_expected.to be_json_eql('false').at_path('hasApplicationPassword') }
end
+
+ context 'with invalid applicationPassword' do
+ let(:params) do
+ super().merge(
+ applicationPassword: '123'
+ )
+ end
+
+ before do
+ mock_nextcloud_application_credentials_validation(storage.host, password: '123', response_code: 401)
+ end
+
+ subject { last_response.body }
+
+ it { is_expected.to be_json_eql('Password is not valid.'.to_json).at_path('message') }
+ end
end
end
diff --git a/modules/storages/spec/support/storage_server_helpers.rb b/modules/storages/spec/support/storage_server_helpers.rb
index e6f6e71b0b5..955ab2b90ab 100644
--- a/modules/storages/spec/support/storage_server_helpers.rb
+++ b/modules/storages/spec/support/storage_server_helpers.rb
@@ -90,6 +90,28 @@ module StorageServerHelpers
body: response_body
)
end
+
+ def mock_nextcloud_application_credentials_validation(nextcloud_host,
+ username: 'OpenProject',
+ password: 'Password123',
+ response_code: nil,
+ response_headers: nil,
+ response_body: nil)
+ response_code ||= 200
+ response_headers ||= {
+ 'Content-Type' => 'text/html; charset=UTF-8',
+ 'Authorization' => "Basic #{Base64::strict_encode64("#{username}:#{password}")}"
+ }
+
+ stub_request(
+ :head,
+ File.join(nextcloud_host, 'remote.php/dav')
+ ).to_return(
+ status: response_code,
+ headers: response_headers,
+ body: response_body
+ )
+ end
end
RSpec.configure do |c|