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 validation for file storages: Host attribute needs to provide a secure context

Frank Bergmannb20d4db96554
diff --git a/app/validators/secure_context_uri_validator.rb b/app/validators/secure_context_uri_validator.rb
new file mode 100644
index 00000000000..e2e9802638d
--- /dev/null
+++ b/app/validators/secure_context_uri_validator.rb
@@ -0,0 +1,71 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+# Please see https://w3c.github.io/webappsec-secure-contexts/
+# for a definition of "secure contexts".
+# Basically, a host has to have either a HTTPS scheme or be
+# localhost to provide a secure context.
+class SecureContextUriValidator < ActiveModel::EachValidator
+ def validate_each(contract, attribute, value)
+ begin
+ uri = URI.parse(value)
+ rescue StandardError
+ contract.errors.add(attribute, :could_not_parse_host_uri)
+ return
+ end
+
+ # The URI could be parsable but not contain a host name
+ if uri.host.nil?
+ contract.errors.add(attribute, :could_not_parse_host_uri)
+ return
+ end
+
+ unless secure_context_uri?(uri)
+ contract.errors.add(attribute, :uri_not_secure_context)
+ end
+ end
+
+ private
+
+ def secure_context_uri?(uri)
+ return true if uri.scheme == 'https' # https is always safe
+ return true if uri.host == 'localhost' # Simple localhost
+ return true if uri.host =~ /\.localhost\.?$/ # i.e. 'foo.localhost' or 'foo.localhost.'
+
+ # Check for loopback interface. The constructor can throw an exception for non IP addresses.
+ # Those are invalid. And if the host is an IP address then we can check if it is loopback.
+ begin
+ return true if IPAddr.new(uri.host).loopback?
+ rescue StandardError
+ return false
+ end
+
+ # uri.host is an IP but not a loopback
+ false
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index 000c15f182a..41d4825b317 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -35,7 +35,7 @@ require 'uri'
# (normally it's a model).
module Storages::Storages
class BaseContract < ::ModelContract
- MINIMAL_NEXTCLOUD_VERSION = 23
+ MINIMAL_NEXTCLOUD_VERSION = 22
include ::Storages::Storages::Concerns::ManageStoragesGuarded
include ActiveModel::Validations
@@ -50,6 +50,6 @@ module Storages::Storages
validates :host, url: true, length: { maximum: 255 }
# Check that a host actually is a storage server.
# But only do so if the validations above for URL were successful.
- validates :host, nextcloud_compatible_host: true, unless: -> { errors.include?(:host) }
+ validates :host, secure_context_uri: true, nextcloud_compatible_host: true, unless: -> { errors.include?(:host) }
end
end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 2adbb48267b..0383a06cc3c 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -30,6 +30,8 @@ en:
is not fully set up. That Nextcloud instance still redirects to a URL containing 'index.php'. That is a
strong 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.rb
index f55324a2d27..f723a37681f 100644
--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
@@ -116,6 +116,18 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
include_examples 'contract is invalid', host: :url
end
+ context 'when host is an unsafe IP' do
+ let(:storage_host) { 'http://172.16.193.146' }
+
+ include_examples 'contract is invalid', host: :uri_not_secure_context
+ end
+
+ context 'when host is an unsafe hostname' do
+ let(:storage_host) { 'http://nc.openproject.com' }
+
+ include_examples 'contract is invalid', host: :uri_not_secure_context
+ end
+
context 'when provider_type is nextcloud' do
let(:capabilities_response_body) { nil } # use default
let(:capabilities_response_code) { nil } # use default
@@ -195,5 +207,19 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true d
end
end
end
+
+ context 'when host secure' do
+ context 'when host is localhost' do
+ let(:storage_host) { 'http://localhost:1234' }
+
+ include_examples 'contract is valid'
+ end
+
+ context 'when host uses https protocol' do
+ let(:storage_host) { 'https://172.16.193.146' }
+
+ include_examples 'contract is valid'
+ end
+ end
end
end
diff --git a/spec/validator/secure_context_uri_validator_spec.rb b/spec/validator/secure_context_uri_validator_spec.rb
new file mode 100644
index 00000000000..74a3c2aaac2
--- /dev/null
+++ b/spec/validator/secure_context_uri_validator_spec.rb
@@ -0,0 +1,134 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+# require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+require 'spec_helper'
+
+describe SecureContextUriValidator do
+ let(:host) { nil }
+ let(:model_class) do
+ Class.new do
+ include ActiveModel::Validations
+ def self.model_name
+ ActiveModel::Name.new(self, nil, "ValidatedModel")
+ end
+
+ attr_accessor :host
+
+ validates :host, secure_context_uri: true
+ end
+ end
+ let(:model_instance) { model_class.new.tap { |instance| instance.host = host } }
+
+ before { model_instance.validate }
+
+ context 'with empty URI' do
+ ['', ' ', nil].each do |uri|
+ describe "when URI is '#{uri}'" do
+ let(:host) { uri }
+
+ it "adds an :could_not_parse_host_uri error" do
+ expect(model_instance.errors).to include(:host)
+ expect(model_instance.errors.first.type).to be :could_not_parse_host_uri
+ end
+ end
+ end
+ end
+
+ context 'with invalid URI' do
+ %w(some_string http://<>ample.com).each do |uri|
+ describe "when URI is '#{uri}'" do
+ let(:host) { uri }
+
+ it "adds an :could_not_parse_host_uri error" do
+ expect(model_instance.errors).to include(:host)
+ expect(model_instance.errors.first.type).to be :could_not_parse_host_uri
+ end
+ end
+ end
+ end
+
+ context 'with valid URI' do
+ context 'when host is missing' do
+ let(:host) { 'https://' }
+
+ it "adds an :could_not_parse_host_uri error" do
+ expect(model_instance.errors).to include(:host)
+ expect(model_instance.errors.first.type).to be :could_not_parse_host_uri
+ end
+ end
+
+ context 'when not providing a Secure Context' do
+ %w{http://128.0.0.1 http://foo.com http://[::2]}.each do |uri|
+ describe "when URI is '#{uri}'" do
+ let(:host) { uri }
+
+ it "adds a :uri_not_secure_context error" do
+ expect(model_instance.errors).to include(:host)
+ expect(model_instance.errors.first.type).to be :uri_not_secure_context
+ end
+ end
+ end
+ end
+
+ context 'when providing a Secure Context' do
+ context 'with a loopback IP' do
+ %w{http://127.0.0.1 http://127.1.1.1}.each do |uri|
+ describe "when URI is '#{uri}'" do
+ let(:host) { uri }
+
+ it "does not add an error" do
+ expect(model_instance.errors).not_to include(:host)
+ end
+ end
+ end
+ end
+
+ context 'with a domain name' do
+ %w(https://example.com http://localhost http://.localhost http://foo.localhost. http://foo.localhost).each do |uri|
+ describe "when URI is '#{uri}'" do
+ let(:host) { uri }
+
+ it "does not add an error" do
+ expect(model_instance.errors).not_to include(:host)
+ end
+ end
+ end
+ end
+
+ context 'with IPV6 loopback URI' do
+ let(:host) { 'http://[::1]' }
+
+ it "does not add an error" do
+ expect(model_instance.errors).not_to include(:host)
+ end
+ end
+ end
+ end
+end