0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails 正規表現での^,$

Last updated at Posted at 2020-11-16

郵便番号を正規表現でチェックする

エラーが出た実装

user.rb
class User < ActiveRecord::Base
  validates :zip_code, format: { with: /^\d{3}\-?\d{4}$/ }
end

これでアプリケーションを実行すると以下のエラーが出ました。

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?

セキュリティリスクがあるので

  • 行頭の ^\A
  • 行末の $\z

を使え、と言われた。

修正した実装

user.rb
class User < ActiveRecord::Base
  validates :zip_code, format: { with: /\A\d{3}\-?\d{4}\z/ }
end

これでエラーは出なくなりました。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?