diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex | 25 | ||||
| -rw-r--r-- | test/pleroma/upload/filter/only_media_test.exs | 32 | ||||
| -rw-r--r-- | test/pleroma/user_test.exs | 11 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/activity_pub_controller_test.exs | 1 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/activity_pub_test.exs | 1 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/publisher_test.exs | 15 | ||||
| -rw-r--r-- | test/pleroma/web/activity_pub/transmogrifier_test.exs | 64 | ||||
| -rw-r--r-- | test/pleroma/web/common_api_test.exs | 2 | ||||
| -rw-r--r-- | test/pleroma/web/federator_test.exs | 6 | ||||
| -rw-r--r-- | test/pleroma/web/media_proxy/media_proxy_controller_test.exs | 16 | ||||
| -rw-r--r-- | test/pleroma/web/metadata/providers/twitter_card_test.exs | 3 | ||||
| -rw-r--r-- | test/pleroma/web/plugs/uploaded_media_plug_test.exs | 26 | ||||
| -rw-r--r-- | test/pleroma/web/rich_media/parser_test.exs | 4 | ||||
| -rw-r--r-- | test/pleroma/web/streamer_test.exs | 77 | ||||
| -rw-r--r-- | test/support/factory.ex | 1 | 
15 files changed, 191 insertions, 93 deletions
| diff --git a/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex new file mode 100644 index 000000000..226383c3c --- /dev/null +++ b/test/pleroma/ecto_type/activity_pub/object_validators/bare_uri_test.ex @@ -0,0 +1,25 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.BareUriTest do +  use Pleroma.DataCase, async: true + +  alias Pleroma.EctoType.ActivityPub.ObjectValidators.BareUri + +  test "diaspora://" do +    text = "diaspora://alice@fediverse.example/post/deadbeefdeadbeefdeadbeefdeadbeef" +    assert {:ok, text} = BareUri.cast(text) +  end + +  test "nostr:" do +    text = "nostr:note1gwdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" +    assert {:ok, text} = BareUri.cast(text) +  end + +  test "errors for non-URIs" do +    assert :error == SafeText.cast(1) +    assert :error == SafeText.cast("foo") +    assert :error == SafeText.cast("foo bar") +  end +end diff --git a/test/pleroma/upload/filter/only_media_test.exs b/test/pleroma/upload/filter/only_media_test.exs new file mode 100644 index 000000000..75be070a1 --- /dev/null +++ b/test/pleroma/upload/filter/only_media_test.exs @@ -0,0 +1,32 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Upload.Filter.OnlyMediaTest do +  use Pleroma.DataCase, async: true + +  alias Pleroma.Upload +  alias Pleroma.Upload.Filter.OnlyMedia + +  test "Allows media Content-Type" do +    ["audio/mpeg", "image/jpeg", "video/mp4"] +    |> Enum.each(fn type -> +      upload = %Upload{ +        content_type: type +      } + +      assert {:ok, :noop} = OnlyMedia.filter(upload) +    end) +  end + +  test "Disallows non-media Content-Type" do +    ["application/javascript", "application/pdf", "text/html"] +    |> Enum.each(fn type -> +      upload = %Upload{ +        content_type: type +      } + +      assert {:error, _} = OnlyMedia.filter(upload) +    end) +  end +end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index c16312a65..7f60b959a 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -1844,7 +1844,6 @@ defmodule Pleroma.UserTest do          confirmation_token: "qqqq",          domain_blocks: ["lain.com"],          is_active: false, -        ap_enabled: true,          is_moderator: true,          is_admin: true,          mascot: %{"a" => "b"}, @@ -1885,7 +1884,6 @@ defmodule Pleroma.UserTest do               confirmation_token: nil,               domain_blocks: [],               is_active: false, -             ap_enabled: false,               is_moderator: false,               is_admin: false,               mascot: nil, @@ -2473,8 +2471,7 @@ defmodule Pleroma.UserTest do          insert(:user,            local: false,            follower_address: "http://localhost:4001/users/masto_closed/followers", -          following_address: "http://localhost:4001/users/masto_closed/following", -          ap_enabled: true +          following_address: "http://localhost:4001/users/masto_closed/following"          )        assert other_user.following_count == 0 @@ -2495,8 +2492,7 @@ defmodule Pleroma.UserTest do          insert(:user,            local: false,            follower_address: "http://localhost:4001/users/masto_closed/followers", -          following_address: "http://localhost:4001/users/masto_closed/following", -          ap_enabled: true +          following_address: "http://localhost:4001/users/masto_closed/following"          )        assert other_user.following_count == 0 @@ -2517,8 +2513,7 @@ defmodule Pleroma.UserTest do          insert(:user,            local: false,            follower_address: "http://localhost:4001/users/masto_closed/followers", -          following_address: "http://localhost:4001/users/masto_closed/following", -          ap_enabled: true +          following_address: "http://localhost:4001/users/masto_closed/following"          )        assert other_user.following_count == 0 diff --git a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs index ef91066c1..62eb9b5a3 100644 --- a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs @@ -575,7 +575,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        user =          insert(:user,            ap_id: "https://mastodon.example.org/users/raymoo", -          ap_enabled: true,            local: false,            last_refreshed_at: nil          ) diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index a6f8b6152..54fc6ef0a 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -174,7 +174,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)        assert user.ap_id == user_id        assert user.nickname == "admin@mastodon.example.org" -      assert user.ap_enabled        assert user.follower_address == "http://mastodon.example.org/users/admin/followers"      end diff --git a/test/pleroma/web/activity_pub/publisher_test.exs b/test/pleroma/web/activity_pub/publisher_test.exs index e2db3d575..c5137cbb7 100644 --- a/test/pleroma/web/activity_pub/publisher_test.exs +++ b/test/pleroma/web/activity_pub/publisher_test.exs @@ -276,8 +276,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        follower =          insert(:user, %{            local: false, -          inbox: "https://domain.com/users/nick1/inbox", -          ap_enabled: true +          inbox: "https://domain.com/users/nick1/inbox"          })        actor = insert(:user, follower_address: follower.ap_id) @@ -313,8 +312,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        follower =          insert(:user, %{            local: false, -          inbox: "https://domain.com/users/nick1/inbox", -          ap_enabled: true +          inbox: "https://domain.com/users/nick1/inbox"          })        actor = insert(:user, follower_address: follower.ap_id) @@ -348,8 +346,7 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        follower =          insert(:user, %{            local: false, -          inbox: "https://domain.com/users/nick1/inbox", -          ap_enabled: true +          inbox: "https://domain.com/users/nick1/inbox"          })        actor = insert(:user, follower_address: follower.ap_id) @@ -382,15 +379,13 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        fetcher =          insert(:user,            local: false, -          inbox: "https://domain.com/users/nick1/inbox", -          ap_enabled: true +          inbox: "https://domain.com/users/nick1/inbox"          )        another_fetcher =          insert(:user,            local: false, -          inbox: "https://domain2.com/users/nick1/inbox", -          ap_enabled: true +          inbox: "https://domain2.com/users/nick1/inbox"          )        actor = insert(:user) diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index f76606479..3e0c8dc65 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -8,7 +8,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do    alias Pleroma.Activity    alias Pleroma.Object -  alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.Web.ActivityPub.Transmogrifier    alias Pleroma.Web.ActivityPub.Utils @@ -353,69 +352,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do      end    end -  describe "user upgrade" do -    test "it upgrades a user to activitypub" do -      user = -        insert(:user, %{ -          nickname: "rye@niu.moe", -          local: false, -          ap_id: "https://niu.moe/users/rye", -          follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"}) -        }) - -      user_two = insert(:user) -      Pleroma.FollowingRelationship.follow(user_two, user, :follow_accept) - -      {:ok, activity} = CommonAPI.post(user, %{status: "test"}) -      {:ok, unrelated_activity} = CommonAPI.post(user_two, %{status: "test"}) -      assert "http://localhost:4001/users/rye@niu.moe/followers" in activity.recipients - -      user = User.get_cached_by_id(user.id) -      assert user.note_count == 1 - -      {:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye") -      ObanHelpers.perform_all() - -      assert user.ap_enabled -      assert user.note_count == 1 -      assert user.follower_address == "https://niu.moe/users/rye/followers" -      assert user.following_address == "https://niu.moe/users/rye/following" - -      user = User.get_cached_by_id(user.id) -      assert user.note_count == 1 - -      activity = Activity.get_by_id(activity.id) -      assert user.follower_address in activity.recipients - -      assert %{ -               "url" => [ -                 %{ -                   "href" => -                     "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg" -                 } -               ] -             } = user.avatar - -      assert %{ -               "url" => [ -                 %{ -                   "href" => -                     "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png" -                 } -               ] -             } = user.banner - -      refute "..." in activity.recipients - -      unrelated_activity = Activity.get_by_id(unrelated_activity.id) -      refute user.follower_address in unrelated_activity.recipients - -      user_two = User.get_cached_by_id(user_two.id) -      assert User.following?(user_two, user) -      refute "..." in User.following(user_two) -    end -  end -    describe "actor rewriting" do      test "it fixes the actor URL property to be a proper URI" do        data = %{ diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index 968d826a2..3112d831b 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -1339,7 +1339,7 @@ defmodule Pleroma.Web.CommonAPITest do      test "cancels a pending follow for a remote user" do        follower = insert(:user) -      followed = insert(:user, is_locked: true, local: false, ap_enabled: true) +      followed = insert(:user, is_locked: true, local: false)        assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =                 CommonAPI.follow(follower, followed) diff --git a/test/pleroma/web/federator_test.exs b/test/pleroma/web/federator_test.exs index 1ffe6aae1..6826e6c2f 100644 --- a/test/pleroma/web/federator_test.exs +++ b/test/pleroma/web/federator_test.exs @@ -78,16 +78,14 @@ defmodule Pleroma.Web.FederatorTest do          local: false,          nickname: "nick1@domain.com",          ap_id: "https://domain.com/users/nick1", -        inbox: inbox1, -        ap_enabled: true +        inbox: inbox1        })        insert(:user, %{          local: false,          nickname: "nick2@domain2.com",          ap_id: "https://domain2.com/users/nick2", -        inbox: inbox2, -        ap_enabled: true +        inbox: inbox2        })        dt = NaiveDateTime.utc_now() diff --git a/test/pleroma/web/media_proxy/media_proxy_controller_test.exs b/test/pleroma/web/media_proxy/media_proxy_controller_test.exs index 5246bf0c4..9ce092fd8 100644 --- a/test/pleroma/web/media_proxy/media_proxy_controller_test.exs +++ b/test/pleroma/web/media_proxy/media_proxy_controller_test.exs @@ -6,7 +6,9 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do    use Pleroma.Web.ConnCase    import Mock +  import Mox +  alias Pleroma.ReverseProxy.ClientMock    alias Pleroma.Web.MediaProxy    alias Plug.Conn @@ -74,6 +76,20 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do          assert %Conn{status: 404, resp_body: "Not Found"} = get(conn, url)        end      end + +    test "it applies sandbox CSP to MediaProxy requests", %{conn: conn} do +      media_url = "https://lain.com/image.png" +      media_proxy_url = MediaProxy.encode_url(media_url) + +      ClientMock +      |> expect(:request, fn :get, ^media_url, _, _, _ -> +        {:ok, 200, [{"content-type", "image/png"}]} +      end) + +      %Conn{resp_headers: headers} = get(conn, media_proxy_url) + +      assert {"content-security-policy", "sandbox;"} in headers +    end    end    describe "Media Preview Proxy" do diff --git a/test/pleroma/web/metadata/providers/twitter_card_test.exs b/test/pleroma/web/metadata/providers/twitter_card_test.exs index be4cfbe7b..f8d01c5c8 100644 --- a/test/pleroma/web/metadata/providers/twitter_card_test.exs +++ b/test/pleroma/web/metadata/providers/twitter_card_test.exs @@ -182,7 +182,8 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCardTest do               {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},               {:meta, [name: "twitter:description", content: "pleroma in a nutshell"], []},               {:meta, [name: "twitter:card", content: "summary_large_image"], []}, -             {:meta, [name: "twitter:player", content: "https://pleroma.gov/tenshi.png"], []}, +             {:meta, [name: "twitter:image", content: "https://pleroma.gov/tenshi.png"], []}, +             {:meta, [name: "twitter:image:alt", content: ""], []},               {:meta, [name: "twitter:player:width", content: "1280"], []},               {:meta, [name: "twitter:player:height", content: "1024"], []},               {:meta, [name: "twitter:card", content: "player"], []}, diff --git a/test/pleroma/web/plugs/uploaded_media_plug_test.exs b/test/pleroma/web/plugs/uploaded_media_plug_test.exs index 8323ff6ab..dbf8ca5ec 100644 --- a/test/pleroma/web/plugs/uploaded_media_plug_test.exs +++ b/test/pleroma/web/plugs/uploaded_media_plug_test.exs @@ -40,4 +40,30 @@ defmodule Pleroma.Web.Plugs.UploadedMediaPlugTest do               &(&1 == {"content-disposition", ~s[inline; filename="\\"cofe\\".gif"]})             )    end + +  test "denies access to media if wrong Host", %{ +    attachment_url: attachment_url +  } do +    conn = get(build_conn(), attachment_url) + +    assert conn.status == 200 + +    new_media_base = "http://media.localhost:8080" + +    %{scheme: new_media_scheme, host: new_media_host, port: new_media_port} = +      URI.parse(new_media_base) + +    clear_config([Pleroma.Upload, :base_url], new_media_base) + +    conn = get(build_conn(), attachment_url) + +    expected_url = +      URI.parse(attachment_url) +      |> Map.put(:host, new_media_host) +      |> Map.put(:port, new_media_port) +      |> Map.put(:scheme, new_media_scheme) +      |> URI.to_string() + +    assert redirected_to(conn, 302) == expected_url +  end  end diff --git a/test/pleroma/web/rich_media/parser_test.exs b/test/pleroma/web/rich_media/parser_test.exs index ffdc4e5d7..9064138a6 100644 --- a/test/pleroma/web/rich_media/parser_test.exs +++ b/test/pleroma/web/rich_media/parser_test.exs @@ -129,7 +129,7 @@ defmodule Pleroma.Web.RichMedia.ParserTest do                }}    end -  test "parses OEmbed" do +  test "parses OEmbed and filters HTML tags" do      assert Parser.parse("http://example.com/oembed") ==               {:ok,                %{ @@ -139,7 +139,7 @@ defmodule Pleroma.Web.RichMedia.ParserTest do                  "flickr_type" => "photo",                  "height" => "768",                  "html" => -                  "<a data-flickr-embed=\"true\" href=\"https://www.flickr.com/photos/bees/2362225867/\" title=\"Bacon Lollys by \u202E\u202D\u202Cbees\u202C, on Flickr\"><img src=\"https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg\" width=\"1024\" height=\"768\" alt=\"Bacon Lollys\"></a><script async src=\"https://embedr.flickr.com/assets/client-code.js\" charset=\"utf-8\"></script>", +                  "<a href=\"https://www.flickr.com/photos/bees/2362225867/\" title=\"Bacon Lollys by \u202E\u202D\u202Cbees\u202C, on Flickr\"><img src=\"https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg\" width=\"1024\" height=\"768\" alt=\"Bacon Lollys\"/></a>",                  "license" => "All Rights Reserved",                  "license_id" => 0,                  "provider_name" => "Flickr", diff --git a/test/pleroma/web/streamer_test.exs b/test/pleroma/web/streamer_test.exs index 8b0c84164..7ab0e379b 100644 --- a/test/pleroma/web/streamer_test.exs +++ b/test/pleroma/web/streamer_test.exs @@ -29,6 +29,26 @@ defmodule Pleroma.Web.StreamerTest do        assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil, nil)      end +    test "rejects local public streams if restricted_unauthenticated is on" do +      clear_config([:restrict_unauthenticated, :timelines, :local], true) + +      assert {:error, :unauthorized} = Streamer.get_topic("public:local", nil, nil) +      assert {:error, :unauthorized} = Streamer.get_topic("public:local:media", nil, nil) +    end + +    test "rejects remote public streams if restricted_unauthenticated is on" do +      clear_config([:restrict_unauthenticated, :timelines, :federated], true) + +      assert {:error, :unauthorized} = Streamer.get_topic("public", nil, nil) +      assert {:error, :unauthorized} = Streamer.get_topic("public:media", nil, nil) + +      assert {:error, :unauthorized} = +               Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"}) + +      assert {:error, :unauthorized} = +               Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"}) +    end +      test "allows instance streams" do        assert {:ok, "public:remote:lain.com"} =                 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"}) @@ -69,6 +89,63 @@ defmodule Pleroma.Web.StreamerTest do        end      end +    test "allows local public streams if restricted_unauthenticated is on", %{ +      user: user, +      token: oauth_token +    } do +      clear_config([:restrict_unauthenticated, :timelines, :local], true) + +      %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user) +      %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user) + +      assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token) + +      assert {:ok, "public:local:media"} = +               Streamer.get_topic("public:local:media", user, oauth_token) + +      for token <- [read_notifications_token, badly_scoped_token] do +        assert {:error, :unauthorized} = Streamer.get_topic("public:local", user, token) + +        assert {:error, :unauthorized} = Streamer.get_topic("public:local:media", user, token) +      end +    end + +    test "allows remote public streams if restricted_unauthenticated is on", %{ +      user: user, +      token: oauth_token +    } do +      clear_config([:restrict_unauthenticated, :timelines, :federated], true) + +      %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user) +      %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user) + +      assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token) +      assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token) + +      assert {:ok, "public:remote:lain.com"} = +               Streamer.get_topic("public:remote", user, oauth_token, %{"instance" => "lain.com"}) + +      assert {:ok, "public:remote:media:lain.com"} = +               Streamer.get_topic("public:remote:media", user, oauth_token, %{ +                 "instance" => "lain.com" +               }) + +      for token <- [read_notifications_token, badly_scoped_token] do +        assert {:error, :unauthorized} = Streamer.get_topic("public", user, token) +        assert {:error, :unauthorized} = Streamer.get_topic("public:media", user, token) + +        assert {:error, :unauthorized} = +                 Streamer.get_topic("public:remote", user, token, %{ +                   "instance" => "lain.com" +                 }) + +        assert {:error, :unauthorized} = +                 Streamer.get_topic("public:remote:media", user, token, %{ +                   "instance" => "lain.com" +                 }) +      end +    end +      test "allows user streams (with proper OAuth token scopes)", %{        user: user,        token: read_oauth_token diff --git a/test/support/factory.ex b/test/support/factory.ex index 09f02458c..d94544717 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -50,7 +50,6 @@ defmodule Pleroma.Factory do        last_refreshed_at: NaiveDateTime.utc_now(),        notification_settings: %Pleroma.User.NotificationSetting{},        multi_factor_authentication_settings: %Pleroma.MFA.Settings{}, -      ap_enabled: true,        keys: pem      } | 
