Skip to content

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

TypeScript

fix(control-api): hash the bucket key in rate-limit logs

Alfredo Lopez72e3fe195924
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index e21fcf1e4..c0f7b6769 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.610",
+ "version": "0.1.611",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.610",
+ "version": "0.1.611",
"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.json
index 7e01ada66..b9757d8d4 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.610",
+ "version": "0.1.611",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/middleware/rateLimitMiddleware.ts b/control-api/src/middleware/rateLimitMiddleware.ts
index 47d2cbbcb..63f01c186 100644
--- a/control-api/src/middleware/rateLimitMiddleware.ts
+++ b/control-api/src/middleware/rateLimitMiddleware.ts
@@ -1,4 +1,5 @@
import { NextFunction, Request, Response } from 'express'
+import { createHash } from 'node:crypto'
import { rateLimitHitsTotal } from '../observability/metrics.js'
import {
RATE_LIMIT_BACKEND_RETRY_AFTER_SECONDS,
@@ -76,7 +77,8 @@ export function rateLimitMiddleware(opts: {
{
event: 'rate_limit_denied',
bucketType: opts.bucketType,
- bucketKey: key,
+ // Keys carry user ids (e.g. desktopUserId); log their SHA-256.
+ hashedKey: createHash('sha256').update(key).digest('hex'),
count: result.count,
maxPerMinute: opts.maxPerMinute,
},
diff --git a/control-api/src/services/rateLimiterService.ts b/control-api/src/services/rateLimiterService.ts
index b6eff5557..850799bd9 100644
--- a/control-api/src/services/rateLimiterService.ts
+++ b/control-api/src/services/rateLimiterService.ts
@@ -97,10 +97,12 @@ export async function checkAndIncrementWithQuery(
// Fail-open on DB errors — the limiter is an abuse gate, not a security
// boundary. Log and let the request through so a DB blip cannot take
// down all traffic.
+ // Bucket keys carry user and workload ids; the log gets the same SHA-256
+ // the external GFS limiter logs as hashedKey, so the lines still join.
rootLogger.warn(
{
event: 'rate_limit_db_error',
- bucketKey,
+ hashedKey: createHash('sha256').update(bucketKey).digest('hex'),
err: err instanceof Error ? err.message : String(err),
},
'rate limiter DB error, failing open'
diff --git a/control-api/test/middleware.rateLimitMiddleware.test.ts b/control-api/test/middleware.rateLimitMiddleware.test.ts
index e55b1424c..58c936f5a 100644
--- a/control-api/test/middleware.rateLimitMiddleware.test.ts
+++ b/control-api/test/middleware.rateLimitMiddleware.test.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
+import { createHash } from 'node:crypto'
import request from 'supertest'
import type { RateLimitCheck } from '../src/services/rateLimiterService.js'
@@ -116,3 +117,47 @@ describe('rateLimitMiddleware backend-unavailable policy', () => {
])
})
})
+
+describe('rateLimitMiddleware denial log', () => {
+ beforeEach(() => {
+ checkAndIncrement.mockReset()
+ hits.inc.mockReset()
+ })
+
+ it('logs the SHA-256 of the bucket key, never the key or the user id it carries', async () => {
+ const desktopUserId = '5f0c2d1e-7a4b-4c3d-9e8f-0a1b2c3d4e5f'
+ const bucketKey = `gfsgrants-ext-read:user:${desktopUserId}`
+ const warn = vi.fn()
+ checkAndIncrement.mockResolvedValueOnce(available(LIMIT + 1))
+ const app = express()
+ app.get(
+ '/limited',
+ (req, _res, next) => {
+ ;(req as unknown as { log: { warn: typeof warn } }).log = { warn }
+ next()
+ },
+ rateLimitMiddleware({
+ bucketType: 'gfs_grants_external_read',
+ maxPerMinute: LIMIT,
+ getBucketKey: () => bucketKey,
+ onBackendUnavailable: 'closed',
+ }),
+ (_req, res) => res.status(204).end()
+ )
+
+ const response = await request(app).get('/limited')
+
+ expect(response.status).toBe(429)
+ // Witness: the denial wrote exactly one line, and it is the denial event.
+ expect(warn).toHaveBeenCalledTimes(1)
+ const [payload] = warn.mock.calls[0] as [Record<string, unknown>]
+ expect(payload).toEqual({
+ event: 'rate_limit_denied',
+ bucketType: 'gfs_grants_external_read',
+ hashedKey: createHash('sha256').update(bucketKey).digest('hex'),
+ count: LIMIT + 1,
+ maxPerMinute: LIMIT,
+ })
+ expect(JSON.stringify(warn.mock.calls)).not.toContain(desktopUserId)
+ })
+})
diff --git a/control-api/test/services.rateLimiter.test.ts b/control-api/test/services.rateLimiter.test.ts
index 0c01964d1..2739d6e24 100644
--- a/control-api/test/services.rateLimiter.test.ts
+++ b/control-api/test/services.rateLimiter.test.ts
@@ -1,4 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { createHash } from 'node:crypto'
+import { rootLogger } from '../src/observability/logger.js'
import {
acquireRateLimitConcurrencyLease,
checkAndIncrement,
@@ -209,6 +211,27 @@ describe('rateLimiterService', () => {
expect(r.backendAvailable).toBe(false)
})
+ it('logs the SHA-256 of the bucket key on a DB error, never the key or the user id it carries', async () => {
+ const desktopUserId = '5f0c2d1e-7a4b-4c3d-9e8f-0a1b2c3d4e5f'
+ const bucketKey = `gfs-ext:pre:token:user:${desktopUserId}`
+ const warn = vi.spyOn(rootLogger, 'warn').mockImplementation(() => undefined)
+ try {
+ mockRateLimitPoolQuery.mockRejectedValueOnce(new Error('connection refused'))
+ const r = await checkAndIncrement(bucketKey, 5)
+ expect(r.backendAvailable).toBe(false)
+ // Witness: the DB error wrote exactly one line, and it is that event.
+ expect(warn).toHaveBeenCalledTimes(1)
+ expect(warn.mock.calls[0]?.[0]).toEqual({
+ event: 'rate_limit_db_error',
+ hashedKey: createHash('sha256').update(bucketKey).digest('hex'),
+ err: 'connection refused',
+ })
+ expect(JSON.stringify(warn.mock.calls)).not.toContain(desktopUserId)
+ } finally {
+ warn.mockRestore()
+ }
+ })
+
it('fails open when the limiter pool query returns empty rows', async () => {
mockRateLimitPoolQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 })
const r = await checkAndIncrement('test:bucket:emptyrows', 5)
diff --git a/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts b/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
index 8010d06ca..d1086edb3 100644
--- a/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
+++ b/control-api/test/services.rateLimiterPoolSaturation.realPostgres.integration.test.ts
@@ -1,5 +1,5 @@
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
-import { randomBytes } from 'node:crypto'
+import { createHash, randomBytes } from 'node:crypto'
import { performance } from 'node:perf_hooks'
import { Pool, type PoolClient } from 'pg'
import request from 'supertest'
@@ -200,12 +200,12 @@ describeRealPostgres('rate limiter under pool saturation', () => {
expect(results.every(result => result.count === 0)).toBe(true)
const dbErrors = warn.mock.calls
- .map(call => call[0] as { event?: string; bucketKey?: string; err?: string })
+ .map(call => call[0] as { event?: string; hashedKey?: string; err?: string })
.filter(payload => payload.event === 'rate_limit_db_error')
expect(dbErrors).toHaveLength(CONCURRENT_CHECKS)
// Witness that the failure is the pool's acquire timeout and nothing else.
for (const payload of dbErrors) {
- expect(payload.bucketKey).toBe(bucketKey)
+ expect(payload.hashedKey).toBe(createHash('sha256').update(bucketKey).digest('hex'))
expect(payload.err).toMatch(/timeout exceeded when trying to connect/)
}