0fd41ffb862630f3447ad933f5a6acb2990c48be
[akkoma] / lib / pleroma / html.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 filter_tags(html, get_scrubbers())
19 end
20
21 def filter_tags(html, scrubbers) when is_list(scrubbers) do
22 Enum.reduce(scrubbers, html, fn scrubber, html ->
23 filter_tags(html, scrubber)
24 end)
25 end
26
27 def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
28 def filter_tags(html), do: filter_tags(html, nil)
29 def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
30
31 def get_cached_scrubbed_html_for_object(content, scrubbers, object, module) do
32 key = "#{module}#{generate_scrubber_signature(scrubbers)}|#{object.id}"
33 Cachex.fetch!(:scrubber_cache, key, fn _key -> ensure_scrubbed_html(content, scrubbers) end)
34 end
35
36 def get_cached_stripped_html_for_object(content, object, module) do
37 get_cached_scrubbed_html_for_object(
38 content,
39 HtmlSanitizeEx.Scrubber.StripTags,
40 object,
41 module
42 )
43 end
44
45 def ensure_scrubbed_html(
46 content,
47 scrubbers
48 ) do
49 {:commit, filter_tags(content, scrubbers)}
50 end
51
52 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
53 generate_scrubber_signature([scrubber])
54 end
55
56 defp generate_scrubber_signature(scrubbers) do
57 Enum.reduce(scrubbers, "", fn scrubber, signature ->
58 "#{signature}#{to_string(scrubber)}"
59 end)
60 end
61
62 def extract_first_external_url(_, nil), do: {:error, "No content"}
63
64 def extract_first_external_url(object, content) do
65 key = "URL|#{object.id}"
66
67 Cachex.fetch!(:scrubber_cache, key, fn _key ->
68 result =
69 content
70 |> Floki.filter_out("a.mention")
71 |> Floki.attribute("a", "href")
72 |> Enum.at(0)
73
74 {:commit, {:ok, result}}
75 end)
76 end
77 end
78
79 defmodule Pleroma.HTML.Scrubber.TwitterText do
80 @moduledoc """
81 An HTML scrubbing policy which limits to twitter-style text. Only
82 paragraphs, breaks and links are allowed through the filter.
83 """
84
85 @markup Application.get_env(:pleroma, :markup)
86 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
87 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
88
89 require HtmlSanitizeEx.Scrubber.Meta
90 alias HtmlSanitizeEx.Scrubber.Meta
91
92 Meta.remove_cdata_sections_before_scrub()
93 Meta.strip_comments()
94
95 # links
96 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
97 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
98
99 # paragraphs and linebreaks
100 Meta.allow_tag_with_these_attributes("br", [])
101 Meta.allow_tag_with_these_attributes("p", [])
102
103 # microformats
104 Meta.allow_tag_with_these_attributes("span", ["class"])
105
106 # allow inline images for custom emoji
107 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
108
109 if @allow_inline_images do
110 # restrict img tags to http/https only, because of MediaProxy.
111 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
112
113 Meta.allow_tag_with_these_attributes("img", [
114 "width",
115 "height",
116 "title",
117 "alt"
118 ])
119 end
120
121 Meta.strip_everything_not_covered()
122 end
123
124 defmodule Pleroma.HTML.Scrubber.Default do
125 @doc "The default HTML scrubbing policy: no "
126
127 require HtmlSanitizeEx.Scrubber.Meta
128
129 alias HtmlSanitizeEx.Scrubber.Meta
130
131 @markup Application.get_env(:pleroma, :markup)
132 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
133 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
134
135 Meta.remove_cdata_sections_before_scrub()
136 Meta.strip_comments()
137
138 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
139 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
140
141 Meta.allow_tag_with_these_attributes("abbr", ["title"])
142
143 Meta.allow_tag_with_these_attributes("b", [])
144 Meta.allow_tag_with_these_attributes("blockquote", [])
145 Meta.allow_tag_with_these_attributes("br", [])
146 Meta.allow_tag_with_these_attributes("code", [])
147 Meta.allow_tag_with_these_attributes("del", [])
148 Meta.allow_tag_with_these_attributes("em", [])
149 Meta.allow_tag_with_these_attributes("i", [])
150 Meta.allow_tag_with_these_attributes("li", [])
151 Meta.allow_tag_with_these_attributes("ol", [])
152 Meta.allow_tag_with_these_attributes("p", [])
153 Meta.allow_tag_with_these_attributes("pre", [])
154 Meta.allow_tag_with_these_attributes("span", ["class"])
155 Meta.allow_tag_with_these_attributes("strong", [])
156 Meta.allow_tag_with_these_attributes("u", [])
157 Meta.allow_tag_with_these_attributes("ul", [])
158
159 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
160
161 if @allow_inline_images do
162 # restrict img tags to http/https only, because of MediaProxy.
163 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
164
165 Meta.allow_tag_with_these_attributes("img", [
166 "width",
167 "height",
168 "title",
169 "alt"
170 ])
171 end
172
173 @allow_tables Keyword.get(@markup, :allow_tables)
174
175 if @allow_tables do
176 Meta.allow_tag_with_these_attributes("table", [])
177 Meta.allow_tag_with_these_attributes("tbody", [])
178 Meta.allow_tag_with_these_attributes("td", [])
179 Meta.allow_tag_with_these_attributes("th", [])
180 Meta.allow_tag_with_these_attributes("thead", [])
181 Meta.allow_tag_with_these_attributes("tr", [])
182 end
183
184 @allow_headings Keyword.get(@markup, :allow_headings)
185
186 if @allow_headings do
187 Meta.allow_tag_with_these_attributes("h1", [])
188 Meta.allow_tag_with_these_attributes("h2", [])
189 Meta.allow_tag_with_these_attributes("h3", [])
190 Meta.allow_tag_with_these_attributes("h4", [])
191 Meta.allow_tag_with_these_attributes("h5", [])
192 end
193
194 @allow_fonts Keyword.get(@markup, :allow_fonts)
195
196 if @allow_fonts do
197 Meta.allow_tag_with_these_attributes("font", ["face"])
198 end
199
200 Meta.strip_everything_not_covered()
201 end
202
203 defmodule Pleroma.HTML.Transform.MediaProxy do
204 @moduledoc "Transforms inline image URIs to use MediaProxy."
205
206 alias Pleroma.Web.MediaProxy
207
208 def before_scrub(html), do: html
209
210 def scrub_attribute("img", {"src", "http" <> target}) do
211 media_url =
212 ("http" <> target)
213 |> MediaProxy.url()
214
215 {"src", media_url}
216 end
217
218 def scrub_attribute(_tag, attribute), do: attribute
219
220 def scrub({"img", attributes, children}) do
221 attributes =
222 attributes
223 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
224 |> Enum.reject(&is_nil(&1))
225
226 {"img", attributes, children}
227 end
228
229 def scrub({:comment, _children}), do: ""
230
231 def scrub({tag, attributes, children}), do: {tag, attributes, children}
232 def scrub({_tag, children}), do: children
233 def scrub(text), do: text
234 end