User: Use ObjectID type to validate also-known-as field

This commit is contained in:
lain 2021-01-05 13:10:14 +01:00
parent f0e6cff583
commit e802b48d55
2 changed files with 10 additions and 13 deletions

View File

@ -51,7 +51,6 @@ defmodule Pleroma.User do
# credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
@url_regex ~r/^https?:\/\/[^\s]{1,256}$/
@strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
@extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
@ -143,7 +142,7 @@ defmodule Pleroma.User do
field(:allow_following_move, :boolean, default: true)
field(:skip_thread_containment, :boolean, default: false)
field(:actor_type, :string, default: "Person")
field(:also_known_as, {:array, :string}, default: [])
field(:also_known_as, {:array, ObjectValidators.ObjectID}, default: [])
field(:inbox, :string)
field(:shared_inbox, :string)
field(:accepts_chat_messages, :boolean, default: nil)
@ -530,7 +529,6 @@ def update_changeset(struct, params \\ %{}) do
)
|> unique_constraint(:nickname)
|> validate_format(:nickname, local_nickname_regex())
|> validate_also_known_as()
|> validate_length(:bio, max: bio_limit)
|> validate_length(:name, min: 1, max: name_limit)
|> validate_inclusion(:actor_type, ["Person", "Service"])
@ -2456,16 +2454,6 @@ def sanitize_html(%User{} = user, filter) do
|> Map.put(:fields, fields)
end
defp validate_also_known_as(changeset) do
validate_change(changeset, :also_known_as, fn :also_known_as, also_known_as ->
if Enum.all?(also_known_as, fn a -> Regex.match?(@url_regex, a) end) do
[]
else
[also_known_as: "Invalid ap_id format. Must be a URL."]
end
end)
end
def get_host(%User{ap_id: ap_id} = _user) do
URI.parse(ap_id).host
end

View File

@ -228,6 +228,15 @@ test "updates the user's AKAs", %{conn: conn} do
assert user_data["pleroma"]["also_known_as"] == ["https://mushroom.kingdom/users/mario"]
end
test "doesn't update non-url akas", %{conn: conn} do
conn =
patch(conn, "/api/v1/accounts/update_credentials", %{
"also_known_as" => ["aReallyCoolGuy"]
})
assert json_response_and_validate_schema(conn, 403)
end
test "updates the user's avatar", %{user: user, conn: conn} do
new_avatar = %Plug.Upload{
content_type: "image/jpeg",