How good is the Rails sanitize() method?

后端 未结 3 1541
礼貌的吻别
礼貌的吻别 2020-12-29 10:07

Can I use ActionView::Helpers::SanitizeHelper#sanitize on user-entered text that I plan on showing to other users? E.g., will it properly handle all cases described on this

3条回答
  •  臣服心动
    2020-12-29 10:48

    Ryan Grove's Sanitize goes a lot farther than Rails 3 sanitize. It ensures the output HTML is well-formed and has three built-in whitelists:

    Sanitize::Config::RESTRICTED Allows only very simple inline formatting markup. No links, images, or block elements.

    Sanitize::Config::BASIC Allows a variety of markup including formatting tags, links, and lists. Images and tables are not allowed, links are limited to FTP, HTTP, HTTPS, and mailto protocols, and a attribute is added to all links to mitigate SEO spam.

    Sanitize::Config::RELAXED Allows an even wider variety of markup than BASIC, including images and tables. Links are still limited to FTP, HTTP, HTTPS, and mailto protocols, while images are limited to HTTP and HTTPS. In this mode, is not added to links.

提交回复
热议问题