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