Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test: stabilize fixed-window quota checks (CI-REISSUE-001)
Josué86a9b64b25f3
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex da78ca66e..1b6d7666c 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.751",+ "version": "0.1.752","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.751",+ "version": "0.1.752","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 62fe86662..f24fe4a2f 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.751",+ "version": "0.1.752","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/registry.test.ts b/control-api/test/registry.test.tsindex 1d8b9a7e5..48caf5f57 100644--- a/control-api/test/registry.test.ts+++ b/control-api/test/registry.test.ts@@ -105,6 +105,7 @@ describe('POST /registry/identity-voucher', () => {})afterEach(() => {+ vi.restoreAllMocks();(config as Record<string, unknown>).registryConnectionMode = origMode;(config as Record<string, unknown>).registryVoucherPrivateKey = origKey;(config as Record<string, unknown>).registryVoucherKid = origKid@@ -192,6 +193,9 @@ describe('POST /registry/identity-voucher', () => {app.use(createRegistryRouter())// Drain the bucket. Cap is 30/min (set in the route definition).+ // Keep the burst in one quota window, including its final millisecond.+ const fixedNow = Math.floor(Date.now() / 60_000) * 60_000 + 59_999+ vi.spyOn(Date, 'now').mockReturnValue(fixedNow)for (let i = 0; i < 30; i += 1) {const ok = await request(app).post('/registry/identity-voucher')expect(ok.status).toBe(200)diff --git a/control-api/test/routes.mcp-host.renewal.reissue.test.ts b/control-api/test/routes.mcp-host.renewal.reissue.test.tsindex 8304d998f..aa8ee9870 100644--- a/control-api/test/routes.mcp-host.renewal.reissue.test.ts+++ b/control-api/test/routes.mcp-host.renewal.reissue.test.ts@@ -1,4 +1,4 @@-import { beforeEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import jwt from 'jsonwebtoken'import request from 'supertest'import { createApp } from '../src/app.js'@@ -134,6 +134,8 @@ function issueExpiredRefreshToken(describe('POST /api/v1/workflow-auth/reissue', () => {let app: ReturnType<typeof createApp>+ afterEach(() => vi.restoreAllMocks())+beforeEach(() => {vi.clearAllMocks()rateLimitCounts.clear()@@ -389,33 +391,53 @@ describe('POST /api/v1/workflow-auth/reissue', () => {expect(res.body.error).toBe('Internal Server Error')})- it('rate-limits by jti: bursts over approvalRlReissuePerMin return 429', async () => {- const limit = config.approvalRlReissuePerMin- // Note: each reissue needs a DIFFERENT token (jti is single-use), so the- // rate limiter bucket is keyed on (namespace/recipe) NOT per-jti — we- // re-read the middleware to confirm. Actually per the route config the- // bucket key is `reissue:${recipeNamespace}/${recipeName}` because- // getMcpHostRefreshRateLimitKey returns ns/recipe (not jti).- //- // So: limit+1 calls with DIFFERENT tokens for the SAME recipe should- // trip the limiter on the (limit+1)-th call.- for (let i = 0; i < limit; i++) {- const token = issueExpiredRefreshToken()- const res = await request(app)+ it.each([+ { offsetMs: 10_000, retryAfter: '50' },+ { offsetMs: 59_999, retryAfter: '1' },+ ])(+ 'rate-limits a recipe within one window at offset $offsetMs ms',+ async ({ offsetMs, retryAfter }) => {+ const limit = config.approvalRlReissuePerMin+ // Every request must stay in one fixed window, even at its final millisecond.+ // Keep real timers/HTTP and the actual RS256 sign/verify path running.+ const baseNow = Math.floor(Date.now() / 60_000) * 60_000 + offsetMs+ const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(baseNow)+ // JTIs are single-use; distinct tokens still share the namespace/recipe quota.+ for (let i = 0; i < limit; i++) {+ const token = issueExpiredRefreshToken()+ const res = await request(app)+ .post('/api/v1/workflow-auth/reissue')+ .set('Authorization', `Bearer ${token}`)+ .send({ recipe_name: RECIPE })+ expect(res.status).toBe(200)+ }++ const overflowToken = issueExpiredRefreshToken()+ const limited = await request(app).post('/api/v1/workflow-auth/reissue')- .set('Authorization', `Bearer ${token}`)+ .set('Authorization', `Bearer ${overflowToken}`).send({ recipe_name: RECIPE })- expect(res.status).toBe(200)- }+ expect(limited.status).toBe(429)+ expect(limited.headers['retry-after']).toBe(retryAfter)+ expect(limited.headers['x-ratelimit-remaining']).toBe('0')+ expect(limited.body.code).toBe('rate_limited')++ // Denial must not consume the token: the same token works in the next window.+ nowSpy.mockReturnValue(baseNow + 60_000)+ const retried = await request(app)+ .post('/api/v1/workflow-auth/reissue')+ .set('Authorization', `Bearer ${overflowToken}`)+ .send({ recipe_name: RECIPE })+ expect(retried.status).toBe(200)+ expect(retried.body.refreshToken).toBeTypeOf('string')- const overflowToken = issueExpiredRefreshToken()- const limited = await request(app)- .post('/api/v1/workflow-auth/reissue')- .set('Authorization', `Bearer ${overflowToken}`)- .send({ recipe_name: RECIPE })- expect(limited.status).toBe(429)- expect(limited.headers['retry-after']).toBeDefined()- })+ const replayed = await request(app)+ .post('/api/v1/workflow-auth/reissue')+ .set('Authorization', `Bearer ${overflowToken}`)+ .send({ recipe_name: RECIPE })+ expect(replayed.status).toBe(401)+ }+ )it('does not let forged expired refresh claims poison the reissue rate-limit bucket', async () => {const now = Math.floor(Date.now() / 1000)diff --git a/control-api/test/services.rateLimiter.test.ts b/control-api/test/services.rateLimiter.test.tsindex 4a6b56bb7..885f62e8e 100644--- a/control-api/test/services.rateLimiter.test.ts+++ b/control-api/test/services.rateLimiter.test.ts@@ -113,13 +113,14 @@ describe('rateLimiterService', () => {it('allows requests up to the limit and denies the (limit+1)-th in the same window', async () => {const key = 'test:bucket:1'const limit = 5+ const nowMs = 1_700_000_000_000for (let i = 0; i < limit; i++) {- const r = await checkAndIncrement(key, limit)+ const r = await checkAndIncrement(key, limit, nowMs)expect(r.allowed).toBe(true)expect(r.count).toBe(i + 1)expect(r.remaining).toBe(limit - (i + 1))}- const over = await checkAndIncrement(key, limit)+ const over = await checkAndIncrement(key, limit, nowMs)expect(over.allowed).toBe(false)expect(over.count).toBe(limit + 1)expect(over.remaining).toBe(0)@@ -152,8 +153,9 @@ describe('rateLimiterService', () => {it('concurrent increments observe distinct counter values (no race)', async () => {const key = 'test:bucket:concurrent'const limit = 100+ const nowMs = 1_700_000_000_000const results = await Promise.all(- Array.from({ length: 10 }, () => checkAndIncrement(key, limit))+ Array.from({ length: 10 }, () => checkAndIncrement(key, limit, nowMs)))const counts = results.map(r => r.count).sort((a, b) => a - b)expect(counts).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])