Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(gfs): close rate-limit and reconciliation review gaps
Alfredo Lopezdc50b5e694e0
diff --git a/desktop-app/src/__tests__/appService.gfs.test.ts b/desktop-app/src/__tests__/appService.gfs.test.tsindex 26cf90781..b534465c7 100644--- a/desktop-app/src/__tests__/appService.gfs.test.ts+++ b/desktop-app/src/__tests__/appService.gfs.test.ts@@ -213,6 +213,14 @@ type UploadScopeTestService = {scope: ReturnType<typeof scopedUploadRecord>['scope']}>+ gfsPendingUploadJobs: Set<{+ job: {+ snapshot: () => unknown+ suspendForAuth: () => Promise<void>+ }+ promise: Promise<unknown>+ scope: ReturnType<typeof scopedUploadRecord>['scope']+ }>gfsClient: {createResource: ReturnType<typeof vi.fn>replaceFile: ReturnType<typeof vi.fn>@@ -425,8 +433,9 @@ describe('AppService GFS upload security scope', () => {uploadedBytes: record.session.committedBytes,totalBytes: record.fileSize,})),+ suspendForAuth: vi.fn(async () => undefined),}- service.gfsUploadJobs.set(record.uploadId, {+ service.gfsPendingUploadJobs.add({job,promise: Promise.resolve(record.session),scope: record.scope,@@ -562,6 +571,90 @@ describe('AppService GFS upload security scope', () => {}})+ it('fences a producer-created pending job during an authentication boundary', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-pending-auth-fence-'))+ const originalFetch = globalThis.fetch+ const fetchStarted = vi.fn()+ const abortObserved = vi.fn()+ try {+ const filePath = join(root, 'pending.bin')+ await writeFile(filePath, Buffer.from('pending upload'))+ globalThis.fetch = vi.fn(async (_input, init?: RequestInit) => {+ fetchStarted()+ await new Promise<never>((_resolve, reject) => {+ const signal = init?.signal+ const abort = () => {+ abortObserved()+ reject(signal?.reason ?? new Error('synthetic authentication fence'))+ }+ if (signal?.aborted) {+ abort()+ return+ }+ signal?.addEventListener('abort', abort, { once: true })+ })+ throw new Error('unreachable pending capabilities request')+ }) as typeof fetch++ const service = new AppService() as unknown as {+ sessionToken: string | null+ me: UploadScopeTestService['me']+ gfsAuthEpoch: number+ gfsDispatchBlocked: boolean+ gfsScopeIdentity: Omit<+ ReturnType<typeof scopedUploadRecord>['scope'],+ 'drive' | 'authEpoch'+ >+ gfsPendingUploadJobs: UploadScopeTestService['gfsPendingUploadJobs']+ desktopGfsUploadStatePath: () => Promise<string>+ startDesktopGfsUpload: (input: Record<string, unknown>) => Promise<unknown>+ suspendDesktopGfsUploadsForAuthBoundary: () => Promise<void>+ }+ service.sessionToken = 'token-a'+ service.me = {+ id: 'user-a',+ email: '[email protected]',+ name: null,+ picture: null,+ teamId: 'team-a',+ teamName: 'Team A',+ role: null,+ }+ service.gfsAuthEpoch = 7+ service.gfsDispatchBlocked = false+ service.gfsScopeIdentity = {+ ownerId: 'user-a',+ teamId: 'team-a',+ environmentKey: getActiveEnvKey(),+ baseUrl: canonicalExternalBaseUrl(),+ }+ service.desktopGfsUploadStatePath = async () => join(root, 'gfs-upload-sessions.json')++ const start = service.startDesktopGfsUpload({+ filePath,+ name: 'pending.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-rid',+ })+ const startOutcome = start.then(+ () => ({ kind: 'resolved' as const }),+ error => ({ kind: 'rejected' as const, error })+ )+ await vi.waitFor(() => expect(service.gfsPendingUploadJobs.size).toBe(1))+ await vi.waitFor(() => expect(fetchStarted).toHaveBeenCalledTimes(1))+ await service.suspendDesktopGfsUploadsForAuthBoundary()++ await expect(startOutcome).resolves.toMatchObject({ kind: 'rejected' })+ expect(abortObserved).toHaveBeenCalledTimes(1)+ expect(service.gfsPendingUploadJobs.size).toBe(0)+ expect(service.gfsDispatchBlocked).toBe(true)+ } finally {+ globalThis.fetch = originalFetch+ await rm(root, { recursive: true, force: true })+ }+ })+it('fences A on logout, hides its record from B, and permits explicit resume only for exact A scope', async () => {const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-state-scope-'))try {diff --git a/external-rest-api/package-lock.json b/external-rest-api/package-lock.jsonindex deae760e7..8e512a40f 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.75",+ "version": "0.1.76","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-external-rest-api",- "version": "0.1.75",+ "version": "0.1.76","dependencies": {"cors": "^2.8.5","express": "^4.21.2",diff --git a/external-rest-api/package.json b/external-rest-api/package.jsonindex 2b5f2fb80..604223d6d 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.75",+ "version": "0.1.76","description": "External REST API for profile, team management, and RPC token brokerage","main": "dist/main.js","engines": {diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex 76a00b2e0..b971ac89a 100644--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@ -8,7 +8,7 @@ export type ReleaseManifest = {export const releaseManifest: ReleaseManifest = {releaseId: 'v0.7.0',- externalRestApiVersion: '0.1.75',+ externalRestApiVersion: '0.1.76',rpcProxyVersion: '0.1.64',desktopVersion: '0.7.0',minimumDesktopVersion: '0.1.252',diff --git a/external-rest-api/test/middleware.rateLimit.test.ts b/external-rest-api/test/middleware.rateLimit.test.tsnew file mode 100644index 000000000..56f8de939--- /dev/null+++ b/external-rest-api/test/middleware.rateLimit.test.ts@@ -0,0 +1,68 @@+import { describe, expect, it } from 'vitest'+import express from 'express'+import request from 'supertest'+import { createRateLimiter } from '../src/middleware/rateLimit.js'++describe('external-rest rate-limit route contracts', () => {+ it('keeps auth/me-style limiter instances isolated while preserving their own 429 envelope', async () => {+ const app = express()+ app.set('trust proxy', 1)+ app.get(+ '/auth/password-login',+ createRateLimiter({ windowMs: 60_000, maxRequests: 1, errorCode: 'auth_rate_limited' }),+ (_req, res) => res.status(200).json({ ok: true })+ )+ app.get(+ '/me/teams/directory',+ createRateLimiter({ windowMs: 60_000, maxRequests: 1, errorCode: 'directory_rate_limited' }),+ (_req, res) => res.status(200).json({ ok: true })+ )++ const ip = '198.51.100.40'+ await request(app).get('/auth/password-login').set('x-forwarded-for', ip).expect(200)+ await request(app).get('/me/teams/directory').set('x-forwarded-for', ip).expect(200)++ const authLimited = await request(app)+ .get('/auth/password-login')+ .set('x-forwarded-for', ip)+ .expect(429)+ expect(authLimited.body).toMatchObject({+ error: 'auth_rate_limited',+ retryAfterSeconds: expect.any(Number),+ })+ expect(authLimited.headers['retry-after']).toMatch(/^\d+$/)+ expect(authLimited.headers['x-ratelimit-limit']).toBe('1')+ expect(authLimited.headers['x-ratelimit-remaining']).toBe('0')++ const directoryLimited = await request(app)+ .get('/me/teams/directory')+ .set('x-forwarded-for', ip)+ .expect(429)+ expect(directoryLimited.body.error).toBe('directory_rate_limited')+ expect(directoryLimited.headers['x-ratelimit-limit']).toBe('1')+ })++ it('keys the default limiter by the trusted client IP rather than globally', async () => {+ const app = express()+ app.set('trust proxy', 1)+ app.get(+ '/invitations/token/:token',+ createRateLimiter({ windowMs: 60_000, maxRequests: 1 }),+ (_req, res) => res.status(200).json({ ok: true })+ )++ await request(app)+ .get('/invitations/token/one')+ .set('x-forwarded-for', '198.51.100.41')+ .expect(200)+ await request(app)+ .get('/invitations/token/two')+ .set('x-forwarded-for', '198.51.100.42')+ .expect(200)++ await request(app)+ .get('/invitations/token/three')+ .set('x-forwarded-for', '198.51.100.41')+ .expect(429)+ })+})diff --git a/gfs-controller/src/upload/uploadSession.test.ts b/gfs-controller/src/upload/uploadSession.test.tsindex d92b8d7ce..228eeea02 100644--- a/gfs-controller/src/upload/uploadSession.test.ts+++ b/gfs-controller/src/upload/uploadSession.test.ts@@ -20,6 +20,7 @@ class MemoryDb {sessions: Row[] = []parts: Row[] = []failReconciliationReads = false+ failSessionReconciliationReads = falseasync query(text: string, values: unknown[] = []): Promise<{ rows: Row[] }> {const sql = text.replace(/\s+/g, ' ').trim()@@ -34,6 +35,9 @@ class MemoryDb {}}if (sql.startsWith('SELECT * FROM gfs_upload_sessions WHERE upload_id')) {+ if (this.failSessionReconciliationReads) {+ throw new CommitOutcomeUnknownError(new Error('synthetic session reconciliation read failure'))+ }const [id, drive, owner] = values.map(String)return {rows: this.sessions.filter(@@ -490,6 +494,29 @@ class ThrowBeforePartCommitTransactor implements Transactor {}}+class ThrowOnCommitTransactor implements Transactor {+ private transactionCount = 0++ constructor(+ private readonly delegate: Transactor,+ private readonly failOnTransaction: number,+ private readonly afterCommit: () => void+ ) {}++ async transaction<T>(+ fn: (client: TxClient) => Promise<T>,+ options?: TransactionOptions+ ): Promise<T> {+ this.transactionCount += 1+ const result = await this.delegate.transaction(fn, options)+ if (this.transactionCount === this.failOnTransaction) {+ this.afterCommit()+ throw new CommitOutcomeUnknownError(new Error('synthetic completion response loss'))+ }+ return result+ }+}+async function settleWithin<T>(promise: Promise<T>,timeoutMs = 500@@ -730,6 +757,53 @@ describe('GfsUploadSessionService', () => {expect(Date.parse(db.sessions[0]!.expires_at as string)).toBeGreaterThan(originalExpiry)})+ it('preserves a published final path until reconciliation resolves an ambiguous completion commit', async () => {+ tempRoot = await mkdtemp(join(tmpdir(), 'gfsc-upload-final-path-reconcile-'))+ const db = new MemoryDb()+ const created = await service(db).create({+ ...principal,+ operation: 'create',+ parentRid: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',+ name: 'ambiguous-completion.bin',+ sizeBytes: 0,+ idempotencyKey: '70707070-7070-4070-8070-707070707070',+ })+ let publishedPath = ''+ const tx = new ThrowOnCommitTransactor(+ { transaction: async fn => fn(db) },+ 2,+ () => {+ db.failSessionReconciliationReads = true+ }+ )+ const uploads = service(db, TEST_CONFIG, {+ tx,+ finalize: async session => {+ publishedPath = join(+ tempRoot,+ '.uploads',+ session.uploadId,+ 'parts',+ 'published.part'+ )+ await mkdir(join(tempRoot, '.uploads', session.uploadId, 'parts'), { recursive: true })+ await writeFile(publishedPath, 'published before completion response was lost')+ return { resourceId: 'resource-ambiguous', version: 1, sha256: 'b'.repeat(64) }+ },+ })++ await expect(uploads.complete(created.session.uploadId, principal)).rejects.toBeInstanceOf(+ CommitOutcomeUnknownError+ )+ expect(db.sessions[0]).toMatchObject({ state: 'completed', cleanup_at: null })+ await expect(stat(publishedPath)).resolves.toBeDefined()++ db.failSessionReconciliationReads = false+ await expect(uploads.reconcile()).resolves.toEqual({ staleParts: 0, expiredSessions: 0 })+ await expect(stat(publishedPath)).rejects.toMatchObject({ code: 'ENOENT' })+ expect(db.sessions[0]?.cleanup_at).toEqual(expect.any(String))+ })+it('removes a rename-before-commit orphan while the reserved lease is still locked', async () => {tempRoot = await mkdtemp(join(tmpdir(), 'gfsc-upload-orphan-'))const db = new MemoryDb()