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")
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 @markup Application.get_env(:pleroma, :markup)
108 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
110 require HtmlSanitizeEx.Scrubber.Meta
111 alias HtmlSanitizeEx.Scrubber.Meta
113 Meta.remove_cdata_sections_before_scrub()
114 Meta.strip_comments()
117 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
119 Meta.allow_tag_with_this_attribute_values("a", "class", [
127 Meta.allow_tag_with_this_attribute_values("a", "rel", [
134 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
136 # paragraphs and linebreaks
137 Meta.allow_tag_with_these_attributes("br", [])
138 Meta.allow_tag_with_these_attributes("p", [])
141 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
142 Meta.allow_tag_with_these_attributes("span", [])
144 # allow inline images for custom emoji
145 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
147 if @allow_inline_images do
148 # restrict img tags to http/https only, because of MediaProxy.
149 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
151 Meta.allow_tag_with_these_attributes("img", [
160 Meta.strip_everything_not_covered()
163 defmodule Pleroma.HTML.Scrubber.Default do
164 @doc "The default HTML scrubbing policy: no "
166 require HtmlSanitizeEx.Scrubber.Meta
167 alias HtmlSanitizeEx.Scrubber.Meta
168 # credo:disable-for-previous-line
169 # No idea how to fix this one…
171 @markup Application.get_env(:pleroma, :markup)
172 @valid_schemes Pleroma.Config.get([:uri_schemes, :valid_schemes], [])
174 Meta.remove_cdata_sections_before_scrub()
175 Meta.strip_comments()
177 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
179 Meta.allow_tag_with_this_attribute_values("a", "class", [
187 Meta.allow_tag_with_this_attribute_values("a", "rel", [
194 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
196 Meta.allow_tag_with_these_attributes("abbr", ["title"])
198 Meta.allow_tag_with_these_attributes("b", [])
199 Meta.allow_tag_with_these_attributes("blockquote", [])
200 Meta.allow_tag_with_these_attributes("br", [])
201 Meta.allow_tag_with_these_attributes("code", [])
202 Meta.allow_tag_with_these_attributes("del", [])
203 Meta.allow_tag_with_these_attributes("em", [])
204 Meta.allow_tag_with_these_attributes("i", [])
205 Meta.allow_tag_with_these_attributes("li", [])
206 Meta.allow_tag_with_these_attributes("ol", [])
207 Meta.allow_tag_with_these_attributes("p", [])
208 Meta.allow_tag_with_these_attributes("pre", [])
209 Meta.allow_tag_with_these_attributes("strong", [])
210 Meta.allow_tag_with_these_attributes("u", [])
211 Meta.allow_tag_with_these_attributes("ul", [])
213 Meta.allow_tag_with_this_attribute_values("span", "class", ["h-card"])
214 Meta.allow_tag_with_these_attributes("span", [])
216 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
218 if @allow_inline_images do
219 # restrict img tags to http/https only, because of MediaProxy.
220 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
222 Meta.allow_tag_with_these_attributes("img", [
231 @allow_tables Keyword.get(@markup, :allow_tables)
234 Meta.allow_tag_with_these_attributes("table", [])
235 Meta.allow_tag_with_these_attributes("tbody", [])
236 Meta.allow_tag_with_these_attributes("td", [])
237 Meta.allow_tag_with_these_attributes("th", [])
238 Meta.allow_tag_with_these_attributes("thead", [])
239 Meta.allow_tag_with_these_attributes("tr", [])
242 @allow_headings Keyword.get(@markup, :allow_headings)
244 if @allow_headings do
245 Meta.allow_tag_with_these_attributes("h1", [])
246 Meta.allow_tag_with_these_attributes("h2", [])
247 Meta.allow_tag_with_these_attributes("h3", [])
248 Meta.allow_tag_with_these_attributes("h4", [])
249 Meta.allow_tag_with_these_attributes("h5", [])
252 @allow_fonts Keyword.get(@markup, :allow_fonts)
255 Meta.allow_tag_with_these_attributes("font", ["face"])
258 Meta.strip_everything_not_covered()
261 defmodule Pleroma.HTML.Transform.MediaProxy do
262 @moduledoc "Transforms inline image URIs to use MediaProxy."
264 alias Pleroma.Web.MediaProxy
266 def before_scrub(html), do: html
268 def scrub_attribute("img", {"src", "http" <> target}) do
276 def scrub_attribute(_tag, attribute), do: attribute
278 def scrub({"img", attributes, children}) do
281 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
282 |> Enum.reject(&is_nil(&1))
284 {"img", attributes, children}
287 def scrub({:comment, _children}), do: ""
289 def scrub({tag, attributes, children}), do: {tag, attributes, children}
290 def scrub({_tag, children}), do: children
291 def scrub(text), do: text