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