Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(codex): rate limit completion endpoint before body parsing

Alfredo Lopez5c1d73a32743
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index 8ae78fd32..e676e3b1c 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -25,6 +25,7 @@ import { RequestLimitError, streamGate } from './requestLimits.js'
const COMPLETION_KEYS = new Set(['executionTicket', 'requestHash', 'request', 'deadlineMs'])
const ADMIN_KEYS = new Set(['accessToken'])
+const COMPLETION_PATH = '/internal/runtime/v1/codex/completions'
const completionBodySchema = z
.object({
@@ -116,22 +117,20 @@ export function createProxyApps(
const runtimeApp = express()
const ordinaryJson = express.json({ limit: config.maxBodyBytes })
const visualJson = express.json({ limit: config.maxVisualBodyBytes })
- runtimeApp.use((req, res, next) => {
- // Only the image-capable completion endpoint with an already valid platform
- // identity may use the larger transport budget. V1 is still bounded by its
- // contract parser; admin and unauthenticated requests keep their old cap.
- if (
- req.method === 'POST' &&
- req.path === '/internal/runtime/v1/codex/completions' &&
- config.imageInputModels.length > 0 &&
- verifyPlatformJwt(bearer(req), config)
- ) {
+ // Selecting the transport budget needs the platform identity, so this runs as a
+ // route handler after the rate limiter: an unauthenticated caller cannot force
+ // token verification or body parsing ahead of the limit. Only the image-capable
+ // completion endpoint with an already valid platform identity may use the larger
+ // budget. V1 is still bounded by its contract parser, and admin or
+ // unauthenticated requests keep the ordinary cap.
+ const selectTransportBudget = (req: Request, res: Response, next: () => void): void => {
+ if (config.imageInputModels.length > 0 && verifyPlatformJwt(bearer(req), config)) {
visualJson(req, res, next)
return
}
ordinaryJson(req, res, next)
- })
- runtimeApp.post('/internal/runtime/v1/codex/completions', runtimeRateLimit, (req, res) => {
+ }
+ runtimeApp.post(COMPLETION_PATH, runtimeRateLimit, selectTransportBudget, (req, res) => {
if (!req.is('application/json')) {
reject(res, 415, 'unsupported_media_type')
return
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index d53dfc697..7ae05d5f2 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -194,6 +194,30 @@ describe('codex-llm-proxy security surface', () => {
expect(admin.status).toBe(413)
})
+ it('rate limits the completion endpoint before body parsing and authorization', async () => {
+ const { runtimeApp } = createProxyApps(
+ config({ maxBodyBytes: 1024, imageInputModels: ['gpt-5.1'] })
+ )
+ const completion = () => request(runtimeApp).post('/internal/runtime/v1/codex/completions')
+ const oversized = {
+ executionTicket: 'invalid-ticket',
+ requestHash: 'a'.repeat(64),
+ request: { schemaVersion: 'codex-completion-request.v2', pad: 'x'.repeat(4096) },
+ }
+ // With budget left, an unauthenticated oversize request is stopped by the
+ // ordinary transport cap instead of the limiter.
+ const withinBudget = await completion().send(oversized)
+ expect(withinBudget.status).toBe(413)
+ for (let i = 0; i < 59; i += 1) {
+ const accepted = await completion().send({})
+ expect(accepted.status).toBe(401)
+ }
+ // The limiter runs first, so the exhausted window rejects before the identity
+ // check and body parsing turn the same request into a 413.
+ const limited = await completion().send(oversized)
+ expect(limited.status).toBe(429)
+ })
+
it('rejects a platform JWT whose hostRefs do not bind the ticket hostRef', async () => {
const { runtimeApp } = createProxyApps(config())
const foreign = sign(