From fb143be8f086e0d50b2a60901efff66bc7932ee2 Mon Sep 17 00:00:00 2001 From: kristian4res <57638182+kristian4res@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:50:44 +0000 Subject: [PATCH] feat/test: sanitize log messages whilst preserving readability --- server/logger/cloudLogging.ts | 12 +++++++---- server/tests/logger/cloudLogging.test.ts | 26 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 server/tests/logger/cloudLogging.test.ts diff --git a/server/logger/cloudLogging.ts b/server/logger/cloudLogging.ts index 791d41d..d4ef996 100644 --- a/server/logger/cloudLogging.ts +++ b/server/logger/cloudLogging.ts @@ -3,6 +3,12 @@ import { Logging } from "@google-cloud/logging"; import { IncomingMessage } from "http"; import { AuditLog } from "../interfaces/logger"; +export function formatLogMessage(text: string): string { + const message = text.replace(/[^\x20-\x7E\r\n]+/g, ""); + const logFormat = "AUDIT_LOG: message"; + return logFormat.replace("message", message); +} + export default class AuditLogger { projectId: string; logger: Logging; @@ -15,14 +21,12 @@ export default class AuditLogger { } info(logger: IncomingMessage["log"], message: string): void { - const logFormat = "AUDIT_LOG: message"; - const log = logFormat.replace("message", message); + const log = formatLogMessage(message); logger.info(log); } error(logger: IncomingMessage["log"], message: string): void { - const logFormat = "AUDIT_LOG: message"; - const log = logFormat.replace("message", message); + const log = formatLogMessage(message); logger.error(log); } diff --git a/server/tests/logger/cloudLogging.test.ts b/server/tests/logger/cloudLogging.test.ts new file mode 100644 index 0000000..dc7f776 --- /dev/null +++ b/server/tests/logger/cloudLogging.test.ts @@ -0,0 +1,26 @@ +import { formatLogMessage } from "../../logger/cloudLogging"; + +describe("formatLogMessage utility function ensures complex log messages are sanitized but still readable", () => { + it("should preserve newlines and carriage returns", () => { + const inputMessage = "Error: Something went wrong\nDetails: Invalid input\r\nPlease try again."; + const expectedOutput = "AUDIT_LOG: Error: Something went wrong\nDetails: Invalid input\r\nPlease try again."; + + const formattedMessage = formatLogMessage(inputMessage); + + console.log("Expected log format:", JSON.stringify(expectedOutput)); + console.log("Received log format:", JSON.stringify(formattedMessage)); + + expect(formattedMessage).toBe(expectedOutput); + }); + + it("should remove non-printable characters", () => { + const message = "Error: Something went wrong\x01\x02\n at FunctionName (file.js:10:15)\x03\x04\n at AnotherFunction (file.js:20:25)\r\n at YetAnotherFunction (file.js:30:35)"; + const expectedOutput = "AUDIT_LOG: Error: Something went wrong\n at FunctionName (file.js:10:15)\n at AnotherFunction (file.js:20:25)\r\n at YetAnotherFunction (file.js:30:35)"; + const formattedMessage = formatLogMessage(message); + + console.log("Expected log format:", JSON.stringify(expectedOutput)); + console.log("Received log format:", JSON.stringify(formattedMessage)); + + expect(formattedMessage).toBe(expectedOutput); + }); +}); \ No newline at end of file