Defect #43521
openSaving a custom field fails with 500 when regular expression is invalid
0%
Description
When a custom field has the format "Text" or "Long text", an Internal Server Error occurs on save if the "Regular expression" contains an invalid pattern and the "Default value" is not empty.
The reason is that the default value is validated using Regexp.new with the invalid pattern, which raises a RegexpError exception.
source:tags/6.1.0/lib/redmine/field_format.rb#L384:
unless custom_field.regexp.blank? or Regexp.new(custom_field.regexp).match?(value)
errs << ::I18n.t('activerecord.errors.messages.invalid')
end
The attached patch is a workaround for this issue. It rescues RegexpError during the Regexp.new call and skips the regular expression validation when the pattern is invalid, preventing the Internal Server Error. The validity of the regular expression is already checked in CustomField#validate_custom_field, so ignoring RegexpError at this point is safe.
Steps to reproduce:
1. On the Administration > Custom fields page, click "New custom field".
2. Select "Issues" and click "Next".
3. Select "Text" as the "Format", enter any value in "Name", enter [ in "Regular expression", and enter any value in "Default value".
4. Click "Create".
Files
No data to display