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
48 Meta.remove_cdata_sections_before_scrub()
52 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
53 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
55 # paragraphs and linebreaks
56 Meta.allow_tag_with_these_attributes("br", [])
57 Meta.allow_tag_with_these_attributes("p", [])
60 Meta.allow_tag_with_these_attributes("span", [])
62 # allow inline images for custom emoji
63 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
65 if @allow_inline_images do
66 # restrict img tags to http/https only, because of MediaProxy.
67 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
69 Meta.allow_tag_with_these_attributes("img", [
77 Meta.strip_everything_not_covered()
80 defmodule Pleroma.HTML.Scrubber.Default do
81 @doc "The default HTML scrubbing policy: no "
83 require HtmlSanitizeEx.Scrubber.Meta
84 alias HtmlSanitizeEx.Scrubber.Meta
88 @markup Application.get_env(:pleroma, :markup)
89 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
90 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
92 Meta.remove_cdata_sections_before_scrub()
95 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
96 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
98 Meta.allow_tag_with_these_attributes("b", [])
99 Meta.allow_tag_with_these_attributes("blockquote", [])
100 Meta.allow_tag_with_these_attributes("br", [])
101 Meta.allow_tag_with_these_attributes("code", [])
102 Meta.allow_tag_with_these_attributes("del", [])
103 Meta.allow_tag_with_these_attributes("em", [])
104 Meta.allow_tag_with_these_attributes("i", [])
105 Meta.allow_tag_with_these_attributes("li", [])
106 Meta.allow_tag_with_these_attributes("ol", [])
107 Meta.allow_tag_with_these_attributes("p", [])
108 Meta.allow_tag_with_these_attributes("pre", [])
109 Meta.allow_tag_with_these_attributes("span", [])
110 Meta.allow_tag_with_these_attributes("strong", [])
111 Meta.allow_tag_with_these_attributes("u", [])
112 Meta.allow_tag_with_these_attributes("ul", [])
114 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
116 if @allow_inline_images do
117 # restrict img tags to http/https only, because of MediaProxy.
118 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
120 Meta.allow_tag_with_these_attributes("img", [
128 @allow_tables Keyword.get(@markup, :allow_tables)
131 Meta.allow_tag_with_these_attributes("table", [])
132 Meta.allow_tag_with_these_attributes("tbody", [])
133 Meta.allow_tag_with_these_attributes("td", [])
134 Meta.allow_tag_with_these_attributes("th", [])
135 Meta.allow_tag_with_these_attributes("thead", [])
136 Meta.allow_tag_with_these_attributes("tr", [])
139 @allow_headings Keyword.get(@markup, :allow_headings)
141 if @allow_headings do
142 Meta.allow_tag_with_these_attributes("h1", [])
143 Meta.allow_tag_with_these_attributes("h2", [])
144 Meta.allow_tag_with_these_attributes("h3", [])
145 Meta.allow_tag_with_these_attributes("h4", [])
146 Meta.allow_tag_with_these_attributes("h5", [])
149 @allow_fonts Keyword.get(@markup, :allow_fonts)
152 Meta.allow_tag_with_these_attributes("font", ["face"])
155 Meta.strip_everything_not_covered()
158 defmodule Pleroma.HTML.Transform.MediaProxy do
159 @moduledoc "Transforms inline image URIs to use MediaProxy."
161 alias Pleroma.Web.MediaProxy
163 def before_scrub(html), do: html
165 def scrub_attribute("img", {"src", "http" <> target}) do
173 def scrub_attribute(tag, attribute), do: attribute
175 def scrub({"img", attributes, children}) do
178 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
179 |> Enum.reject(&is_nil(&1))
181 {"img", attributes, children}
184 def scrub({tag, attributes, children}), do: {tag, attributes, children}
185 def scrub({tag, children}), do: children
186 def scrub(text), do: text