LoginSignup
0
1

More than 5 years have passed since last update.

Deviseでusernameカラム追加時に,Rails4以降の正規表現でのバリデーションで気をつけたこと

Posted at

Deviseに,usernameカラムを追加した時に,公式のwikiを見ててまずいな,と思ったポイントです.

公式では,追加したusernameのバリデーションに,次の正規表現を使っていました.

# app/models/user.rb

# Only allow letter, number, underscore and punctuation.
validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true

しかし,:multiline => trueがセキュリティー的に問題です.なしだと,こうなります.
image.png

Rails Guide (6.6)によると,Rails 4以降では,^,$ではなく代わりに\A, \zを使うことが推奨されています.なので,こう変えましょう.

  validates_format_of :name, with: /\A[a-zA-Z0-9_\.]*\z/

参考:
http://jumperson.hatenablog.com/entry/2013/06/27/062237
http://guides.rubyonrails.org/security.html#regular-expressions
https://stackoverflow.com/questions/577653/difference-between-a-z-and-in-ruby-regular-expressions

0
1
0

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
1