From f95e0b21c9c272124854a6ee9286fed8fc81deca Mon Sep 17 00:00:00 2001 From: LienThuan04 Date: Fri, 13 Nov 2026 15:57:47 +0700 Subject: [PATCH 1/3] reject empty kv keys --- lib/kv/interface.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kv/interface.ex b/lib/kv/interface.ex index 1a1a0f3..835209d 100644 --- a/lib/kv/interface.ex +++ b/lib/kv/interface.ex @@ -83,7 +83,7 @@ defmodule Lanyard.KV.Interface do {:error, "key must be #{@key_length_limit} characters or less"} - not String.match?(key, ~r/^[a-zA-Z0-9_]*$/) -> + not String.match?(key, ~r/^[a-zA-Z0-9_]+$/) -> Lanyard.Metrics.Collector.inc(:counter, :lanyard_kv_validation_failures_total, [ "key_invalid" ]) From 65e06e9dd0ef269ed7a862efdd0af1dfd8a5038c Mon Sep 17 00:00:00 2001 From: LienThuan04 Date: Fri, 13 Nov 2026 15:57:47 +0700 Subject: [PATCH 2/3] read kv request bodies after auth and answer 413 instead of crashing --- lib/api/routes/v1/users.ex | 70 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/api/routes/v1/users.ex b/lib/api/routes/v1/users.ex index ecea5d2..8f2a92c 100644 --- a/lib/api/routes/v1/users.ex +++ b/lib/api/routes/v1/users.ex @@ -31,47 +31,43 @@ defmodule Lanyard.Api.Routes.V1.Users do end patch "/:id/kv" do - {:ok, body, conn} = Plug.Conn.read_body(conn) + with {:ok, user_id} <- validate_resource_access(conn), + {:ok, body, conn} <- read_kv_body(conn) do + try do + {:ok, parsed} = Jason.decode(body) - case validate_resource_access(conn) do - {:ok, user_id} -> - try do - {:ok, parsed} = Jason.decode(body) - - case Lanyard.KV.Interface.multiset(user_id, parsed) do - {:ok} -> - Util.respond(conn, {:ok}) - - {:error, reason} -> - Util.respond(conn, {:error, :kv_validation_failed, reason}) - end - rescue - _e -> - Util.respond(conn, {:error, :invalid_kv_value, "body must be an object"}) - end + case Lanyard.KV.Interface.multiset(user_id, parsed) do + {:ok} -> + Util.respond(conn, {:ok}) - :no_permission -> - Util.no_permission(conn) + {:error, reason} -> + Util.respond(conn, {:error, :kv_validation_failed, reason}) + end + rescue + _e -> + Util.respond(conn, {:error, :invalid_kv_value, "body must be an object"}) + end + else + :no_permission -> Util.no_permission(conn) + {:error, conn, status, code, message} -> Util.respond(conn, {:error, status, code, message}) end end put "/:id/kv/:field" do %Plug.Conn{params: %{"field" => field}} = conn - {:ok, put_body, conn} = Plug.Conn.read_body(conn) - - case validate_resource_access(conn) do - {:ok, user_id} -> - case Lanyard.KV.Interface.set(String.to_integer(user_id), field, put_body) do - {:ok, _v} -> - Util.respond(conn, {:ok}) - - {:error, reason} -> - Util.respond(conn, {:error, :kv_validation_failed, reason}) - end + with {:ok, user_id} <- validate_resource_access(conn), + {:ok, put_body, conn} <- read_kv_body(conn) do + case Lanyard.KV.Interface.set(String.to_integer(user_id), field, put_body) do + {:ok, _v} -> + Util.respond(conn, {:ok}) - :no_permission -> - Util.no_permission(conn) + {:error, reason} -> + Util.respond(conn, {:error, :kv_validation_failed, reason}) + end + else + :no_permission -> Util.no_permission(conn) + {:error, conn, status, code, message} -> Util.respond(conn, {:error, status, code, message}) end end @@ -92,6 +88,16 @@ defmodule Lanyard.Api.Routes.V1.Users do Util.not_found(conn) end + # Called after authentication. Answers 413 instead of crashing on a body that does not fit in + # one read (Plug.Conn.read_body/2 defaults to 8 MB). + defp read_kv_body(conn) do + case Plug.Conn.read_body(conn) do + {:ok, body, conn} -> {:ok, body, conn} + {:more, _partial, conn} -> {:error, conn, 413, :body_too_large, "request body is too large"} + {:error, _reason} -> {:error, conn, 400, :invalid_body, "could not read the request body"} + end + end + defp validate_resource_access(conn) do %Plug.Conn{params: %{"id" => id}} = conn key = Util.authorization_header(conn) From ec4e7d4c144156dc7ad5beefc9b8804a47f1fdd9 Mon Sep 17 00:00:00 2001 From: LienThuan04 Date: Fri, 13 Nov 2026 20:20:34 +0700 Subject: [PATCH 3/3] handle kv body limits and read timeouts with regression tests --- lib/api/routes/v1/users.ex | 25 +++- test/api/routes/v1/users_test.exs | 196 ++++++++++++++++++++++++++++++ test/kv/interface_test.exs | 13 ++ test/test_helper.exs | 5 + 4 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 test/api/routes/v1/users_test.exs create mode 100644 test/kv/interface_test.exs create mode 100644 test/test_helper.exs diff --git a/lib/api/routes/v1/users.ex b/lib/api/routes/v1/users.ex index 8f2a92c..ed2dceb 100644 --- a/lib/api/routes/v1/users.ex +++ b/lib/api/routes/v1/users.ex @@ -8,6 +8,8 @@ defmodule Lanyard.Api.Routes.V1.Users do plug(:match) plug(:dispatch) + @kv_body_limit 8_000_000 + get "/@me" do key = Util.authorization_header(conn) @@ -88,13 +90,24 @@ defmodule Lanyard.Api.Routes.V1.Users do Util.not_found(conn) end - # Called after authentication. Answers 413 instead of crashing on a body that does not fit in - # one read (Plug.Conn.read_body/2 defaults to 8 MB). + # Read one byte beyond the limit to distinguish oversized bodies from incomplete reads. + # In particular, Bandit can return :more on an HTTP/2 read timeout below the limit. defp read_kv_body(conn) do - case Plug.Conn.read_body(conn) do - {:ok, body, conn} -> {:ok, body, conn} - {:more, _partial, conn} -> {:error, conn, 413, :body_too_large, "request body is too large"} - {:error, _reason} -> {:error, conn, 400, :invalid_body, "could not read the request body"} + case Plug.Conn.read_body(conn, length: @kv_body_limit + 1) do + {status, body, conn} when status in [:ok, :more] and byte_size(body) > @kv_body_limit -> + {:error, conn, 413, :body_too_large, "request body is too large"} + + {:ok, body, conn} -> + {:ok, body, conn} + + {:more, _partial, conn} -> + {:error, conn, 408, :request_timeout, "request body read timed out"} + + {:error, :timeout} -> + {:error, conn, 408, :request_timeout, "request body read timed out"} + + {:error, _reason} -> + {:error, conn, 400, :invalid_body, "could not read the request body"} end end diff --git a/test/api/routes/v1/users_test.exs b/test/api/routes/v1/users_test.exs new file mode 100644 index 0000000..55391a9 --- /dev/null +++ b/test/api/routes/v1/users_test.exs @@ -0,0 +1,196 @@ +defmodule Lanyard.Api.Routes.V1.UsersTest do + use ExUnit.Case, async: false + + import Plug.Conn + import Plug.Test + + alias Lanyard.Api.Routes.V1.Users + alias Lanyard.KV.Interface + alias Lanyard.Presence + + @user_id "123456789012345678" + @body_limit 8_000_000 + + # Only the external Redis process is replaced; routing, validation, and presence + # updates run through the production modules without a Discord connection. + defmodule RedisStub do + use GenServer + + def start_link(keys), do: GenServer.start_link(__MODULE__, keys, name: :local_redis_client) + + @impl true + def init(keys), do: {:ok, keys} + + @impl true + def handle_call({:get, "api_key:" <> key}, _from, keys), do: {:reply, {:ok, Map.get(keys, key)}, keys} + def handle_call({:hgetall, _key}, _from, keys), do: {:reply, {:ok, []}, keys} + + @impl true + def handle_cast(_message, keys), do: {:noreply, keys} + end + + defmodule BodyAdapter do + def read_req_body(%{read_result: result, owner: owner} = state, _opts) do + send(owner, :body_read) + + case result do + {status, body} when status in [:ok, :more] -> {status, body, state} + {:error, reason} -> {:error, reason} + end + end + + defdelegate send_resp(state, status, headers, body), to: Plug.Adapters.Test.Conn + end + + setup do + start_supervised!({RedisStub, %{"valid" => @user_id, "unmonitored" => "999"}}) + :ets.new(:cached_presences, [:named_table, :set, :public]) + :ets.new(:global_subscribers, [:named_table, :set, :public]) + :ets.new(Presence, [:named_table, :set, :public]) + + presence = + start_supervised!( + {Presence, %Presence{user_id: @user_id, discord_user: %{"id" => @user_id}, discord_presence: nil}} + ) + + :ets.insert(Presence, {@user_id, presence}) + + :ok + end + + for {method, suffix} <- [{:patch, ""}, {:put, "/field"}] do + describe "#{method} KV" do + @method method + @suffix suffix + + test "rejects missing authorization before reading the body" do + response = request(@method, @suffix, {:more, ""}, nil) + + assert response.status == 401 + refute_received :body_read + end + + test "rejects invalid authorization before reading the body" do + response = request(@method, @suffix, {:more, ""}, "invalid") + + assert response.status == 401 + refute_received :body_read + end + + test "rejects unmonitored users before reading the body" do + response = request(@method, @suffix, {:more, ""}, "unmonitored", "999") + + assert response.status == 401 + refute_received :body_read + end + + test "rejects a key belonging to a different user before reading the body" do + response = request(@method, @suffix, {:more, ""}, "valid", "999") + + assert response.status == 401 + refute_received :body_read + end + + test "returns 413 for an oversized partial body" do + response = request(@method, @suffix, {:more, String.duplicate("x", @body_limit + 1)}) + + assert_error(response, 413, "body_too_large") + end + + test "returns 413 for an oversized complete body" do + response = request(@method, @suffix, {:ok, String.duplicate("x", @body_limit + 1)}) + + assert_error(response, 413, "body_too_large") + end + + test "returns 408 for a partial body below the size limit" do + response = request(@method, @suffix, {:more, "small body"}) + + assert_error(response, 408, "request_timeout") + end + + test "returns 408 for a partial body at the size limit" do + response = request(@method, @suffix, {:more, String.duplicate("x", @body_limit)}) + + assert_error(response, 408, "request_timeout") + end + + test "returns 408 when the adapter reports a read timeout" do + response = request(@method, @suffix, {:error, :timeout}) + + assert_error(response, 408, "request_timeout") + end + + test "returns 400 when the adapter reports another read error" do + response = request(@method, @suffix, {:error, :closed}) + + assert_error(response, 400, "invalid_body") + end + + test "does not reject a complete body at the size limit as oversized" do + body = if @method == :patch, do: ~s({"field":"value"}), else: "value" + body = body <> String.duplicate(" ", @body_limit - byte_size(body)) + response = request(@method, @suffix, {:ok, body}) + + if @method == :patch do + assert response.status == 204 + assert Interface.get_all(@user_id) == %{"field" => "value"} + else + assert_error(response, 404, "kv_validation_failed") + end + end + + test "rejects an oversized body through Plug's test adapter" do + response = + conn(@method, "/#{@user_id}/kv#{@suffix}", String.duplicate("x", @body_limit + 1)) + |> put_req_header("authorization", "valid") + |> Users.call(Users.init([])) + + assert_error(response, 413, "body_too_large") + end + end + end + + test "PATCH rejects an empty KV key without changing the user's data" do + response = + conn(:patch, "/#{@user_id}/kv", ~s({"":"value"})) + |> put_req_header("authorization", "valid") + |> Users.call(Users.init([])) + + assert_error(response, 404, "kv_validation_failed") + assert Interface.get_all(@user_id) == %{} + end + + test "PATCH writes valid KV data through @me" do + response = + conn(:patch, "/@me/kv", ~s({"field":"value"})) + |> put_req_header("authorization", "valid") + |> Users.call(Users.init([])) + + assert response.status == 204 + assert Interface.get_all(@user_id) == %{"field" => "value"} + end + + test "PUT writes valid KV data" do + response = + conn(:put, "/#{@user_id}/kv/field", "value") + |> put_req_header("authorization", "valid") + |> Users.call(Users.init([])) + + assert response.status == 204 + assert Interface.get_all(@user_id) == %{"field" => "value"} + end + + defp request(method, suffix, result, key \\ "valid", user_id \\ @user_id) do + conn = conn(method, "/#{user_id}/kv#{suffix}", "") + {_, state} = conn.adapter + conn = %{conn | adapter: {BodyAdapter, Map.put(state, :read_result, result)}} + conn = if key, do: put_req_header(conn, "authorization", key), else: conn + Users.call(conn, Users.init([])) + end + + defp assert_error(response, status, code) do + assert response.status == status + assert %{"success" => false, "error" => %{"code" => ^code}} = Jason.decode!(response.resp_body) + end +end diff --git a/test/kv/interface_test.exs b/test/kv/interface_test.exs new file mode 100644 index 0000000..2aac805 --- /dev/null +++ b/test/kv/interface_test.exs @@ -0,0 +1,13 @@ +defmodule Lanyard.KV.InterfaceTest do + use ExUnit.Case, async: true + + alias Lanyard.KV.Interface + + test "rejects an empty key" do + assert {:error, "key must be alphanumeric (a-zA-Z0-9_)"} = Interface.validate_pair({"", "value"}) + end + + test "accepts letters, numbers and underscores" do + assert {:ok} = Interface.validate_pair({"valid_Key123", "value"}) + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..30655ab --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1,5 @@ +ExUnit.start() + +{:ok, _} = Application.ensure_all_started(:prometheus_ex) +{:ok, _} = Application.ensure_all_started(:manifold) +Lanyard.Metrics.Collector.start()