html: allow scrubbing policies to be stackable
[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, scrubber) do
16 html |> Scrubber.scrub(scrubber)
17 end
18
19 def filter_tags(html) do
20 get_scrubbers()
21 |> Enum.reduce(html, fn scrubber, html ->
22 filter_tags(html, scrubber)
23 end)
24 end
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 require HtmlSanitizeEx.Scrubber.Meta
38 alias HtmlSanitizeEx.Scrubber.Meta
39
40 @valid_schemes ["http", "https"]
41
42 Meta.remove_cdata_sections_before_scrub()
43 Meta.strip_comments()
44
45 # links
46 Meta.allow_tag_with_uri_attributes("a", ["href"], @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 @markup Application.get_env(:pleroma, :markup)
58 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
59
60 if @allow_inline_images do
61 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
62
63 Meta.allow_tag_with_these_attributes("img", [
64 "width",
65 "height",
66 "title",
67 "alt"
68 ])
69 end
70 end
71
72 defmodule Pleroma.HTML.Scrubber.Default do
73 @doc "The default HTML scrubbing policy: no "
74
75 require HtmlSanitizeEx.Scrubber.Meta
76 alias HtmlSanitizeEx.Scrubber.Meta
77
78 @valid_schemes ["http", "https"]
79
80 Meta.remove_cdata_sections_before_scrub()
81 Meta.strip_comments()
82
83 Meta.allow_tag_with_uri_attributes("a", ["href"], @valid_schemes)
84 Meta.allow_tag_with_these_attributes("a", ["name", "title"])
85
86 Meta.allow_tag_with_these_attributes("b", [])
87 Meta.allow_tag_with_these_attributes("blockquote", [])
88 Meta.allow_tag_with_these_attributes("br", [])
89 Meta.allow_tag_with_these_attributes("code", [])
90 Meta.allow_tag_with_these_attributes("del", [])
91 Meta.allow_tag_with_these_attributes("em", [])
92 Meta.allow_tag_with_these_attributes("i", [])
93 Meta.allow_tag_with_these_attributes("li", [])
94 Meta.allow_tag_with_these_attributes("ol", [])
95 Meta.allow_tag_with_these_attributes("p", [])
96 Meta.allow_tag_with_these_attributes("pre", [])
97 Meta.allow_tag_with_these_attributes("span", [])
98 Meta.allow_tag_with_these_attributes("strong", [])
99 Meta.allow_tag_with_these_attributes("u", [])
100 Meta.allow_tag_with_these_attributes("ul", [])
101
102 @markup Application.get_env(:pleroma, :markup)
103 @allow_inline_images Keyword.get(@markup, :allow_inline_images)
104
105 if @allow_inline_images do
106 Meta.allow_tag_with_uri_attributes("img", ["src"], @valid_schemes)
107
108 Meta.allow_tag_with_these_attributes("img", [
109 "width",
110 "height",
111 "title",
112 "alt"
113 ])
114 end
115
116 @allow_tables Keyword.get(@markup, :allow_tables)
117
118 if @allow_tables do
119 Meta.allow_tag_with_these_attributes("table", [])
120 Meta.allow_tag_with_these_attributes("tbody", [])
121 Meta.allow_tag_with_these_attributes("td", [])
122 Meta.allow_tag_with_these_attributes("th", [])
123 Meta.allow_tag_with_these_attributes("thead", [])
124 Meta.allow_tag_with_these_attributes("tr", [])
125 end
126
127 @allow_headings Keyword.get(@markup, :allow_headings)
128
129 if @allow_headings do
130 Meta.allow_tag_with_these_attributes("h1", [])
131 Meta.allow_tag_with_these_attributes("h2", [])
132 Meta.allow_tag_with_these_attributes("h3", [])
133 Meta.allow_tag_with_these_attributes("h4", [])
134 Meta.allow_tag_with_these_attributes("h5", [])
135 end
136
137 @allow_fonts Keyword.get(@markup, :allow_fonts)
138
139 if @allow_fonts do
140 Meta.allow_tag_with_these_attributes("font", ["face"])
141 end
142
143 Meta.strip_everything_not_covered()
144 end