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, scrubber) do
16 html |> Scrubber.scrub(scrubber)
19 def filter_tags(html) do
21 |> Enum.reduce(html, fn scrubber, html ->
22 filter_tags(html, scrubber)
26 def strip_tags(html) do
27 html |> Scrubber.scrub(Scrubber.StripTags)
31 defmodule Pleroma.HTML.Scrubber.TwitterText do
33 An HTML scrubbing policy which limits to twitter-style text. Only
34 paragraphs, breaks and links are allowed through the filter.
37 require HtmlSanitizeEx.Scrubber.Meta
38 alias HtmlSanitizeEx.Scrubber.Meta
40 @valid_schemes ["http", "https"]
42 Meta.remove_cdata_sections_before_scrub()
46 Meta.allow_tag_with_uri_attributes("a", ["href"], @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 @markup Application.get_env(:pleroma, :markup)
58 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
60 if @allow_inline_images do
61 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
63 Meta.allow_tag_with_these_attributes("img", [
72 defmodule Pleroma.HTML.Scrubber.Default do
73 @doc "The default HTML scrubbing policy: no "
75 require HtmlSanitizeEx.Scrubber.Meta
76 alias HtmlSanitizeEx.Scrubber.Meta
78 @valid_schemes ["http", "https"]
80 Meta.remove_cdata_sections_before_scrub()
83 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
84 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
86 Meta.allow_tag_with_these_attributes("b", [])
87 Meta.allow_tag_with_these_attributes("blockquote", [])
88 Meta.allow_tag_with_these_attributes("br", [])
89 Meta.allow_tag_with_these_attributes("code", [])
90 Meta.allow_tag_with_these_attributes("del", [])
91 Meta.allow_tag_with_these_attributes("em", [])
92 Meta.allow_tag_with_these_attributes("i", [])
93 Meta.allow_tag_with_these_attributes("li", [])
94 Meta.allow_tag_with_these_attributes("ol", [])
95 Meta.allow_tag_with_these_attributes("p", [])
96 Meta.allow_tag_with_these_attributes("pre", [])
97 Meta.allow_tag_with_these_attributes("span", [])
98 Meta.allow_tag_with_these_attributes("strong", [])
99 Meta.allow_tag_with_these_attributes("u", [])
100 Meta.allow_tag_with_these_attributes("ul", [])
102 @markup Application.get_env(:pleroma, :markup)
103 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
105 if @allow_inline_images do
106 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
108 Meta.allow_tag_with_these_attributes("img", [
116 @allow_tables Keyword.get(@markup, :allow_tables)
119 Meta.allow_tag_with_these_attributes("table", [])
120 Meta.allow_tag_with_these_attributes("tbody", [])
121 Meta.allow_tag_with_these_attributes("td", [])
122 Meta.allow_tag_with_these_attributes("th", [])
123 Meta.allow_tag_with_these_attributes("thead", [])
124 Meta.allow_tag_with_these_attributes("tr", [])
127 @allow_headings Keyword.get(@markup, :allow_headings)
129 if @allow_headings do
130 Meta.allow_tag_with_these_attributes("h1", [])
131 Meta.allow_tag_with_these_attributes("h2", [])
132 Meta.allow_tag_with_these_attributes("h3", [])
133 Meta.allow_tag_with_these_attributes("h4", [])
134 Meta.allow_tag_with_these_attributes("h5", [])
137 @allow_fonts Keyword.get(@markup, :allow_fonts)
140 Meta.allow_tag_with_these_attributes("font", ["face"])
143 Meta.strip_everything_not_covered()
146 defmodule Pleroma.HTML.Transform.MediaProxy do
147 @moduledoc "Transforms inline image URIs to use MediaProxy."
149 alias Pleroma.Web.MediaProxy
151 def before_scrub(html), do: html
153 def scrub_attribute("img", {"src", "http" <> target}) do
161 def scrub_attribute(tag, attribute), do: attribute
163 def scrub({"img", attributes, children}) do
166 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
167 |> Enum.reject(&is_nil(&1))
169 {"img", attributes, children}
172 def scrub({tag, attributes, children}), do: {tag, attributes, children}
173 def scrub({tag, children}), do: children
174 def scrub(text), do: text