mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 06:51:27 +08:00
FIX: Validate permalink_normalizations setting (#16604)
When an admin enters a badly formed regular expression in the permalink_normalizations site setting, a RegexpError exception is generated everytime a URL is normalized (see Permalink.normalize_url). The new validator validates every regular expression present in the setting value (delimited by '|').
This commit is contained in:
22
lib/validators/regexp_list_validator.rb
Normal file
22
lib/validators/regexp_list_validator.rb
Normal file
@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class RegexpListValidator
|
||||
def initialize(opts = {})
|
||||
end
|
||||
|
||||
def valid_value?(value)
|
||||
value.split("|").all? do |regexp|
|
||||
begin
|
||||
Regexp.new(regexp)
|
||||
rescue RegexpError => e
|
||||
@regexp = regexp
|
||||
@error_message = e.message
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def error_message
|
||||
I18n.t("site_settings.errors.invalid_regex_with_message", regex: @regexp, message: @error_message)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user