2019-01-24 09:35:19 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 11:11:47 +00:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-01-24 09:35:19 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
|
|
|
|
alias Pleroma.User
|
|
|
|
|
2019-05-06 02:28:04 +00:00
|
|
|
@moduledoc "Prevent followbots from following with a bit of heuristic"
|
|
|
|
|
2021-06-07 19:22:08 +00:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
2019-01-24 09:35:19 +00:00
|
|
|
|
|
|
|
# XXX: this should become User.normalize_by_ap_id() or similar, really.
|
|
|
|
defp normalize_by_ap_id(%{"id" => id}), do: User.get_cached_by_ap_id(id)
|
|
|
|
defp normalize_by_ap_id(uri) when is_binary(uri), do: User.get_cached_by_ap_id(uri)
|
|
|
|
defp normalize_by_ap_id(_), do: nil
|
|
|
|
|
|
|
|
defp score_nickname("followbot@" <> _), do: 1.0
|
|
|
|
defp score_nickname("federationbot@" <> _), do: 1.0
|
|
|
|
defp score_nickname("federation_bot@" <> _), do: 1.0
|
|
|
|
defp score_nickname(_), do: 0.0
|
|
|
|
|
|
|
|
defp score_displayname("federation bot"), do: 1.0
|
|
|
|
defp score_displayname("federationbot"), do: 1.0
|
|
|
|
defp score_displayname("fedibot"), do: 1.0
|
|
|
|
defp score_displayname(_), do: 0.0
|
|
|
|
|
2022-05-08 18:10:40 +00:00
|
|
|
defp determine_if_followbot(%User{nickname: nickname, name: displayname, actor_type: actor_type}) do
|
2019-08-30 00:38:03 +00:00
|
|
|
# nickname will be a binary string except when following a relay
|
2019-01-24 09:35:19 +00:00
|
|
|
nick_score =
|
2019-08-30 00:38:03 +00:00
|
|
|
if is_binary(nickname) do
|
|
|
|
nickname
|
|
|
|
|> String.downcase()
|
|
|
|
|> score_nickname()
|
|
|
|
else
|
|
|
|
0.0
|
|
|
|
end
|
2019-01-24 09:35:19 +00:00
|
|
|
|
2019-03-07 12:13:48 +00:00
|
|
|
# displayname will either be a binary string or nil, if a displayname isn't set.
|
2019-01-24 09:35:19 +00:00
|
|
|
name_score =
|
2019-03-07 12:13:48 +00:00
|
|
|
if is_binary(displayname) do
|
|
|
|
displayname
|
|
|
|
|> String.downcase()
|
|
|
|
|> score_displayname()
|
|
|
|
else
|
|
|
|
0.0
|
|
|
|
end
|
2019-01-24 09:35:19 +00:00
|
|
|
|
2022-05-08 18:10:40 +00:00
|
|
|
# actor_type "Service" is a Bot account
|
|
|
|
actor_type_score =
|
|
|
|
if actor_type == "Service" do
|
|
|
|
1.0
|
|
|
|
else
|
|
|
|
0.0
|
|
|
|
end
|
|
|
|
|
|
|
|
nick_score + name_score + actor_type_score
|
2019-01-24 09:35:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp determine_if_followbot(_), do: 0.0
|
|
|
|
|
2022-05-08 18:10:40 +00:00
|
|
|
defp bot_allowed?(%{"object" => target}, bot_actor) do
|
|
|
|
%User{} = user = normalize_by_ap_id(target)
|
|
|
|
|
|
|
|
User.following?(user, bot_actor)
|
|
|
|
end
|
|
|
|
|
2019-01-24 09:35:19 +00:00
|
|
|
@impl true
|
|
|
|
def filter(%{"type" => "Follow", "actor" => actor_id} = message) do
|
|
|
|
%User{} = actor = normalize_by_ap_id(actor_id)
|
|
|
|
|
|
|
|
score = determine_if_followbot(actor)
|
|
|
|
|
2022-05-08 18:10:40 +00:00
|
|
|
if score < 0.8 || bot_allowed?(message, actor) do
|
2019-01-24 09:35:19 +00:00
|
|
|
{:ok, message}
|
|
|
|
else
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
|
2019-01-24 09:35:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(message), do: {:ok, message}
|
2019-08-13 21:52:54 +00:00
|
|
|
|
|
|
|
@impl true
|
2019-08-13 22:36:24 +00:00
|
|
|
def describe, do: {:ok, %{}}
|
2019-01-24 09:35:19 +00:00
|
|
|
end
|