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