You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Rails 4.2 and above this gem will be responsible for sanitizing HTML fragments in Rails
4
4
applications, i.e. in the `sanitize`, `sanitize_css`, `strip_tags` and `strip_links` methods.
5
5
6
-
Rails Html Sanitizer is only intended to be used with Rails applications. If you need similar functionality in non Rails apps consider using [Loofah](https://github.com/flavorjones/loofah) directly (that's what handles sanitization under the hood).
7
-
8
-
## Installation
9
-
10
-
Add this line to your application's Gemfile:
11
-
12
-
gem 'rails-html-sanitizer'
13
-
14
-
And then execute:
15
-
16
-
$ bundle
17
-
18
-
Or install it yourself as:
19
-
20
-
$ gem install rails-html-sanitizer
6
+
Rails HTML Sanitizer is only intended to be used with Rails applications. If you need similar functionality in non Rails apps consider using [Loofah](https://github.com/flavorjones/loofah) directly (that's what handles sanitization under the hood).
21
7
22
8
## Usage
23
9
24
10
### A note on HTML entities
25
11
26
-
__Rails::HTML sanitizers are intended to be used by the view layer, at page-render time. They are *not* intended to sanitize persisted strings that will sanitized *again* at page-render time.__
12
+
__RailsHTML sanitizers are intended to be used by the view layer, at page-render time. They are *not* intended to sanitize persisted strings that will sanitized *again* at page-render time.__
27
13
28
14
Proper HTML sanitization will replace some characters with HTML entities. For example, `<` will be replaced with `<` to ensure that the markup is well-formed.
29
15
@@ -47,62 +33,101 @@ You might simply choose to persist the untrusted string as-is (the raw input), a
47
33
48
34
That raw string, if rendered in an non-HTML context (like SMS), must also be sanitized by a method appropriate for that context. You may wish to look into using [Loofah](https://github.com/flavorjones/loofah) or [Sanitize](https://github.com/rgrove/sanitize) to customize how this sanitization works, including omitting HTML entities in the final string.
49
35
50
-
If you really want to sanitize the string that's stored in your database, you may wish to look into [Loofah::ActiveRecord](https://github.com/flavorjones/loofah-activerecord) rather than use the Rails::HTML sanitizers.
36
+
If you really want to sanitize the string that's stored in your database, you may wish to look into [Loofah::ActiveRecord](https://github.com/flavorjones/loofah-activerecord) rather than use the Rails HTML sanitizers.
37
+
38
+
39
+
### A note on module names
40
+
41
+
In versions < 1.6, the only module defined by this library was `Rails::Html`. Starting in 1.6, we define three additional modules:
42
+
43
+
-`Rails::HTML` for general functionality (replacing `Rails::Html`)
44
+
-`Rails::HTML4` containing sanitizers that parse content as HTML4
45
+
-`Rails::HTML5` containing sanitizers that parse content as HTML5
46
+
47
+
The following aliases are maintained for backwards compatibility:
48
+
49
+
-`Rails::Html` points to `Rails::HTML`
50
+
-`Rails::HTML::FullSanitizer` points to `Rails::HTML4::FullSanitizer`
51
+
-`Rails::HTML::LinkSanitizer` points to `Rails::HTML4::LinkSanitizer`
52
+
-`Rails::HTML::SafeListSanitizer` points to `Rails::HTML4::SafeListSanitizer`
51
53
52
54
53
55
### Sanitizers
54
56
55
-
All sanitizers respond to `sanitize`.
57
+
All sanitizers respond to `sanitize`, and are available in variants that use either HTML4 or HTML5 parsing, under the `Rails::HTML4` and `Rails::HTML5` namespaces, respectively.
56
58
57
59
#### FullSanitizer
58
60
59
61
```ruby
60
-
full_sanitizer =Rails::Html::FullSanitizer.new
62
+
full_sanitizer =Rails::HTML5::FullSanitizer.new
61
63
full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
62
64
# => Bold no more! See more here...
63
65
```
64
66
67
+
or, if you insist on parsing the content as HTML4:
68
+
69
+
```ruby
70
+
full_sanitizer =Rails::HTML4::FullSanitizer.new
71
+
full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
72
+
# => Bold no more! See more here...
73
+
```
74
+
75
+
HTML5 version:
76
+
77
+
78
+
65
79
#### LinkSanitizer
66
80
67
81
```ruby
68
-
link_sanitizer =Rails::Html::LinkSanitizer.new
82
+
link_sanitizer =Rails::HTML5::LinkSanitizer.new
69
83
link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
70
84
# => Only the link text will be kept.
71
85
```
72
86
87
+
or, if you insist on parsing the content as HTML4:
88
+
89
+
```ruby
90
+
link_sanitizer =Rails::HTML4::LinkSanitizer.new
91
+
link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
92
+
# => Only the link text will be kept.
93
+
```
94
+
95
+
73
96
#### SafeListSanitizer
74
97
98
+
This sanitizer is also available as an HTML4 variant, but for simplicity we'll document only the HTML5 variant below.
Where `PermitScrubber` picks out tags and attributes to permit in sanitization,
134
-
`Rails::Html::TargetScrubber` targets them for removal. See https://github.com/flavorjones/loofah/blob/main/lib/loofah/html5/safelist.rb for the tag list.
159
+
`Rails::HTML::TargetScrubber` targets them for removal. See https://github.com/flavorjones/loofah/blob/main/lib/loofah/html5/safelist.rb for the tag list.
135
160
136
161
**Note:** by default, it will scrub anything that is not part of the permitted tags from
0 commit comments