Skip to content

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

TypeScript

test(mcp-host): extend the log key guard to nested and wrapped payloads

Alfredo Lopezefbcda468aae
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index d36460e4b..126c0d67a 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.444",
+ "version": "0.1.445",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.444",
+ "version": "0.1.445",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index f850f608b..e189791ca 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.444",
+ "version": "0.1.445",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/agent/__tests__/taskExecutor.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
index 289283bc7..8ffda2d2e 100644
--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts
+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
@@ -2581,7 +2581,7 @@ describe('TaskExecutor subscription context window (#731 R3-4)', () => {
// The logger binds its sink at import time, so load it after the spy.
const fresh = await import('../../logger')
fresh.logger.info(...resolved)
- // Control: the same sink still redacts secret-named keys.
+ // Control: the same logger still redacts secret-named keys before the sink.
fresh.logger.info(
{
accessToken: 'fixture-access-token',
@@ -2616,6 +2616,7 @@ describe('TaskExecutor subscription context window (#731 R3-4)', () => {
expect(control).not.toContain('fixture-')
} finally {
sink.mockRestore()
+ // Importing the logger replaces console.log/error/warn process-wide.
console.log = previousConsole.log
console.error = previousConsole.error
console.warn = previousConsole.warn
diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.ts
index e94ad31bd..42a7b50d2 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -1007,7 +1007,8 @@ export class TaskExecutor {
taskId: this.taskId,
provider,
model: this.deps.modelName,
- // Not `contextWindowTokens`: the logger redacts every key matching /token/i.
+ // Not `contextWindowTokens`: SENSITIVE_KEY_RE in logger.ts redacts it;
+ // logPayloadKeys.test.ts rejects such keys.
contextWindow: contextWindowTokens,
source,
},
diff --git a/mcp-host/src/logPayloadKeys.test.ts b/mcp-host/src/logPayloadKeys.test.ts
index 8aa3ebc74..5b7eaeaa5 100644
--- a/mcp-host/src/logPayloadKeys.test.ts
+++ b/mcp-host/src/logPayloadKeys.test.ts
@@ -5,32 +5,87 @@ import ts from 'typescript'
import { redactUnknown } from './logger'
// A log field whose key the logger redacts reaches the emitted line as
-// "[Redacted]", so a count such as `contextWindowTokens` is lost (#803). This
-// scans every log call in src and rejects object-literal keys that
-// `redactUnknown` would replace. Spread and variable payloads are not resolved.
+// "[Redacted]", and a key outside SAFE_OBJECT_KEY is dropped from it, so a
+// count such as `contextWindowTokens` is lost (#803). This scans
+// .debug/.info/.warn/.error/.log calls in non-test files under src and rejects
+// such keys in object-literal arguments, including nested objects, array
+// items, `as`/`satisfies`/conditional wrappers and literal computed keys.
+// Payloads passed as variables or spreads of variables are not resolved.
const LOG_METHODS = new Set(['debug', 'info', 'warn', 'error', 'log'])
type KeyUse = { file: string; line: number; key: string }
-function isRedactedKey(key: string): boolean {
- return (redactUnknown({ [key]: 1 }) as Record<string, unknown>)[key] === '[Redacted]'
+function isLostKey(key: string): boolean {
+ const out = redactUnknown({ [key]: 1 }) as Record<string, unknown>
+ return !(key in out) || out[key] === '[Redacted]'
}
-function redactedLogKeys(file: string, source: string): { calls: number; hits: KeyUse[] } {
+function literalKey(name: ts.PropertyName): string | undefined {
+ if (
+ ts.isIdentifier(name) ||
+ ts.isStringLiteral(name) ||
+ ts.isNoSubstitutionTemplateLiteral(name)
+ ) {
+ return name.text
+ }
+ if (
+ ts.isComputedPropertyName(name) &&
+ (ts.isStringLiteral(name.expression) || ts.isNoSubstitutionTemplateLiteral(name.expression))
+ ) {
+ return name.expression.text
+ }
+ return undefined
+}
+
+function unwrap(expr: ts.Expression): ts.Expression {
+ let current = expr
+ while (
+ ts.isAsExpression(current) ||
+ ts.isSatisfiesExpression(current) ||
+ ts.isParenthesizedExpression(current) ||
+ ts.isNonNullExpression(current) ||
+ ts.isTypeAssertionExpression(current)
+ ) {
+ current = current.expression
+ }
+ return current
+}
+
+function redactedLogKeys(
+ file: string,
+ source: string
+): { calls: number; hits: KeyUse[]; visited: KeyUse[] } {
const sf = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true)
let calls = 0
const hits: KeyUse[] = []
+ const visited: KeyUse[] = []
+ const visitValue = (expr: ts.Expression): void => {
+ const value = unwrap(expr)
+ if (ts.isObjectLiteralExpression(value)) visitObject(value)
+ else if (ts.isArrayLiteralExpression(value)) value.elements.forEach(visitValue)
+ else if (ts.isConditionalExpression(value)) {
+ visitValue(value.whenTrue)
+ visitValue(value.whenFalse)
+ }
+ }
const visitObject = (obj: ts.ObjectLiteralExpression): void => {
for (const prop of obj.properties) {
- const name = ts.isSpreadAssignment(prop) ? undefined : prop.name
- if (name && (ts.isIdentifier(name) || ts.isStringLiteral(name)) && isRedactedKey(name.text)) {
- const line = sf.getLineAndCharacterOfPosition(prop.getStart(sf)).line + 1
- hits.push({ file, line, key: name.text })
+ if (ts.isSpreadAssignment(prop)) {
+ visitValue(prop.expression)
+ continue
}
- if (ts.isPropertyAssignment(prop) && ts.isObjectLiteralExpression(prop.initializer)) {
- visitObject(prop.initializer)
+ const key = prop.name && literalKey(prop.name)
+ if (key !== undefined) {
+ const use = {
+ file,
+ line: sf.getLineAndCharacterOfPosition(prop.getStart(sf)).line + 1,
+ key,
+ }
+ visited.push(use)
+ if (isLostKey(key)) hits.push(use)
}
+ if (ts.isPropertyAssignment(prop)) visitValue(prop.initializer)
}
}
const visit = (node: ts.Node): void => {
@@ -40,14 +95,12 @@ function redactedLogKeys(file: string, source: string): { calls: number; hits: K
LOG_METHODS.has(node.expression.name.text)
) {
calls += 1
- for (const arg of node.arguments) {
- if (ts.isObjectLiteralExpression(arg)) visitObject(arg)
- }
+ node.arguments.forEach(visitValue)
}
ts.forEachChild(node, visit)
}
visit(sf)
- return { calls, hits }
+ return { calls, hits, visited }
}
function sourceFiles(dir: string): string[] {
@@ -68,37 +121,57 @@ function sourceFiles(dir: string): string[] {
}
describe('log payload keys survive redaction (#803)', () => {
- it('detects redacted keys in a log payload, nested ones included', () => {
+ it('detects redacted and dropped keys in every payload shape it resolves', () => {
const { calls, hits } = redactedLogKeys(
'fixture.ts',
[
"logger.info({ event: 'x', contextWindowTokens: 1, nested: { refreshToken: 'r' } }, 'm')",
"console.warn('[X] y', { ok: true, 'api-key': 'k' })",
"logger.debug({ contextWindow: 1, source: 'catalog' }, 'clean')",
+ "logger.info({ contextWindowTokens }, 'shorthand, the #803 shape')",
+ "logger.info({ accessToken: 1 } as LogFields, 'as')",
+ "logger.info(({ secret: 1 }) satisfies LogFields, 'parenthesized satisfies')",
+ "logger.info(ok ? { password: 1 } : {}, 'conditional')",
+ "logger.info({ items: [{ cookie: 1 }] }, 'array item')",
+ "logger.info({ ['authorization']: 1, [`dsn`]: 2 }, 'computed')",
+ "logger.info({ 'context window': 1 }, 'dropped by SAFE_OBJECT_KEY')",
].join('\n')
)
- expect(calls).toBe(3)
- expect(hits).toEqual([
- { file: 'fixture.ts', line: 1, key: 'contextWindowTokens' },
- { file: 'fixture.ts', line: 1, key: 'refreshToken' },
- { file: 'fixture.ts', line: 2, key: 'api-key' },
+ expect(calls).toBe(10)
+ expect(hits.map(h => `${h.line} ${h.key}`)).toEqual([
+ '1 contextWindowTokens',
+ '1 refreshToken',
+ '2 api-key',
+ '4 contextWindowTokens',
+ '5 accessToken',
+ '6 secret',
+ '7 password',
+ '8 cookie',
+ '9 authorization',
+ '9 dsn',
+ '10 context window',
])
})
- it('no log call in src passes a key the logger would redact', () => {
+ it('no log call in src passes a key the logger would redact or drop', () => {
const files = sourceFiles(__dirname)
let calls = 0
const hits: string[] = []
+ const visited: KeyUse[] = []
for (const file of files) {
const rel = path.relative(__dirname, file)
const result = redactedLogKeys(rel, fs.readFileSync(file, 'utf8'))
calls += result.calls
+ visited.push(...result.visited)
hits.push(...result.hits.map(h => `${h.file}:${h.line} ${h.key}`))
}
- // Witness: the scan read the real tree, so an empty result is not an empty scan.
- expect(files).toContain(path.join(__dirname, 'agent', 'taskExecutor.ts'))
+ // Witness: the scan read the real tree and reached the #803 call site, so
+ // an empty result is not an empty scan.
expect(files.length).toBeGreaterThan(200)
expect(calls).toBeGreaterThan(400)
+ expect(visited).toContainEqual(
+ expect.objectContaining({ file: path.join('agent', 'taskExecutor.ts'), key: 'contextWindow' })
+ )
expect(hits).toEqual([])
})
})