mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2025-03-13 07:02:41 +00:00
Require HTTP signatures (if enabled) for routes used by both C2S and S2S AP API
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
8250a9764e
commit
ad953143bb
4 changed files with 60 additions and 4 deletions
changelog.d
lib/pleroma/web
test/pleroma/web/activity_pub
1
changelog.d/ensure-authorized-fetch.security
Normal file
1
changelog.d/ensure-authorized-fetch.security
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Require HTTP signatures (if enabled) for routes used by both C2S and S2S AP API
|
|
@ -19,9 +19,17 @@ defmodule Pleroma.Web.Plugs.HTTPSignaturePlug do
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
|
def call(%{assigns: %{valid_signature: true}} = conn, _opts), do: conn
|
||||||
|
|
||||||
|
# skip for C2S requests from authenticated users
|
||||||
|
def call(%{assigns: %{user: %Pleroma.User{}}} = conn, _opts) do
|
||||||
|
if get_format(conn) in ["json", "activity+json"] do
|
||||||
|
# ensure access token is provided for 2FA
|
||||||
|
Pleroma.Web.Plugs.EnsureAuthenticatedPlug.call(conn, %{})
|
||||||
|
else
|
||||||
conn
|
conn
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def call(conn, _opts) do
|
def call(conn, _opts) do
|
||||||
if get_format(conn) in ["json", "activity+json"] do
|
if get_format(conn) in ["json", "activity+json"] do
|
||||||
|
|
|
@ -907,17 +907,30 @@ defmodule Pleroma.Web.Router do
|
||||||
plug(:after_auth)
|
plug(:after_auth)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# AP interactions used by both S2S and C2S
|
||||||
|
pipeline :activitypub_server_or_client do
|
||||||
|
plug(:ap_service_actor)
|
||||||
|
plug(:fetch_session)
|
||||||
|
plug(:authenticate)
|
||||||
|
plug(:after_auth)
|
||||||
|
plug(:http_signature)
|
||||||
|
end
|
||||||
|
|
||||||
scope "/", Pleroma.Web.ActivityPub do
|
scope "/", Pleroma.Web.ActivityPub do
|
||||||
pipe_through([:activitypub_client])
|
pipe_through([:activitypub_client])
|
||||||
|
|
||||||
get("/api/ap/whoami", ActivityPubController, :whoami)
|
get("/api/ap/whoami", ActivityPubController, :whoami)
|
||||||
get("/users/:nickname/inbox", ActivityPubController, :read_inbox)
|
get("/users/:nickname/inbox", ActivityPubController, :read_inbox)
|
||||||
|
|
||||||
get("/users/:nickname/outbox", ActivityPubController, :outbox)
|
|
||||||
post("/users/:nickname/outbox", ActivityPubController, :update_outbox)
|
post("/users/:nickname/outbox", ActivityPubController, :update_outbox)
|
||||||
post("/api/ap/upload_media", ActivityPubController, :upload_media)
|
post("/api/ap/upload_media", ActivityPubController, :upload_media)
|
||||||
|
end
|
||||||
|
|
||||||
|
scope "/", Pleroma.Web.ActivityPub do
|
||||||
|
pipe_through([:activitypub_server_or_client])
|
||||||
|
|
||||||
|
get("/users/:nickname/outbox", ActivityPubController, :outbox)
|
||||||
|
|
||||||
# The following two are S2S as well, see `ActivityPub.fetch_follow_information_for_user/1`:
|
|
||||||
get("/users/:nickname/followers", ActivityPubController, :followers)
|
get("/users/:nickname/followers", ActivityPubController, :followers)
|
||||||
get("/users/:nickname/following", ActivityPubController, :following)
|
get("/users/:nickname/following", ActivityPubController, :following)
|
||||||
get("/users/:nickname/collections/featured", ActivityPubController, :pinned)
|
get("/users/:nickname/collections/featured", ActivityPubController, :pinned)
|
||||||
|
|
|
@ -1323,6 +1323,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /users/:nickname/outbox" do
|
describe "GET /users/:nickname/outbox" do
|
||||||
|
setup do
|
||||||
|
Mox.stub_with(Pleroma.StaticStubbedConfigMock, Pleroma.Config)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
test "it paginates correctly", %{conn: conn} do
|
test "it paginates correctly", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
conn = assign(conn, :user, user)
|
conn = assign(conn, :user, user)
|
||||||
|
@ -1462,6 +1467,35 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
||||||
assert [answer_outbox] = outbox_get["orderedItems"]
|
assert [answer_outbox] = outbox_get["orderedItems"]
|
||||||
assert answer_outbox["id"] == activity.data["id"]
|
assert answer_outbox["id"] == activity.data["id"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it works with authorized fetch forced when authenticated" do
|
||||||
|
clear_config([:activitypub, :authorized_fetch_mode], true)
|
||||||
|
|
||||||
|
user = insert(:user)
|
||||||
|
outbox_endpoint = user.ap_id <> "/outbox"
|
||||||
|
|
||||||
|
conn =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> put_req_header("accept", "application/activity+json")
|
||||||
|
|> get(outbox_endpoint)
|
||||||
|
|
||||||
|
assert json_response(conn, 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it fails with authorized fetch forced when unauthenticated", %{conn: conn} do
|
||||||
|
clear_config([:activitypub, :authorized_fetch_mode], true)
|
||||||
|
|
||||||
|
user = insert(:user)
|
||||||
|
outbox_endpoint = user.ap_id <> "/outbox"
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> put_req_header("accept", "application/activity+json")
|
||||||
|
|> get(outbox_endpoint)
|
||||||
|
|
||||||
|
assert response(conn, 401)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "POST /users/:nickname/outbox (C2S)" do
|
describe "POST /users/:nickname/outbox (C2S)" do
|
||||||
|
|
Loading…
Reference in a new issue