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