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 require HtmlSanitizeEx.Scrubber.Meta
40 alias HtmlSanitizeEx.Scrubber.Meta
59 Meta.remove_cdata_sections_before_scrub()
63 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
64 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
66 # paragraphs and linebreaks
67 Meta.allow_tag_with_these_attributes("br", [])
68 Meta.allow_tag_with_these_attributes("p", [])
71 Meta.allow_tag_with_these_attributes("span", [])
73 # allow inline images for custom emoji
74 @markup Application.get_env(:pleroma, :markup)
75 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
77 if @allow_inline_images do
78 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
80 Meta.allow_tag_with_these_attributes("img", [
88 Meta.strip_everything_not_covered()
91 defmodule Pleroma.HTML.Scrubber.Default do
92 @doc "The default HTML scrubbing policy: no "
94 require HtmlSanitizeEx.Scrubber.Meta
95 alias HtmlSanitizeEx.Scrubber.Meta
97 @valid_schemes ["http", "https"]
99 Meta.remove_cdata_sections_before_scrub()
100 Meta.strip_comments()
102 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
103 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
105 Meta.allow_tag_with_these_attributes("b", [])
106 Meta.allow_tag_with_these_attributes("blockquote", [])
107 Meta.allow_tag_with_these_attributes("br", [])
108 Meta.allow_tag_with_these_attributes("code", [])
109 Meta.allow_tag_with_these_attributes("del", [])
110 Meta.allow_tag_with_these_attributes("em", [])
111 Meta.allow_tag_with_these_attributes("i", [])
112 Meta.allow_tag_with_these_attributes("li", [])
113 Meta.allow_tag_with_these_attributes("ol", [])
114 Meta.allow_tag_with_these_attributes("p", [])
115 Meta.allow_tag_with_these_attributes("pre", [])
116 Meta.allow_tag_with_these_attributes("span", [])
117 Meta.allow_tag_with_these_attributes("strong", [])
118 Meta.allow_tag_with_these_attributes("u", [])
119 Meta.allow_tag_with_these_attributes("ul", [])
121 @markup Application.get_env(:pleroma, :markup)
122 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
124 if @allow_inline_images do
125 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
127 Meta.allow_tag_with_these_attributes("img", [
135 @allow_tables Keyword.get(@markup, :allow_tables)
138 Meta.allow_tag_with_these_attributes("table", [])
139 Meta.allow_tag_with_these_attributes("tbody", [])
140 Meta.allow_tag_with_these_attributes("td", [])
141 Meta.allow_tag_with_these_attributes("th", [])
142 Meta.allow_tag_with_these_attributes("thead", [])
143 Meta.allow_tag_with_these_attributes("tr", [])
146 @allow_headings Keyword.get(@markup, :allow_headings)
148 if @allow_headings do
149 Meta.allow_tag_with_these_attributes("h1", [])
150 Meta.allow_tag_with_these_attributes("h2", [])
151 Meta.allow_tag_with_these_attributes("h3", [])
152 Meta.allow_tag_with_these_attributes("h4", [])
153 Meta.allow_tag_with_these_attributes("h5", [])
156 @allow_fonts Keyword.get(@markup, :allow_fonts)
159 Meta.allow_tag_with_these_attributes("font", ["face"])
162 Meta.strip_everything_not_covered()
165 defmodule Pleroma.HTML.Transform.MediaProxy do
166 @moduledoc "Transforms inline image URIs to use MediaProxy."
168 alias Pleroma.Web.MediaProxy
170 def before_scrub(html), do: html
172 def scrub_attribute("img", {"src", "http" <> target}) do
180 def scrub_attribute(tag, attribute), do: attribute
182 def scrub({"img", attributes, children}) do
185 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
186 |> Enum.reject(&is_nil(&1))
188 {"img", attributes, children}
191 def scrub({tag, attributes, children}), do: {tag, attributes, children}
192 def scrub({tag, children}), do: children
193 def scrub(text), do: text