1 defmodule Pleroma.HTML do
2 alias HtmlSanitizeEx.Scrubber
4 @markup Application.get_env(:pleroma, :markup)
6 defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
7 defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
8 defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
10 def get_scrubbers() do
11 Keyword.get(@markup, :scrub_policy)
15 def filter_tags(html, nil) do
17 |> Enum.reduce(html, fn scrubber, html ->
18 filter_tags(html, scrubber)
22 def filter_tags(html, scrubber) do
23 html |> Scrubber.scrub(scrubber)
26 def filter_tags(html), do: filter_tags(html, nil)
28 def strip_tags(html) do
29 html |> Scrubber.scrub(Scrubber.StripTags)
33 defmodule Pleroma.HTML.Scrubber.TwitterText do
35 An HTML scrubbing policy which limits to twitter-style text. Only
36 paragraphs, breaks and links are allowed through the filter.
39 @markup Application.get_env(:pleroma, :markup)
40 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
41 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
43 require HtmlSanitizeEx.Scrubber.Meta
44 alias HtmlSanitizeEx.Scrubber.Meta
46 Meta.remove_cdata_sections_before_scrub()
50 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
51 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
53 # paragraphs and linebreaks
54 Meta.allow_tag_with_these_attributes("br", [])
55 Meta.allow_tag_with_these_attributes("p", [])
58 Meta.allow_tag_with_these_attributes("span", [])
60 # allow inline images for custom emoji
61 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
63 if @allow_inline_images do
64 # restrict img tags to http/https only, because of MediaProxy.
65 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
67 Meta.allow_tag_with_these_attributes("img", [
75 Meta.strip_everything_not_covered()
78 defmodule Pleroma.HTML.Scrubber.Default do
79 @doc "The default HTML scrubbing policy: no "
81 require HtmlSanitizeEx.Scrubber.Meta
82 alias HtmlSanitizeEx.Scrubber.Meta
84 @markup Application.get_env(:pleroma, :markup)
85 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
86 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
88 Meta.remove_cdata_sections_before_scrub()
91 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
92 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
94 Meta.allow_tag_with_these_attributes("abbr", ["title"])
96 Meta.allow_tag_with_these_attributes("b", [])
97 Meta.allow_tag_with_these_attributes("blockquote", [])
98 Meta.allow_tag_with_these_attributes("br", [])
99 Meta.allow_tag_with_these_attributes("code", [])
100 Meta.allow_tag_with_these_attributes("del", [])
101 Meta.allow_tag_with_these_attributes("em", [])
102 Meta.allow_tag_with_these_attributes("i", [])
103 Meta.allow_tag_with_these_attributes("li", [])
104 Meta.allow_tag_with_these_attributes("ol", [])
105 Meta.allow_tag_with_these_attributes("p", [])
106 Meta.allow_tag_with_these_attributes("pre", [])
107 Meta.allow_tag_with_these_attributes("span", [])
108 Meta.allow_tag_with_these_attributes("strong", [])
109 Meta.allow_tag_with_these_attributes("u", [])
110 Meta.allow_tag_with_these_attributes("ul", [])
112 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
114 if @allow_inline_images do
115 # restrict img tags to http/https only, because of MediaProxy.
116 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
118 Meta.allow_tag_with_these_attributes("img", [
126 @allow_tables Keyword.get(@markup, :allow_tables)
129 Meta.allow_tag_with_these_attributes("table", [])
130 Meta.allow_tag_with_these_attributes("tbody", [])
131 Meta.allow_tag_with_these_attributes("td", [])
132 Meta.allow_tag_with_these_attributes("th", [])
133 Meta.allow_tag_with_these_attributes("thead", [])
134 Meta.allow_tag_with_these_attributes("tr", [])
137 @allow_headings Keyword.get(@markup, :allow_headings)
139 if @allow_headings do
140 Meta.allow_tag_with_these_attributes("h1", [])
141 Meta.allow_tag_with_these_attributes("h2", [])
142 Meta.allow_tag_with_these_attributes("h3", [])
143 Meta.allow_tag_with_these_attributes("h4", [])
144 Meta.allow_tag_with_these_attributes("h5", [])
147 @allow_fonts Keyword.get(@markup, :allow_fonts)
150 Meta.allow_tag_with_these_attributes("font", ["face"])
153 Meta.strip_everything_not_covered()
156 defmodule Pleroma.HTML.Transform.MediaProxy do
157 @moduledoc "Transforms inline image URIs to use MediaProxy."
159 alias Pleroma.Web.MediaProxy
161 def before_scrub(html), do: html
163 def scrub_attribute("img", {"src", "http" <> target}) do
171 def scrub_attribute(tag, attribute), do: attribute
173 def scrub({"img", attributes, children}) do
176 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
177 |> Enum.reject(&is_nil(&1))
179 {"img", attributes, children}
182 def scrub({:comment, children}), do: ""
184 def scrub({tag, attributes, children}), do: {tag, attributes, children}
185 def scrub({tag, children}), do: children
186 def scrub(text), do: text