Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): keep caller metadata from overwriting structural log fields

Yudhi Armyndharis99b5ee35d3eb
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index 27afb547b..8def36d5b 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -95,6 +95,50 @@ describe('LoggerService', () => {
});
});
+ // Plugin ctx.logger meta reaches the logger verbatim, so caller metadata must not be able to rewrite
+ // the fields a reader trusts to say who logged what, at which level.
+ describe('structural fields', () => {
+ it('keeps level, context, message, timestamp, trace and requestId out of reach of caller metadata', () => {
+ logger.log('[plugin-a] hi', {
+ level: 'error',
+ context: 'AuthService',
+ message: 'API key rotated',
+ timestamp: '1970-01-01T00:00:00.000Z',
+ trace: 'forged stack',
+ requestId: 'forged',
+ pluginId: 'plugin-a',
+ });
+
+ const output = getLogOutput(consoleSpy) as unknown as Record<string, unknown>;
+ expect(output.level).toBe('info');
+ expect(output.context).toBe('TestContext');
+ expect(output.message).toBe('[plugin-a] hi');
+ expect(output.timestamp).not.toBe('1970-01-01T00:00:00.000Z');
+ expect(output).not.toHaveProperty('trace');
+ expect(output).not.toHaveProperty('requestId');
+ expect(output.pluginId).toBe('plugin-a');
+ });
+
+ it("still takes error()'s trace and string context from its own parameters", () => {
+ const errorSpy = jest.spyOn(console, 'error');
+ logger.error('boom', 'real stack', 'OtherContext');
+
+ const output = getLogOutput(errorSpy);
+ expect(output.context).toBe('OtherContext');
+ expect(output.trace).toBe('real stack');
+ });
+
+ it("keeps error()'s own trace over one smuggled in its metadata", () => {
+ const errorSpy = jest.spyOn(console, 'error');
+ logger.error('boom', 'real stack', { trace: 'forged stack', sessionId: 's1' });
+
+ const output = getLogOutput(errorSpy);
+ expect(output.trace).toBe('real stack');
+ expect(output.context).toBe('TestContext');
+ expect(output.sessionId).toBe('s1');
+ });
+ });
+
describe('warn', () => {
it('should log warning messages to console.warn', () => {
const warnSpy = jest.spyOn(console, 'warn');
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 77056c4c2..05c1f8046 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -96,8 +96,7 @@ export class LoggerService implements NestLoggerService {
}
error(message: string, trace?: string, context?: string | LogContext): void {
- const ctx = typeof context === 'string' ? { context } : context;
- this.writeLog(LogLevel.ERROR, message, { ...ctx, trace });
+ this.writeLog(LogLevel.ERROR, message, context, trace);
}
warn(message: string, context?: string | LogContext): void {
@@ -112,7 +111,7 @@ export class LoggerService implements NestLoggerService {
this.writeLog(LogLevel.VERBOSE, message, context);
}
- private writeLog(level: LogLevel, message: string, context?: string | LogContext): void {
+ private writeLog(level: LogLevel, message: string, context?: string | LogContext, trace?: string): void {
if (!this.shouldLog(level)) return;
const timestamp = new Date().toISOString();
@@ -123,12 +122,17 @@ export class LoggerService implements NestLoggerService {
// Stamp every log line with the active request id (set by requestContextMiddleware) so a request
// can be traced across logs. Absent outside a request scope (boot, workers, cron).
const requestId = getRequestId();
+ // Caller metadata never replaces the structural fields: a plugin's ctx.logger meta arrives here
+ // verbatim, and a `level`, `context` or `message` key in it would otherwise forge the whole line.
const logEntry = {
timestamp,
level,
context: contextName,
message,
- ...metadata,
+ ...Object.fromEntries(
+ Object.entries(metadata).filter(([key]) => !STRUCTURAL_KEYS.has(key) && key !== 'requestId'),
+ ),
+ ...(trace !== undefined ? { trace } : {}),
...(requestId ? { requestId } : {}),
};