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

[#48001] Restrict timestamps outside enterprise to "yesterday"in query timestamps backend

Dombi Attila951ca3492590
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rb
index 18bdf8dc577..06763da1ca7 100644
--- a/app/contracts/queries/base_contract.rb
+++ b/app/contracts/queries/base_contract.rb
@@ -60,7 +60,7 @@ module Queries
validate :validate_project
validate :user_allowed_to_make_public
- validate :timestamps_are_parsable
+ validate :allowed_timestamps
def validate_project
errors.add :project, :error_not_found if project_id.present? && !project_visible?
@@ -84,12 +84,20 @@ module Queries
end
end
- def timestamps_are_parsable
- invalid_timestamps = model.timestamps.reject(&:valid?)
+ def allowed_timestamps
+ valid_timestamps, invalid_timestamps = model.timestamps.partition(&:valid?)
if invalid_timestamps.any?
errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")
end
+
+ return if EnterpriseToken.allows_to?(:baseline_comparison)
+
+ forbidden_timestamps = valid_timestamps.select { |t| t.to_time < Date.yesterday }
+
+ if forbidden_timestamps.any?
+ errors.add :timestamps, :forbidden, values: forbidden_timestamps.join(", ")
+ end
end
end
end
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index f74eb0e12cc..3ee962c36cd 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -27,6 +27,8 @@
#++
class Timestamp
+ ALLOWED_DATE_KEYWORDS = ["oneDayAgo", "lastWorkingDay", "oneWeekAgo", "oneMonthAgo"].freeze
+
delegate :hash, to: :to_s
class Exception < StandardError; end
@@ -34,7 +36,7 @@ class Timestamp
class TimestampParser
DATE_KEYWORD_REGEX =
%r{
- ^(?:oneDayAgo|lastWorkingDay|oneWeekAgo|oneMonthAgo) # match the relative date keyword
+ ^(?:#{ALLOWED_DATE_KEYWORDS.join("|")}) # match the relative date keyword
@(?:([0-1]?[0-9]|2[0-3]):[0-5]?[0-9]) # match the hour part
[+-](?:([0-1]?[0-9]|2[0-3]):[0-5]?[0-9])$ # match the timezone offset
}x
diff --git a/app/services/authorization/enterprise_service.rb b/app/services/authorization/enterprise_service.rb
index 53e871de95e..d6409849329 100644
--- a/app/services/authorization/enterprise_service.rb
+++ b/app/services/authorization/enterprise_service.rb
@@ -31,6 +31,7 @@ class Authorization::EnterpriseService
GUARDED_ACTIONS = %i(
attribute_help_texts
+ baseline_comparison
board_view
conditional_highlighting
custom_actions
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 43aa538782c..0bb891cc7cc 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -809,6 +809,7 @@ en:
format: "%{message}"
timestamps:
invalid: "Timestamps contain invalid values: %{values}"
+ forbidden: "Timestamps contain forbidden values: %{values}"
format: "%{message}"
group_by_hierarchies_exclusive: "is mutually exclusive with group by '%{group_by}'. You cannot activate both."
filters:
diff --git a/spec/contracts/queries/base_contract_spec.rb b/spec/contracts/queries/base_contract_spec.rb
new file mode 100644
index 00000000000..9434f58401e
--- /dev/null
+++ b/spec/contracts/queries/base_contract_spec.rb
@@ -0,0 +1,136 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+require 'contracts/shared/model_contract_shared_context'
+require_relative 'shared_contract_examples'
+
+describe Queries::BaseContract do
+ include_context 'ModelContract shared context'
+ include_context 'with queries contract'
+
+ describe 'timestamps', with_flag: { show_changes: true } do
+ let(:query) { build_stubbed(:query, timestamps:) }
+
+ context 'with EE', with_ee: %i[baseline_comparison] do
+ Timestamp::ALLOWED_DATE_KEYWORDS.each do |timestamp_date_keyword|
+ context "when the '#{timestamp_date_keyword}' value is provided" do
+ let(:timestamps) { ["#{timestamp_date_keyword}@12:00+00:00"] }
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+
+ context "when the shortcut value 'now' is provided" do
+ let(:timestamps) { ["now"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when a duration value is provided" do
+ let(:timestamps) { ["P-2D"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when an iso8601 datetime value is provided" do
+ let(:timestamps) { [1.week.ago.iso8601] }
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+
+ context 'without EE', with_ee: false do
+ context "when the 'oneDayAgo' value is provided" do
+ let(:timestamps) { ["oneDayAgo@12:00+00:00"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when the shortcut value 'now' is provided" do
+ let(:timestamps) { ["now"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when the 'PT0S' duration value is provided" do
+ let(:timestamps) { ["PT0S"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when the 'P-1D' duration value is provided" do
+ let(:timestamps) { ["P-1D"] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when an iso8601 datetime value from yesterday is provided" do
+ let(:timestamps) { [1.day.ago.beginning_of_day.iso8601] }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's yesterday" do
+ let(:timestamps) { "lastWorkingDay@00:00+00:00" }
+
+ it_behaves_like 'contract is valid'
+ end
+
+ Timestamp::ALLOWED_DATE_KEYWORDS[2..].each do |timestamp_date_keyword|
+ context "when the '#{timestamp_date_keyword}' value is provided" do
+ let(:timestamps) { ["#{timestamp_date_keyword}@12:00+00:00"] }
+
+ it_behaves_like 'contract is invalid', timestamps: :forbidden
+ end
+ end
+
+ context "when the 'lastWorkingDay' value is provided and it's before yesterday" do
+ let(:timestamps) { "lastWorkingDay@00:00+00:00" }
+
+ before do
+ allow(Day).to receive(:last_working) { Day.first }
+ end
+
+ it_behaves_like 'contract is invalid', timestamps: :forbidden
+ end
+
+ context "when a duration value older than yesterday is provided" do
+ let(:timestamps) { ["P-2D"] }
+
+ it_behaves_like 'contract is invalid', timestamps: :forbidden
+ end
+
+ context "when an iso8601 datetime value older than yesterday is provided" do
+ let(:timestamps) { [2.days.ago.end_of_day.iso8601] }
+
+ it_behaves_like 'contract is invalid', timestamps: :forbidden
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb
index aee954177fa..fda50be988e 100644
--- a/spec/requests/api/v3/queries/create_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb
@@ -28,7 +28,8 @@
require 'spec_helper'
require 'rack/test'
-describe "POST /api/v3/queries/form", with_flag: { show_changes: true } do
+describe "POST /api/v3/queries/form",
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
include API::V3::Utilities::PathHelper
let(:path) { api_v3_paths.create_query_form }
@@ -617,6 +618,47 @@ describe "POST /api/v3/queries/form", with_flag: { show_changes: true } do
.to eq "Public - The user has no permission to create public views."
end
end
+
+ context 'with EE token', with_ee: %i[baseline_comparison] do
+ describe 'timestamps' do
+ context 'with a value within 1 day' do
+ let(:timestamps) { "oneDayAgo@00:00+00:00" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
+ context 'with a value older than 1 day' do
+ let(:timestamps) { "P-2D" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+ end
+ end
+
+ context 'without EE token', with_ee: false do
+ describe 'timestamps' do
+ context 'with a value within 1 day' do
+ let(:timestamps) { "oneDayAgo@00:00+00:00" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
+ context 'with a value older than 1 day' do
+ let(:timestamps) { "P-2D" }
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain forbidden values: P-2D"
+ end
+ end
+ end
+ end
end
describe 'posting to a project-query form with a CF present only there (Regression #29873)' do
diff --git a/spec/requests/api/v3/queries/create_query_spec.rb b/spec/requests/api/v3/queries/create_query_spec.rb
index 5a00a14bbee..7d3453ac90b 100644
--- a/spec/requests/api/v3/queries/create_query_spec.rb
+++ b/spec/requests/api/v3/queries/create_query_spec.rb
@@ -28,11 +28,12 @@
require 'spec_helper'
-describe "POST /api/v3/queries", with_flag: { show_changes: true } do
+describe "POST /api/v3/queries",
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
shared_let(:user) { create(:admin) }
shared_let(:status) { create(:status) }
shared_let(:project) { create(:project) }
- shared_let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00+00:00', "P0D"] }
+ let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00+00:00', "P0D"] }
let(:default_params) do
{
@@ -98,6 +99,10 @@ describe "POST /api/v3/queries", with_flag: { show_changes: true } do
login_as user
end
+ def json
+ JSON.parse last_response.body
+ end
+
describe "creating a query" do
before do
header "Content-Type", "application/json"
@@ -109,8 +114,6 @@ describe "POST /api/v3/queries", with_flag: { show_changes: true } do
end
it 'renders the created query' do
- json = JSON.parse(last_response.body)
-
expect(json["_type"]).to eq "Query"
expect(json["name"]).to eq "Dummy Query"
end
@@ -133,6 +136,25 @@ describe "POST /api/v3/queries", with_flag: { show_changes: true } do
expect(filter.operator).to eq "="
expect(filter.values).to eq [status.id.to_s]
end
+
+ context 'without EE', with_ee: false do
+ it 'yields a 422 error given a timestamp older than 1 day' do
+ expect(last_response.status).to eq 422
+ expect(json["message"]).to eq "Timestamps contain forbidden values: #{timestamps.first}"
+ end
+
+ context 'when timestamps are within 1 day' do
+ let(:timestamps) { ["oneDayAgo@12:00+00:00"] }
+
+ it 'returns 201 (created)' do
+ expect(last_response.status).to eq(201)
+ end
+
+ it 'updates the query timestamps' do
+ expect(Query.first.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })
+ end
+ end
+ end
end
context "with invalid parameters" do
@@ -141,10 +163,6 @@ describe "POST /api/v3/queries", with_flag: { show_changes: true } do
post "/api/v3/queries", params.to_json
end
- def json
- JSON.parse last_response.body
- end
-
it "yields a 422 error given an unknown project" do
params[:_links][:project][:href] = "/api/v3/projects/#{project.id}42"
diff --git a/spec/requests/api/v3/queries/update_form_api_spec.rb b/spec/requests/api/v3/queries/update_form_api_spec.rb
index 164eb2f87f2..828a6e45d05 100644
--- a/spec/requests/api/v3/queries/update_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/update_form_api_spec.rb
@@ -28,7 +28,8 @@
require 'spec_helper'
require 'rack/test'
-describe "POST /api/v3/queries/form", with_flag: { show_changes: true } do
+describe "POST /api/v3/queries/form",
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
include API::V3::Utilities::PathHelper
let(:path) { api_v3_paths.query_form(query.id) }
@@ -576,6 +577,47 @@ describe "POST /api/v3/queries/form", with_flag: { show_changes: true } do
end
end
+ context 'with EE token', with_ee: %i[baseline_comparison] do
+ describe 'timestamps' do
+ context 'with a value within 1 day' do
+ let(:timestamps) { "oneDayAgo@00:00+00:00" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
+ context 'with a value older than 1 day' do
+ let(:timestamps) { "P-2D" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+ end
+ end
+
+ context 'without EE token', with_ee: false do
+ describe 'timestamps' do
+ context 'with a value within 1 day' do
+ let(:timestamps) { "oneDayAgo@00:00+00:00" }
+
+ it 'has the timestamp set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq [timestamps]
+ end
+ end
+
+ context 'with a value older than 1 day' do
+ let(:timestamps) { "P-2D" }
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain forbidden values: P-2D"
+ end
+ end
+ end
+ end
+
context "with an unauthorized user trying to set the query public" do
let(:user) { create(:user) }
let(:permissions) { [:view_work_packages] }
diff --git a/spec/requests/api/v3/queries/update_query_spec.rb b/spec/requests/api/v3/queries/update_query_spec.rb
index 0a9c59b9a25..42c13136c56 100644
--- a/spec/requests/api/v3/queries/update_query_spec.rb
+++ b/spec/requests/api/v3/queries/update_query_spec.rb
@@ -28,11 +28,12 @@
require 'spec_helper'
-describe "PATCH /api/v3/queries/:id" do
+describe "PATCH /api/v3/queries/:id",
+ with_ee: %i[baseline_comparison], with_flag: { show_changes: true } do
shared_let(:user) { create(:admin) }
shared_let(:status) { create(:status) }
shared_let(:project) { create(:project) }
- shared_let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00+00:00', "P0D"] }
+ let(:timestamps) { [1.week.ago.iso8601, 'lastWorkingDay@12:00+00:00', "P0D"] }
let!(:query) do
create(
@@ -113,7 +114,7 @@ describe "PATCH /api/v3/queries/:id" do
login_as user
end
- describe "updating a query", with_flag: { show_changes: true } do
+ describe "updating a query" do
before do
header "Content-Type", "application/json"
patch "/api/v3/queries/#{query.id}", params.to_json
@@ -124,8 +125,6 @@ describe "PATCH /api/v3/queries/:id" do
end
it 'renders the updated query' do
- json = JSON.parse(last_response.body)
-
expect(json["_type"]).to eq "Query"
expect(json["name"]).to eq "Dummy Query"
end
@@ -153,12 +152,29 @@ describe "PATCH /api/v3/queries/:id" do
let(:params) { {} }
it "does not change anything" do
- json = JSON.parse(last_response.body)
-
expect(json["_type"]).to eq "Query"
expect(json["name"]).to eq "A Query"
end
end
+
+ context 'without EE', with_ee: false do
+ it 'yields a 422 error given a timestamp older than 1 day' do
+ expect(last_response.status).to eq 422
+ expect(json["message"]).to eq "Timestamps contain forbidden values: #{timestamps.first}"
+ end
+
+ context 'when timestamps are within 1 day' do
+ let(:timestamps) { ["oneDayAgo@12:00+00:00"] }
+
+ it 'returns 200 (ok)' do
+ expect(last_response.status).to eq(200)
+ end
+
+ it 'updates the query timestamps' do
+ expect(Query.first.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })
+ end
+ end
+ end
end
context "with invalid parameters" do
diff --git a/spec/services/authorization/enterprise_service_spec.rb b/spec/services/authorization/enterprise_service_spec.rb
index e5b8b36faf3..d17026afc57 100644
--- a/spec/services/authorization/enterprise_service_spec.rb
+++ b/spec/services/authorization/enterprise_service_spec.rb
@@ -73,11 +73,23 @@ describe Authorization::EnterpriseService do
end
end
- %i(define_custom_style
+ %i(attribute_help_texts
+ baseline_comparison
+ board_view
+ conditional_highlighting
+ custom_actions
+ custom_fields_in_projects_list
+ date_alerts
+ define_custom_style
edit_attribute_groups
- work_package_query_relation_columns
- attribute_help_texts
- grid_widget_wp_graph).each do |guarded_action|
+ grid_widget_wp_graph
+ ldap_groups
+ openid_providers
+ placeholder_users
+ readonly_work_packages
+ team_planner_view
+ two_factor_authentication
+ work_package_query_relation_columns).each do |guarded_action|
context "guarded action #{guarded_action}" do
let(:action) { guarded_action }