Support TLD wildcards in MRF matches

Fixes #431
This commit is contained in:
FloatingGhost 2023-01-15 18:57:49 +00:00
parent 2fc5fb7f5a
commit 90088cce11
3 changed files with 34 additions and 2 deletions

View file

@ -161,10 +161,17 @@ defmodule Pleroma.Web.ActivityPub.MRF do
# - https://extra.baddomain.net/ # - https://extra.baddomain.net/
# Does NOT match the following: # Does NOT match the following:
# - https://maybebaddomain.net/ # - https://maybebaddomain.net/
# *.baddomain.net
def subdomain_regex("*." <> domain), do: subdomain_regex(domain) def subdomain_regex("*." <> domain), do: subdomain_regex(domain)
# baddomain.net
def subdomain_regex(domain) do def subdomain_regex(domain) do
~r/^(.+\.)?#{Regex.escape(domain)}$/i if String.ends_with?(domain, ".*") do
~r/^(.+\.)?#{Regex.escape(String.replace_suffix(domain, ".*", ""))}\.(.+)$/i
else
~r/^(.+\.)?#{Regex.escape(domain)}$/i
end
end end
@spec subdomains_regex([String.t()]) :: [Regex.t()] @spec subdomains_regex([String.t()]) :: [Regex.t()]

View file

@ -48,6 +48,31 @@ defmodule Pleroma.Web.ActivityPub.MRFTest do
refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother") refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother")
end end
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
test "matches are case-insensitive" do test "matches are case-insensitive" do
regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"]) regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])

View file

@ -40,7 +40,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest
end end
test "a note with a language validates" do test "a note with a language validates" do
user = insert(:user, %{ap_id: "https://mastodon.social/users/akkoma_ap_integration_tester"}) insert(:user, %{ap_id: "https://mastodon.social/users/akkoma_ap_integration_tester"})
note = File.read!("test/fixtures/mastodon/note_with_language.json") |> Jason.decode!() note = File.read!("test/fixtures/mastodon/note_with_language.json") |> Jason.decode!()
%{ %{