From 08db6765c851fd5bd4ded74df0c1e86c0233cdf9 Mon Sep 17 00:00:00 2001 From: Atsuko Karagi Date: Mon, 19 Dec 2022 20:32:16 +0000 Subject: [PATCH] Respect restrict_unauthenticated in /api/v1/accounts/lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../api_spec/operations/account_operation.ex | 1 + .../controllers/account_controller.ex | 9 ++-- .../controllers/account_controller_test.exs | 44 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index b91c224d2b..4a126c61b6 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -479,6 +479,7 @@ def lookup_operation do ], responses: %{ 200 => Operation.response("Account", "application/json", Account), + 401 => Operation.response("Error", "application/json", ApiError), 404 => Operation.response("Error", "application/json", ApiError) } } diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 11ed75bda8..fb14ab1722 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -32,14 +32,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do plug(Pleroma.Web.ApiSpec.CastAndValidate) - plug(:skip_auth when action in [:create, :lookup]) + plug(:skip_auth when action in [:create]) plug(:skip_public_check when action in [:show, :statuses]) plug( OAuthScopesPlug, %{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]} - when action in [:show, :followers, :following] + when action in [:show, :followers, :following, :lookup] ) plug( @@ -538,8 +538,9 @@ def blocks(%{assigns: %{user: user}} = conn, params) do end @doc "GET /api/v1/accounts/lookup" - def lookup(conn, %{acct: nickname} = _params) do - with %User{} = user <- User.get_by_nickname(nickname) do + def lookup(%{assigns: %{user: for_user}} = conn, %{acct: nickname} = _params) do + with %User{} = user <- User.get_by_nickname(nickname), + :visible <- User.visible_for(user, for_user) do render(conn, "show.json", user: user, skip_visibility_check: true diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 773fd4be5b..340efb24bc 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -2055,6 +2055,50 @@ test "account lookup", %{conn: conn} do |> json_response_and_validate_schema(404) end + test "account lookup with restrict unauthenticated profiles for local" do + clear_config([:restrict_unauthenticated, :profiles, :local], true) + + user = insert(:user, local: true) + reading_user = insert(:user) + + conn = + build_conn() + |> get("/api/v1/accounts/lookup?acct=#{user.nickname}") + + assert json_response_and_validate_schema(conn, 401) + + conn = + build_conn() + |> assign(:user, reading_user) + |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"])) + |> get("/api/v1/accounts/lookup?acct=#{user.nickname}") + + assert %{"id" => id} = json_response_and_validate_schema(conn, 200) + assert id == user.id + end + + test "account lookup with restrict unauthenticated profiles for remote" do + clear_config([:restrict_unauthenticated, :profiles, :remote], true) + + user = insert(:user, nickname: "user@example.com", local: false) + reading_user = insert(:user) + + conn = + build_conn() + |> get("/api/v1/accounts/lookup?acct=#{user.nickname}") + + assert json_response_and_validate_schema(conn, 401) + + conn = + build_conn() + |> assign(:user, reading_user) + |> assign(:token, insert(:oauth_token, user: reading_user, scopes: ["read:accounts"])) + |> get("/api/v1/accounts/lookup?acct=#{user.nickname}") + + assert %{"id" => id} = json_response_and_validate_schema(conn, 200) + assert id == user.id + end + test "create a note on a user" do %{conn: conn} = oauth_access(["write:accounts", "read:follows"]) other_user = insert(:user)