formatting
[akkoma] / lib / pleroma / html.ex
1 defmodule Pleroma.HTML do
2 alias HtmlSanitizeEx.Scrubber
3
4 defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
5 defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
6 defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
7
8 def get_scrubbers() do
9 Pleroma.Config.get([:markup, :scrub_policy])
10 |> get_scrubbers
11 end
12
13 def filter_tags(html, nil) do
14 get_scrubbers()
15 |> Enum.reduce(html, fn scrubber, html ->
16 filter_tags(html, scrubber)
17 end)
18 end
19
20 def filter_tags(html, scrubber) do
21 html |> Scrubber.scrub(scrubber)
22 end
23
24 def filter_tags(html), do: filter_tags(html, nil)
25
26 def strip_tags(html) do
27 html |> Scrubber.scrub(Scrubber.StripTags)
28 end
29 end
30
31 defmodule Pleroma.HTML.Scrubber.TwitterText do
32 @moduledoc """
33 An HTML scrubbing policy which limits to twitter-style text. Only
34 paragraphs, breaks and links are allowed through the filter.
35 """
36
37 @markup Application.get_env(:pleroma, :markup)
38 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
39 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
40
41 require HtmlSanitizeEx.Scrubber.Meta
42 alias HtmlSanitizeEx.Scrubber.Meta
43
44 Meta.remove_cdata_sections_before_scrub()
45 Meta.strip_comments()
46
47 # links
48 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
49 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
50
51 # paragraphs and linebreaks
52 Meta.allow_tag_with_these_attributes("br", [])
53 Meta.allow_tag_with_these_attributes("p", [])
54
55 # microformats
56 Meta.allow_tag_with_these_attributes("span", [])
57
58 # allow inline images for custom emoji
59 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
60
61 if @allow_inline_images do
62 # restrict img tags to http/https only, because of MediaProxy.
63 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
64
65 Meta.allow_tag_with_these_attributes("img", [
66 "width",
67 "height",
68 "title",
69 "alt"
70 ])
71 end
72
73 Meta.strip_everything_not_covered()
74 end
75
76 defmodule Pleroma.HTML.Scrubber.Default do
77 @doc "The default HTML scrubbing policy: no "
78
79 require HtmlSanitizeEx.Scrubber.Meta
80 alias HtmlSanitizeEx.Scrubber.Meta
81
82 @markup Application.get_env(:pleroma, :markup)
83 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
84 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
85
86 Meta.remove_cdata_sections_before_scrub()
87 Meta.strip_comments()
88
89 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
90 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
91
92 Meta.allow_tag_with_these_attributes("abbr", ["title"])
93
94 Meta.allow_tag_with_these_attributes("b", [])
95 Meta.allow_tag_with_these_attributes("blockquote", [])
96 Meta.allow_tag_with_these_attributes("br", [])
97 Meta.allow_tag_with_these_attributes("code", [])
98 Meta.allow_tag_with_these_attributes("del", [])
99 Meta.allow_tag_with_these_attributes("em", [])
100 Meta.allow_tag_with_these_attributes("i", [])
101 Meta.allow_tag_with_these_attributes("li", [])
102 Meta.allow_tag_with_these_attributes("ol", [])
103 Meta.allow_tag_with_these_attributes("p", [])
104 Meta.allow_tag_with_these_attributes("pre", [])
105 Meta.allow_tag_with_these_attributes("span", [])
106 Meta.allow_tag_with_these_attributes("strong", [])
107 Meta.allow_tag_with_these_attributes("u", [])
108 Meta.allow_tag_with_these_attributes("ul", [])
109
110 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
111
112 if @allow_inline_images do
113 # restrict img tags to http/https only, because of MediaProxy.
114 Meta.allow_tag_with_uri_attributes("img", ["src"], ["http", "https"])
115
116 Meta.allow_tag_with_these_attributes("img", [
117 "width",
118 "height",
119 "title",
120 "alt"
121 ])
122 end
123
124 @allow_tables Keyword.get(@markup, :allow_tables)
125
126 if @allow_tables do
127 Meta.allow_tag_with_these_attributes("table", [])
128 Meta.allow_tag_with_these_attributes("tbody", [])
129 Meta.allow_tag_with_these_attributes("td", [])
130 Meta.allow_tag_with_these_attributes("th", [])
131 Meta.allow_tag_with_these_attributes("thead", [])
132 Meta.allow_tag_with_these_attributes("tr", [])
133 end
134
135 @allow_headings Keyword.get(@markup, :allow_headings)
136
137 if @allow_headings do
138 Meta.allow_tag_with_these_attributes("h1", [])
139 Meta.allow_tag_with_these_attributes("h2", [])
140 Meta.allow_tag_with_these_attributes("h3", [])
141 Meta.allow_tag_with_these_attributes("h4", [])
142 Meta.allow_tag_with_these_attributes("h5", [])
143 end
144
145 @allow_fonts Keyword.get(@markup, :allow_fonts)
146
147 if @allow_fonts do
148 Meta.allow_tag_with_these_attributes("font", ["face"])
149 end
150
151 Meta.strip_everything_not_covered()
152 end
153
154 defmodule Pleroma.HTML.Transform.MediaProxy do
155 @moduledoc "Transforms inline image URIs to use MediaProxy."
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