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