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
42 @valid_schemes ["http", "https"]
44 Meta.remove_cdata_sections_before_scrub()
48 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
49 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
51 # paragraphs and linebreaks
52 Meta.allow_tag_with_these_attributes("br", [])
53 Meta.allow_tag_with_these_attributes("p", [])
56 Meta.allow_tag_with_these_attributes("span", [])
58 # allow inline images for custom emoji
59 @markup Application.get_env(:pleroma, :markup)
60 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
62 if @allow_inline_images do
63 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
65 Meta.allow_tag_with_these_attributes("img", [
74 defmodule Pleroma.HTML.Scrubber.Default do
75 @doc "The default HTML scrubbing policy: no "
77 require HtmlSanitizeEx.Scrubber.Meta
78 alias HtmlSanitizeEx.Scrubber.Meta
80 @valid_schemes ["http", "https"]
82 Meta.remove_cdata_sections_before_scrub()
85 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
86 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
88 Meta.allow_tag_with_these_attributes("b", [])
89 Meta.allow_tag_with_these_attributes("blockquote", [])
90 Meta.allow_tag_with_these_attributes("br", [])
91 Meta.allow_tag_with_these_attributes("code", [])
92 Meta.allow_tag_with_these_attributes("del", [])
93 Meta.allow_tag_with_these_attributes("em", [])
94 Meta.allow_tag_with_these_attributes("i", [])
95 Meta.allow_tag_with_these_attributes("li", [])
96 Meta.allow_tag_with_these_attributes("ol", [])
97 Meta.allow_tag_with_these_attributes("p", [])
98 Meta.allow_tag_with_these_attributes("pre", [])
99 Meta.allow_tag_with_these_attributes("span", [])
100 Meta.allow_tag_with_these_attributes("strong", [])
101 Meta.allow_tag_with_these_attributes("u", [])
102 Meta.allow_tag_with_these_attributes("ul", [])
104 @markup Application.get_env(:pleroma, :markup)
105 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
107 if @allow_inline_images do
108 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
110 Meta.allow_tag_with_these_attributes("img", [
118 @allow_tables Keyword.get(@markup, :allow_tables)
121 Meta.allow_tag_with_these_attributes("table", [])
122 Meta.allow_tag_with_these_attributes("tbody", [])
123 Meta.allow_tag_with_these_attributes("td", [])
124 Meta.allow_tag_with_these_attributes("th", [])
125 Meta.allow_tag_with_these_attributes("thead", [])
126 Meta.allow_tag_with_these_attributes("tr", [])
129 @allow_headings Keyword.get(@markup, :allow_headings)
131 if @allow_headings do
132 Meta.allow_tag_with_these_attributes("h1", [])
133 Meta.allow_tag_with_these_attributes("h2", [])
134 Meta.allow_tag_with_these_attributes("h3", [])
135 Meta.allow_tag_with_these_attributes("h4", [])
136 Meta.allow_tag_with_these_attributes("h5", [])
139 @allow_fonts Keyword.get(@markup, :allow_fonts)
142 Meta.allow_tag_with_these_attributes("font", ["face"])
145 Meta.strip_everything_not_covered()
148 defmodule Pleroma.HTML.Transform.MediaProxy do
149 @moduledoc "Transforms inline image URIs to use MediaProxy."
151 alias Pleroma.Web.MediaProxy
153 def before_scrub(html), do: html
155 def scrub_attribute("img", {"src", "http" <> target}) do
163 def scrub_attribute(tag, attribute), do: attribute
165 def scrub({"img", attributes, children}) do
168 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
169 |> Enum.reject(&is_nil(&1))
171 {"img", attributes, children}
174 def scrub({tag, attributes, children}), do: {tag, attributes, children}
175 def scrub({tag, children}), do: children
176 def scrub(text), do: text