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 ensure_scrubbed_html(content, scrubbers, activity.data["object"]["fake"] || false)
39 def get_cached_stripped_html_for_activity(content, activity, key) do
40 get_cached_scrubbed_html_for_activity(
42 HtmlSanitizeEx.Scrubber.StripTags,
48 def ensure_scrubbed_html(
53 {:commit, filter_tags(content, scrubbers)}
56 def ensure_scrubbed_html(
61 {:ignore, filter_tags(content, scrubbers)}
64 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
65 generate_scrubber_signature([scrubber])
68 defp generate_scrubber_signature(scrubbers) do
69 Enum.reduce(scrubbers, "", fn scrubber, signature ->
70 "#{signature}#{to_string(scrubber)}"
74 def extract_first_external_url(_, nil), do: {:error, "No content"}
76 def extract_first_external_url(object, content) do
77 key = "URL|#{object.id}"
79 Cachex.fetch!(:scrubber_cache, key, fn _key ->
82 |> Floki.filter_out("a.mention")
83 |> Floki.attribute("a", "href")
86 {:commit, {:ok, result}}
91 defmodule Pleroma.HTML.Scrubber.TwitterText do
93 An HTML scrubbing policy which limits to twitter-style text. Only
94 paragraphs, breaks and links are allowed through the filter.
97 @markup Application.get_env(:pleroma, :markup)
98 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
100 require HtmlSanitizeEx.Scrubber.Meta
101 alias HtmlSanitizeEx.Scrubber.Meta
103 Meta.remove_cdata_sections_before_scrub()
104 Meta.strip_comments()
107 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
108 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
110 Meta.allow_tag_with_this_attribute_values("a", "rel", [
117 # paragraphs and linebreaks
118 Meta.allow_tag_with_these_attributes("br", [])
119 Meta.allow_tag_with_these_attributes("p", [])
122 Meta.allow_tag_with_these_attributes("span", ["class"])
124 # allow inline images for custom emoji
125 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
127 if @allow_inline_images do
128 # restrict img tags to http/https only, because of MediaProxy.
129 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
131 Meta.allow_tag_with_these_attributes("img", [
139 Meta.strip_everything_not_covered()
142 defmodule Pleroma.HTML.Scrubber.Default do
143 @doc "The default HTML scrubbing policy: no "
145 require HtmlSanitizeEx.Scrubber.Meta
146 alias HtmlSanitizeEx.Scrubber.Meta
147 # credo:disable-for-previous-line
148 # No idea how to fix this one…
150 @markup Application.get_env(:pleroma, :markup)
151 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
153 Meta.remove_cdata_sections_before_scrub()
154 Meta.strip_comments()
156 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
157 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
159 Meta.allow_tag_with_this_attribute_values("a", "rel", [
166 Meta.allow_tag_with_these_attributes("abbr", ["title"])
168 Meta.allow_tag_with_these_attributes("b", [])
169 Meta.allow_tag_with_these_attributes("blockquote", [])
170 Meta.allow_tag_with_these_attributes("br", [])
171 Meta.allow_tag_with_these_attributes("code", [])
172 Meta.allow_tag_with_these_attributes("del", [])
173 Meta.allow_tag_with_these_attributes("em", [])
174 Meta.allow_tag_with_these_attributes("i", [])
175 Meta.allow_tag_with_these_attributes("li", [])
176 Meta.allow_tag_with_these_attributes("ol", [])
177 Meta.allow_tag_with_these_attributes("p", [])
178 Meta.allow_tag_with_these_attributes("pre", [])
179 Meta.allow_tag_with_these_attributes("span", ["class"])
180 Meta.allow_tag_with_these_attributes("strong", [])
181 Meta.allow_tag_with_these_attributes("u", [])
182 Meta.allow_tag_with_these_attributes("ul", [])
184 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
186 if @allow_inline_images do
187 # restrict img tags to http/https only, because of MediaProxy.
188 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
190 Meta.allow_tag_with_these_attributes("img", [
198 @allow_tables Keyword.get(@markup, :allow_tables)
201 Meta.allow_tag_with_these_attributes("table", [])
202 Meta.allow_tag_with_these_attributes("tbody", [])
203 Meta.allow_tag_with_these_attributes("td", [])
204 Meta.allow_tag_with_these_attributes("th", [])
205 Meta.allow_tag_with_these_attributes("thead", [])
206 Meta.allow_tag_with_these_attributes("tr", [])
209 @allow_headings Keyword.get(@markup, :allow_headings)
211 if @allow_headings do
212 Meta.allow_tag_with_these_attributes("h1", [])
213 Meta.allow_tag_with_these_attributes("h2", [])
214 Meta.allow_tag_with_these_attributes("h3", [])
215 Meta.allow_tag_with_these_attributes("h4", [])
216 Meta.allow_tag_with_these_attributes("h5", [])
219 @allow_fonts Keyword.get(@markup, :allow_fonts)
222 Meta.allow_tag_with_these_attributes("font", ["face"])
225 Meta.strip_everything_not_covered()
228 defmodule Pleroma.HTML.Transform.MediaProxy do
229 @moduledoc "Transforms inline image URIs to use MediaProxy."
231 alias Pleroma.Web.MediaProxy
233 def before_scrub(html), do: html
235 def scrub_attribute("img", {"src", "http" <> target}) do
243 def scrub_attribute(_tag, attribute), do: attribute
245 def scrub({"img", attributes, children}) do
248 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
249 |> Enum.reject(&is_nil(&1))
251 {"img", attributes, children}
254 def scrub({:comment, _children}), do: ""
256 def scrub({tag, attributes, children}), do: {tag, attributes, children}
257 def scrub({_tag, children}), do: children
258 def scrub(text), do: text