A platform for community discussion. Free, open, simple.
SECURITY: Run custom field validations with save_custom_fields
Daniel Waterworth4494d6253178
diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rbindex 503103691d3..690c976430f 100644--- a/app/models/concerns/has_custom_fields.rb+++ b/app/models/concerns/has_custom_fields.rb@@ -172,7 +172,7 @@ module HasCustomFieldsvalidate :custom_fields_max_items, unless: :custom_fields_clean?validate :custom_fields_value_length, unless: :custom_fields_clean?- after_save :save_custom_fields+ after_save { save_custom_fields(run_validations: false) }endattr_reader :preloaded_custom_fields@@ -265,7 +265,13 @@ module HasCustomFieldson_custom_fields_changeend- def save_custom_fields(force = false)+ def save_custom_fields(force = false, run_validations: true)+ if run_validations+ custom_fields_max_items+ custom_fields_value_length+ raise_validation_error unless errors.empty?+ end+if force || !custom_fields_clean?ActiveRecord::Base.transaction dodup = @custom_fields.dup.with_indifferent_accessdiff --git a/spec/lib/concern/has_custom_fields_spec.rb b/spec/lib/concern/has_custom_fields_spec.rbindex 8c7edc9f115..b23c8cc1834 100644--- a/spec/lib/concern/has_custom_fields_spec.rb+++ b/spec/lib/concern/has_custom_fields_spec.rb@@ -406,6 +406,7 @@ RSpec.describe HasCustomFields dotest_item.custom_fields = { "foo" => "aa" }expect { test_item.save! }.to raise_error(ActiveRecord::RecordInvalid)+ expect { test_item.save_custom_fields }.to raise_error(ActiveRecord::RecordInvalid)enddescribe "upsert_custom_fields" do