Skip to content

Commit 0572fb0

Browse files
authored
Add payload size limit (#1151)
* Add payload size limit * Improve code quality
1 parent f5f8890 commit 0572fb0

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

lib/cadet_web/controllers/chat_controller.ex

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule CadetWeb.ChatController do
66
use PhoenixSwagger
77

88
alias Cadet.Chatbot.{Conversation, LlmConversations}
9+
@max_content_size 1000
910

1011
def init_chat(conn, %{"section" => section, "initialContext" => initialContext}) do
1112
user = conn.assigns.current_user
@@ -21,7 +22,8 @@ defmodule CadetWeb.ChatController do
2122
"conversation_init.json",
2223
%{
2324
conversation_id: conversation.id,
24-
last_message: conversation.messages |> List.last()
25+
last_message: conversation.messages |> List.last(),
26+
max_content_size: @max_content_size
2527
}
2628
)
2729

@@ -51,13 +53,15 @@ defmodule CadetWeb.ChatController do
5153
response(200, "OK")
5254
response(400, "Missing or invalid parameter(s)")
5355
response(401, "Unauthorized")
56+
response(422, "Message exceeds the maximum allowed length")
5457
response(500, "When OpenAI API returns an error")
5558
end
5659

5760
def chat(conn, %{"conversationId" => conversation_id, "message" => user_message}) do
5861
user = conn.assigns.current_user
5962

60-
with {:ok, conversation} <-
63+
with true <- String.length(user_message) <= @max_content_size || {:error, :message_too_long},
64+
{:ok, conversation} <-
6165
LlmConversations.get_conversation_for_user(user.id, conversation_id),
6266
{:ok, updated_conversation} <-
6367
LlmConversations.add_message(conversation, "user", user_message),
@@ -85,6 +89,13 @@ defmodule CadetWeb.ChatController do
8589
send_resp(conn, 500, error_message)
8690
end
8791
else
92+
{:error, :message_too_long} ->
93+
send_resp(
94+
conn,
95+
:unprocessable_entity,
96+
"Message exceeds the maximum allowed length of #{@max_content_size}"
97+
)
98+
8899
{:error, {:not_found, error_message}} ->
89100
send_resp(conn, :not_found, error_message)
90101

@@ -107,4 +118,6 @@ defmodule CadetWeb.ChatController do
107118

108119
conversation.prepend_context ++ messages_payload
109120
end
121+
122+
def max_content_length, do: @max_content_size
110123
end

lib/cadet_web/views/chat_view.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
defmodule CadetWeb.ChatView do
22
use CadetWeb, :view
33

4-
def render("conversation_init.json", %{conversation_id: id, last_message: last}) do
5-
%{conversationId: id, response: last}
4+
def render("conversation_init.json", %{
5+
conversation_id: id,
6+
last_message: last,
7+
max_content_size: size
8+
}) do
9+
%{conversationId: id, response: last, maxContentSize: size}
610
end
711

812
def render("conversation.json", %{conversation_id: id, response: response}) do

test/cadet_web/controllers/chat_controller_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ defmodule CadetWeb.ChatControllerTest do
7676
end
7777
end
7878

79+
@tag authenticate: :student
80+
@tag requires_setup: true
81+
test "The content length is too long",
82+
%{conn: conn, conversation_id: conversation_id} do
83+
assert conversation_id != nil
84+
max_message_length = ChatController.max_content_length()
85+
message_exceed_length = String.duplicate("a", max_message_length + 1)
86+
87+
conn =
88+
post(conn, "/v2/chats/#{conversation_id}/message", %{
89+
"conversation_id" => conversation_id,
90+
"message" => "#{message_exceed_length}"
91+
})
92+
93+
assert response(conn, :unprocessable_entity) ==
94+
"Message exceeds the maximum allowed length of #{max_message_length}"
95+
end
96+
97+
@tag authenticate: :student
98+
@tag requires_setup: true
99+
test "The content length less than the maximum allowed length but conversation belongs to another user",
100+
%{conn: conn, conversation_id: conversation_id} do
101+
assert conversation_id != nil
102+
max_message_length = ChatController.max_content_length()
103+
message_exceed_length = String.duplicate("a", max_message_length)
104+
105+
conn =
106+
post(conn, "/v2/chats/#{conversation_id}/message", %{
107+
"conversation_id" => conversation_id,
108+
"message" => "#{message_exceed_length}"
109+
})
110+
111+
assert response(conn, :not_found) == "Conversation not found"
112+
end
113+
79114
@tag authenticate: :student
80115
test "invalid conversation id", %{conn: conn} do
81116
conversation_id = "-1"

0 commit comments

Comments
 (0)