2020-10-12 11:00:50 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-10-12 11:00:50 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-07-22 08:33:58 -06:00
|
|
|
defmodule Pleroma.Web.ActivityPub.MRFTest do
|
|
|
|
use ExUnit.Case, async: true
|
2019-08-19 09:34:29 -06:00
|
|
|
use Pleroma.Tests.Helpers
|
2019-07-22 08:33:58 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.MRF
|
|
|
|
|
|
|
|
test "subdomains_regex/1" do
|
|
|
|
assert MRF.subdomains_regex(["unsafe.tld", "*.unsafe.tld"]) == [
|
2022-11-06 16:50:32 -07:00
|
|
|
~r/^(.+\.)?unsafe\.tld$/i,
|
2022-11-07 15:33:18 -07:00
|
|
|
~r/^(.+\.)?unsafe\.tld$/i
|
2019-07-22 08:33:58 -06:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subdomain_match/2" do
|
|
|
|
test "common domains" do
|
|
|
|
regexes = MRF.subdomains_regex(["unsafe.tld", "unsafe2.tld"])
|
|
|
|
|
2022-11-06 16:50:32 -07:00
|
|
|
assert regexes == [~r/^(.+\.)?unsafe\.tld$/i, ~r/^(.+\.)?unsafe2\.tld$/i]
|
2019-07-22 08:33:58 -06:00
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe.tld")
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe2.tld")
|
|
|
|
|
|
|
|
refute MRF.subdomain_match?(regexes, "example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "wildcard domains with one subdomain" do
|
2022-11-06 16:50:32 -07:00
|
|
|
regexes = MRF.subdomains_regex(["unsafe.tld"])
|
2019-07-22 08:33:58 -06:00
|
|
|
|
2022-11-06 16:50:32 -07:00
|
|
|
assert regexes == [~r/^(.+\.)?unsafe\.tld$/i]
|
2019-07-22 08:33:58 -06:00
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe.tld")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.unsafe.tld")
|
|
|
|
refute MRF.subdomain_match?(regexes, "anotherunsafe.tld")
|
|
|
|
refute MRF.subdomain_match?(regexes, "unsafe.tldanother")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "wildcard domains with two subdomains" do
|
2022-11-06 16:50:32 -07:00
|
|
|
regexes = MRF.subdomains_regex(["unsafe.tld"])
|
2019-07-22 08:33:58 -06:00
|
|
|
|
2022-11-06 16:50:32 -07:00
|
|
|
assert regexes == [~r/^(.+\.)?unsafe\.tld$/i]
|
2019-07-22 08:33:58 -06:00
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe.tld")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.sub.unsafe.tld")
|
|
|
|
refute MRF.subdomain_match?(regexes, "sub.anotherunsafe.tld")
|
|
|
|
refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother")
|
|
|
|
end
|
2019-08-10 15:18:26 -06:00
|
|
|
|
2023-01-15 11:57:49 -07:00
|
|
|
test "wildcard on the tld" do
|
|
|
|
regexes = MRF.subdomains_regex(["somewhere.*"])
|
|
|
|
|
|
|
|
assert regexes == [~r/^(.+\.)?somewhere\.(.+)$/i]
|
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "somewhere.net")
|
|
|
|
assert MRF.subdomain_match?(regexes, "somewhere.com")
|
|
|
|
assert MRF.subdomain_match?(regexes, "somewhere.somewherelese.net")
|
|
|
|
refute MRF.subdomain_match?(regexes, "somewhere")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "wildcards on subdomain _and_ tld" do
|
|
|
|
regexes = MRF.subdomains_regex(["*.somewhere.*"])
|
|
|
|
|
|
|
|
assert regexes == [~r/^(.+\.)?somewhere\.(.+)$/i]
|
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "somewhere.net")
|
|
|
|
assert MRF.subdomain_match?(regexes, "somewhere.com")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.somewhere.net")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.somewhere.com")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.sub.somewhere.net")
|
|
|
|
assert MRF.subdomain_match?(regexes, "sub.sub.somewhere.com")
|
|
|
|
refute MRF.subdomain_match?(regexes, "somewhere")
|
|
|
|
end
|
|
|
|
|
2019-08-10 15:18:26 -06:00
|
|
|
test "matches are case-insensitive" do
|
|
|
|
regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
|
|
|
|
|
2022-11-06 16:50:32 -07:00
|
|
|
assert regexes == [~r/^(.+\.)?UnSafe\.TLD$/i, ~r/^(.+\.)?UnSAFE2\.Tld$/i]
|
2019-08-10 15:18:26 -06:00
|
|
|
|
|
|
|
assert MRF.subdomain_match?(regexes, "UNSAFE.TLD")
|
|
|
|
assert MRF.subdomain_match?(regexes, "UNSAFE2.TLD")
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe.tld")
|
|
|
|
assert MRF.subdomain_match?(regexes, "unsafe2.tld")
|
|
|
|
|
|
|
|
refute MRF.subdomain_match?(regexes, "EXAMPLE.COM")
|
|
|
|
refute MRF.subdomain_match?(regexes, "example.com")
|
|
|
|
end
|
2019-07-22 08:33:58 -06:00
|
|
|
end
|
2019-08-13 16:19:15 -06:00
|
|
|
|
2020-10-02 06:51:39 -06:00
|
|
|
describe "instance_list_from_tuples/1" do
|
|
|
|
test "returns a list of instances from a list of {instance, reason} tuples" do
|
|
|
|
list = [{"some.tld", "a reason"}, {"other.tld", "another reason"}]
|
|
|
|
expected = ["some.tld", "other.tld"]
|
|
|
|
|
|
|
|
assert MRF.instance_list_from_tuples(list) == expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-13 16:19:15 -06:00
|
|
|
describe "describe/0" do
|
|
|
|
test "it works as expected with noop policy" do
|
2020-09-25 11:46:49 -06:00
|
|
|
clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.NoOpPolicy])
|
|
|
|
|
2019-08-13 16:19:15 -06:00
|
|
|
expected = %{
|
2023-05-23 06:16:20 -06:00
|
|
|
mrf_policies: [
|
|
|
|
"NoOpPolicy",
|
|
|
|
"HashtagPolicy",
|
|
|
|
"InlineQuotePolicy",
|
|
|
|
"NormalizeMarkup",
|
|
|
|
"DirectMessageDisabledPolicy"
|
|
|
|
],
|
2020-12-28 15:21:53 -07:00
|
|
|
mrf_hashtag: %{
|
|
|
|
federated_timeline_removal: [],
|
|
|
|
reject: [],
|
|
|
|
sensitive: ["nsfw"]
|
|
|
|
},
|
2019-08-13 16:19:15 -06:00
|
|
|
exclusions: false
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, ^expected} = MRF.describe()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works as expected with mock policy" do
|
2020-03-21 00:47:05 -06:00
|
|
|
clear_config([:mrf, :policies], [MRFModuleMock])
|
2019-08-13 16:19:15 -06:00
|
|
|
|
|
|
|
expected = %{
|
2023-05-23 06:16:20 -06:00
|
|
|
mrf_policies: [
|
|
|
|
"MRFModuleMock",
|
|
|
|
"HashtagPolicy",
|
|
|
|
"InlineQuotePolicy",
|
|
|
|
"NormalizeMarkup",
|
|
|
|
"DirectMessageDisabledPolicy"
|
|
|
|
],
|
2019-08-13 16:19:15 -06:00
|
|
|
mrf_module_mock: "some config data",
|
2020-12-28 15:21:53 -07:00
|
|
|
mrf_hashtag: %{
|
|
|
|
federated_timeline_removal: [],
|
|
|
|
reject: [],
|
|
|
|
sensitive: ["nsfw"]
|
|
|
|
},
|
2019-08-13 16:19:15 -06:00
|
|
|
exclusions: false
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, ^expected} = MRF.describe()
|
|
|
|
end
|
|
|
|
end
|
2020-11-10 09:18:53 -07:00
|
|
|
|
|
|
|
test "config_descriptions/0" do
|
|
|
|
descriptions = MRF.config_descriptions()
|
|
|
|
|
|
|
|
good_mrf = Enum.find(descriptions, fn %{key: key} -> key == :good_mrf end)
|
|
|
|
|
|
|
|
assert good_mrf == %{
|
|
|
|
key: :good_mrf,
|
|
|
|
related_policy: "Fixtures.Modules.GoodMRF",
|
|
|
|
label: "Good MRF",
|
|
|
|
description: "Some description",
|
|
|
|
group: :pleroma,
|
|
|
|
tab: :mrf,
|
|
|
|
type: :group
|
|
|
|
}
|
|
|
|
end
|
2019-07-22 08:33:58 -06:00
|
|
|
end
|