diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/plugs/rate_limiter/rate_limiter.ex | 27 | ||||
| -rw-r--r-- | lib/pleroma/plugs/remote_ip.ex | 7 | ||||
| -rw-r--r-- | lib/pleroma/user.ex | 32 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 39 | ||||
| -rw-r--r-- | lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex | 33 | ||||
| -rw-r--r-- | lib/pleroma/workers/background_worker.ex | 4 | 
6 files changed, 47 insertions, 95 deletions
| diff --git a/lib/pleroma/plugs/rate_limiter/rate_limiter.ex b/lib/pleroma/plugs/rate_limiter/rate_limiter.ex index c3f6351c8..1529da717 100644 --- a/lib/pleroma/plugs/rate_limiter/rate_limiter.ex +++ b/lib/pleroma/plugs/rate_limiter/rate_limiter.ex @@ -78,7 +78,7 @@ defmodule Pleroma.Plugs.RateLimiter do    end    def call(conn, plug_opts) do -    if disabled?() do +    if disabled?(conn) do        handle_disabled(conn)      else        action_settings = action_settings(plug_opts) @@ -87,9 +87,9 @@ defmodule Pleroma.Plugs.RateLimiter do    end    defp handle_disabled(conn) do -    if Config.get(:env) == :prod do -      Logger.warn("Rate limiter is disabled for localhost/socket") -    end +    Logger.warn( +      "Rate limiter disabled due to forwarded IP not being found. Please ensure your reverse proxy is providing the X-Forwarded-For header or disable the RemoteIP plug/rate limiter." +    )      conn    end @@ -109,16 +109,21 @@ defmodule Pleroma.Plugs.RateLimiter do      end    end -  def disabled? do +  def disabled?(conn) do      localhost_or_socket = -      Config.get([Pleroma.Web.Endpoint, :http, :ip]) -      |> Tuple.to_list() -      |> Enum.join(".") -      |> String.match?(~r/^local|^127.0.0.1/) +      case Config.get([Pleroma.Web.Endpoint, :http, :ip]) do +        {127, 0, 0, 1} -> true +        {0, 0, 0, 0, 0, 0, 0, 1} -> true +        {:local, _} -> true +        _ -> false +      end -    remote_ip_disabled = not Config.get([Pleroma.Plugs.RemoteIp, :enabled]) +    remote_ip_not_found = +      if Map.has_key?(conn.assigns, :remote_ip_found), +        do: !conn.assigns.remote_ip_found, +        else: false -    localhost_or_socket and remote_ip_disabled +    localhost_or_socket and remote_ip_not_found    end    @inspect_bucket_not_found {:error, :not_found} diff --git a/lib/pleroma/plugs/remote_ip.ex b/lib/pleroma/plugs/remote_ip.ex index 2eca4f8f6..0ac9050d0 100644 --- a/lib/pleroma/plugs/remote_ip.ex +++ b/lib/pleroma/plugs/remote_ip.ex @@ -7,6 +7,8 @@ defmodule Pleroma.Plugs.RemoteIp do    This is a shim to call [`RemoteIp`](https://git.pleroma.social/pleroma/remote_ip) but with runtime configuration.    """ +  import Plug.Conn +    @behaviour Plug    @headers ~w[ @@ -26,11 +28,12 @@ defmodule Pleroma.Plugs.RemoteIp do    def init(_), do: nil -  def call(conn, _) do +  def call(%{remote_ip: original_remote_ip} = conn, _) do      config = Pleroma.Config.get(__MODULE__, [])      if Keyword.get(config, :enabled, false) do -      RemoteIp.call(conn, remote_ip_opts(config)) +      %{remote_ip: new_remote_ip} = conn = RemoteIp.call(conn, remote_ip_opts(config)) +      assign(conn, :remote_ip_found, original_remote_ip != new_remote_ip)      else        conn      end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 7531757f5..db510d957 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -839,10 +839,6 @@ defmodule Pleroma.User do        _e ->          with [_nick, _domain] <- String.split(nickname, "@"),               {:ok, user} <- fetch_by_nickname(nickname) do -          if Pleroma.Config.get([:fetch_initial_posts, :enabled]) do -            fetch_initial_posts(user) -          end -            {:ok, user}          else            _e -> {:error, "not found " <> nickname} @@ -850,11 +846,6 @@ defmodule Pleroma.User do      end    end -  @doc "Fetch some posts when the user has just been federated with" -  def fetch_initial_posts(user) do -    BackgroundWorker.enqueue("fetch_initial_posts", %{"user_id" => user.id}) -  end -    @spec get_followers_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()    def get_followers_query(%User{} = user, nil) do      User.Query.build(%{followers: user, deactivated: false}) @@ -1320,16 +1311,6 @@ defmodule Pleroma.User do      Repo.delete(user)    end -  def perform(:fetch_initial_posts, %User{} = user) do -    pages = Pleroma.Config.get!([:fetch_initial_posts, :pages]) - -    # Insert all the posts in reverse order, so they're in the right order on the timeline -    user.source_data["outbox"] -    |> Utils.fetch_ordered_collection(pages) -    |> Enum.reverse() -    |> Enum.each(&Pleroma.Web.Federator.incoming_ap_doc/1) -  end -    def perform(:deactivate_async, user, status), do: deactivate(user, status)    @spec perform(atom(), User.t(), list()) :: list() | {:error, any()} @@ -1458,18 +1439,7 @@ defmodule Pleroma.User do      if !is_nil(user) and !needs_update?(user) do        {:ok, user}      else -      # Whether to fetch initial posts for the user (if it's a new user & the fetching is enabled) -      should_fetch_initial = is_nil(user) and Pleroma.Config.get([:fetch_initial_posts, :enabled]) - -      resp = fetch_by_ap_id(ap_id) - -      if should_fetch_initial do -        with {:ok, %User{} = user} <- resp do -          fetch_initial_posts(user) -        end -      end - -      resp +      fetch_by_ap_id(ap_id)      end    end diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 2bc958670..15dd2ed45 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -784,45 +784,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do    defp build_flag_object(_), do: [] -  @doc """ -  Fetches the OrderedCollection/OrderedCollectionPage from `from`, limiting the amount of pages fetched after -  the first one to `pages_left` pages. -  If the amount of pages is higher than the collection has, it returns whatever was there. -  """ -  def fetch_ordered_collection(from, pages_left, acc \\ []) do -    with {:ok, response} <- Tesla.get(from), -         {:ok, collection} <- Jason.decode(response.body) do -      case collection["type"] do -        "OrderedCollection" -> -          # If we've encountered the OrderedCollection and not the page, -          # just call the same function on the page address -          fetch_ordered_collection(collection["first"], pages_left) - -        "OrderedCollectionPage" -> -          if pages_left > 0 do -            # There are still more pages -            if Map.has_key?(collection, "next") do -              # There are still more pages, go deeper saving what we have into the accumulator -              fetch_ordered_collection( -                collection["next"], -                pages_left - 1, -                acc ++ collection["orderedItems"] -              ) -            else -              # No more pages left, just return whatever we already have -              acc ++ collection["orderedItems"] -            end -          else -            # Got the amount of pages needed, add them all to the accumulator -            acc ++ collection["orderedItems"] -          end - -        _ -> -          {:error, "Not an OrderedCollection or OrderedCollectionPage"} -      end -    end -  end -    #### Report-related helpers    def get_reports(params, page, page_size) do      params = diff --git a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex index 0e160bbfc..dae7f0f2f 100644 --- a/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex @@ -101,6 +101,11 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do        conn        |> put_view(ConversationView)        |> render("participation.json", %{participation: participation, for: user}) +    else +      _error -> +        conn +        |> put_status(404) +        |> json(%{"error" => "Unknown conversation id"})      end    end @@ -108,9 +113,9 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do          %{assigns: %{user: user}} = conn,          %{"id" => participation_id} = params        ) do -    participation = Participation.get(participation_id, preload: [:conversation]) - -    if user.id == participation.user_id do +    with %Participation{} = participation <- +           Participation.get(participation_id, preload: [:conversation]), +         true <- user.id == participation.user_id do        params =          params          |> Map.put("blocking_user", user) @@ -126,6 +131,11 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do        |> add_link_headers(activities)        |> put_view(StatusView)        |> render("index.json", %{activities: activities, for: user, as: :activity}) +    else +      _error -> +        conn +        |> put_status(404) +        |> json(%{"error" => "Unknown conversation id"})      end    end @@ -133,15 +143,22 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do          %{assigns: %{user: user}} = conn,          %{"id" => participation_id, "recipients" => recipients}        ) do -    participation = -      participation_id -      |> Participation.get() - -    with true <- user.id == participation.user_id, +    with %Participation{} = participation <- Participation.get(participation_id), +         true <- user.id == participation.user_id,           {:ok, participation} <- Participation.set_recipients(participation, recipients) do        conn        |> put_view(ConversationView)        |> render("participation.json", %{participation: participation, for: user}) +    else +      {:error, message} -> +        conn +        |> put_status(:bad_request) +        |> json(%{"error" => message}) + +      _error -> +        conn +        |> put_status(404) +        |> json(%{"error" => "Unknown conversation id"})      end    end diff --git a/lib/pleroma/workers/background_worker.ex b/lib/pleroma/workers/background_worker.ex index 598df6580..0f8ece2c4 100644 --- a/lib/pleroma/workers/background_worker.ex +++ b/lib/pleroma/workers/background_worker.ex @@ -10,10 +10,6 @@ defmodule Pleroma.Workers.BackgroundWorker do    use Pleroma.Workers.WorkerHelper, queue: "background"    @impl Oban.Worker -  def perform(%{"op" => "fetch_initial_posts", "user_id" => user_id}, _job) do -    user = User.get_cached_by_id(user_id) -    User.perform(:fetch_initial_posts, user) -  end    def perform(%{"op" => "deactivate_user", "user_id" => user_id, "status" => status}, _job) do      user = User.get_cached_by_id(user_id) | 
