Merge branch 'permissive-webfinger' into 'develop'

WebFinger query default to return JSON when no or unrecognized accept header is provided.

See merge request pleroma/pleroma!4345
This commit is contained in:
Moon Man 2025-03-20 16:51:53 +00:00
commit 2d2445d82e
3 changed files with 51 additions and 9 deletions

View file

@ -0,0 +1 @@
Don't require an Accept header for WebFinger queries and default to JSON.

View file

@ -41,5 +41,17 @@ defmodule Pleroma.Web.WebFinger.WebFingerController do
end
end
# Default to JSON when no format is specified or format is not recognized
def webfinger(%{assigns: %{format: _format}} = conn, %{"resource" => resource}) do
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
json(conn, response)
else
_e ->
conn
|> put_status(404)
|> json("Couldn't find user")
end
end
def webfinger(conn, _params), do: send_resp(conn, 400, "Bad Request")
end

View file

@ -55,6 +55,26 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
]
end
test "Webfinger defaults to JSON when no Accept header is provided" do
user =
insert(:user,
ap_id: "https://hyrule.world/users/zelda",
also_known_as: ["https://mushroom.kingdom/users/toad"]
)
response =
build_conn()
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
|> json_response(200)
assert response["subject"] == "acct:#{user.nickname}@localhost"
assert response["aliases"] == [
"https://hyrule.world/users/zelda",
"https://mushroom.kingdom/users/toad"
]
end
test "reach user on tld, while pleroma is running on subdomain" do
clear_config([Pleroma.Web.Endpoint, :url, :host], "sub.example.com")
@ -109,16 +129,25 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
assert result == "Couldn't find user"
end
test "Sends a 404 when invalid format" do
user = insert(:user)
test "Returns JSON when format is not supported" do
user =
insert(:user,
ap_id: "https://hyrule.world/users/zelda",
also_known_as: ["https://mushroom.kingdom/users/toad"]
)
assert capture_log(fn ->
assert_raise Phoenix.NotAcceptableError, fn ->
build_conn()
|> put_req_header("accept", "text/html")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
end
end) =~ "no supported media type in accept header"
response =
build_conn()
|> put_req_header("accept", "text/html")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
|> json_response(200)
assert response["subject"] == "acct:#{user.nickname}@localhost"
assert response["aliases"] == [
"https://hyrule.world/users/zelda",
"https://mushroom.kingdom/users/toad"
]
end
test "Sends a 400 when resource param is missing" do