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
Validate _links in API requests
Jan Sandbrink1587c2f2335d
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex a183c85bfca..11d09a32648 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -4779,6 +4779,8 @@ en:code_500_missing_enterprise_token: "The request can not be handled due to invalid or missing Enterprise token."bad_request:emoji_reactions_activity_type_not_supported: "This activity type does not support emoji reactions."+ invalid_link: "The link under key '%{key}' is not valid."+ links_not_an_object: "_links must be a JSON object."conflict:multiple_reminders_not_allowed: |-You can only set one reminder at a time for a work package. Please delete or update the existing reminder.diff --git a/lib/api/decorators/linked_resource.rb b/lib/api/decorators/linked_resource.rbindex 8efc8ffac13..a5f7a3eacab 100644--- a/lib/api/decorators/linked_resource.rb+++ b/lib/api/decorators/linked_resource.rb@@ -42,6 +42,8 @@ module APIdef from_hash(hash, *)return super unless hash && hash["_links"]+ validate_links!(hash["_links"])+copied_hash = hash.deep_duprepresentable_attrs.find_all do |dfn|@@ -69,6 +71,15 @@ module APIendend+ def validate_links!(links)+ raise ::API::Errors::BadRequest.new(I18n.t("api_v3.errors.bad_request.links_not_an_object")) unless links.is_a?(Hash)++ invalid, = links.find { |_, link| !(link.is_a?(Hash) || link.is_a?(Array)) }+ return if invalid.nil?++ raise ::API::Errors::BadRequest.new(I18n.t("api_v3.errors.bad_request.invalid_link", key: invalid))+ end+module ClassMethodsdef resource(name,getter:,diff --git a/spec/lib/api/decorators/linked_resource_spec.rb b/spec/lib/api/decorators/linked_resource_spec.rbnew file mode 100644index 00000000000..afc2364389c--- /dev/null+++ b/spec/lib/api/decorators/linked_resource_spec.rb@@ -0,0 +1,89 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 "spec_helper"++RSpec.describe API::Decorators::LinkedResource do+ let(:representer) do+ Class.new(API::Decorators::Single) do+ include API::Decorators::LinkedResource++ resource_link :foo,+ getter: -> { represented["foo"] },+ setter: ->(fragment:, **) { represented["foo"] = fragment["href"] }+ end+ end+ let(:represented) { {} }+ let(:current_user) { create(:user) }++ describe "#from_hash" do+ subject { representer.new(represented, current_user:).from_hash(input_hash) }++ let(:input_hash) do+ {+ "_links" => {+ "foo" => { "href" => "https://example.com" }+ }+ }+ end++ it "parses the link" do+ expect { subject }.to change { represented["foo"] }.from(nil).to("https://example.com")+ end++ context "when passing link as string" do+ let(:input_hash) do+ {+ "_links" => {+ "foo" => "https://example.com"+ }+ }+ end++ it "raises an error" do+ expect { subject }.to raise_error(API::Errors::BadRequest)+ end+ end++ context "when passing _links as array" do+ let(:input_hash) do+ {+ "_links" => [+ { "foo" => { "href" => "https://example.com" } }+ ]+ }+ end++ it "raises an error" do+ expect { subject }.to raise_error(API::Errors::BadRequest)+ end+ end+ end+end