Skip to content

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

TypeScript

fix(gfs): delegate drive validation to control-api

Alfredo Lopez9ad48e910a70
diff --git a/external-rest-api/package-lock.json b/external-rest-api/package-lock.json
index 386e7dfda..a4226df63 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.66",
+ "version": "0.1.67",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-external-rest-api",
- "version": "0.1.66",
+ "version": "0.1.67",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/external-rest-api/package.json b/external-rest-api/package.json
index cb06400d7..109e55862 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.66",
+ "version": "0.1.67",
"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/gfs.ts b/external-rest-api/src/routes/gfs.ts
index 0e96cdeee..2b4c4dbff 100644
--- a/external-rest-api/src/routes/gfs.ts
+++ b/external-rest-api/src/routes/gfs.ts
@@ -1,4 +1,4 @@
-import { type Request, Router } from 'express'
+import { Router } from 'express'
import type { NextFunction, Response } from 'express'
import { type AugmentedRequest, ipKeyGenerator, rateLimit } from 'express-rate-limit'
import { Readable } from 'node:stream'
@@ -7,28 +7,10 @@ import { config } from '../config.js'
import { ControlApiError, controlApiRequest, controlApiStreamRequest } from '../controlApiClient.js'
import { type AuthedRequest, extractAuthToken, requireAuth } from '../middleware/auth.js'
-const UPLOAD_DRIVE_WIRE_RE = /^\S(?:[\s\S]*\S)?$/
-
type GfsRouterOptions = {
edgeRequestLimit?: number
}
-/**
- * Validate only the public wire format. Drive ownership and authorization are
- * re-established by control-api/GFSC from the authenticated subject; this
- * helper must never become an authorization decision.
- */
-function uploadDriveFromRequest(req: Request): string | null {
- const rawDrive = req.query.drive
- return typeof rawDrive === 'string' && UPLOAD_DRIVE_WIRE_RE.test(rawDrive) ? rawDrive : null
-}
-
-function uploadCreateBodyMatchesDrive(req: Request, path: string, drive: string): boolean {
- if (req.method !== 'POST' || path !== '/external/gfs/uploads') return true
- if (!req.body || typeof req.body !== 'object' || Array.isArray(req.body)) return false
- return (req.body as { drive?: unknown }).drive === drive
-}
-
/**
* End-user gfs (Global File System) surface — the user side of the delegation UI
* (Desktop / profile). Pure passthrough on the Session-JWT plane: every route
@@ -434,19 +416,6 @@ async function proxyUploadStream(
path: string
): Promise<void> {
try {
- const drive = uploadDriveFromRequest(req)
- if (!drive) {
- res.status(400).json({ error: 'drive_required' })
- return
- }
- if (
- method === 'POST' &&
- path === '/external/gfs/uploads' &&
- !uploadCreateBodyMatchesDrive(req, path, drive)
- ) {
- res.status(400).json({ error: 'drive_mismatch' })
- return
- }
const extraHeaders: Record<string, string> = {}
for (const name of [
'content-type',
@@ -494,7 +463,11 @@ async function proxyUploadStream(
// these query parameters makes every resume request fetch page one and
// can silently truncate a session with more than 256 parts.
query: {
- drive,
+ // Drive syntax, body/query agreement, ownership, and authorization are
+ // validated once by control-api before admission/token minting. The
+ // relay must not make a security decision from a user-controlled drive
+ // value or maintain a second, divergent drive policy.
+ drive: typeof req.query.drive === 'string' ? req.query.drive : undefined,
limit: typeof req.query.limit === 'string' ? req.query.limit : undefined,
cursor: typeof req.query.cursor === 'string' ? req.query.cursor : undefined,
},
diff --git a/external-rest-api/test/routes.gfs.test.ts b/external-rest-api/test/routes.gfs.test.ts
index 6bdf15b6e..213c2b7ae 100644
--- a/external-rest-api/test/routes.gfs.test.ts
+++ b/external-rest-api/test/routes.gfs.test.ts
@@ -130,7 +130,24 @@ describe('routes/gfs /me/gfs/* (user session passthrough → /external/gfs/*)',
expect(clientMock.controlApiStreamRequest).toHaveBeenCalledTimes(2)
})
- it('rejects a missing or create-body-mismatched upload drive at the public edge', async () => {
+ it('delegates missing and create-body-mismatched drive validation to control-api', async () => {
+ clientMock.controlApiStreamRequest.mockImplementation(
+ async (
+ method: string,
+ path: string,
+ options: { query?: { drive?: string }; body?: unknown }
+ ) => {
+ const drive = options.query?.drive
+ const body = typeof options.body === 'string' ? JSON.parse(options.body) : undefined
+ if (!drive) {
+ throw new ControlApiError('drive required', 400, { error: 'drive_required' })
+ }
+ if (method === 'POST' && path === '/external/gfs/uploads' && body?.drive !== drive) {
+ throw new ControlApiError('drive mismatch', 400, { error: 'drive_mismatch' })
+ }
+ return new Response(JSON.stringify({ ok: true }), { status: 200 })
+ }
+ )
const missing = await request(buildApp())
.get('/me/gfs/capabilities')
.set('authorization', 'Bearer sess-xyz')
@@ -142,10 +159,21 @@ describe('routes/gfs /me/gfs/* (user session passthrough → /external/gfs/*)',
expect(missing.body).toEqual({ error: 'drive_required' })
expect(mismatched.status).toBe(400)
expect(mismatched.body).toEqual({ error: 'drive_mismatch' })
- expect(clientMock.controlApiStreamRequest).not.toHaveBeenCalled()
+ expect(clientMock.controlApiStreamRequest).toHaveBeenCalledTimes(2)
})
- it('rejects malformed drive wire values without forwarding them upstream', async () => {
+ it('forwards malformed drive values unchanged for control-api to reject', async () => {
+ const forwardedDrives: unknown[] = []
+ clientMock.controlApiStreamRequest.mockImplementation(
+ async (_method: string, _path: string, options: { query?: { drive?: string } }) => {
+ const drive = options.query?.drive
+ forwardedDrives.push(drive)
+ if (!drive || drive.trim() !== drive) {
+ throw new ControlApiError('drive required', 400, { error: 'drive_required' })
+ }
+ return new Response(JSON.stringify({ ok: true }), { status: 200 })
+ }
+ )
const malformed = ['', ' archive', 'archive ', '\tarchive', 'archive\n']
for (const drive of malformed) {
const response = await request(buildApp())
@@ -159,7 +187,8 @@ describe('routes/gfs /me/gfs/* (user session passthrough → /external/gfs/*)',
.set('authorization', 'Bearer sess-xyz')
expect(arrayValue.status).toBe(400)
expect(arrayValue.body).toEqual({ error: 'drive_required' })
- expect(clientMock.controlApiStreamRequest).not.toHaveBeenCalled()
+ expect(clientMock.controlApiStreamRequest).toHaveBeenCalledTimes(6)
+ expect(forwardedDrives).toEqual([...malformed, undefined])
})
it('preserves one non-main drive and the part stream through every upload lifecycle relay', async () => {