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