Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(logger): guard the message and trace slots

Yudhi Armyndharis9bc11ec0eba7
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index c6703e0d8..e9bbac467 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -302,6 +302,79 @@ describe('LoggerService', () => {
});
});
+ // Nest passes the framework's own message and trace through, so the two slots hold whatever it was handed.
+ describe('a message or trace that cannot be read', () => {
+ const revokedProxy = (): object => {
+ const { proxy, revoke } = Proxy.revocable({}, {});
+ revoke();
+ return proxy;
+ };
+ const throwingPrototype = (): object =>
+ new Proxy(
+ {},
+ {
+ getPrototypeOf: (): never => {
+ throw new Error('getPrototypeOf');
+ },
+ },
+ );
+ const throwingMessage = (): Error =>
+ Object.defineProperty(new Error('boom'), 'message', {
+ get: (): never => {
+ throw new Error('message getter');
+ },
+ });
+
+ describe.each([LogFormat.JSON, LogFormat.PRETTY])('in %s output', format => {
+ beforeEach(() => {
+ LoggerService.setLogFormat(format);
+ });
+
+ it.each([
+ ['a revoked proxy', revokedProxy],
+ ['a proxy whose prototype read throws', throwingPrototype],
+ ['an Error whose message getter throws', throwingMessage],
+ ])('writes %s passed as the message as [Unprintable]', (_name, unreadable) => {
+ const errorSpy = jest.spyOn(console, 'error');
+
+ expect(() => logger.log(unreadable())).not.toThrow();
+ expect(() => logger.error(unreadable())).not.toThrow();
+
+ expect(getRawOutput(consoleSpy)).toContain('[Unprintable]');
+ expect(getRawOutput(errorSpy)).toContain('[Unprintable]');
+ });
+
+ it.each([
+ ['a revoked proxy', revokedProxy],
+ ['a proxy whose prototype read throws', throwingPrototype],
+ ])('writes %s passed as the trace as [Unprintable] and keeps the message', (_name, unreadable) => {
+ const errorSpy = jest.spyOn(console, 'error');
+
+ expect(() => logger.error('saved-message', unreadable())).not.toThrow();
+
+ expect(getRawOutput(errorSpy)).toContain('saved-message');
+ expect(getRawOutput(errorSpy)).toContain('[Unprintable]');
+ });
+
+ it('keeps the message when an Error whose message getter throws is passed as the trace', () => {
+ const errorSpy = jest.spyOn(console, 'error');
+
+ expect(() => logger.error('saved-message', throwingMessage())).not.toThrow();
+
+ expect(getRawOutput(errorSpy)).toContain('saved-message');
+ });
+
+ it('keeps a readable trace when the message cannot be read', () => {
+ const errorSpy = jest.spyOn(console, 'error');
+
+ logger.error(revokedProxy(), 'saved-trace');
+
+ expect(getRawOutput(errorSpy)).toContain('[Unprintable]');
+ expect(getRawOutput(errorSpy)).toContain('saved-trace');
+ });
+ });
+ });
+
describe('error', () => {
it('should log error messages to console.error', () => {
const errorSpy = jest.spyOn(console, 'error');
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index 7497ccb0a..1f37a78b5 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -80,6 +80,9 @@ export function redactSecrets(value: unknown, options: RedactOptions = {}): unkn
return walk(value, []);
}
+/** Written in place of a message or trace that cannot be read. */
+const UNPRINTABLE = '[Unprintable]';
+
/** A non-string, non-Error message as one line of text (redacted like metadata, never throwing). */
function stringifyMessage(message: unknown): string {
try {
@@ -88,7 +91,7 @@ function stringifyMessage(message: unknown): string {
try {
return String(message);
} catch {
- return '[Unprintable]';
+ return UNPRINTABLE;
}
}
}
@@ -174,18 +177,29 @@ export class LoggerService implements NestLoggerService {
private writeLog(level: LogLevel, rawMessage: unknown, context?: string | LogContext, rawTrace?: unknown): void {
if (!this.shouldLog(level)) return;
- // An Error serializes to `{}`, so take its message and stack apart instead of losing both.
- if (rawMessage instanceof Error) {
- rawTrace ??= rawMessage.stack;
- rawMessage = rawMessage.message;
+ // The message and trace are whatever the caller (or Nest) handed over, so reading them is guarded like the metadata.
+ let message: string;
+ try {
+ // An Error serializes to `{}`, so take its message and stack apart instead of losing both.
+ if (rawMessage instanceof Error) {
+ rawTrace ??= rawMessage.stack;
+ rawMessage = rawMessage.message;
+ }
+ message = typeof rawMessage === 'string' ? rawMessage : stringifyMessage(rawMessage);
+ } catch {
+ message = UNPRINTABLE;
+ }
+ let trace: string | undefined;
+ try {
+ trace =
+ rawTrace === undefined || typeof rawTrace === 'string'
+ ? rawTrace
+ : rawTrace instanceof Error
+ ? (rawTrace.stack ?? rawTrace.message)
+ : stringifyMessage(rawTrace);
+ } catch {
+ trace = UNPRINTABLE;
}
- const message = typeof rawMessage === 'string' ? rawMessage : stringifyMessage(rawMessage);
- const trace =
- rawTrace === undefined || typeof rawTrace === 'string'
- ? rawTrace
- : rawTrace instanceof Error
- ? (rawTrace.stack ?? rawTrace.message)
- : stringifyMessage(rawTrace);
const timestamp = new Date().toISOString();
const contextName = typeof context === 'string' ? context : this.context;