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