Skip to content

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

TypeScript

fix(mcp-host): limit artifact listing and download per caller

marcoteb27bf45834da0
diff --git a/mcp-host/src/__tests__/server.artifacts.test.ts b/mcp-host/src/__tests__/server.artifacts.test.ts
index 48cfc8666..6600ee013 100644
--- a/mcp-host/src/__tests__/server.artifacts.test.ts
+++ b/mcp-host/src/__tests__/server.artifacts.test.ts
@@ -386,6 +386,34 @@ describe('RPCServer runtime artifact routes', () => {
}
})
+ it('limits each caller on their own, so one cannot starve the others', async () => {
+ outputDir = makeOutputDir('user-1')
+ fs.writeFileSync(path.join(outputDir, 'report.md'), '# ok\n')
+ const { server, baseUrl } = await startServer(outputDir)
+
+ try {
+ const list = (userId: string) =>
+ fetch(`${baseUrl}/v1/runtime/artifacts`, { headers: edgeHeaders(userId) })
+ for (let call = 0; call < 600; call++) {
+ const res = await list('user-1')
+ expect(res.status).toBe(200)
+ await res.arrayBuffer()
+ }
+ const limited = await list('user-1')
+ expect(limited.status).toBe(429)
+ expect(await limited.json()).toEqual({ error: 'Too Many Requests', retryable: true })
+ const download = await fetch(`${baseUrl}/v1/runtime/artifacts/report.md/download`, {
+ headers: rpcEdgeHeaders,
+ })
+ expect(download.status).toBe(429)
+ // Another caller, through the same rpc-proxy address, is still served.
+ const other = await list('user-2')
+ expect(other.status).toBe(200)
+ } finally {
+ await server.stop()
+ }
+ })
+
it('does not serve CLERUM_OUTPUT_DIR in chat when a resolver is wired', async () => {
outputDir = makeOutputDir('user-1')
const shared = path.join(hostRoot, 'outputs')
diff --git a/mcp-host/src/server.ts b/mcp-host/src/server.ts
index b85fe77d3..44be73e0c 100644
--- a/mcp-host/src/server.ts
+++ b/mcp-host/src/server.ts
@@ -4,6 +4,7 @@
* Routing and authz are split into dedicated modules under src/server/.
*/
import express, { type NextFunction, type Request, type Response } from 'express'
+import { ipKeyGenerator, rateLimit } from 'express-rate-limit'
import * as fs from 'fs'
import * as http from 'http'
import * as path from 'path'
@@ -710,11 +711,27 @@ export class RPCServer {
}
next()
}
+ // A listing reads and stats the caller's folder and a download reads and
+ // redacts a file, so each caller gets a bucket of their own: every call
+ // comes through the same rpc-proxy address, where one bucket would let a
+ // single caller hold the Host's disk and starve the rest.
+ const limitArtifactCalls = rateLimit({
+ windowMs: 60_000,
+ limit: 600,
+ standardHeaders: 'draft-8',
+ legacyHeaders: false,
+ keyGenerator: req => {
+ const userId = getRuntimeCallerContext(req)?.userId
+ return userId ? `caller:${userId}` : ipKeyGenerator(req.ip ?? '')
+ },
+ message: { error: 'Too Many Requests', retryable: true },
+ })
this.app.get(
'/v1/runtime/artifacts',
rejectWorkflowRuntimeArtifacts,
runtimeEdgeGuard(['rpc-proxy']),
+ limitArtifactCalls,
(req, res) => {
try {
// Only the caller's own folder, re-verified per request; none yet
@@ -757,6 +774,7 @@ export class RPCServer {
'/v1/runtime/artifacts/:filename/download',
rejectWorkflowRuntimeArtifacts,
runtimeEdgeGuard(['rpc-proxy']),
+ limitArtifactCalls,
(req, res) => {
const filename = req.params.filename as string
if (isInternalWorkflowArtifactName(filename)) {