Fix merge conflict
[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 end
62
63 defmodule Pleroma.HTML.Scrubber.TwitterText do
64 @moduledoc """
65 An HTML scrubbing policy which limits to twitter-style text. Only
66 paragraphs, breaks and links are allowed through the filter.
67 """
68
69 @markup Application.get_env(:pleroma, :markup)
70 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
71 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
72
73 require HtmlSanitizeEx.Scrubber.Meta
74 alias HtmlSanitizeEx.Scrubber.Meta
75
76 Meta.remove_cdata_sections_before_scrub()
77 Meta.strip_comments()
78
79 # links
80 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
81 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
82
83 # paragraphs and linebreaks
84 Meta.allow_tag_with_these_attributes("br", [])
85 Meta.allow_tag_with_these_attributes("p", [])
86
87 # microformats
88 Meta.allow_tag_with_these_attributes("span", ["class"])
89
90 # allow inline images for custom emoji
91 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
92
93 if @allow_inline_images do
94 # restrict img tags to http/https only, because of MediaProxy.
95 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
96
97 Meta.allow_tag_with_these_attributes("img", [
98 "width",
99 "height",
100 "title",
101 "alt"
102 ])
103 end
104
105 Meta.strip_everything_not_covered()
106 end
107
108 defmodule Pleroma.HTML.Scrubber.Default do
109 @doc "The default HTML scrubbing policy: no "
110
111 require HtmlSanitizeEx.Scrubber.Meta
112 alias HtmlSanitizeEx.Scrubber.Meta
113
114 @markup Application.get_env(:pleroma, :markup)
115 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
116 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
117
118 Meta.remove_cdata_sections_before_scrub()
119 Meta.strip_comments()
120
121 Meta.allow_tag_with_uri_attributes("a", ["href", "data-user", "data-tag"], @valid_schemes)
122 Meta.allow_tag_with_these_attributes("a", ["name", "title", "class"])
123
124 Meta.allow_tag_with_these_attributes("abbr", ["title"])
125
126 Meta.allow_tag_with_these_attributes("b", [])
127 Meta.allow_tag_with_these_attributes("blockquote", [])
128 Meta.allow_tag_with_these_attributes("br", [])
129 Meta.allow_tag_with_these_attributes("code", [])
130 Meta.allow_tag_with_these_attributes("del", [])
131 Meta.allow_tag_with_these_attributes("em", [])
132 Meta.allow_tag_with_these_attributes("i", [])
133 Meta.allow_tag_with_these_attributes("li", [])
134 Meta.allow_tag_with_these_attributes("ol", [])
135 Meta.allow_tag_with_these_attributes("p", [])
136 Meta.allow_tag_with_these_attributes("pre", [])
137 Meta.allow_tag_with_these_attributes("span", ["class"])
138 Meta.allow_tag_with_these_attributes("strong", [])
139 Meta.allow_tag_with_these_attributes("u", [])
140 Meta.allow_tag_with_these_attributes("ul", [])
141
142 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
143
144 if @allow_inline_images do
145 # restrict img tags to http/https only, because of MediaProxy.
146 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
147
148 Meta.allow_tag_with_these_attributes("img", [
149 "width",
150 "height",
151 "title",
152 "alt"
153 ])
154 end
155
156 @allow_tables Keyword.get(@markup, :allow_tables)
157
158 if @allow_tables do
159 Meta.allow_tag_with_these_attributes("table", [])
160 Meta.allow_tag_with_these_attributes("tbody", [])
161 Meta.allow_tag_with_these_attributes("td", [])
162 Meta.allow_tag_with_these_attributes("th", [])
163 Meta.allow_tag_with_these_attributes("thead", [])
164 Meta.allow_tag_with_these_attributes("tr", [])
165 end
166
167 @allow_headings Keyword.get(@markup, :allow_headings)
168
169 if @allow_headings do
170 Meta.allow_tag_with_these_attributes("h1", [])
171 Meta.allow_tag_with_these_attributes("h2", [])
172 Meta.allow_tag_with_these_attributes("h3", [])
173 Meta.allow_tag_with_these_attributes("h4", [])
174 Meta.allow_tag_with_these_attributes("h5", [])
175 end
176
177 @allow_fonts Keyword.get(@markup, :allow_fonts)
178
179 if @allow_fonts do
180 Meta.allow_tag_with_these_attributes("font", ["face"])
181 end
182
183 Meta.strip_everything_not_covered()
184 end
185
186 defmodule Pleroma.HTML.Transform.MediaProxy do
187 @moduledoc "Transforms inline image URIs to use MediaProxy."
188
189 alias Pleroma.Web.MediaProxy
190
191 def before_scrub(html), do: html
192
193 def scrub_attribute("img", {"src", "http" <> target}) do
194 media_url =
195 ("http" <> target)
196 |> MediaProxy.url()
197
198 {"src", media_url}
199 end
200
201 def scrub_attribute(_tag, attribute), do: attribute
202
203 def scrub({"img", attributes, children}) do
204 attributes =
205 attributes
206 |> Enum.map(fn attr -> scrub_attribute("img", attr) end)
207 |> Enum.reject(&is_nil(&1))
208
209 {"img", attributes, children}
210 end
211
212 def scrub({:comment, _children}), do: ""
213
214 def scrub({tag, attributes, children}), do: {tag, attributes, children}
215 def scrub({_tag, children}), do: children
216 def scrub(text), do: text
217 end