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(content, scrubbers, activity, key \\ "") do
32 key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
34 Cachex.fetch!(:scrubber_cache, key, fn _key ->
35 object = Pleroma.Object.normalize(activity)
36 ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false)
40 def get_cached_stripped_html_for_activity(content, activity, key) do
41 get_cached_scrubbed_html_for_activity(
43 HtmlSanitizeEx.Scrubber.StripTags,
49 def ensure_scrubbed_html(
54 {:commit, filter_tags(content, scrubbers)}
57 def ensure_scrubbed_html(
62 {:ignore, filter_tags(content, scrubbers)}
65 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
66 generate_scrubber_signature([scrubber])
69 defp generate_scrubber_signature(scrubbers) do
70 Enum.reduce(scrubbers, "", fn scrubber, signature ->
71 "#{signature}#{to_string(scrubber)}"
75 def extract_first_external_url(_, nil), do: {:error, "No content"}
77 def extract_first_external_url(object, content) do
78 key = "URL|#{object.id}"
80 Cachex.fetch!(:scrubber_cache, key, fn _key ->
83 |> Floki.filter_out("a.mention")
84 |> Floki.attribute("a", "href")
87 {:commit, {:ok, result}}
92 defmodule Pleroma.HTML.Scrubber.TwitterText do
94 An HTML scrubbing policy which limits to twitter-style text. Only
95 paragraphs, breaks and links are allowed through the filter.
98 @markup Application.get_env(:pleroma, :markup)
99 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
101 require HtmlSanitizeEx.Scrubber.Meta
102 alias HtmlSanitizeEx.Scrubber.Meta
104 Meta.remove_cdata_sections_before_scrub()
105 Meta.strip_comments()
108 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
110 Meta.allow_tag_with_this_attribute_values("a", "class", [
118 Meta.allow_tag_with_this_attribute_values("a", "rel", [
125 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
127 # paragraphs and linebreaks
128 Meta.allow_tag_with_these_attributes("br", [])
129 Meta.allow_tag_with_these_attributes("p", [])
132 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
133 Meta.allow_tag_with_these_attributes("span", [])
135 # allow inline images for custom emoji
136 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
138 if @allow_inline_images do
139 # restrict img tags to http/https only, because of MediaProxy.
140 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
142 Meta.allow_tag_with_these_attributes("img", [
150 Meta.strip_everything_not_covered()
153 defmodule Pleroma.HTML.Scrubber.Default do
154 @doc "The default HTML scrubbing policy: no "
156 require HtmlSanitizeEx.Scrubber.Meta
157 alias HtmlSanitizeEx.Scrubber.Meta
158 # credo:disable-for-previous-line
159 # No idea how to fix this one…
161 @markup Application.get_env(:pleroma, :markup)
162 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
164 Meta.remove_cdata_sections_before_scrub()
165 Meta.strip_comments()
167 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
169 Meta.allow_tag_with_this_attribute_values("a", "class", [
177 Meta.allow_tag_with_this_attribute_values("a", "rel", [
184 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
186 Meta.allow_tag_with_these_attributes("abbr", ["title"])
188 Meta.allow_tag_with_these_attributes("b", [])
189 Meta.allow_tag_with_these_attributes("blockquote", [])
190 Meta.allow_tag_with_these_attributes("br", [])
191 Meta.allow_tag_with_these_attributes("code", [])
192 Meta.allow_tag_with_these_attributes("del", [])
193 Meta.allow_tag_with_these_attributes("em", [])
194 Meta.allow_tag_with_these_attributes("i", [])
195 Meta.allow_tag_with_these_attributes("li", [])
196 Meta.allow_tag_with_these_attributes("ol", [])
197 Meta.allow_tag_with_these_attributes("p", [])
198 Meta.allow_tag_with_these_attributes("pre", [])
199 Meta.allow_tag_with_these_attributes("strong", [])
200 Meta.allow_tag_with_these_attributes("u", [])
201 Meta.allow_tag_with_these_attributes("ul", [])
203 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
204 Meta.allow_tag_with_these_attributes("span", [])
206 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
208 if @allow_inline_images do
209 # restrict img tags to http/https only, because of MediaProxy.
210 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
212 Meta.allow_tag_with_these_attributes("img", [
220 @allow_tables Keyword.get(@markup, :allow_tables)
223 Meta.allow_tag_with_these_attributes("table", [])
224 Meta.allow_tag_with_these_attributes("tbody", [])
225 Meta.allow_tag_with_these_attributes("td", [])
226 Meta.allow_tag_with_these_attributes("th", [])
227 Meta.allow_tag_with_these_attributes("thead", [])
228 Meta.allow_tag_with_these_attributes("tr", [])
231 @allow_headings Keyword.get(@markup, :allow_headings)
233 if @allow_headings do
234 Meta.allow_tag_with_these_attributes("h1", [])
235 Meta.allow_tag_with_these_attributes("h2", [])
236 Meta.allow_tag_with_these_attributes("h3", [])
237 Meta.allow_tag_with_these_attributes("h4", [])
238 Meta.allow_tag_with_these_attributes("h5", [])
241 @allow_fonts Keyword.get(@markup, :allow_fonts)
244 Meta.allow_tag_with_these_attributes("font", ["face"])
247 Meta.strip_everything_not_covered()
250 defmodule Pleroma.HTML.Transform.MediaProxy do
251 @moduledoc "Transforms inline image URIs to use MediaProxy."
253 alias Pleroma.Web.MediaProxy
255 def before_scrub(html), do: html
257 def scrub_attribute("img", {"src", "http" <> target}) do
265 def scrub_attribute(_tag, attribute), do: attribute
267 def scrub({"img", attributes, children}) do
270 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
271 |> Enum.reject(&is_nil(&1))
273 {"img", attributes, children}
276 def scrub({:comment, _children}), do: ""
278 def scrub({tag, attributes, children}), do: {tag, attributes, children}
279 def scrub({_tag, children}), do: children
280 def scrub(text), do: text