1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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]
12 def get_scrubbers() do
13 Pleroma.Config.get([:markup, :scrub_policy])
17 def filter_tags(html, nil) do
19 |> Enum.reduce(html, fn scrubber, html ->
20 filter_tags(html, scrubber)
24 def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
25 def filter_tags(html), do: filter_tags(html, nil)
26 def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
29 defmodule Pleroma.HTML.Scrubber.TwitterText do
31 An HTML scrubbing policy which limits to twitter-style text. Only
32 paragraphs, breaks and links are allowed through the filter.
35 @markup Application.get_env(:pleroma, :markup)
36 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
37 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
39 require HtmlSanitizeEx.Scrubber.Meta
40 alias HtmlSanitizeEx.Scrubber.Meta
42 Meta.remove_cdata_sections_before_scrub()
46 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
47 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
49 # paragraphs and linebreaks
50 Meta.allow_tag_with_these_attributes("br", [])
51 Meta.allow_tag_with_these_attributes("p", [])
54 Meta.allow_tag_with_these_attributes("span", [])
56 # allow inline images for custom emoji
57 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
59 if @allow_inline_images do
60 # restrict img tags to http/https only, because of MediaProxy.
61 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
63 Meta.allow_tag_with_these_attributes("img", [
71 Meta.strip_everything_not_covered()
74 defmodule Pleroma.HTML.Scrubber.Default do
75 @doc "The default HTML scrubbing policy: no "
77 require HtmlSanitizeEx.Scrubber.Meta
78 alias HtmlSanitizeEx.Scrubber.Meta
80 @markup Application.get_env(:pleroma, :markup)
81 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
82 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
84 Meta.remove_cdata_sections_before_scrub()
87 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
88 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
90 Meta.allow_tag_with_these_attributes("abbr", ["title"])
92 Meta.allow_tag_with_these_attributes("b", [])
93 Meta.allow_tag_with_these_attributes("blockquote", [])
94 Meta.allow_tag_with_these_attributes("br", [])
95 Meta.allow_tag_with_these_attributes("code", [])
96 Meta.allow_tag_with_these_attributes("del", [])
97 Meta.allow_tag_with_these_attributes("em", [])
98 Meta.allow_tag_with_these_attributes("i", [])
99 Meta.allow_tag_with_these_attributes("li", [])
100 Meta.allow_tag_with_these_attributes("ol", [])
101 Meta.allow_tag_with_these_attributes("p", [])
102 Meta.allow_tag_with_these_attributes("pre", [])
103 Meta.allow_tag_with_these_attributes("span", [])
104 Meta.allow_tag_with_these_attributes("strong", [])
105 Meta.allow_tag_with_these_attributes("u", [])
106 Meta.allow_tag_with_these_attributes("ul", [])
108 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
110 if @allow_inline_images do
111 # restrict img tags to http/https only, because of MediaProxy.
112 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
114 Meta.allow_tag_with_these_attributes("img", [
122 @allow_tables Keyword.get(@markup, :allow_tables)
125 Meta.allow_tag_with_these_attributes("table", [])
126 Meta.allow_tag_with_these_attributes("tbody", [])
127 Meta.allow_tag_with_these_attributes("td", [])
128 Meta.allow_tag_with_these_attributes("th", [])
129 Meta.allow_tag_with_these_attributes("thead", [])
130 Meta.allow_tag_with_these_attributes("tr", [])
133 @allow_headings Keyword.get(@markup, :allow_headings)
135 if @allow_headings do
136 Meta.allow_tag_with_these_attributes("h1", [])
137 Meta.allow_tag_with_these_attributes("h2", [])
138 Meta.allow_tag_with_these_attributes("h3", [])
139 Meta.allow_tag_with_these_attributes("h4", [])
140 Meta.allow_tag_with_these_attributes("h5", [])
143 @allow_fonts Keyword.get(@markup, :allow_fonts)
146 Meta.allow_tag_with_these_attributes("font", ["face"])
149 Meta.strip_everything_not_covered()
152 defmodule Pleroma.HTML.Transform.MediaProxy do
153 @moduledoc "Transforms inline image URIs to use MediaProxy."
155 alias Pleroma.Web.MediaProxy
157 def before_scrub(html), do: html
159 def scrub_attribute("img", {"src", "http" <> target}) do
167 def scrub_attribute(_tag, attribute), do: attribute
169 def scrub({"img", attributes, children}) do
172 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
173 |> Enum.reject(&is_nil(&1))
175 {"img", attributes, children}
178 def scrub({:comment, _children}), do: ""
180 def scrub({tag, attributes, children}), do: {tag, attributes, children}
181 def scrub({_tag, children}), do: children
182 def scrub(text), do: text