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

[#64347] get rid of inefficient regular expression

Tobias Dillmann127cee26aefb
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 06f76c891e9..0924d3c70f3 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -149,10 +149,14 @@ class CustomField < ApplicationRecord
# Once this basic validation passes, the formula will be parsed and validated by Dentaku, which builds an AST
# and ensures that the formula is really valid. A welcome side effect of the basic validation done here is that
# it prevents built-in functions from being used in the formula, which we do not want to allow.
- common_chars = '[\+\-\/\*\(\)\s\d\.]'
- pattern = /\A#{common_chars}+(?:cf_\d+#{common_chars}*)*\z/
+ allowed_chars = %w[+ - / * ( )] + [" "]
+ split_pattern = /\s|(\+)|(-)|(\*)|(\()|(\))/
+ pattern = /^(cf_\d+|\d+\.?\d*|\.\d+.|[#{allowed_chars.join}]+)$/
+ valid_formula_string = formula_string.split(split_pattern).reject(&:empty?).all? do |token|
+ token.match?(pattern)
+ end
- errors.add(:formula, :invalid) unless formula_string.match?(pattern)
+ errors.add(:formula, :invalid) unless valid_formula_string
# TODO: check for valid (i.e. visible & enabled) custom field references (see #cf_ids_used_in_formula)