Revert some changes in html.ex
[akkoma] / lib / pleroma / html.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTML do
6 alias HtmlSanitizeEx.Scrubber
7
8 defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
9 defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
10 defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
11
12 def get_scrubbers() do
13 Pleroma.Config.get([:markup, :scrub_policy])
14 |> get_scrubbers
15 end
16
17 def filter_tags(html, nil) do
18 get_scrubbers()
19 |> Enum.reduce(html, fn scrubber, html ->
20 filter_tags(html, scrubber)
21 end)
22 end
23
24 def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
25 def filter_tags(html), do: filter_tags(html, nil)
26 def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
27 end
28
29 defmodule Pleroma.HTML.Scrubber.TwitterText do
30 @moduledoc """
31 An HTML scrubbing policy which limits to twitter-style text. Only
32 paragraphs, breaks and links are allowed through the filter.
33 """
34
35 @markup Application.get_env(:pleroma, :markup)
36 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
37 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
38
39 require HtmlSanitizeEx.Scrubber.Meta
40 alias HtmlSanitizeEx.Scrubber.Meta
41
42 def version do 0 end
43 Meta.remove_cdata_sections_before_scrub()
44 Meta.strip_comments()
45
46 # links
47 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
48 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
49
50 # paragraphs and linebreaks
51 Meta.allow_tag_with_these_attributes("br", [])
52 Meta.allow_tag_with_these_attributes("p", [])
53
54 # microformats
55 Meta.allow_tag_with_these_attributes("span", [])
56
57 # allow inline images for custom emoji
58 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
59
60 if @allow_inline_images do
61 # restrict img tags to http/https only, because of MediaProxy.
62 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
63
64 Meta.allow_tag_with_these_attributes("img", [
65 "width",
66 "height",
67 "title",
68 "alt"
69 ])
70 end
71
72 Meta.strip_everything_not_covered()
73 end
74
75 defmodule Pleroma.HTML.Scrubber.Default do
76 @doc "The default HTML scrubbing policy: no "
77
78 require HtmlSanitizeEx.Scrubber.Meta
79 alias HtmlSanitizeEx.Scrubber.Meta
80 def version do 0 end
81 @markup Application.get_env(:pleroma, :markup)
82 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
83 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
84
85 Meta.remove_cdata_sections_before_scrub()
86 Meta.strip_comments()
87
88 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
89 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
90
91 Meta.allow_tag_with_these_attributes("abbr", ["title"])
92
93 Meta.allow_tag_with_these_attributes("b", [])
94 Meta.allow_tag_with_these_attributes("blockquote", [])
95 Meta.allow_tag_with_these_attributes("br", [])
96 Meta.allow_tag_with_these_attributes("code", [])
97 Meta.allow_tag_with_these_attributes("del", [])
98 Meta.allow_tag_with_these_attributes("em", [])
99 Meta.allow_tag_with_these_attributes("i", [])
100 Meta.allow_tag_with_these_attributes("li", [])
101 Meta.allow_tag_with_these_attributes("ol", [])
102 Meta.allow_tag_with_these_attributes("p", [])
103 Meta.allow_tag_with_these_attributes("pre", [])
104 Meta.allow_tag_with_these_attributes("span", [])
105 Meta.allow_tag_with_these_attributes("strong", [])
106 Meta.allow_tag_with_these_attributes("u", [])
107 Meta.allow_tag_with_these_attributes("ul", [])
108
109 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
110
111 if @allow_inline_images do
112 # restrict img tags to http/https only, because of MediaProxy.
113 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
114
115 Meta.allow_tag_with_these_attributes("img", [
116 "width",
117 "height",
118 "title",
119 "alt"
120 ])
121 end
122
123 @allow_tables Keyword.get(@markup, :allow_tables)
124
125 if @allow_tables do
126 Meta.allow_tag_with_these_attributes("table", [])
127 Meta.allow_tag_with_these_attributes("tbody", [])
128 Meta.allow_tag_with_these_attributes("td", [])
129 Meta.allow_tag_with_these_attributes("th", [])
130 Meta.allow_tag_with_these_attributes("thead", [])
131 Meta.allow_tag_with_these_attributes("tr", [])
132 end
133
134 @allow_headings Keyword.get(@markup, :allow_headings)
135
136 if @allow_headings do
137 Meta.allow_tag_with_these_attributes("h1", [])
138 Meta.allow_tag_with_these_attributes("h2", [])
139 Meta.allow_tag_with_these_attributes("h3", [])
140 Meta.allow_tag_with_these_attributes("h4", [])
141 Meta.allow_tag_with_these_attributes("h5", [])
142 end
143
144 @allow_fonts Keyword.get(@markup, :allow_fonts)
145
146 if @allow_fonts do
147 Meta.allow_tag_with_these_attributes("font", ["face"])
148 end
149
150 Meta.strip_everything_not_covered()
151 end
152
153 defmodule Pleroma.HTML.Transform.MediaProxy do
154 @moduledoc "Transforms inline image URIs to use MediaProxy."
155
156 alias Pleroma.Web.MediaProxy
157
158 def version do 0 end
159 def before_scrub(html), do: html
160
161 def scrub_attribute("img", {"src", "http" <> target}) do
162 media_url =
163 ("http" <> target)
164 |> MediaProxy.url()
165
166 {"src", media_url}
167 end
168
169 def scrub_attribute(_tag, attribute), do: attribute
170
171 def scrub({"img", attributes, children}) do
172 attributes =
173 attributes
174 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
175 |> Enum.reject(&is_nil(&1))
176
177 {"img", attributes, children}
178 end
179
180 def scrub({:comment, _children}), do: ""
181
182 def scrub({tag, attributes, children}), do: {tag, attributes, children}
183 def scrub({_tag, children}), do: children
184 def scrub(text), do: text
185 end