Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85951243
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
33 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/changelog.d/current_token.add b/changelog.d/current_token.add
new file mode 100644
index 000000000..f8004ef7b
--- /dev/null
+++ b/changelog.d/current_token.add
@@ -0,0 +1 @@
+OAuth token list API now includes property to signify which token is being used to access the API
diff --git a/changelog.d/idempotency-in-flight.fix b/changelog.d/idempotency-in-flight.fix
new file mode 100644
index 000000000..1c267748e
--- /dev/null
+++ b/changelog.d/idempotency-in-flight.fix
@@ -0,0 +1 @@
+Reject requests replaying an `Idempotency-Key` while the original request is still in progress, instead of executing them again and creating duplicates
diff --git a/changelog.d/token-activity.add b/changelog.d/token-activity.add
new file mode 100644
index 000000000..258f283eb
--- /dev/null
+++ b/changelog.d/token-activity.add
@@ -0,0 +1 @@
+Added "last active at" field to OAuth token management
diff --git a/lib/pleroma/web/o_auth/token.ex b/lib/pleroma/web/o_auth/token.ex
index d96425094..50dd399ca 100644
--- a/lib/pleroma/web/o_auth/token.ex
+++ b/lib/pleroma/web/o_auth/token.ex
@@ -1,145 +1,153 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.Token do
use Ecto.Schema
import Ecto.Changeset
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Authorization
alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.OAuth.Token.Query
@type t :: %__MODULE__{}
schema "oauth_tokens" do
field(:token, :string)
field(:refresh_token, :string)
field(:scopes, {:array, :string}, default: [])
field(:valid_until, :naive_datetime_usec)
+ field(:last_active, :naive_datetime_usec)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:app, App)
timestamps()
end
def lifespan do
Pleroma.Config.get!([:oauth2, :token_expires_in])
end
@doc "Gets token by unique access token"
@spec get_by_token(String.t()) :: {:ok, t()} | {:error, :not_found}
def get_by_token(token) do
token
|> Query.get_by_token()
|> Repo.find_resource()
end
@doc "Gets token for app by access token"
@spec get_by_token(App.t(), String.t()) :: {:ok, t()} | {:error, :not_found}
def get_by_token(%App{id: app_id} = _app, token) do
Query.get_by_app(app_id)
|> Query.get_by_token(token)
|> Repo.find_resource()
end
@doc "Gets token for app by refresh token"
@spec get_by_refresh_token(App.t(), String.t()) :: {:ok, t()} | {:error, :not_found}
def get_by_refresh_token(%App{id: app_id} = _app, token) do
Query.get_by_app(app_id)
|> Query.get_by_refresh_token(token)
|> Query.preload([:user])
|> Repo.find_resource()
end
@spec exchange_token(App.t(), Authorization.t()) ::
{:ok, Token.t()} | {:error, Ecto.Changeset.t()}
def exchange_token(app, auth) do
with {:ok, auth} <- Authorization.use_token(auth),
true <- auth.app_id == app.id do
user = if auth.user_id, do: User.get_cached_by_id(auth.user_id), else: %User{}
create(
app,
user,
%{scopes: auth.scopes}
)
end
end
defp put_token(changeset) do
changeset
|> change(%{token: Token.Utils.generate_token()})
|> validate_required([:token])
|> unique_constraint(:token)
end
defp put_refresh_token(changeset, attrs) do
refresh_token = Map.get(attrs, :refresh_token, Token.Utils.generate_token())
changeset
|> change(%{refresh_token: refresh_token})
|> validate_required([:refresh_token])
|> unique_constraint(:refresh_token)
end
defp put_valid_until(changeset, attrs) do
valid_until =
Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan()))
changeset
|> change(%{valid_until: valid_until})
|> validate_required([:valid_until])
end
+ @spec update_activity(Token.t()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
+ def update_activity(token) do
+ token
+ |> Ecto.Changeset.change(last_active: NaiveDateTime.utc_now())
+ |> Repo.update()
+ end
+
@spec create(App.t(), User.t(), map()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
def create(%App{} = app, %User{} = user, attrs \\ %{}) do
with {:ok, token} <- do_create(app, user, attrs) do
if Pleroma.Config.get([:oauth2, :clean_expired_tokens]) do
Pleroma.Workers.PurgeExpiredToken.new(%{token_id: token.id, mod: __MODULE__},
scheduled_at: DateTime.from_naive!(token.valid_until, "Etc/UTC")
)
|> Oban.insert()
end
{:ok, token}
end
end
defp do_create(app, user, attrs) do
%__MODULE__{user_id: user.id, app_id: app.id}
|> cast(%{scopes: attrs[:scopes] || app.scopes}, [:scopes])
|> validate_required([:scopes, :app_id])
|> put_valid_until(attrs)
|> put_token()
|> put_refresh_token(attrs)
|> Repo.insert()
end
def delete_user_tokens(%User{id: user_id}) do
Query.get_by_user(user_id)
|> Repo.delete_all()
end
def delete_user_token(%User{id: user_id}, token_id) do
Query.get_by_user(user_id)
|> Query.get_by_id(token_id)
|> Repo.delete_all()
end
def get_user_tokens(%User{id: user_id}) do
Query.get_by_user(user_id)
|> Query.preload([:app])
|> Repo.all()
end
def expired?(%__MODULE__{valid_until: valid_until}) do
NaiveDateTime.diff(NaiveDateTime.utc_now(), valid_until) > 0
end
def expired?(_), do: false
end
diff --git a/lib/pleroma/web/o_auth/token_controller.ex b/lib/pleroma/web/o_auth/token_controller.ex
index 296b60510..7df6b875b 100644
--- a/lib/pleroma/web/o_auth/token_controller.ex
+++ b/lib/pleroma/web/o_auth/token_controller.ex
@@ -1,60 +1,60 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.TokenController do
use Pleroma.Web, :controller
alias Pleroma.User
alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.OAuth.TokenView
alias Pleroma.Web.Plugs.OAuthScopesPlug
require Logger
plug(:skip_auth when action == :confirm_email)
plug(OAuthScopesPlug, %{scopes: ["read:security"]} when action == :oauth_tokens)
plug(OAuthScopesPlug, %{scopes: ["write:security"]} when action == :revoke_token)
action_fallback(:errors)
def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
with %User{} = user <- User.get_cached_by_id(uid),
true <- user.local and !user.is_confirmed and user.confirmation_token == token,
{:ok, _} <- User.confirm(user) do
redirect(conn, to: "/")
end
end
- def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
+ def oauth_tokens(%{assigns: %{user: user, token: current_token}} = conn, _params) do
with oauth_tokens <- Token.get_user_tokens(user) do
conn
|> put_view(TokenView)
- |> render("index.json", %{tokens: oauth_tokens})
+ |> render("index.json", %{tokens: oauth_tokens, current_token: current_token})
end
end
def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
Token.delete_user_token(user, id)
json_reply(conn, 201, "")
end
defp errors(conn, {:param_cast, _}) do
conn
|> put_status(400)
|> json("Invalid parameters")
end
defp errors(conn, _) do
conn
|> put_status(500)
|> json("Something went wrong")
end
defp json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")
|> send_resp(status, json)
end
end
diff --git a/lib/pleroma/web/o_auth/token_view.ex b/lib/pleroma/web/o_auth/token_view.ex
index f9894399a..09e0a18bc 100644
--- a/lib/pleroma/web/o_auth/token_view.ex
+++ b/lib/pleroma/web/o_auth/token_view.ex
@@ -1,22 +1,26 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.TokenView do
use Pleroma.Web, :view
- def render("index.json", %{tokens: tokens}) do
+ def render("index.json", %{tokens: tokens, current_token: current_token}) do
tokens
- |> render_many(Pleroma.Web.OAuth.TokenView, "show.json")
+ |> render_many(Pleroma.Web.OAuth.TokenView, "show.json", %{
+ current_token: current_token
+ })
|> Enum.filter(&Enum.any?/1)
end
- def render("show.json", %{token: token_entry}) do
+ def render("show.json", %{token: token_entry, current_token: current_token}) do
%{
id: token_entry.id,
valid_until: token_entry.valid_until,
+ last_active: token_entry.last_active,
app_name: token_entry.app.client_name,
- scopes: token_entry.scopes
+ scopes: token_entry.scopes,
+ is_current_token: token_entry.id == current_token.id
}
end
end
diff --git a/lib/pleroma/web/plugs/idempotency_plug.ex b/lib/pleroma/web/plugs/idempotency_plug.ex
index 1cb328622..4add0e28d 100644
--- a/lib/pleroma/web/plugs/idempotency_plug.ex
+++ b/lib/pleroma/web/plugs/idempotency_plug.ex
@@ -1,120 +1,148 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.IdempotencyPlug do
import Phoenix.Controller, only: [json: 2]
import Plug.Conn
alias Pleroma.Config
@behaviour Plug
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
+ # How long a claim placed on a key survives without the owning request
+ # completing. Should comfortably exceed the longest expected request; it only
+ # bounds how long a key stays blocked when the owner dies before sending a
+ # response (in which case no `before_send` callback ever runs).
+ @claim_ttl :timer.seconds(60)
+
+ @in_progress :in_progress
+
@impl true
def init(opts), do: opts
# Sending idempotency keys in `GET` and `DELETE` requests has no effect
# and should be avoided, as these requests are idempotent by definition.
@impl true
def call(%{method: method} = conn, _) when method in ["POST", "PUT", "PATCH"] do
case get_req_header(conn, "idempotency-key") do
[key] -> process_request(conn, key)
_ -> conn
end
end
def call(conn, _), do: conn
def process_request(conn, key) do
cache_key = {conn.method, conn.request_path, actor_key(conn), key}
- case @cachex.get(:idempotency_cache, cache_key) do
- {:ok, nil} ->
+ # Read and claim under the same lock so a retry cannot overwrite a
+ # claim or response written after a cache miss.
+ case @cachex.get_and_update(:idempotency_cache, cache_key, fn
+ nil -> {:commit, @in_progress, ttl: @claim_ttl}
+ existing -> {:ignore, existing}
+ end) do
+ {:commit, @in_progress, _options} ->
cache_response(conn, key, cache_key)
- {:ok, record} ->
+ {:ignore, @in_progress} ->
+ render_in_progress(conn, key)
+
+ {:ignore, record} ->
send_cached(conn, key, record)
- {atom, message} when atom in [:ignore, :error] ->
+ {:error, message} ->
render_error(conn, message)
end
end
defp cache_response(conn, key, cache_key) do
register_before_send(conn, &maybe_cache_response(&1, key, cache_key))
end
defp maybe_cache_response(
%{private: %{skip_idempotency_cache: true}} = conn,
_key,
- _cache_key
- ),
- do: conn
+ cache_key
+ ) do
+ # Sensitive responses are not cached; release the claim so that the key
+ # is not reported as in progress until the claim expires.
+ {:ok, _} = @cachex.del(:idempotency_cache, cache_key)
+ conn
+ end
defp maybe_cache_response(conn, key, cache_key) do
[request_id] = get_resp_header(conn, "x-request-id")
content_type = get_content_type(conn)
record = {request_id, content_type, conn.status, conn.resp_body}
{:ok, _} = @cachex.put(:idempotency_cache, cache_key, record)
conn
|> put_resp_header("idempotency-key", key)
|> put_resp_header("x-original-request-id", request_id)
end
defp actor_key(%{assigns: assigns}) do
{principal_key(assigns), token_key(assigns[:token]), staff_privileges()}
end
defp principal_key(%{user: %{id: id} = user}) do
{:user, id, Map.get(user, :is_admin), Map.get(user, :is_moderator)}
end
defp principal_key(%{app: %{id: id}}), do: {:app, id}
defp principal_key(_assigns), do: nil
defp token_key(%{id: id} = token), do: {id, token |> Map.get(:scopes, []) |> Enum.sort()}
defp token_key(_token), do: nil
defp staff_privileges do
{
Config.get([:instance, :admin_privileges], []) |> Enum.sort(),
Config.get([:instance, :moderator_privileges], []) |> Enum.sort()
}
end
defp send_cached(conn, key, record) do
{request_id, content_type, status, body} = record
conn
|> put_resp_header("idempotency-key", key)
|> put_resp_header("idempotent-replayed", "true")
|> put_resp_header("x-original-request-id", request_id)
|> put_resp_content_type(content_type)
|> send_resp(status, body)
|> halt()
end
+ defp render_in_progress(conn, key) do
+ conn
+ |> put_resp_header("idempotency-key", key)
+ |> put_status(:conflict)
+ |> json(%{error: "A request with this idempotency key is already in progress"})
+ |> halt()
+ end
+
defp render_error(conn, message) do
conn
|> put_status(:unprocessable_entity)
|> json(%{error: message})
|> halt()
end
defp get_content_type(conn) do
[content_type] = get_resp_header(conn, "content-type")
if String.contains?(content_type, ";") do
content_type
|> String.split(";")
|> hd()
else
content_type
end
end
end
diff --git a/lib/pleroma/web/plugs/o_auth_plug.ex b/lib/pleroma/web/plugs/o_auth_plug.ex
index b59ac9d3e..b63a20068 100644
--- a/lib/pleroma/web/plugs/o_auth_plug.ex
+++ b/lib/pleroma/web/plugs/o_auth_plug.ex
@@ -1,109 +1,133 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.OAuthPlug do
@moduledoc "Performs OAuth authentication by token from params / headers / cookies."
import Plug.Conn
import Ecto.Query
alias Pleroma.Helpers.AuthHelper
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Token
@realm_reg Regex.compile!("Bearer\:?\s+(.*)$", "i")
+ @update_interval :timer.hours(1)
+
def init(options), do: options
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
def call(conn, _) do
with {:ok, token_str} <- fetch_token_str(conn) do
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
false <- Token.expired?(user_token) do
- conn
- |> assign(:token, user_token)
- |> assign(:user, user)
+ with true <- needs_update?(user_token),
+ {:ok, updated_token} <- Token.update_activity(user_token) do
+ conn
+ |> assign(:token, updated_token)
+ |> assign(:user, user)
+ else
+ _ ->
+ conn
+ |> assign(:token, user_token)
+ |> assign(:user, user)
+ end
else
_ ->
with {:ok, app, app_token} <- fetch_app_and_token(token_str),
false <- Token.expired?(app_token) do
- conn
- |> assign(:token, app_token)
- |> assign(:app, app)
+ with true <- needs_update?(app_token),
+ {:ok, updated_token} <- Token.update_activity(app_token) do
+ conn
+ |> assign(:token, updated_token)
+ |> assign(:app, app)
+ else
+ _ ->
+ conn
+ |> assign(:token, app_token)
+ |> assign(:app, app)
+ end
else
_ -> conn
end
end
else
_ -> conn
end
end
+ defp needs_update?(%Token{last_active: nil}), do: true
+
+ defp needs_update?(%Token{last_active: last_active}) do
+ NaiveDateTime.diff(NaiveDateTime.utc_now(), last_active, :millisecond) >= @update_interval
+ end
+
# Gets user by token
#
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
defp fetch_user_and_token(token) do
token_query =
from(t in Token,
where: t.token == ^token
)
with %Token{user_id: user_id} = token_record <- Repo.one(token_query),
false <- is_nil(user_id),
%User{} = user <- User.get_cached_by_id(user_id) do
{:ok, user, token_record}
else
_ -> nil
end
end
@spec fetch_app_and_token(String.t()) :: {:ok, App.t(), Token.t()} | nil
defp fetch_app_and_token(token) do
query =
from(t in Token, where: t.token == ^token, join: app in assoc(t, :app), preload: [app: app])
with %Token{app: app} = token_record <- Repo.one(query) do
{:ok, app, token_record}
end
end
# Gets token string from conn (in params / headers / session)
#
@spec fetch_token_str(Plug.Conn.t() | list(String.t())) :: :no_token_found | {:ok, String.t()}
defp fetch_token_str(%Plug.Conn{params: %{"access_token" => access_token}} = _conn) do
{:ok, access_token}
end
defp fetch_token_str(%Plug.Conn{} = conn) do
headers = get_req_header(conn, "authorization")
with {:ok, token} <- fetch_token_str(headers) do
{:ok, token}
else
_ -> fetch_token_from_session(conn)
end
end
defp fetch_token_str([token | tail]) do
trimmed_token = String.trim(token)
case Regex.run(@realm_reg, trimmed_token) do
[_, match] -> {:ok, String.trim(match)}
_ -> fetch_token_str(tail)
end
end
defp fetch_token_str([]), do: :no_token_found
@spec fetch_token_from_session(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
defp fetch_token_from_session(conn) do
case AuthHelper.get_session_token(conn) do
nil -> :no_token_found
token -> {:ok, token}
end
end
end
diff --git a/priv/repo/migrations/20261106185600_add_token_activity.exs b/priv/repo/migrations/20261106185600_add_token_activity.exs
new file mode 100644
index 000000000..98f4b0184
--- /dev/null
+++ b/priv/repo/migrations/20261106185600_add_token_activity.exs
@@ -0,0 +1,13 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.AddTokenActivity do
+ use Ecto.Migration
+
+ def change do
+ alter table(:oauth_tokens) do
+ add_if_not_exists(:last_active, :naive_datetime_usec)
+ end
+ end
+end
diff --git a/test/pleroma/web/o_auth/token_controller_test.exs b/test/pleroma/web/o_auth/token_controller_test.exs
index 08acf27bf..6b2b0aeec 100644
--- a/test/pleroma/web/o_auth/token_controller_test.exs
+++ b/test/pleroma/web/o_auth/token_controller_test.exs
@@ -1,111 +1,133 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.TokenControllerTest do
use Pleroma.Web.ConnCase, async: true
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.OAuth.Token
import Pleroma.Factory
describe "GET /api/account/confirm_email/:id/:token" do
setup do
{:ok, user} =
insert(:user)
|> User.confirmation_changeset(set_confirmation: false)
|> Repo.update()
refute user.is_confirmed
[user: user]
end
test "it redirects to root url", %{conn: conn, user: user} do
conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
assert 302 == conn.status
end
test "it confirms the user account", %{conn: conn, user: user} do
get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}")
user = User.get_cached_by_id(user.id)
assert user.is_confirmed
refute user.confirmation_token
end
test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do
conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}")
assert 500 == conn.status
end
test "it returns 500 if token is invalid", %{conn: conn, user: user} do
conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token")
assert 500 == conn.status
end
end
describe "GET /api/oauth_tokens" do
test "renders list" do
%{conn: conn} = oauth_access(["read:security"])
keys =
conn
|> get("/api/oauth_tokens")
|> json_response(200)
|> hd()
|> Map.keys()
- assert Enum.sort(keys) == Enum.sort(["id", "app_name", "valid_until", "scopes"])
+ assert Enum.sort(keys) ==
+ Enum.sort([
+ "id",
+ "app_name",
+ "valid_until",
+ "scopes",
+ "is_current_token",
+ "last_active"
+ ])
+ end
+
+ test "marks which token is being used for the request" do
+ %{conn: conn, user: user, token: current} = oauth_access(["read:security"])
+ other = insert(:oauth_token, user: user)
+
+ tokens =
+ conn
+ |> get("/api/oauth_tokens")
+ |> json_response(200)
+ |> Map.new(&{&1["id"], &1["is_current_token"]})
+
+ assert tokens[current.id] == true
+ assert tokens[other.id] == false
end
test "is allowed with the read scope" do
%{conn: conn} = oauth_access(["read"])
assert [_] = conn |> get("/api/oauth_tokens") |> json_response(200)
end
test "is forbidden without read:security" do
%{conn: conn} = oauth_access(["profile", "read:accounts", "write:security"])
assert %{"error" => "Insufficient permissions: read:security."} =
conn |> get("/api/oauth_tokens") |> json_response(403)
end
end
describe "DELETE /api/oauth_tokens/:id" do
test "revokes the token" do
%{conn: conn, user: user, token: token} = oauth_access(["write:security"])
other = insert(:oauth_token, user: user)
conn = delete(conn, "/api/oauth_tokens/#{other.id}")
assert conn.status == 201
assert [%Token{id: id}] = Token.get_user_tokens(user)
assert id == token.id
end
test "is allowed with the write scope" do
%{conn: conn, user: user} = oauth_access(["write"])
other = insert(:oauth_token, user: user)
assert delete(conn, "/api/oauth_tokens/#{other.id}").status == 201
end
test "is forbidden without write:security" do
%{conn: conn, user: user} = oauth_access(["profile", "read", "write:accounts"])
other = insert(:oauth_token, user: user)
assert %{"error" => "Insufficient permissions: write:security."} =
conn |> delete("/api/oauth_tokens/#{other.id}") |> json_response(403)
assert length(Token.get_user_tokens(user)) == 2
end
end
end
diff --git a/test/pleroma/web/plugs/idempotency_plug_test.exs b/test/pleroma/web/plugs/idempotency_plug_test.exs
index cc7580ca2..cb25cbce5 100644
--- a/test/pleroma/web/plugs/idempotency_plug_test.exs
+++ b/test/pleroma/web/plugs/idempotency_plug_test.exs
@@ -1,245 +1,312 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.IdempotencyPlugTest do
# Relies on Cachex, has to stay synchronous
use Pleroma.DataCase
import Plug.Conn
import Plug.Test
alias Pleroma.Web.Plugs.IdempotencyPlug
alias Plug.Conn
test "returns result from cache" do
key = "test1"
orig_request_id = "test1"
second_request_id = "test2"
body = "testing"
status = 200
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", orig_request_id)
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(status, body)
conn =
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", second_request_id)
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
assert_raise Conn.AlreadySentError, fn ->
Conn.send_resp(conn, :im_a_teapot, "no cofe")
end
assert conn.resp_body == body
assert conn.status == status
assert [^second_request_id] = Conn.get_resp_header(conn, "x-request-id")
assert [^orig_request_id] = Conn.get_resp_header(conn, "x-original-request-id")
assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
assert ["true"] = Conn.get_resp_header(conn, "idempotent-replayed")
assert ["application/json; charset=utf-8"] = Conn.get_resp_header(conn, "content-type")
end
test "pass conn downstream if the cache not found" do
key = "test2"
orig_request_id = "test3"
body = "testing"
status = 200
conn =
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", orig_request_id)
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(status, body)
assert conn.resp_body == body
assert conn.status == status
assert [] = Conn.get_resp_header(conn, "idempotent-replayed")
assert [^key] = Conn.get_resp_header(conn, "idempotency-key")
end
test "does not cache responses marked as sensitive" do
key = Ecto.UUID.generate()
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "sensitive-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.put_private(:skip_idempotency_cache, true)
|> Conn.send_resp(200, "secret")
conn =
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "second-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
refute conn.halted
refute conn.resp_body
end
test "does not replay legacy unscoped cache entries" do
key = Ecto.UUID.generate()
record = {"legacy-request", "application/json", 200, "secret"}
assert {:ok, true} = Cachex.put(:idempotency_cache, key, record)
conn =
:post
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "new-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
refute conn.halted
refute conn.resp_body
end
test "scopes cached responses to the route and authenticated actor" do
key = Ecto.UUID.generate()
:post
|> conn("/cofe")
|> Conn.assign(:user, %{id: "user-one", is_admin: false, is_moderator: false})
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "original-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(200, "one")
for {path, user} <- [
{"/tea", %{id: "user-one", is_admin: false, is_moderator: false}},
{"/cofe", %{id: "user-two", is_admin: false, is_moderator: false}},
{"/cofe", %{id: "user-one", is_admin: true, is_moderator: false}}
] do
conn =
:post
|> conn(path)
|> Conn.assign(:user, user)
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", Ecto.UUID.generate())
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
refute conn.halted
refute conn.resp_body
end
end
test "scopes cached responses to the OAuth application and token" do
key = Ecto.UUID.generate()
:post
|> conn("/cofe")
|> Conn.assign(:app, %{id: "app-one"})
|> Conn.assign(:token, %{id: "token-one", scopes: ["write"]})
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "original-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(200, "one")
for {app, token} <- [
{%{id: "app-two"}, %{id: "token-two", scopes: ["write"]}},
{%{id: "app-one"}, %{id: "token-two", scopes: ["write"]}},
{%{id: "app-one"}, %{id: "token-one", scopes: ["read"]}}
] do
conn =
:post
|> conn("/cofe")
|> Conn.assign(:app, app)
|> Conn.assign(:token, token)
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", Ecto.UUID.generate())
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
refute conn.halted
refute conn.resp_body
end
end
+ test "rejects a duplicate request while the original is still in flight" do
+ key = Ecto.UUID.generate()
+
+ # The first request passed the plug and is still being processed; its
+ # response — and with it the cache entry — does not exist yet.
+ conn_one =
+ :post
+ |> conn("/cofe")
+ |> put_req_header("idempotency-key", key)
+ |> Conn.put_resp_header("x-request-id", "original-request")
+ |> Conn.put_resp_content_type("application/json")
+ |> IdempotencyPlug.call([])
+
+ refute conn_one.halted
+
+ conn_two =
+ :post
+ |> conn("/cofe")
+ |> put_req_header("idempotency-key", key)
+ |> Conn.put_resp_header("x-request-id", "duplicate-request")
+ |> Conn.put_resp_content_type("application/json")
+ |> IdempotencyPlug.call([])
+
+ assert conn_two.halted
+ assert conn_two.status == 409
+
+ # Once the original response has been sent, the key replays it again
+ Conn.send_resp(conn_one, 200, "testing")
+
+ conn_three =
+ :post
+ |> conn("/cofe")
+ |> put_req_header("idempotency-key", key)
+ |> Conn.put_resp_header("x-request-id", "third-request")
+ |> Conn.put_resp_content_type("application/json")
+ |> IdempotencyPlug.call([])
+
+ assert conn_three.halted
+ assert conn_three.status == 200
+ assert conn_three.resp_body == "testing"
+ assert ["true"] = Conn.get_resp_header(conn_three, "idempotent-replayed")
+ assert ["original-request"] = Conn.get_resp_header(conn_three, "x-original-request-id")
+ end
+
+ test "admits exactly one of several concurrent requests with the same key" do
+ key = Ecto.UUID.generate()
+
+ conns =
+ 1..10
+ |> Task.async_stream(fn i ->
+ :post
+ |> conn("/cofe")
+ |> put_req_header("idempotency-key", key)
+ |> Conn.put_resp_header("x-request-id", "request-#{i}")
+ |> Conn.put_resp_content_type("application/json")
+ |> IdempotencyPlug.call([])
+ end)
+ |> Enum.map(fn {:ok, conn} -> conn end)
+
+ assert [winner] = Enum.reject(conns, & &1.halted)
+ refute winner.resp_body
+
+ losers = Enum.filter(conns, & &1.halted)
+ assert length(losers) == 9
+ assert Enum.all?(losers, &(&1.status == 409))
+ end
+
test "scopes cached responses to configured staff privileges" do
key = Ecto.UUID.generate()
clear_config([:instance, :admin_privileges], [:users_delete])
:post
|> conn("/cofe")
|> Conn.assign(:user, %{id: "admin", is_admin: true, is_moderator: false})
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "original-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(200, "one")
clear_config([:instance, :admin_privileges], [])
conn =
:post
|> conn("/cofe")
|> Conn.assign(:user, %{id: "admin", is_admin: true, is_moderator: false})
|> put_req_header("idempotency-key", key)
|> Conn.put_resp_header("x-request-id", "second-request")
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
refute conn.halted
refute conn.resp_body
end
test "passes conn downstream if idempotency is not present in headers" do
orig_request_id = "test4"
body = "testing"
status = 200
conn =
:post
|> conn("/cofe")
|> Conn.put_resp_header("x-request-id", orig_request_id)
|> Conn.put_resp_content_type("application/json")
|> IdempotencyPlug.call([])
|> Conn.send_resp(status, body)
assert [] = Conn.get_resp_header(conn, "idempotency-key")
end
test "doesn't work with GET/DELETE" do
key = "test3"
body = "testing"
status = 200
conn =
:get
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> IdempotencyPlug.call([])
|> Conn.send_resp(status, body)
assert [] = Conn.get_resp_header(conn, "idempotency-key")
conn =
:delete
|> conn("/cofe")
|> put_req_header("idempotency-key", key)
|> IdempotencyPlug.call([])
|> Conn.send_resp(status, body)
assert [] = Conn.get_resp_header(conn, "idempotency-key")
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Nov 19, 9:59 AM (1 d, 23 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1841108
Default Alt Text
(33 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline