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
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)
32 defmodule Pleroma.HTML.Scrubber.TwitterText do
34 An HTML scrubbing policy which limits to twitter-style text. Only
35 paragraphs, breaks and links are allowed through the filter.
38 @markup Application.get_env(:pleroma, :markup)
39 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
40 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
42 require HtmlSanitizeEx.Scrubber.Meta
43 alias HtmlSanitizeEx.Scrubber.Meta
49 Meta.remove_cdata_sections_before_scrub()
53 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
54 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
56 # paragraphs and linebreaks
57 Meta.allow_tag_with_these_attributes("br", [])
58 Meta.allow_tag_with_these_attributes("p", [])
61 Meta.allow_tag_with_these_attributes("span", [])
63 # allow inline images for custom emoji
64 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
66 if @allow_inline_images do
67 # restrict img tags to http/https only, because of MediaProxy.
68 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
70 Meta.allow_tag_with_these_attributes("img", [
78 Meta.strip_everything_not_covered()
81 defmodule Pleroma.HTML.Scrubber.Default do
82 @doc "The default HTML scrubbing policy: no "
84 require HtmlSanitizeEx.Scrubber.Meta
85 alias HtmlSanitizeEx.Scrubber.Meta
91 @markup Application.get_env(:pleroma, :markup)
92 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
93 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
95 Meta.remove_cdata_sections_before_scrub()
98 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
99 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
101 Meta.allow_tag_with_these_attributes("abbr", ["title"])
103 Meta.allow_tag_with_these_attributes("b", [])
104 Meta.allow_tag_with_these_attributes("blockquote", [])
105 Meta.allow_tag_with_these_attributes("br", [])
106 Meta.allow_tag_with_these_attributes("code", [])
107 Meta.allow_tag_with_these_attributes("del", [])
108 Meta.allow_tag_with_these_attributes("em", [])
109 Meta.allow_tag_with_these_attributes("i", [])
110 Meta.allow_tag_with_these_attributes("li", [])
111 Meta.allow_tag_with_these_attributes("ol", [])
112 Meta.allow_tag_with_these_attributes("p", [])
113 Meta.allow_tag_with_these_attributes("pre", [])
114 Meta.allow_tag_with_these_attributes("span", [])
115 Meta.allow_tag_with_these_attributes("strong", [])
116 Meta.allow_tag_with_these_attributes("u", [])
117 Meta.allow_tag_with_these_attributes("ul", [])
119 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
121 if @allow_inline_images do
122 # restrict img tags to http/https only, because of MediaProxy.
123 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
125 Meta.allow_tag_with_these_attributes("img", [
133 @allow_tables Keyword.get(@markup, :allow_tables)
136 Meta.allow_tag_with_these_attributes("table", [])
137 Meta.allow_tag_with_these_attributes("tbody", [])
138 Meta.allow_tag_with_these_attributes("td", [])
139 Meta.allow_tag_with_these_attributes("th", [])
140 Meta.allow_tag_with_these_attributes("thead", [])
141 Meta.allow_tag_with_these_attributes("tr", [])
144 @allow_headings Keyword.get(@markup, :allow_headings)
146 if @allow_headings do
147 Meta.allow_tag_with_these_attributes("h1", [])
148 Meta.allow_tag_with_these_attributes("h2", [])
149 Meta.allow_tag_with_these_attributes("h3", [])
150 Meta.allow_tag_with_these_attributes("h4", [])
151 Meta.allow_tag_with_these_attributes("h5", [])
154 @allow_fonts Keyword.get(@markup, :allow_fonts)
157 Meta.allow_tag_with_these_attributes("font", ["face"])
160 Meta.strip_everything_not_covered()
163 defmodule Pleroma.HTML.Transform.MediaProxy do
164 @moduledoc "Transforms inline image URIs to use MediaProxy."
166 alias Pleroma.Web.MediaProxy
172 def before_scrub(html), do: html
174 def scrub_attribute("img", {"src", "http" <> target}) do
182 def scrub_attribute(_tag, attribute), do: attribute
184 def scrub({"img", attributes, children}) do
187 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
188 |> Enum.reject(&is_nil(&1))
190 {"img", attributes, children}
193 def scrub({:comment, _children}), do: ""
195 def scrub({tag, attributes, children}), do: {tag, attributes, children}
196 def scrub({_tag, children}), do: children
197 def scrub(text), do: text