ensure utf-8 nicknames on nickname GETs and user validator #1057
2 changed files with 24 additions and 0 deletions
ensure utf-8 nicknames on nickname gets and user validator
commit
0d7bbab384
|
|
@ -1211,6 +1211,7 @@ defmodule Pleroma.User do
|
|||
end
|
||||
|
||||
def get_cached_by_nickname(nickname) do
|
||||
if String.valid?(nickname) do
|
||||
key = "nickname:#{nickname}"
|
||||
|
||||
@cachex.fetch!(:user_cache, key, fn _ ->
|
||||
|
|
@ -1219,9 +1220,14 @@ defmodule Pleroma.User do
|
|||
{:error, _error} -> {:ignore, nil}
|
||||
end
|
||||
end)
|
||||
else
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
def get_cached_by_nickname_or_id(nickname_or_id, opts \\ []) do
|
||||
if String.valid?(nickname_or_id) do
|
||||
|
floatingghost marked this conversation as resolved
|
||||
|
||||
restrict_to_local = Config.get([:instance, :limit_to_local_content])
|
||||
|
||||
cond do
|
||||
|
|
@ -1237,14 +1243,21 @@ defmodule Pleroma.User do
|
|||
true ->
|
||||
nil
|
||||
end
|
||||
else
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@spec get_by_nickname(String.t()) :: User.t() | nil
|
||||
def get_by_nickname(nickname) do
|
||||
if String.valid?(nickname) do
|
||||
Repo.get_by(User, nickname: nickname) ||
|
||||
if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
|
||||
Repo.get_by(User, nickname: local_nickname(nickname))
|
||||
end
|
||||
else
|
||||
:error
|
||||
|
floatingghost marked this conversation as resolved
Oneric
commented
Returning The same applies to the other two modified functions Returning `:error` here breaks the documented type spec. It apparently still works fine anyway for `/api/v1/accounts/:nick`, but might still crash for other users of this function. I don’t think it’s necessary to distinguish this from a normal "not found" response though, after all no such user exists in the db and so just returning `nil` seems fine.
The same applies to the other two modified functions
|
||||
end
|
||||
end
|
||||
|
||||
def get_by_email(email), do: Repo.get_by(User, email: email)
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
|||
assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
|
||||
assert id == user.id
|
||||
end
|
||||
|
||||
|
||||
test "accounts fetches correct account for nicknames beginning with numbers", %{conn: conn} do
|
||||
# Need to set an old-style integer ID to reproduce the problem
|
||||
|
|
@ -177,6 +178,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
|||
|> get("/api/v1/accounts/#{user.id}")
|
||||
|> json_response_and_validate_schema(:not_found)
|
||||
end
|
||||
|
||||
test "returns 404 when the username is not valid utf8" do
|
||||
nick = <<0xc0>>
|
||||
refute String.valid?(nick)
|
||||
|
||||
assert %{"error" => "Can't find user"} =
|
||||
build_conn()
|
||||
|> get("/api/v1/accounts/#{nick}")
|
||||
|> json_response_and_validate_schema(404)
|
||||
end
|
||||
end
|
||||
|
||||
defp local_and_remote_users do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue
Since
FlakeIdcan handle this and id-type arguments are presumably much more common it might be good to optimise this by only testingString.validif there was no id match. E.g. something like:Though it might be nicer to split the "allowed to lookup" checks handled in the
condinto a private function returning a boolean and then also handle this as just anotherwithclause:i just removed the check in nickname_by_id and let it live in the get_cached_by_nickname which should be suitable