-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
sanitization_filter.rb
190 lines (184 loc) · 4.28 KB
/
sanitization_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true
require "selma"
class HTMLPipeline
# A special filter with sanization routines and allowlists. This module defines
# what HTML is allowed in user provided content and fixes up issues with
# unbalanced tags and whatnot.
#
# See the Selma docs for more information on the underlying library:
#
# https://github.com/gjtorikian/selma/#readme
#
# This filter does not write additional information to the context.
class SanitizationFilter
VALID_PROTOCOLS = Selma::Sanitizer::Config::VALID_PROTOCOLS.dup
# The main sanitization allowlist. Only these elements and attributes are
# allowed through by default.
DEFAULT_CONFIG = Selma::Sanitizer::Config.freeze_config({
elements: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"br",
"b",
"i",
"strong",
"em",
"a",
"pre",
"code",
"img",
"tt",
"div",
"ins",
"del",
"sup",
"sub",
"p",
"picture",
"ol",
"ul",
"table",
"thead",
"tbody",
"tfoot",
"blockquote",
"dl",
"dt",
"dd",
"kbd",
"q",
"samp",
"var",
"hr",
"ruby",
"rt",
"rp",
"li",
"tr",
"td",
"th",
"s",
"strike",
"summary",
"details",
"caption",
"figure",
"figcaption",
"abbr",
"bdo",
"cite",
"dfn",
"mark",
"small",
"source",
"span",
"time",
"wbr",
],
attributes: {
"a" => ["href"],
"img" => ["src", "longdesc", "loading", "alt"],
"div" => ["itemscope", "itemtype"],
"blockquote" => ["cite"],
"del" => ["cite"],
"ins" => ["cite"],
"q" => ["cite"],
"source" => ["srcset"],
all: [
"abbr",
"accept",
"accept-charset",
"accesskey",
"action",
"align",
"alt",
"aria-describedby",
"aria-hidden",
"aria-label",
"aria-labelledby",
"axis",
"border",
"char",
"charoff",
"charset",
"checked",
"clear",
"cols",
"colspan",
"compact",
"coords",
"datetime",
"dir",
"disabled",
"enctype",
"for",
"frame",
"headers",
"height",
"hreflang",
"hspace",
"id",
"ismap",
"label",
"lang",
"maxlength",
"media",
"method",
"multiple",
"name",
"nohref",
"noshade",
"nowrap",
"open",
"progress",
"prompt",
"readonly",
"rel",
"rev",
"role",
"rows",
"rowspan",
"rules",
"scope",
"selected",
"shape",
"size",
"span",
"start",
"summary",
"tabindex",
"title",
"type",
"usemap",
"valign",
"value",
"width",
"itemprop",
],
},
protocols: {
"a" => { "href" => Selma::Sanitizer::Config::VALID_PROTOCOLS }.freeze,
"blockquote" => { "cite" => ["http", "https", :relative].freeze },
"del" => { "cite" => ["http", "https", :relative].freeze },
"ins" => { "cite" => ["http", "https", :relative].freeze },
"q" => { "cite" => ["http", "https", :relative].freeze },
"img" => {
"src" => ["http", "https", :relative].freeze,
"longdesc" => ["http", "https", :relative].freeze,
},
},
})
class << self
def call(html, config)
raise ArgumentError, "html must be a String, not #{html.class}" unless html.is_a?(String)
raise ArgumentError, "config must be a Hash, not #{config.class}" unless config.is_a?(Hash)
sanitization_config = Selma::Sanitizer.new(config)
Selma::Rewriter.new(sanitizer: sanitization_config).rewrite(html)
end
end
end
end