Support TLD wildcards in MRF matches
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 15 Jan 2023 18:57:49 +0000 (18:57 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 15 Jan 2023 18:57:49 +0000 (18:57 +0000)
Fixes #431

lib/pleroma/web/activity_pub/mrf.ex
test/pleroma/web/activity_pub/mrf_test.exs
test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs

index dae6d7f6a1ae74512336d46f2efd5e98fc6779f3..6ecd62c99e17f2ccbd4a0c0fb33bdb2050a34fbd 100644 (file)
@@ -161,10 +161,17 @@ defmodule Pleroma.Web.ActivityPub.MRF do
   # - https://extra.baddomain.net/
   # Does NOT match the following:
   # - https://maybebaddomain.net/
+
+  # *.baddomain.net
   def subdomain_regex("*." <> domain), do: subdomain_regex(domain)
 
+  # baddomain.net
   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
 
   @spec subdomains_regex([String.t()]) :: [Regex.t()]
index 86ad0ab6b2ed4224e724ae1caa984895b45df2f0..7359398febd0c27c14cb85cb45712fca68dc382b 100644 (file)
@@ -48,6 +48,31 @@ defmodule Pleroma.Web.ActivityPub.MRFTest do
       refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother")
     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
       regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
 
index 0d39dee67e5a88e118ef54f4d3f308757affffb7..62ac5e0510ff3f32afbe1bed6997ab4ed13e9811 100644 (file)
@@ -40,7 +40,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidatorTest
     end
 
     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!()
 
       %{