Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(gfs): close the two review nits on the CodeQL rate-limit fix
Alfredo Lopez998188b15447
diff --git a/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts b/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.tsindex 47d3d8056..5007419cf 100644--- a/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts+++ b/control-api/test/gfsBlobReconciliation.realPostgres.integration.test.ts@@ -128,11 +128,17 @@ describeRealPostgres('GFS blob reconciliation on real PostgreSQL + on-disk BlobSexpect(existsSync(blobPath)).toBe(true)expect(await manifestExists(written.blobKey)).toBe(true)- const { metrics } = metricsStub()- const result = await reconcileExpiredBlobs(manifests, blobs, metrics, { olderThanMs: 1000, limit: 16 })+ const recorder = metricsStub()+ const result = await reconcileExpiredBlobs(manifests, blobs, recorder.metrics, {+ olderThanMs: 1000,+ limit: 16,+ })expect(result.deleted).toBe(1)expect(result.failures).toBe(0)+ // Read the recording stub too: the reconciler's failure metric must stay+ // untouched, not just its returned tally.+ expect(recorder.failures).toBe(0)// Business-truth: the bytes are gone from the PVC and the manifest row is gone.expect(existsSync(blobPath)).toBe(false)expect(await manifestExists(written.blobKey)).toBe(false)@@ -164,13 +170,19 @@ describeRealPostgres('GFS blob reconciliation on real PostgreSQL + on-disk BlobSawait ageCandidate(written.blobKey)const blobPath = resolveBlobKeyPath(blobRoot, written.blobKey)- const { metrics } = metricsStub()- const result = await reconcileExpiredBlobs(manifests, blobs, metrics, { olderThanMs: 1000, limit: 16 })+ const recorder = metricsStub()+ const result = await reconcileExpiredBlobs(manifests, blobs, recorder.metrics, {+ olderThanMs: 1000,+ limit: 16,+ })// The real NOT EXISTS predicate must exclude the referenced blob from the// delete claim, so no bytes are deleted and the bytes survive on the PVC.expect(result.deleted).toBe(0)expect(result.failures).toBe(0)+ // Read the recording stub too: the reconciler's failure metric must stay+ // untouched, not just its returned tally.+ expect(recorder.failures).toBe(0)// Business-truth that matters: the bytes are protected.expect(existsSync(blobPath)).toBe(true)// The now-redundant staged manifest IS metadata-GC'd by removeCommittedMetadata@@ -198,11 +210,17 @@ describeRealPostgres('GFS blob reconciliation on real PostgreSQL + on-disk BlobSawait ageCandidate(legacyBlobKey)expect(existsSync(legacyPath)).toBe(true)- const { metrics } = metricsStub()- const result = await reconcileExpiredBlobs(manifests, blobs, metrics, { olderThanMs: 1000, limit: 16 })+ const recorder = metricsStub()+ const result = await reconcileExpiredBlobs(manifests, blobs, recorder.metrics, {+ olderThanMs: 1000,+ limit: 16,+ })expect(result.deleted).toBe(1)expect(result.failures).toBe(0)+ // Read the recording stub too: the reconciler's failure metric must stay+ // untouched, not just its returned tally.+ expect(recorder.failures).toBe(0)// Business-truth: the flat legacy bytes are gone and the manifest row is gone.expect(existsSync(legacyPath)).toBe(false)expect(await manifestExists(legacyBlobKey)).toBe(false)diff --git a/control-api/test/gfsLegacyStandaloneGrantsRateLimit.test.ts b/control-api/test/gfsLegacyStandaloneGrantsRateLimit.test.tsnew file mode 100644index 000000000..55a244c1a--- /dev/null+++ b/control-api/test/gfsLegacyStandaloneGrantsRateLimit.test.ts@@ -0,0 +1,139 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import express from 'express'+import request from 'supertest'++// Route-level regression guard for the rate limiter on the legacy-standalone+// grant report (CodeQL #795 fix). The limiter service itself is covered+// elsewhere; what was untested is the WIRING — that this specific route still+// runs the limiter, keyed per-admin on its own bucket, before doing any+// database work. Dropping `legacyGrantReportRateLimit` from the route would+// leave every assertion here failing rather than silently un-limiting an+// authenticated endpoint.+const ADMIN_SUB = vi.hoisted(() => 'admin-42')+const mockQuery = vi.hoisted(() => vi.fn())+const mockCheckAndIncrement = vi.hoisted(() => vi.fn())++vi.mock('../src/db.js', () => ({+ pool: { query: (...args: unknown[]) => mockQuery(...args) },+ withTransaction: vi.fn(),+}))++vi.mock('../src/services/rateLimiterService.js', () => ({+ checkAndIncrement: (...args: unknown[]) => mockCheckAndIncrement(...args),+}))++// Mirrors the real middleware's contract: it authenticates and publishes the+// admin claims on the request. The limiter keys on `adminAuth.sub`, so the+// stub must populate it exactly like `requireAuthForControlUI` does.+vi.mock('../src/middleware/controlUIAuth.js', () => ({+ requireAuthForControlUI: (+ req: express.Request,+ res: express.Response,+ next: express.NextFunction+ ) => {+ if (req.header('x-test-auth') !== 'operator') {+ res.status(401).json({ error: 'Unauthorized' })+ return+ }+ ;(req as express.Request & { adminAuth?: { sub: string } }).adminAuth = { sub: ADMIN_SUB }+ next()+ },+}))++const RESOURCE_ID = '20000000-0000-4000-8000-000000000001'++function allowed(remaining = 29) {+ return {+ allowed: true,+ remaining,+ resetMs: Date.now() + 60_000,+ windowStartMs: Date.now(),+ count: 30 - remaining,+ }+}++function denied() {+ return {+ allowed: false,+ remaining: 0,+ resetMs: Date.now() + 30_000,+ windowStartMs: Date.now(),+ count: 31,+ }+}++async function buildApp() {+ const { registerLegacyStandaloneGrantReportRoute } = await import(+ '../src/routes/gfs/legacyStandaloneGrants.js'+ )+ const router = express.Router()+ registerLegacyStandaloneGrantReportRoute(router)+ const app = express()+ app.use(express.json())+ app.use(router)+ return app+}++describe('GET /gfs/grants/legacy-standalone rate limiting', () => {+ beforeEach(() => {+ mockQuery.mockReset()+ mockCheckAndIncrement.mockReset()+ })++ it('serves the report and consumes its own per-admin bucket when under the limit', async () => {+ mockCheckAndIncrement.mockResolvedValue(allowed())+ mockQuery.mockResolvedValue({+ rows: [+ {+ id: '10000000-0000-4000-8000-000000000001',+ drive: 'main',+ resource_id: RESOURCE_ID,+ permissions: ['read'],+ inherit: true,+ },+ ],+ })++ const response = await request(await buildApp())+ .get('/gfs/grants/legacy-standalone')+ .set('x-test-auth', 'operator')++ // Operability: the limiter must not stand between an operator and a+ // legitimate report — the normal path still returns the inventory.+ expect(response.status).toBe(200)+ expect(response.body.grants).toHaveLength(1)+ // Wiring proof: the route consumed the DEDICATED bucket key at the+ // documented 30/min ceiling, not the grant-mutation bucket and not some+ // shared/unkeyed budget.+ expect(mockCheckAndIncrement).toHaveBeenCalledTimes(1)+ expect(mockCheckAndIncrement).toHaveBeenCalledWith(`gfsgrants-legacy:${ADMIN_SUB}`, 30)+ })++ it('returns 429 with Retry-After and never reaches the database once the bucket is exhausted', async () => {+ mockCheckAndIncrement.mockResolvedValue(denied())++ const response = await request(await buildApp())+ .get('/gfs/grants/legacy-standalone')+ .set('x-test-auth', 'operator')++ expect(response.status).toBe(429)+ expect(response.body).toMatchObject({ error: 'Too Many Requests' })+ expect(response.body.retryAfterSeconds).toBeGreaterThan(0)+ expect(response.headers['retry-after']).toBeDefined()+ expect(response.headers['x-ratelimit-limit']).toBe('30')+ expect(response.headers['x-ratelimit-remaining']).toBe('0')+ // The property that makes this a DoS control rather than a cosmetic header:+ // the request is rejected BEFORE the handler runs its Postgres query.+ expect(mockQuery).not.toHaveBeenCalled()+ })++ it('rejects unauthenticated callers before the limiter spends a token', async () => {+ const response = await request(await buildApp()).get('/gfs/grants/legacy-standalone')++ expect(response.status).toBe(401)+ // Auth precedes the limiter, so an anonymous flood cannot drain a real+ // admin's bucket (and the limiter's key is never null in practice).+ expect(mockCheckAndIncrement).not.toHaveBeenCalled()+ expect(mockQuery).not.toHaveBeenCalled()+ })+})