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