1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTML do
6 alias HtmlSanitizeEx.Scrubber
8 def filter_tags(html, scrubbers) when is_list(scrubbers) do
9 Enum.reduce(scrubbers, html, fn scrubber, html ->
10 filter_tags(html, scrubber)
14 def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
15 def filter_tags(html), do: filter_tags(html, nil)
16 def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
19 defmodule Pleroma.HTML.Scrubber.TwitterText do
21 An HTML scrubbing policy which limits to twitter-style text. Only
22 paragraphs, breaks and links are allowed through the filter.
25 @markup Application.get_env(:pleroma, :markup)
26 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
27 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
29 require HtmlSanitizeEx.Scrubber.Meta
30 alias HtmlSanitizeEx.Scrubber.Meta
36 Meta.remove_cdata_sections_before_scrub()
40 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
41 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
43 # paragraphs and linebreaks
44 Meta.allow_tag_with_these_attributes("br", [])
45 Meta.allow_tag_with_these_attributes("p", [])
48 Meta.allow_tag_with_these_attributes("span", [])
50 # allow inline images for custom emoji
51 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
53 if @allow_inline_images do
54 # restrict img tags to http/https only, because of MediaProxy.
55 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
57 Meta.allow_tag_with_these_attributes("img", [
65 Meta.strip_everything_not_covered()
68 defmodule Pleroma.HTML.Scrubber.Default do
69 @doc "The default HTML scrubbing policy: no "
71 require HtmlSanitizeEx.Scrubber.Meta
72 alias HtmlSanitizeEx.Scrubber.Meta
78 @markup Application.get_env(:pleroma, :markup)
79 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
80 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
82 Meta.remove_cdata_sections_before_scrub()
85 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
86 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
88 Meta.allow_tag_with_these_attributes("abbr", ["title"])
90 Meta.allow_tag_with_these_attributes("b", [])
91 Meta.allow_tag_with_these_attributes("blockquote", [])
92 Meta.allow_tag_with_these_attributes("br", [])
93 Meta.allow_tag_with_these_attributes("code", [])
94 Meta.allow_tag_with_these_attributes("del", [])
95 Meta.allow_tag_with_these_attributes("em", [])
96 Meta.allow_tag_with_these_attributes("i", [])
97 Meta.allow_tag_with_these_attributes("li", [])
98 Meta.allow_tag_with_these_attributes("ol", [])
99 Meta.allow_tag_with_these_attributes("p", [])
100 Meta.allow_tag_with_these_attributes("pre", [])
101 Meta.allow_tag_with_these_attributes("span", [])
102 Meta.allow_tag_with_these_attributes("strong", [])
103 Meta.allow_tag_with_these_attributes("u", [])
104 Meta.allow_tag_with_these_attributes("ul", [])
106 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
108 if @allow_inline_images do
109 # restrict img tags to http/https only, because of MediaProxy.
110 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
112 Meta.allow_tag_with_these_attributes("img", [
120 @allow_tables Keyword.get(@markup, :allow_tables)
123 Meta.allow_tag_with_these_attributes("table", [])
124 Meta.allow_tag_with_these_attributes("tbody", [])
125 Meta.allow_tag_with_these_attributes("td", [])
126 Meta.allow_tag_with_these_attributes("th", [])
127 Meta.allow_tag_with_these_attributes("thead", [])
128 Meta.allow_tag_with_these_attributes("tr", [])
131 @allow_headings Keyword.get(@markup, :allow_headings)
133 if @allow_headings do
134 Meta.allow_tag_with_these_attributes("h1", [])
135 Meta.allow_tag_with_these_attributes("h2", [])
136 Meta.allow_tag_with_these_attributes("h3", [])
137 Meta.allow_tag_with_these_attributes("h4", [])
138 Meta.allow_tag_with_these_attributes("h5", [])
141 @allow_fonts Keyword.get(@markup, :allow_fonts)
144 Meta.allow_tag_with_these_attributes("font", ["face"])
147 Meta.strip_everything_not_covered()
150 defmodule Pleroma.HTML.Transform.MediaProxy do
151 @moduledoc "Transforms inline image URIs to use MediaProxy."
157 alias Pleroma.Web.MediaProxy
159 def before_scrub(html), do: html
161 def scrub_attribute("img", {"src", "http" <> target}) do
169 def scrub_attribute(_tag, attribute), do: attribute
171 def scrub({"img", attributes, children}) do
174 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
175 |> Enum.reject(&is_nil(&1))
177 {"img", attributes, children}
180 def scrub({:comment, _children}), do: ""
182 def scrub({tag, attributes, children}), do: {tag, attributes, children}
183 def scrub({_tag, children}), do: children
184 def scrub(text), do: text