Skip to content

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

TypeScript

fix(gfs): guard member retirement before auth

Alfredo Lopez9dac670e722f
diff --git a/external-rest-api/package-lock.json b/external-rest-api/package-lock.json
index c589ae108..e7eecf31b 100644
--- a/external-rest-api/package-lock.json
+++ b/external-rest-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-external-rest-api",
- "version": "0.1.69",
+ "version": "0.1.70",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-external-rest-api",
- "version": "0.1.69",
+ "version": "0.1.70",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/external-rest-api/package.json b/external-rest-api/package.json
index b98e1c008..fe0cdaf72 100644
--- a/external-rest-api/package.json
+++ b/external-rest-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-external-rest-api",
- "version": "0.1.69",
+ "version": "0.1.70",
"description": "External REST API for profile, team management, and RPC token brokerage",
"main": "dist/main.js",
"engines": {
diff --git a/external-rest-api/src/routes/members.ts b/external-rest-api/src/routes/members.ts
index 8b2f466b4..20d7e7dae 100644
--- a/external-rest-api/src/routes/members.ts
+++ b/external-rest-api/src/routes/members.ts
@@ -1,5 +1,5 @@
import { type NextFunction, type Response, Router } from 'express'
-import { rateLimit } from 'express-rate-limit'
+import { ipKeyGenerator, rateLimit } from 'express-rate-limit'
import { randomUUID } from 'node:crypto'
import { ControlApiError } from '../controlApiClient.js'
import { type AuthedRequest, extractAuthToken, requireAuth } from '../middleware/auth.js'
@@ -21,21 +21,35 @@ const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const MAX_INVITATION_TEAM_ASSIGNMENTS = 50
const MAX_INVITEE_NAME_LENGTH = 120
const UUID_ANY_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
-const MEMBER_RETIREMENT_RATE_LIMIT_PER_MINUTE = 30
+const MEMBER_RETIREMENT_EDGE_RATE_LIMIT_PER_MINUTE = 30
+const MEMBER_RETIREMENT_SUBJECT_RATE_LIMIT_PER_MINUTE = 30
const MAX_RETIREMENT_REASON_LENGTH = 512
const MAX_RETIREMENT_IDEMPOTENCY_KEY_LENGTH = 256
-// Retirement is an authenticated destructive operation. Keep the guard at
-// the public route boundary so CodeQL and deployed edge instrumentation both
-// see a concrete limiter before the downstream authorization/service call.
-const memberRetirementRateLimit = rateLimit({
+// The IP backstop runs before authentication, so an unauthenticated flood
+// cannot consume authorization or Control API work. It is only a coarse edge
+// guard; the verified-subject bucket below owns authenticated fairness.
+const memberRetirementEdgeRateLimit = rateLimit({
windowMs: 60_000,
- limit: MEMBER_RETIREMENT_RATE_LIMIT_PER_MINUTE,
+ limit: MEMBER_RETIREMENT_EDGE_RATE_LIMIT_PER_MINUTE,
standardHeaders: 'draft-7',
legacyHeaders: false,
- identifier: 'external-member-retirement',
+ identifier: 'external-member-retirement-edge',
+ keyGenerator: req => `member-retirement-ip:${ipKeyGenerator(req.ip ?? 'unknown')}`,
+})
+
+// This second bucket is deliberately after requireAuth: only the verified
+// session subject can select its key, never a request body/query/header value.
+const memberRetirementSubjectRateLimit = rateLimit({
+ windowMs: 60_000,
+ limit: MEMBER_RETIREMENT_SUBJECT_RATE_LIMIT_PER_MINUTE,
+ standardHeaders: 'draft-7',
+ legacyHeaders: false,
+ identifier: 'external-member-retirement-subject',
keyGenerator: req => {
const userId = (req as AuthedRequest).auth?.userId
+ // The middleware is ordered after requireAuth. Keep a fail-closed key for
+ // an impossible wiring regression rather than accepting a client value.
return userId ? `member-retirement:${userId}` : 'member-retirement:unauthenticated'
},
})
@@ -282,8 +296,9 @@ export function createMembersRouter(): Router {
router.delete(
'/members/:userId',
+ memberRetirementEdgeRateLimit,
requireAuth,
- memberRetirementRateLimit,
+ memberRetirementSubjectRateLimit,
validateMemberRetirementRequest,
async (req, res, next) => {
try {
diff --git a/external-rest-api/test/routes.members.test.ts b/external-rest-api/test/routes.members.test.ts
index 98c91ee78..071ddf1e8 100644
--- a/external-rest-api/test/routes.members.test.ts
+++ b/external-rest-api/test/routes.members.test.ts
@@ -34,6 +34,7 @@ const TARGET_USER_ID = '11111111-1111-4111-8111-111111111111'
function makeApp() {
const app = express()
+ app.set('trust proxy', 1)
app.use(express.json())
app.use(createMembersRouter())
return app
@@ -133,6 +134,7 @@ describe('routes/members', () => {
await request(makeApp())
.delete(`/members/${TARGET_USER_ID}`)
.set('authorization', 'Bearer good-token')
+ .set('x-forwarded-for', '198.51.100.10')
.set('Idempotency-Key', `retire-attempt-${attempt}`)
.send({ reason: `retirement attempt ${attempt}` })
.expect(200)
@@ -141,6 +143,7 @@ describe('routes/members', () => {
await request(makeApp())
.delete(`/members/${TARGET_USER_ID}`)
.set('authorization', 'Bearer good-token')
+ .set('x-forwarded-for', '198.51.100.10')
.set('Idempotency-Key', 'retire-attempt-31')
.set('x-correlation-id', '22222222-2222-4222-8222-222222222222')
.send({ reason: 'changed reason cannot bypass the verified-actor bucket' })