html: lock down allowed class attributes to only those related to microformats
[akkoma] / lib / pleroma / html.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTML do
6 alias HtmlSanitizeEx.Scrubber
7
8 defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
9 defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
10 defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
11
12 def get_scrubbers do
13 Pleroma.Config.get([:markup, :scrub_policy])
14 |> get_scrubbers
15 end
16
17 def filter_tags(html, nil) do
18 filter_tags(html, get_scrubbers())
19 end
20
21 def filter_tags(html, scrubbers) when is_list(scrubbers) do
22 Enum.reduce(scrubbers, html, fn scrubber, html ->
23 filter_tags(html, scrubber)
24 end)
25 end
26
27 def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
28 def filter_tags(html), do: filter_tags(html, nil)
29 def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
30
31 def get_cached_scrubbed_html_for_activity(content, scrubbers, activity, key \\ "") do
32 key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
33
34 Cachex.fetch!(:scrubber_cache, key, fn _key ->
35 ensure_scrubbed_html(content, scrubbers, activity.data["object"]["fake"] || false)
36 end)
37 end
38
39 def get_cached_stripped_html_for_activity(content, activity, key) do
40 get_cached_scrubbed_html_for_activity(
41 content,
42 HtmlSanitizeEx.Scrubber.StripTags,
43 activity,
44 key
45 )
46 end
47
48 def ensure_scrubbed_html(
49 content,
50 scrubbers,
51 false = _fake
52 ) do
53 {:commit, filter_tags(content, scrubbers)}
54 end
55
56 def ensure_scrubbed_html(
57 content,
58 scrubbers,
59 true = _fake
60 ) do
61 {:ignore, filter_tags(content, scrubbers)}
62 end
63
64 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
65 generate_scrubber_signature([scrubber])
66 end
67
68 defp generate_scrubber_signature(scrubbers) do
69 Enum.reduce(scrubbers, "", fn scrubber, signature ->
70 "#{signature}#{to_string(scrubber)}"
71 end)
72 end
73
74 def extract_first_external_url(_, nil), do: {:error, "No content"}
75
76 def extract_first_external_url(object, content) do
77 key = "URL|#{object.id}"
78
79 Cachex.fetch!(:scrubber_cache, key, fn _key ->
80 result =
81 content
82 |> Floki.filter_out("a.mention")
83 |> Floki.attribute("a", "href")
84 |> Enum.at(0)
85
86 {:commit, {:ok, result}}
87 end)
88 end
89 end
90
91 defmodule Pleroma.HTML.Scrubber.TwitterText do
92 @moduledoc """
93 An HTML scrubbing policy which limits to twitter-style text. Only
94 paragraphs, breaks and links are allowed through the filter.
95 """
96
97 @markup Application.get_env(:pleroma, :markup)
98 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
99
100 require HtmlSanitizeEx.Scrubber.Meta
101 alias HtmlSanitizeEx.Scrubber.Meta
102
103 Meta.remove_cdata_sections_before_scrub()
104 Meta.strip_comments()
105
106 # links
107 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
108
109 Meta.allow_tag_with_this_attribute_values("a", "class", [
110 "hashtag",
111 "u-url",
112 "mention",
113 "u-url mention",
114 "mention u-url"
115 ])
116
117 Meta.allow_tag_with_this_attribute_values("a", "rel", [
118 "tag",
119 "nofollow",
120 "noopener",
121 "noreferrer"
122 ])
123
124 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
125
126 # paragraphs and linebreaks
127 Meta.allow_tag_with_these_attributes("br", [])
128 Meta.allow_tag_with_these_attributes("p", [])
129
130 # microformats
131 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
132 Meta.allow_tag_with_these_attributes("span", [])
133
134 # allow inline images for custom emoji
135 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
136
137 if @allow_inline_images do
138 # restrict img tags to http/https only, because of MediaProxy.
139 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
140
141 Meta.allow_tag_with_these_attributes("img", [
142 "width",
143 "height",
144 "title",
145 "alt"
146 ])
147 end
148
149 Meta.strip_everything_not_covered()
150 end
151
152 defmodule Pleroma.HTML.Scrubber.Default do
153 @doc "The default HTML scrubbing policy: no "
154
155 require HtmlSanitizeEx.Scrubber.Meta
156 alias HtmlSanitizeEx.Scrubber.Meta
157 # credo:disable-for-previous-line
158 # No idea how to fix this one…
159
160 @markup Application.get_env(:pleroma, :markup)
161 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
162
163 Meta.remove_cdata_sections_before_scrub()
164 Meta.strip_comments()
165
166 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
167
168 Meta.allow_tag_with_this_attribute_values("a", "class", [
169 "hashtag",
170 "u-url",
171 "mention",
172 "u-url mention",
173 "mention u-url"
174 ])
175
176 Meta.allow_tag_with_this_attribute_values("a", "rel", [
177 "tag",
178 "nofollow",
179 "noopener",
180 "noreferrer"
181 ])
182
183 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
184
185 Meta.allow_tag_with_these_attributes("abbr", ["title"])
186
187 Meta.allow_tag_with_these_attributes("b", [])
188 Meta.allow_tag_with_these_attributes("blockquote", [])
189 Meta.allow_tag_with_these_attributes("br", [])
190 Meta.allow_tag_with_these_attributes("code", [])
191 Meta.allow_tag_with_these_attributes("del", [])
192 Meta.allow_tag_with_these_attributes("em", [])
193 Meta.allow_tag_with_these_attributes("i", [])
194 Meta.allow_tag_with_these_attributes("li", [])
195 Meta.allow_tag_with_these_attributes("ol", [])
196 Meta.allow_tag_with_these_attributes("p", [])
197 Meta.allow_tag_with_these_attributes("pre", [])
198 Meta.allow_tag_with_these_attributes("strong", [])
199 Meta.allow_tag_with_these_attributes("u", [])
200 Meta.allow_tag_with_these_attributes("ul", [])
201
202 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
203 Meta.allow_tag_with_these_attributes("span", [])
204
205 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
206
207 if @allow_inline_images do
208 # restrict img tags to http/https only, because of MediaProxy.
209 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
210
211 Meta.allow_tag_with_these_attributes("img", [
212 "width",
213 "height",
214 "title",
215 "alt"
216 ])
217 end
218
219 @allow_tables Keyword.get(@markup, :allow_tables)
220
221 if @allow_tables do
222 Meta.allow_tag_with_these_attributes("table", [])
223 Meta.allow_tag_with_these_attributes("tbody", [])
224 Meta.allow_tag_with_these_attributes("td", [])
225 Meta.allow_tag_with_these_attributes("th", [])
226 Meta.allow_tag_with_these_attributes("thead", [])
227 Meta.allow_tag_with_these_attributes("tr", [])
228 end
229
230 @allow_headings Keyword.get(@markup, :allow_headings)
231
232 if @allow_headings do
233 Meta.allow_tag_with_these_attributes("h1", [])
234 Meta.allow_tag_with_these_attributes("h2", [])
235 Meta.allow_tag_with_these_attributes("h3", [])
236 Meta.allow_tag_with_these_attributes("h4", [])
237 Meta.allow_tag_with_these_attributes("h5", [])
238 end
239
240 @allow_fonts Keyword.get(@markup, :allow_fonts)
241
242 if @allow_fonts do
243 Meta.allow_tag_with_these_attributes("font", ["face"])
244 end
245
246 Meta.strip_everything_not_covered()
247 end
248
249 defmodule Pleroma.HTML.Transform.MediaProxy do
250 @moduledoc "Transforms inline image URIs to use MediaProxy."
251
252 alias Pleroma.Web.MediaProxy
253
254 def before_scrub(html), do: html
255
256 def scrub_attribute("img", {"src", "http" <> target}) do
257 media_url =
258 ("http" <> target)
259 |> MediaProxy.url()
260
261 {"src", media_url}
262 end
263
264 def scrub_attribute(_tag, attribute), do: attribute
265
266 def scrub({"img", attributes, children}) do
267 attributes =
268 attributes
269 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
270 |> Enum.reject(&is_nil(&1))
271
272 {"img", attributes, children}
273 end
274
275 def scrub({:comment, _children}), do: ""
276
277 def scrub({tag, attributes, children}), do: {tag, attributes, children}
278 def scrub({_tag, children}), do: children
279 def scrub(text), do: text
280 end