Skip to content

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

TypeScript

fix: preserve password callback diagnostic provenance

Josuédb67533dbceb
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index addda9e45..a45491636 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.785",
+ "version": "0.1.786",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.785",
+ "version": "0.1.786",
"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 3d2fc31f1..8b27b361e 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.785",
+ "version": "0.1.786",
"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.ts
index 1af2d3a1f..592100bf4 100644
--- a/control-api/src/http/errorHandler.ts
+++ b/control-api/src/http/errorHandler.ts
@@ -3,7 +3,10 @@ import { ApiException } from '@kubernetes/client-node'
import { STATUS_CODES } from 'node:http'
import { clearRateLimitHeaders } from '../middleware/rateLimitMiddleware.js'
import { rootLogger } from '../observability/logger.js'
-import { PasswordMutationInternalError } from '../services/auth/passwordAdmissionError.js'
+import {
+ PasswordMutationInternalError,
+ passwordMutationCauseKind,
+} from '../services/auth/passwordAdmissionError.js'
import { memberRegistrationErrorResponse } from '../services/memberRegistrationErrors.js'
// Only errors we construct with these codes are ever forwarded verbatim by the
@@ -161,6 +164,15 @@ export function clerumErrorHandler(
const log = req.log ?? rootLogger
if (err instanceof PasswordMutationInternalError) {
+ log.error(
+ {
+ event: 'password_mutation_callback_failed',
+ stage: 'mutation_callback',
+ causeKind: passwordMutationCauseKind(err.cause),
+ correlationId,
+ },
+ 'password mutation callback failed'
+ )
clearRateLimitHeaders(res)
res.removeHeader('Retry-After')
res.setHeader('Cache-Control', 'no-store')
diff --git a/control-api/src/services/auth/passwordAdmissionError.ts b/control-api/src/services/auth/passwordAdmissionError.ts
index 2bafff79a..504655b17 100644
--- a/control-api/src/services/auth/passwordAdmissionError.ts
+++ b/control-api/src/services/auth/passwordAdmissionError.ts
@@ -17,9 +17,27 @@ export class PasswordAdmissionError extends Error {
/** Safe, non-retryable result for an unexpected password mutation callback failure. */
export class PasswordMutationInternalError extends Error {
readonly code = 'internal_error'
+ readonly cause: unknown
- constructor() {
+ constructor(cause?: unknown) {
super('internal_error')
this.name = 'PasswordMutationInternalError'
+ Object.defineProperty(this, 'cause', { value: cause, enumerable: false })
}
}
+
+export type PasswordMutationCauseKind =
+ 'type_error' | 'reference_error' | 'range_error' | 'syntax_error' | 'error' | 'non_error'
+
+export function passwordMutationCauseKind(cause: unknown): PasswordMutationCauseKind {
+ try {
+ if (cause instanceof TypeError) return 'type_error'
+ if (cause instanceof ReferenceError) return 'reference_error'
+ if (cause instanceof RangeError) return 'range_error'
+ if (cause instanceof SyntaxError) return 'syntax_error'
+ if (cause instanceof Error) return 'error'
+ } catch {
+ // A hostile thrown value must not prevent the sanitized response path.
+ }
+ return 'non_error'
+}
diff --git a/control-api/src/services/auth/passwordWorkOwnership.ts b/control-api/src/services/auth/passwordWorkOwnership.ts
index 11d07bcb1..21fdac9ae 100644
--- a/control-api/src/services/auth/passwordWorkOwnership.ts
+++ b/control-api/src/services/auth/passwordWorkOwnership.ts
@@ -106,11 +106,7 @@ async function reservePasswordWork() {
if (error instanceof PasswordAdmissionError || queryFailures.has(error)) {
throw error
}
- logger.error(
- { event: 'password_mutation_callback_failed' },
- 'password mutation callback failed'
- )
- throw new PasswordMutationInternalError()
+ throw new PasswordMutationInternalError(error)
}
await releaseOwned(db)
return outcome
diff --git a/control-api/test/http.errorHandler.passwordInternalError.test.ts b/control-api/test/http.errorHandler.passwordInternalError.test.ts
index db11175e5..db339f320 100644
--- a/control-api/test/http.errorHandler.passwordInternalError.test.ts
+++ b/control-api/test/http.errorHandler.passwordInternalError.test.ts
@@ -1,13 +1,20 @@
-import { describe, expect, it } from 'vitest'
+import { describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
import { clerumErrorHandler } from '../src/http/errorHandler.js'
import { correlationIdMiddleware } from '../src/middleware/correlationId.js'
import { PasswordMutationInternalError } from '../src/services/auth/passwordAdmissionError.js'
-function appThrowingPasswordMutationInternalError() {
+function appThrowingPasswordMutationInternalError(
+ cause: unknown,
+ logError: ReturnType<typeof vi.fn>
+) {
const app = express()
app.use(correlationIdMiddleware)
+ app.use((req, _res, next) => {
+ req.log = { error: logError } as unknown as NonNullable<typeof req.log>
+ next()
+ })
app.post('/boom', (_req, res, next) => {
res.set({
'RateLimit-Limit': '10',
@@ -18,31 +25,55 @@ function appThrowingPasswordMutationInternalError() {
'X-RateLimit-Reset': '1776000000',
'Retry-After': '30',
})
- next(new PasswordMutationInternalError())
+ next(new PasswordMutationInternalError(cause))
})
app.use(clerumErrorHandler)
return app
}
describe('clerumErrorHandler password mutation internal response', () => {
- it('clears inherited rate metadata and marks the sanitized 500 as non-cacheable', async () => {
- const correlationId = '0f1056c1-0f18-4818-8183-000000000085'
- const response = await request(appThrowingPasswordMutationInternalError())
- .post('/boom')
- .set('x-correlation-id', correlationId)
+ it.each([
+ ['TypeError', () => new TypeError('private callback diagnostic'), 'type_error'],
+ ['ReferenceError', () => new ReferenceError('private callback diagnostic'), 'reference_error'],
+ ['RangeError', () => new RangeError('private callback diagnostic'), 'range_error'],
+ ['SyntaxError', () => new SyntaxError('private callback diagnostic'), 'syntax_error'],
+ ['Error', () => new Error('private callback diagnostic'), 'error'],
+ ['non-Error', () => 'private callback diagnostic', 'non_error'],
+ ] as const)(
+ 'logs only safe callback provenance for %s and marks the sanitized 500 non-cacheable',
+ async (_label, makeCause, causeKind) => {
+ const correlationId = '0f1056c1-0f18-4818-8183-000000000085'
+ const logError = vi.fn()
+ const response = await request(
+ appThrowingPasswordMutationInternalError(makeCause(), logError)
+ )
+ .post('/boom')
+ .set('x-correlation-id', correlationId)
- expect(response.status).toBe(500)
- expect(response.body).toEqual({
- error: 'internal_error',
- correlationId,
- })
- expect(response.headers['cache-control']).toBe('no-store')
- expect(response.headers['retry-after']).toBeUndefined()
- expect(response.headers['ratelimit-limit']).toBeUndefined()
- expect(response.headers['ratelimit-remaining']).toBeUndefined()
- expect(response.headers['ratelimit-policy']).toBeUndefined()
- expect(response.headers['x-ratelimit-limit']).toBeUndefined()
- expect(response.headers['x-ratelimit-remaining']).toBeUndefined()
- expect(response.headers['x-ratelimit-reset']).toBeUndefined()
- })
+ expect(response.status).toBe(500)
+ expect(response.body).toEqual({
+ error: 'internal_error',
+ correlationId,
+ })
+ expect(response.headers['cache-control']).toBe('no-store')
+ expect(response.headers['retry-after']).toBeUndefined()
+ expect(response.headers['ratelimit-limit']).toBeUndefined()
+ expect(response.headers['ratelimit-remaining']).toBeUndefined()
+ expect(response.headers['ratelimit-policy']).toBeUndefined()
+ expect(response.headers['x-ratelimit-limit']).toBeUndefined()
+ expect(response.headers['x-ratelimit-remaining']).toBeUndefined()
+ expect(response.headers['x-ratelimit-reset']).toBeUndefined()
+ expect(logError).toHaveBeenCalledTimes(1)
+ expect(logError).toHaveBeenCalledWith(
+ {
+ event: 'password_mutation_callback_failed',
+ stage: 'mutation_callback',
+ causeKind,
+ correlationId,
+ },
+ 'password mutation callback failed'
+ )
+ expect(JSON.stringify(logError.mock.calls)).not.toContain('private callback diagnostic')
+ }
+ )
})
diff --git a/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts
index 3e7f6a38d..9a0c57494 100644
--- a/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts
+++ b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts
@@ -194,26 +194,31 @@ realPg('password-work operation preservation on PostgreSQL 16', () => {
it('rolls back a prior database write when the mutation callback throws a TypeError', async () => {
const fixture = await resetFixture()
+ const callbackFailure = new TypeError('private callback diagnostic')
const originalName = (
await local.query('SELECT name FROM users WHERE id = $1', [fixture.userId])
).rows[0].name
- await expect(
- runPasswordMutation(
- async transaction => {
- await transaction.query('UPDATE users SET name = $2 WHERE id = $1', [
- fixture.userId,
- 'Must be rolled back',
- ])
- throw new TypeError('private callback diagnostic')
- },
- { mode: 'standard' }
- )
- ).rejects.toMatchObject({
+ const failure = await runPasswordMutation(
+ async transaction => {
+ await transaction.query('UPDATE users SET name = $2 WHERE id = $1', [
+ fixture.userId,
+ 'Must be rolled back',
+ ])
+ throw callbackFailure
+ },
+ { mode: 'standard' }
+ ).catch(error => error)
+
+ expect(failure).toMatchObject({
name: 'PasswordMutationInternalError',
code: 'internal_error',
message: 'internal_error',
})
+ const causeDescriptor = Object.getOwnPropertyDescriptor(failure, 'cause')
+ expect(causeDescriptor?.value).toBe(callbackFailure)
+ expect(causeDescriptor?.enumerable).toBe(false)
+ expect(JSON.stringify(failure)).not.toContain(callbackFailure.message)
const persistedName = (
await local.query('SELECT name FROM users WHERE id = $1', [fixture.userId])
diff --git a/control-api/test/passwordWorkOwnership.operation.test.ts b/control-api/test/passwordWorkOwnership.operation.test.ts
index ad47d3fca..2dbbe45c5 100644
--- a/control-api/test/passwordWorkOwnership.operation.test.ts
+++ b/control-api/test/passwordWorkOwnership.operation.test.ts
@@ -76,23 +76,29 @@ describe('password-work reservation preservation', () => {
it.each([
['TypeError', () => new TypeError('private callback details')],
['Error', () => new Error('private callback details')],
+ ['non-Error', () => 'private callback details'],
])(
- 'classifies an unexpected %s from commitWith as a non-retryable internal error',
+ 'retains an unexpected %s callback failure as a non-enumerable cause',
async (_name, makeError) => {
- await expect(
- withPasswordWorkLease(lease =>
- lease.commitWith(
- async () => {
- throw makeError()
- },
- { mode: 'one_use_recovery' }
- )
+ const cause = makeError()
+ const error = await withPasswordWorkLease(lease =>
+ lease.commitWith(
+ async () => {
+ throw cause
+ },
+ { mode: 'one_use_recovery' }
)
- ).rejects.toMatchObject({
+ ).catch(failure => failure)
+
+ expect(error).toMatchObject({
name: 'PasswordMutationInternalError',
code: 'internal_error',
message: 'internal_error',
})
+ const causeDescriptor = Object.getOwnPropertyDescriptor(error, 'cause')
+ expect(causeDescriptor?.value).toBe(cause)
+ expect(causeDescriptor?.enumerable).toBe(false)
+ expect(JSON.stringify(error)).not.toContain('private callback details')
expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(1)
}
)