Permalink
Browse files

Invert conditional to avoid double checking for Regexp

  • Loading branch information...
1 parent 9014a79 commit 374d465f2894dc5b120cc6a5f28619a6e60407be @carlosantoniodasilva carlosantoniodasilva committed Nov 15, 2013
Showing with 12 additions and 11 deletions.
  1. +12 −11 activemodel/lib/active_model/validations/format.rb
@@ -32,23 +32,24 @@ def record_error(record, attribute, name, value)
record.errors.add(attribute, :invalid, options.except(name).merge!(value: value))
end
- def regexp_using_multiline_anchors?(regexp)
- source = regexp.source
- source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$"))
- end
-
def check_options_validity(name)
if option = options[name]
- if !option.is_a?(Regexp) && !option.respond_to?(:call)
+ if option.is_a?(Regexp)
+ if options[:multiline] != true && regexp_using_multiline_anchors?(option)
+ raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \
+ "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \
+ ":multiline => true option?"
+ end
+ elsif !option.respond_to?(:call)
raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"
- elsif option.is_a?(Regexp) &&
- regexp_using_multiline_anchors?(option) && options[:multiline] != true
- raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \
- "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \
- ":multiline => true option?"
end
end
end
+
+ def regexp_using_multiline_anchors?(regexp)
+ source = regexp.source
+ source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$"))
+ end
end
module HelperMethods

0 comments on commit 374d465

Please sign in to comment.