Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): sanitize raw K8s error message and stack in internal logs
Diego Zacariasf3e857ab78ea
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 4f27babd1..31b8ca7a5 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","dependencies": {"@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-api/package.json b/control-api/package.jsonindex 1c47faa52..c73ab25e9 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/http/errorHandler.ts b/control-api/src/http/errorHandler.tsindex 09716564a..cc9ccac70 100644--- a/control-api/src/http/errorHandler.ts+++ b/control-api/src/http/errorHandler.ts@@ -92,6 +92,29 @@ function clientErrorMessage(err: unknown, status: number): string {return err instanceof Error ? err.message : 'Bad Request'}+/**+ * Log-safe rendering of an error's message. A raw @kubernetes/client-node+ * ApiException `.message` embeds the metav1.Status body AND every apiserver+ * response header (Audit-Id, x-kubernetes-pf-*) — writing it to a log puts+ * infrastructure PID/PII into internal logs (S1). Collapse K8s exceptions to the+ * same sanitized text the client sees (metav1.Status `body.message` or the+ * status text, never headers); non-K8s errors keep their author-controlled+ * detail. Used for EVERY branch that logs an error message, not just the 4xx one.+ */+function safeErrorLogMessage(err: unknown): string {+ if (isK8sApiException(err)) return clientErrorMessage(err, extractErrorStatus(err) ?? 500)+ return err instanceof Error ? err.message : String(err)+}++/**+ * Log-safe stack: a K8s ApiException's `.stack` begins with its `.message`, so it+ * leaks the same headers — drop it for K8s exceptions and keep it otherwise.+ */+function safeErrorLogStack(err: unknown): string | undefined {+ if (isK8sApiException(err)) return undefined+ return err instanceof Error ? err.stack : undefined+}+/*** Global Express error handler for control-api.*@@ -123,7 +146,7 @@ export function clerumErrorHandler({event: memberRegistration.error,correlationId,- err: err instanceof Error ? err.message : String(err),+ err: safeErrorLogMessage(err),},'member registration unavailable')@@ -145,7 +168,7 @@ export function clerumErrorHandler(event: 'forwarded_client_error',correlationId,status: errStatus,- err: err instanceof Error ? err.message : String(err),+ err: safeErrorLogMessage(err),},'forwarded client error from upstream')@@ -174,7 +197,7 @@ export function clerumErrorHandler(correlationId,status: integrationStatus,code: integrationCode,- err: err instanceof Error ? err.message : String(err),+ err: safeErrorLogMessage(err),},'forwarded registry integration error')@@ -190,8 +213,8 @@ export function clerumErrorHandler({event: 'unhandled_error',correlationId,- err: err instanceof Error ? err.message : String(err),- stack: err instanceof Error ? err.stack : undefined,+ err: safeErrorLogMessage(err),+ stack: safeErrorLogStack(err),},'unhandled error')diff --git a/control-api/test/http.errorHandler.k8sStatus.test.ts b/control-api/test/http.errorHandler.k8sStatus.test.tsindex 7d2943ccc..28179f147 100644--- a/control-api/test/http.errorHandler.k8sStatus.test.ts+++ b/control-api/test/http.errorHandler.k8sStatus.test.ts@@ -1,5 +1,6 @@-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import express from 'express'+import type { Request } from 'express'import { ApiException } from '@kubernetes/client-node'import request from 'supertest'import { clerumErrorHandler } from '../src/http/errorHandler.js'@@ -19,6 +20,22 @@ function appThrowing(err: unknown) {return app}+/**+ * Like `appThrowing` but injects a spy logger on `req.log` so a test can assert+ * on what the handler writes to the INTERNAL log (not just the client body).+ */+function appThrowingWithLogSpy(err: unknown) {+ const warn = vi.fn()+ const spyLogger = { warn, error: vi.fn(), info: vi.fn(), debug: vi.fn() }+ const app = express()+ app.get('/boom', (req: Request, _res, next) => {+ ;(req as unknown as { log: unknown }).log = spyLogger+ next(err)+ })+ app.use(clerumErrorHandler)+ return { app, warn }+}+describe('clerumErrorHandler — K8s status forwarding (UT-2a)', () => {it('forwards {code:422} as 422 with a sanitized (non-leaking) body', async () => {const res = await request(appThrowing({ code: 422, message: 'raw k8s internals' })).get('/boom')@@ -95,4 +112,44 @@ describe('clerumErrorHandler — K8s status forwarding (UT-2a)', () => {// The safe metav1.Status body.message may surface as the client-facing text.expect(res.body.error).toBe('hosts.clerum.io "product-agent" already exists')})++ /**+ * S1 follow-up (info disclosure to INTERNAL logs): the 4xx branch already+ * sanitizes the CLIENT body, but the internal `log.warn` still logged the raw+ * `err.message` of a K8s ApiException — which embeds the metav1.Status body AND+ * every apiserver header (Audit-Id, x-kubernetes-pf-*). That is PID/PII of+ * infrastructure in internal logs. The logged message must be sanitized the+ * same way the client body is: status + a safe message, never the headers or+ * the raw `.message`.+ */+ it('does NOT log the raw ApiException message (headers/audit-id) for a forwarded 4xx', async () => {+ const body = {+ kind: 'Status',+ apiVersion: 'v1',+ status: 'Failure',+ message: 'hosts.clerum.io "product-agent" already exists',+ reason: 'AlreadyExists',+ code: 409,+ }+ const headers = {+ 'audit-id': 'a1b2c3-secret-audit-uuid',+ 'x-kubernetes-pf-flowschema-uid': 'internal-flowschema-uid',+ 'content-type': 'application/json',+ }+ const err = new ApiException(409, 'Conflict', body, headers)++ // Sanity: the real producer really does embed the leak in `.message`.+ expect(err.message).toContain('Headers:')+ expect(err.message).toContain('audit-id')++ const { app, warn } = appThrowingWithLogSpy(err)+ await request(app).get('/boom').expect(409)++ expect(warn).toHaveBeenCalled()+ const logged = JSON.stringify(warn.mock.calls)+ expect(logged).not.toContain('Headers:')+ expect(logged).not.toContain('audit-id')+ expect(logged).not.toContain('x-kubernetes-pf')+ expect(logged).not.toContain(err.message)+ })})