def call(%{assigns: %{valid_signature: true}, params: %{"actor" => actor}} = conn, _opts) do
with actor_id <- Utils.get_ap_id(actor),
{:user, %User{} = user} <- {:user, user_from_key_id(conn)},
+ {:federate, true} <- {:federate, should_federate?(user)},
{:user_match, true} <- {:user_match, user.ap_id == actor_id} do
conn
|> assign(:user, user)
{:user_match, false} ->
Logger.debug("Failed to map identity from signature (payload actor mismatch)")
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{inspect(actor)}")
- assign(conn, :valid_signature, false)
+
+ conn
+ |> assign(:valid_signature, false)
# remove me once testsuite uses mapped capabilities instead of what we do now
{:user, nil} ->
Logger.debug("Failed to map identity from signature (lookup failure)")
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{actor}")
+
+ conn
+ |> assign(:valid_signature, false)
+
+ {:federate, false} ->
+ Logger.debug("Identity from signature is instance blocked")
+ Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{actor}")
+
conn
+ |> assign(:valid_signature, false)
end
end
# no payload, probably a signed fetch
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
- with %User{} = user <- user_from_key_id(conn) do
+ with %User{} = user <- user_from_key_id(conn),
+ {:federate, true} <- {:federate, should_federate?(user)} do
conn
|> assign(:user, user)
|> AuthHelper.skip_oauth()
else
+ {:federate, false} ->
+ Logger.debug("Identity from signature is instance blocked")
+ Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
+
+ conn
+ |> assign(:valid_signature, false)
+
+ nil ->
+ Logger.debug("Failed to map identity from signature (lookup failure)")
+ Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
+
+ only_permit_user_routes(conn)
+
_ ->
Logger.debug("Failed to map identity from signature (no payload actor mismatch)")
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
- assign(conn, :valid_signature, false)
+
+ conn
+ |> assign(:valid_signature, false)
end
end
# no signature at all
def call(conn, _opts), do: conn
+ defp only_permit_user_routes(%{path_info: ["users", _]} = conn) do
+ conn
+ |> assign(:limited_ap, true)
+ end
+
+ defp only_permit_user_routes(conn) do
+ conn
+ |> assign(:valid_signature, false)
+ end
+
defp key_id_from_conn(conn) do
with %{"keyId" => key_id} <- HTTPSignatures.signature_for_conn(conn),
{:ok, ap_id} <- Signature.key_id_to_actor_id(key_id) do
nil
end
end
+
+ defp should_federate?(%User{ap_id: ap_id}), do: should_federate?(ap_id)
+
+ defp should_federate?(ap_id) do
+ if Pleroma.Config.get([:activitypub, :authorized_fetch_mode], false) do
+ Pleroma.Web.ActivityPub.Publisher.should_federate?(ap_id)
+ else
+ true
+ end
+ end
end
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header(
+ "signature",
+ "keyId=\"http://mastodon.example.org/users/admin/main-key\""
+ )
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{user.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
sender_url = data["actor"]
+ sender = insert(:user, ap_id: data["actor"])
+
Instances.set_consistently_unreachable(sender_url)
refute Instances.reachable?(sender_url)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{sender.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
assert "ok" ==
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{followed_relay.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", accept)
|> json_response(200)
actor = "https://example.com/users/lain"
+ insert(:user,
+ ap_id: actor,
+ featured_address: "https://example.com/users/lain/collections/featured"
+ )
+
Tesla.Mock.mock(fn
%{
method: :get,
assert "ok" ==
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
|> json_response(200)
ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
assert Activity.get_by_ap_id(data["id"])
user = User.get_cached_by_ap_id(data["actor"])
+
assert user.pinned_objects[data["object"]]
data = %{
assert "ok" ==
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
|> json_response(200)
actor = "https://example.com/users/lain"
+ sender =
+ insert(:user,
+ ap_id: actor,
+ featured_address: "https://example.com/users/lain/collections/featured"
+ )
+
Tesla.Mock.mock(fn
%{
method: :get,
assert "ok" ==
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{sender.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
|> json_response(200)
assert "ok" ==
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
|> json_response(200)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{announcer.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{recipient.nickname}/inbox", data)
conn =
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data)
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{recipient.nickname}/inbox", data)
|> json_response(200)
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{reported_user.nickname}/inbox", data)
|> json_response(200)
conn
|> assign(:valid_signature, true)
+ |> put_req_header("signature", "keyId=\"#{remote_actor}/main-key\"")
|> put_req_header("content-type", "application/activity+json")
|> post("/users/#{reported_user.nickname}/inbox", data)
|> json_response(200)