2018-12-23 13:11:29 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-11-26 12:22:56 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:11:29 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-04-02 05:13:14 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.HTTPSignaturePlugTest do
|
2022-11-26 12:22:56 -07:00
|
|
|
use Pleroma.Web.ConnCase, async: false
|
2023-08-04 05:50:50 -06:00
|
|
|
@moduletag :mocked
|
2022-08-05 13:31:32 -06:00
|
|
|
import Pleroma.Factory
|
2018-04-02 05:13:14 -06:00
|
|
|
alias Pleroma.Web.Plugs.HTTPSignaturePlug
|
2022-11-26 12:22:56 -07:00
|
|
|
alias Pleroma.Instances.Instance
|
|
|
|
alias Pleroma.Repo
|
2018-04-02 05:13:14 -06:00
|
|
|
|
|
|
|
import Plug.Conn
|
2019-12-19 06:17:18 -07:00
|
|
|
import Phoenix.Controller, only: [put_format: 2]
|
2018-04-02 05:13:14 -06:00
|
|
|
import Mock
|
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
setup_with_mocks([
|
|
|
|
{HTTPSignatures, [],
|
|
|
|
[
|
|
|
|
signature_for_conn: fn _ ->
|
2024-06-12 11:40:44 -06:00
|
|
|
%{
|
|
|
|
"keyId" => "http://mastodon.example.org/users/admin#main-key",
|
|
|
|
"created" => "1234567890",
|
|
|
|
}
|
2022-11-26 12:22:56 -07:00
|
|
|
end,
|
|
|
|
validate_conn: fn conn ->
|
|
|
|
Map.get(conn.assigns, :valid_signature, true)
|
|
|
|
end
|
|
|
|
]}
|
|
|
|
]) do
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp submit_to_plug(host), do: submit_to_plug(host, :get, "/doesntmattter")
|
|
|
|
|
|
|
|
defp submit_to_plug(host, method, path) do
|
|
|
|
params = %{"actor" => "http://#{host}/users/admin"}
|
|
|
|
|
|
|
|
build_conn(method, path, params)
|
|
|
|
|> put_req_header(
|
|
|
|
"signature",
|
|
|
|
"keyId=\"http://#{host}/users/admin#main-key"
|
|
|
|
)
|
|
|
|
|> put_format("activity+json")
|
|
|
|
|> HTTPSignaturePlug.call(%{})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it call HTTPSignatures to check validity if the actor signed it" do
|
2018-04-02 05:13:14 -06:00
|
|
|
params = %{"actor" => "http://mastodon.example.org/users/admin"}
|
|
|
|
conn = build_conn(:get, "/doesntmattter", params)
|
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header(
|
|
|
|
"signature",
|
|
|
|
"keyId=\"http://mastodon.example.org/users/admin#main-key"
|
|
|
|
)
|
|
|
|
|> put_format("activity+json")
|
|
|
|
|> HTTPSignaturePlug.call(%{})
|
2018-04-02 05:13:14 -06:00
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
assert conn.assigns.valid_signature == true
|
|
|
|
assert conn.assigns.signature_actor_id == params["actor"]
|
|
|
|
assert conn.halted == false
|
|
|
|
assert called(HTTPSignatures.validate_conn(:_))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it sets request signatures property on the instance" do
|
|
|
|
host = "mastodon.example.org"
|
|
|
|
conn = submit_to_plug(host)
|
|
|
|
assert conn.assigns.valid_signature == true
|
|
|
|
instance = Repo.get_by(Instance, %{host: host})
|
|
|
|
assert instance.has_request_signatures
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not set request signatures property on the instance when using inbox" do
|
|
|
|
host = "mastodon.example.org"
|
|
|
|
conn = submit_to_plug(host, :post, "/inbox")
|
|
|
|
assert conn.assigns.valid_signature == true
|
|
|
|
|
|
|
|
# we don't even create the instance entry if its just POST /inbox
|
|
|
|
refute Repo.get_by(Instance, %{host: host})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not set request signatures property on the instance when its cached" do
|
|
|
|
host = "mastodon.example.org"
|
|
|
|
Cachex.put(:request_signatures_cache, host, true)
|
|
|
|
conn = submit_to_plug(host)
|
|
|
|
assert conn.assigns.valid_signature == true
|
|
|
|
|
|
|
|
# we don't even create the instance entry if it was already done
|
|
|
|
refute Repo.get_by(Instance, %{host: host})
|
2018-04-02 05:13:14 -06:00
|
|
|
end
|
2019-12-16 08:24:03 -07:00
|
|
|
|
2019-12-16 11:39:59 -07:00
|
|
|
describe "requires a signature when `authorized_fetch_mode` is enabled" do
|
2019-12-16 08:24:03 -07:00
|
|
|
setup do
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:activitypub, :authorized_fetch_mode], true)
|
2019-12-16 08:24:03 -07:00
|
|
|
|
|
|
|
params = %{"actor" => "http://mastodon.example.org/users/admin"}
|
2019-12-19 06:17:18 -07:00
|
|
|
conn = build_conn(:get, "/doesntmattter", params) |> put_format("activity+json")
|
2019-12-16 08:24:03 -07:00
|
|
|
|
|
|
|
[conn: conn]
|
|
|
|
end
|
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
test "and signature is present and incorrect", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
|
|
|
|> put_req_header(
|
|
|
|
"signature",
|
|
|
|
"keyId=\"http://mastodon.example.org/users/admin#main-key"
|
|
|
|
)
|
|
|
|
|> HTTPSignaturePlug.call(%{})
|
|
|
|
|
|
|
|
assert conn.assigns.valid_signature == false
|
|
|
|
assert called(HTTPSignatures.validate_conn(:_))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "and signature is correct", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header(
|
|
|
|
"signature",
|
|
|
|
"keyId=\"http://mastodon.example.org/users/admin#main-key"
|
|
|
|
)
|
|
|
|
|> HTTPSignaturePlug.call(%{})
|
|
|
|
|
|
|
|
assert conn.assigns.valid_signature == true
|
|
|
|
assert called(HTTPSignatures.validate_conn(:_))
|
2019-12-16 08:24:03 -07:00
|
|
|
end
|
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
test "and halts the connection when `signature` header is not present", %{conn: conn} do
|
2019-12-16 08:24:03 -07:00
|
|
|
conn = HTTPSignaturePlug.call(conn, %{})
|
|
|
|
assert conn.assigns[:valid_signature] == nil
|
|
|
|
end
|
2022-11-26 12:22:56 -07:00
|
|
|
end
|
2022-08-05 13:31:32 -06:00
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
test "aliases redirected /object endpoints", _ do
|
|
|
|
obj = insert(:note)
|
|
|
|
act = insert(:note_activity, note: obj)
|
|
|
|
params = %{"actor" => "someparam"}
|
|
|
|
path = URI.parse(obj.data["id"]).path
|
|
|
|
conn = build_conn(:get, path, params)
|
2022-08-27 12:05:48 -06:00
|
|
|
|
2022-11-26 12:22:56 -07:00
|
|
|
assert ["/notice/#{act.id}", "/notice/#{act.id}?actor=someparam"] ==
|
|
|
|
HTTPSignaturePlug.route_aliases(conn)
|
2019-12-16 08:24:03 -07:00
|
|
|
end
|
2024-06-12 11:40:44 -06:00
|
|
|
|
|
|
|
test "(created) psudoheader", _ do
|
|
|
|
conn = build_conn(:get, "/doesntmattter")
|
|
|
|
conn = HTTPSignaturePlug.maybe_put_created_psudoheader(conn)
|
|
|
|
created_header = List.keyfind(conn.req_headers, "(created)", 0)
|
|
|
|
assert {_, "1234567890"} = created_header
|
|
|
|
end
|
2018-04-02 05:13:14 -06:00
|
|
|
end
|