1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTML do
6 # Scrubbers are compiled on boot so they can be configured in OTP releases
7 # @on_load :compile_scrubbers
9 def compile_scrubbers do
10 dir = Path.join(:code.priv_dir(:pleroma), "scrubbers")
13 |> Pleroma.Utils.compile_dir()
15 {:error, _errors, _warnings} ->
16 raise "Compiling scrubbers failed"
18 {:ok, _modules, _warnings} ->
23 defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
24 defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
25 defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
28 Pleroma.Config.get([:markup, :scrub_policy])
32 def filter_tags(html, nil) do
33 filter_tags(html, get_scrubbers())
36 def filter_tags(html, scrubbers) when is_list(scrubbers) do
37 Enum.reduce(scrubbers, html, fn scrubber, html ->
38 filter_tags(html, scrubber)
42 def filter_tags(html, scrubber) do
43 {:ok, content} = FastSanitize.Sanitizer.scrub(html, scrubber)
47 def filter_tags(html), do: filter_tags(html, nil)
48 def strip_tags(html), do: filter_tags(html, FastSanitize.Sanitizer.StripTags)
50 def ensure_scrubbed_html(
58 |> filter_tags(scrubbers)
68 @spec extract_first_external_url_from_object(Pleroma.Object.t()) :: String.t() | nil
69 def extract_first_external_url_from_object(%{data: %{"content" => content}})
70 when is_binary(content) do
72 |> Floki.parse_fragment!()
73 |> Floki.find("a:not(.mention,.hashtag,.attachment,[rel~=\"tag\"])")
75 |> Floki.attribute("href")
79 def extract_first_external_url_from_object(_), do: nil