1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTML do
6 alias HtmlSanitizeEx.Scrubber
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]
13 Pleroma.Config.get([:markup, :scrub_policy])
17 def filter_tags(html, nil) do
18 filter_tags(html, get_scrubbers())
21 def filter_tags(html, scrubbers) when is_list(scrubbers) do
22 Enum.reduce(scrubbers, html, fn scrubber, html ->
23 filter_tags(html, scrubber)
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)
31 def get_cached_scrubbed_html_for_activity(
36 callback \\ fn x -> x end
38 key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
40 Cachex.fetch!(:scrubber_cache, key, fn _key ->
41 object = Pleroma.Object.normalize(activity)
42 ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
46 def get_cached_stripped_html_for_activity(content, activity, key) do
47 get_cached_scrubbed_html_for_activity(
49 HtmlSanitizeEx.Scrubber.StripTags,
52 &HtmlEntities.decode/1
56 def ensure_scrubbed_html(
64 |> filter_tags(scrubbers)
74 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
75 generate_scrubber_signature([scrubber])
78 defp generate_scrubber_signature(scrubbers) do
79 Enum.reduce(scrubbers, "", fn scrubber, signature ->
80 "#{signature}#{to_string(scrubber)}"
84 def extract_first_external_url(_, nil), do: {:error, "No content"}
86 def extract_first_external_url(object, content) do
87 key = "URL|#{object.id}"
89 Cachex.fetch!(:scrubber_cache, key, fn _key ->
92 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"]")
93 |> Floki.attribute("a", "href")
96 {:commit, {:ok, result}}
101 defmodule Pleroma.HTML.Scrubber.TwitterText do
103 An HTML scrubbing policy which limits to twitter-style text. Only
104 paragraphs, breaks and links are allowed through the filter.
107 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
109 require HtmlSanitizeEx.Scrubber.Meta
110 alias HtmlSanitizeEx.Scrubber.Meta
112 Meta.remove_cdata_sections_before_scrub()
113 Meta.strip_comments()
116 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
118 Meta.allow_tag_with_this_attribute_values("a", "class", [
126 Meta.allow_tag_with_this_attribute_values("a", "rel", [
133 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
135 # paragraphs and linebreaks
136 Meta.allow_tag_with_these_attributes("br", [])
137 Meta.allow_tag_with_these_attributes("p", [])
140 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
141 Meta.allow_tag_with_these_attributes("span", [])
143 # allow inline images for custom emoji
144 if Pleroma.Config.get([:markup, :allow_inline_images]) do
145 # restrict img tags to http/https only, because of MediaProxy.
146 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
148 Meta.allow_tag_with_these_attributes("img", [
157 Meta.strip_everything_not_covered()
160 defmodule Pleroma.HTML.Scrubber.Default do
161 @doc "The default HTML scrubbing policy: no "
163 require HtmlSanitizeEx.Scrubber.Meta
164 alias HtmlSanitizeEx.Scrubber.Meta
165 # credo:disable-for-previous-line
166 # No idea how to fix this one…
168 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
170 Meta.remove_cdata_sections_before_scrub()
171 Meta.strip_comments()
173 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
175 Meta.allow_tag_with_this_attribute_values("a", "class", [
183 Meta.allow_tag_with_this_attribute_values("a", "rel", [
191 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
193 Meta.allow_tag_with_these_attributes("abbr", ["title"])
195 Meta.allow_tag_with_these_attributes("b", [])
196 Meta.allow_tag_with_these_attributes("blockquote", [])
197 Meta.allow_tag_with_these_attributes("br", [])
198 Meta.allow_tag_with_these_attributes("code", [])
199 Meta.allow_tag_with_these_attributes("del", [])
200 Meta.allow_tag_with_these_attributes("em", [])
201 Meta.allow_tag_with_these_attributes("i", [])
202 Meta.allow_tag_with_these_attributes("li", [])
203 Meta.allow_tag_with_these_attributes("ol", [])
204 Meta.allow_tag_with_these_attributes("p", [])
205 Meta.allow_tag_with_these_attributes("pre", [])
206 Meta.allow_tag_with_these_attributes("strong", [])
207 Meta.allow_tag_with_these_attributes("sub", [])
208 Meta.allow_tag_with_these_attributes("sup", [])
209 Meta.allow_tag_with_these_attributes("u", [])
210 Meta.allow_tag_with_these_attributes("ul", [])
212 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
213 Meta.allow_tag_with_these_attributes("span", [])
215 @allow_inline_images Pleroma.Config.get([:markup, :allow_inline_images])
217 if @allow_inline_images do
218 # restrict img tags to http/https only, because of MediaProxy.
219 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
221 Meta.allow_tag_with_these_attributes("img", [
230 if Pleroma.Config.get([:markup, :allow_tables]) do
231 Meta.allow_tag_with_these_attributes("table", [])
232 Meta.allow_tag_with_these_attributes("tbody", [])
233 Meta.allow_tag_with_these_attributes("td", [])
234 Meta.allow_tag_with_these_attributes("th", [])
235 Meta.allow_tag_with_these_attributes("thead", [])
236 Meta.allow_tag_with_these_attributes("tr", [])
239 if Pleroma.Config.get([:markup, :allow_headings]) do
240 Meta.allow_tag_with_these_attributes("h1", [])
241 Meta.allow_tag_with_these_attributes("h2", [])
242 Meta.allow_tag_with_these_attributes("h3", [])
243 Meta.allow_tag_with_these_attributes("h4", [])
244 Meta.allow_tag_with_these_attributes("h5", [])
247 if Pleroma.Config.get([:markup, :allow_fonts]) do
248 Meta.allow_tag_with_these_attributes("font", ["face"])
251 Meta.strip_everything_not_covered()
254 defmodule Pleroma.HTML.Transform.MediaProxy do
255 @moduledoc "Transforms inline image URIs to use MediaProxy."
257 alias Pleroma.Web.MediaProxy
259 def before_scrub(html), do: html
261 def scrub_attribute("img", {"src", "http" <> target}) do
269 def scrub_attribute(_tag, attribute), do: attribute
271 def scrub({"img", attributes, children}) do
274 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
275 |> Enum.reject(&is_nil(&1))
277 {"img", attributes, children}
280 def scrub({:comment, _children}), do: ""
282 def scrub({tag, attributes, children}), do: {tag, attributes, children}
283 def scrub({_tag, children}), do: children
284 def scrub(text), do: text
287 defmodule Pleroma.HTML.Scrubber.LinksOnly do
289 An HTML scrubbing policy which limits to links only.
292 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
294 require HtmlSanitizeEx.Scrubber.Meta
295 alias HtmlSanitizeEx.Scrubber.Meta
297 Meta.remove_cdata_sections_before_scrub()
298 Meta.strip_comments()
301 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
303 Meta.allow_tag_with_this_attribute_values("a", "rel", [
312 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
313 Meta.strip_everything_not_covered()