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
[#64348] Only allow patternized variables in formula string
Tobias Dillmannb8e42a93937e
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 229f6074e73..26fedbe8c36 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -107,11 +107,8 @@ module CustomField::CalculatedValueenddef valid_formula_syntax?- # Accept both cf_123 and {{cf_123}} in the formula string- f = formula_string.gsub(/\{\{(cf_\d+)}}/, '\1')-# Attempt to parse the formula. If no error is returned, the formula is syntactically valid.- calculator.ast(f)+ calculator.ast(formula_str_without_patterns)truerescue Dentaku::ParseError, Dentaku::TokenizerErrorfalse@@ -121,13 +118,13 @@ module CustomField::CalculatedValue# List of allowed characters in a formula. This only performs a very basic validation.# The allowed characters are:# Our mathematical operators, whitespace, digits and decimal points- # Additionally, the formula may contain references to custom fields in the form of `cf_123` or `{{cf_123}}`+ # Additionally, the formula may contain references to custom fields in the form of `{{cf_123}}`# where 123 is the ID of the custom field.# 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.allowed_chars = OPERATORS + [" "]- allowed_tokens = /\A(cf_\d+|\{\{cf_\d+}}|\d+\.?\d*|\.\d+)\z/+ allowed_tokens = /\A(\{\{cf_\d+}}|\d+\.?\d*|\.\d+)\z/formula_string.split(Regexp.union(allowed_chars)).reject(&:empty?).all? do |token|token.match?(allowed_tokens)@@ -135,10 +132,13 @@ module CustomField::CalculatedValueend# Returns a list of custom field IDs used in the formula.- # For a formula like `2 + cf_12 + cf_4` or `2 + {{cf_12}} + {{cf_4}}` it returns `[12, 4]`.+ # For a formula like `2 + {{cf_12}} + {{cf_4}}` it returns `[12, 4]`.def cf_ids_used_in_formula(formula_str)- # Match both cf_123 and {{cf_123}}- formula_str.scan(/\b(?:cf_(\d+)|\{\{cf_(\d+)}})\b/).flatten.compact.map(&:to_i)+ formula_str.scan(/\b\{\{cf_(\d+)}}\b/).flatten.compact.map(&:to_i)+ end++ def formula_str_without_patterns+ formula_string.gsub(/\{\{(cf_\d+)}}/, '\1')endendend