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