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
enforce checking authorization by adding a before_action
ulfertsa3aa24082310
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 4375d2aa21d..41584ef4d6f 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -47,6 +47,7 @@ class ApplicationController < ActionController::Baseinclude ErrorsHelperinclude Accounts::CurrentUserinclude Accounts::UserLogin+ include Accounts::Authorizationinclude ::OpenProject::Authentication::SessionExpiryinclude AdditionalUrlHelpersinclude OpenProjectErrorHelper@@ -130,7 +131,8 @@ class ApplicationController < ActionController::Basepayload: ::OpenProject::Logging::ThreadPoolContextBuilder.build!end- before_action :user_setup,+ before_action :authorization_check_required,+ :user_setup,:set_localization,:tag_request,:check_if_login_required,@@ -359,19 +361,14 @@ class ApplicationController < ActionController::Base# Find a project based on params[:project_id]# TODO: some subclasses override this, see about merging their logic- def find_optional_project- find_optional_project_and_raise_error+ def authorize_in_optional_project+ @project = Project.find(params[:project_id]) if params[:project_id].present?++ do_authorize({ controller: params[:controller], action: params[:action] }, global: params[:project_id].blank?)rescue ActiveRecord::RecordNotFoundrender_404end- def find_optional_project_and_raise_error- @project = Project.find(params[:project_id]) if params[:project_id].present?- allowed = User.current.allowed_based_on_permission_context?({ controller: params[:controller], action: params[:action] },- project: @project)- allowed ? true : deny_access- end-# Finds and sets @project based on @object.projectdef find_project_from_associationrender_404 if @object.blank?diff --git a/app/controllers/concerns/accounts/authorization.rb b/app/controllers/concerns/accounts/authorization.rbnew file mode 100644index 00000000000..cfed68a66b2--- /dev/null+++ b/app/controllers/concerns/accounts/authorization.rb@@ -0,0 +1,139 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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.+# ++++##+# Intended to be used by the ApplicationController to provide authorization helpers+module Accounts::Authorization+ extend ActiveSupport::Concern++ METHODS_ENFORCING_AUTHORIZATION = %i[require_admin authorize authorize_global authorize_in_optional_project].freeze++ included do+ class_attribute :authorization_ensured,+ default: {+ only: [],+ except: [],+ generally_allowed: false,+ controller: self+ }+ end++ def require_admin+ return unless require_login++ render_403 unless current_user.admin?+ end++ def authorization_check_required+ unless authorization_is_ensured?(params[:action])+ if Rails.env.development?+ raise <<-MESSAGE+ Authorization check required for #{params[:action]} in #{self.class.name}.++ Use any method of 'authorize', 'authorize_global', 'authorize_in_optional_project'+ or 'require_admin' to ensure authorization. If authorization is checked by any other means,+ affirm the same by calling 'authorization_checked!' in the controller. If the authorization does+ not need to be checked for this action, affirm the same by calling 'no_authorization_required!'+ MESSAGE+ else+ render_403+ end+ end+ end++ private++ def authorization_is_ensured?(action)+ return false if authorization_ensured.nil?++ (authorization_ensured[:generally_allowed] == true || authorization_ensured[:only].include?(action.to_sym)) &&+ authorization_ensured[:except].exclude?(action.to_sym)+ end++ class_methods do+ # Overriding before_action of rails to check if any authorization method is by now defined.+ def before_action(*names, &)+ if METHODS_ENFORCING_AUTHORIZATION.intersect?(names)+ no_authorization_required!(only: names.last.is_a?(Hash) ? Array(names.last[:only]) : [],+ except: names.last.is_a?(Hash) ? Array(names.last[:except]) : [])+ end++ super+ end++ def no_authorization_required!(only: [], except: [])+ only = Array(only)+ except = Array(except)++ # A class_attribute is used so that inheritance works also for defined only/except actions.+ # But since the only/accept arrays are only modified in place, the same object would be used from the+ # ApplicationController downwards. So whenever it is detected that the controller the authorization_ensured+ # object is defined for changes, we clone it so that henceforth all changes are local.+ clone_authorization_ensured unless self == authorization_ensured[:controller]++ if only.any? || except.any?+ update_authorization_ensured_on_actions(only:, except:)+ else+ update_authorization_ensured_on_all+ end+ end++ alias :authorization_checked! :no_authorization_required!++ def update_authorization_ensured_on_actions(only: [], except: [])+ update_authorization_ensured_on_action_only(only)+ update_authorization_ensured_on_action_except(only, except)+ end++ def update_authorization_ensured_on_action_only(only)+ authorization_ensured[:generally_allowed] = true if only.empty?++ if only.any?+ authorization_ensured[:only] += only+ authorization_ensured[:only].uniq!+ end+ end++ def update_authorization_ensured_on_action_except(only, except)+ authorization_ensured[:except] += except - authorization_ensured[:only] if except.any?+ authorization_ensured[:except] -= only if only.any?+ authorization_ensured[:except].uniq!+ end++ def update_authorization_ensured_on_all+ authorization_ensured[:generally_allowed] = true+ end++ def clone_authorization_ensured+ self.authorization_ensured = { only: authorization_ensured[:only].dup,+ except: authorization_ensured[:except].dup,+ generally_allowed: authorization_ensured[:generally_allowed],+ controller: self }+ end+ end+enddiff --git a/spec/controllers/concerns/authorization_spec.rb b/spec/controllers/concerns/authorization_spec.rbnew file mode 100644index 00000000000..97ce57187a9--- /dev/null+++ b/spec/controllers/concerns/authorization_spec.rb@@ -0,0 +1,302 @@+# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-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.+# ++++require "spec_helper"++RSpec.describe ApplicationController, "enforcement of authorization" do # rubocop:disable RSpec/FilePath, RSpec/SpecFilePathFormat+ shared_let(:user) { create(:user) }++ controller_setup = Module.new do+ extend ActiveSupport::Concern++ included do+ def index+ render plain: "OK"+ end++ private++ def require_admin; end+ def authorize_global; end++ def authorize; end++ def authorize_in_optional_project; end++ def other_before_action; end+ end+ end++ current_user { user }++ shared_examples "succeeds" do+ it "succeeds" do+ get :index++ expect(response)+ .to have_http_status :ok+ end+ end++ shared_examples "is prevented" do+ it "fails with a 403" do+ get :index++ expect(response)+ .to have_http_status :forbidden+ end+ end++ context "without authorization or authorization forfeiting" do+ controller do+ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "with authorization checked with require_admin" do+ controller do+ before_action :require_admin++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization checked with authorize_global" do+ controller do+ before_action :authorize_global++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization checked with authorize" do+ controller do+ before_action :authorize++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization checked with authorize_in_optional_project" do+ controller do+ before_action :authorize_in_optional_project++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with another before action specified" do+ controller do+ before_action :other_before_action++ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "when forfeiting authorization checks" do+ controller do+ no_authorization_required!++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when stating that authorization has been checked" do+ controller do+ authorization_checked!++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization check in the superclass" do+ controller(described_class) do+ before_action :require_admin+ end++ controller(controller_class) do+ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when stating that authorization has been checked in the superclass" do+ controller(described_class) do+ authorization_checked!+ end++ controller(controller_class) do+ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when forfeiting authorization checks in the superclass" do+ controller(described_class) do+ no_authorization_required!+ end++ controller(controller_class) do+ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when forfeiting authorization checks on this specific action with only" do+ controller do+ no_authorization_required! only: %i[index]++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when forfeiting authorization checks on another action with only" do+ controller do+ no_authorization_required! only: %i[some_other_action]++ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "when forfeiting authorization checks on all but another action with except" do+ controller do+ no_authorization_required! except: %i[some_other_action]++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "when forfeiting authorization on all other actions with except" do+ controller do+ no_authorization_required! except: %i[index]++ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "with authorization checked on another action with only" do+ controller do+ before_action :require_admin, only: %i[some_other_action]++ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "with authorization checked on the action with only" do+ controller do+ before_action :require_admin, only: %i[index]++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization checked on all but this action with except" do+ controller do+ before_action :require_admin, except: %i[index]++ include controller_setup+ end++ it_behaves_like "is prevented"+ end++ context "with authorization checked on all but another action with except" do+ controller do+ before_action :require_admin, except: %i[another_action]++ def another_action; end++ include controller_setup+ end++ it_behaves_like "succeeds"+ end++ context "with authorization checked in a sibling class" do+ # Superclass+ controller do+ include controller_setup+ end++ anonymous_superclass = controller_class++ # Sibling class+ controller(anonymous_superclass) do+ before_action :require_admin+ end++ # actually tested class+ controller(anonymous_superclass) do+ # Nothing extra+ end++ it_behaves_like "is prevented"+ end++ context "with authorization checked by a number of different actions" do+ controller do+ before_action :require_admin, except: %i[index]+ before_action :authorize, only: %i[index]++ include controller_setup+ end++ it_behaves_like "succeeds"+ end+end