Skip to content

Commit 85797ae

Browse files
committed
fix(runtime): return pruned helper routes before parsing
1 parent f225ae8 commit 85797ae

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

lib/codex_pooler_web/plugs/runtime_ingress.ex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngress do
2424

2525
@json_error_type "invalid_request_error"
2626

27+
@pruned_runtime_helper_routes [
28+
{"GET", ["backend-api", "codex", "agent-identities", "jwks"]},
29+
{"GET", ["backend-api", "wham", "agent-identities", "jwks"]},
30+
{"POST", ["api", "codex", "rate-limit-reset-credits", "consume"]},
31+
{"POST", ["wham", "rate-limit-reset-credits", "consume"]},
32+
{"POST", ["backend-api", "wham", "rate-limit-reset-credits", "consume"]},
33+
{"POST", ["backend-api", "codex", "thread", "goal", "get"]},
34+
{"POST", ["backend-api", "codex", "thread", "goal", "set"]},
35+
{"POST", ["backend-api", "codex", "thread", "goal", "clear"]},
36+
{"POST", ["backend-api", "codex", "analytics-events", "events"]},
37+
{"POST", ["backend-api", "codex", "memories", "trace_summarize"]},
38+
{"POST", ["backend-api", "codex", "alpha", "search"]},
39+
{"POST", ["backend-api", "codex", "realtime", "calls"]},
40+
{"POST", ["backend-api", "codex", "safety", "arc"]}
41+
]
42+
2743
def init(opts), do: opts
2844

2945
def call(conn, _opts) do
@@ -36,6 +52,9 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngress do
3652
|> admit_mcp_request()
3753
|> prepare_mcp_body(settings)
3854

55+
pruned_runtime_helper_request?(conn) ->
56+
send_pruned_runtime_helper_absent(conn)
57+
3958
runtime_path?(conn.path_info) ->
4059
settings = OperationalSettings.current()
4160

@@ -379,6 +398,11 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngress do
379398

380399
def protected_backend_raw_request?(_conn), do: false
381400

401+
@spec pruned_runtime_helper_request?(Plug.Conn.t()) :: boolean()
402+
defp pruned_runtime_helper_request?(%Plug.Conn{method: method, path_info: path_info}) do
403+
{method, path_info} in @pruned_runtime_helper_routes
404+
end
405+
382406
defp decode_or_send_compressed_body(conn, settings) do
383407
case CompressedBody.decode(conn, settings) do
384408
{:ok, conn} -> conn
@@ -387,6 +411,13 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngress do
387411
end
388412
end
389413

414+
defp send_pruned_runtime_helper_absent(conn) do
415+
conn
416+
|> put_resp_content_type("text/html")
417+
|> send_resp(404, "Not Found")
418+
|> halt()
419+
end
420+
390421
defp runtime_path?(path_info) do
391422
Enum.any?(@runtime_prefixes, &List.starts_with?(path_info, &1))
392423
end

test/codex_pooler_web/plugs/runtime_ingress_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngressTest do
33

44
import CodexPooler.PoolerFixtures
55

6+
alias CodexPooler.Accounting.Request
67
alias CodexPooler.Catalog.PricingSnapshot
78
alias CodexPooler.FakeUpstream
89
alias CodexPooler.Gateway.OperationalSettings
@@ -304,6 +305,34 @@ defmodule CodexPoolerWeb.Plugs.RuntimeIngressTest do
304305
assert response(conn, 404) =~ "Not Found"
305306
end
306307
end
308+
309+
test "malformed pruned thread goal set returns absent before parsing or side effects", %{
310+
conn: conn
311+
} do
312+
setup_runtime_ingress(%OperationalSettings{})
313+
314+
conn =
315+
conn
316+
|> put_req_header("content-type", "application/json")
317+
|> post("/backend-api/codex/thread/goal/set", ~s({"goal":))
318+
319+
assert response(conn, 404) =~ "Not Found"
320+
assert Repo.aggregate(Request, :count) == 0
321+
end
322+
323+
test "malformed pruned reset-credit consume returns absent before parsing or side effects", %{
324+
conn: conn
325+
} do
326+
setup_runtime_ingress(%OperationalSettings{})
327+
328+
conn =
329+
conn
330+
|> put_req_header("content-type", "application/json")
331+
|> post("/api/codex/rate-limit-reset-credits/consume", ~s({"redeem_request_id":))
332+
333+
assert response(conn, 404) =~ "Not Found"
334+
assert Repo.aggregate(Request, :count) == 0
335+
end
307336
end
308337

309338
describe "compressed runtime API requests" do

0 commit comments

Comments
 (0)