LoginSignup
3
4

More than 5 years have passed since last update.

Railsでオートリンクを有効にしつつサニタイズ

Posted at

本文をサニタイズしつつ、オートリンクを有効化したいので以下のように設定した。オートリンクはrails_autolinkというgemが必要。

application.rb
config.after_initialize do
  ActionView::Base.sanitized_allowed_tags.clear
  ActionView::Base.sanitized_allowed_tags = ["a"]
  ActionView::Base.sanitized_allowed_attributes = ["rel", "target"]
end

許可するtagをクリアしたあとにホワイトリスト方式で許可するタグを指定した。あと、デフォルトではrel="nofollow"とかtarget="_blank"がサニタイズされてしまうので、許可したい属性を追加した。

3
4
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
3
4