Handling of line beginning and line ending in regular expressions in Ruby

When I tried to check the validation of the model with a regular expression in Rails, an error occurred, so make a note as a memorandum.

environment

Ruby 2.5.1 Rails 5.2

Implementation in error

user.rb


class User < ApplicationRecord
  validates :age, format: { with: /^[0-9]+$/, message: "Can only be entered as a number."}
end

Details of the error

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?

As for the content of the error, it seems that it is said that \ A, \ z can be used because ^, $ has a security risk. ^A $z If it is not the above method, you can add the : multiline => true option to prevent the error from occurring even in the code containing the vulnerability.

According to my research, regular expressions have become stricter as a security measure since Rails 4.

In Ruby, if you want to make a match with a specific head and end, it seems better to implement it as follows. There is a way to add the : multiline => true option, but in the case of validation processing etc., I felt that it is better not to specify it unless there is a special reason.

user.rb


class User < ApplicationRecord
  validates :age, format: { with: /\A[0-9]+\z/, message: "Can only be entered as a number."}
end

For the time being, it is defined as follows in the official Ruby reference.

^ Matches the beginning of a line. The beginning of a line means the beginning of a character string or after a line break. $ Matches the end of a line. The end of a line means the end of a character string or before a line break. \ A Matches the beginning of a string. \ z Matches the end of the string.

References

Use \ A and \ z instead of ^ and $ for regular expression validation Regular expressions have become stricter in Rails 4. Ruby 2.7.0 Reference Manual Regular Expression

Recommended Posts

Handling of line beginning and line ending in regular expressions in Ruby
About regular expressions in Ruby
Handling of date and time in Ruby. Use Date and Time properly.
Summary of hashes and symbols in Ruby
About regular expressions used in ruby sub method
colorize and regular expressions
[Ruby] Exclude and replace specific patterns with regular expressions
In fact, Ruby distinguishes between line breaks and whitespace.
[Java] Comparison method of character strings and comparison method using regular expressions
[Java] Summary of regular expressions
[Ruby] Exception handling in functions
Judgment of fractions in Ruby
Basics of sending Gmail in Ruby
Implementation of ls command in Ruby
[Ruby] then keyword and case in
Write keys and values in Ruby
Implementation of validation using regular expressions
The difference between puts and print in Ruby is not just the presence or absence of line breaks
[Ruby basics] About the role of true and break in the while statement
Name a group of regular expressions (Java)
Acquisition of article information in ruby ​​scraping
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
Make bubble sort and selection sort in Ruby
Explanation of Ruby Time and Date objects
Regular expressions that match 99% of email addresses
Tips for gRPC error handling in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Comparison of JavaScript objects and Ruby classes
Discrimination of Enums in Java 7 and above