From cc4e9dfa08909f1d5fc5cba8030778a8db9c8b40 Mon Sep 17 00:00:00 2001 From: zkldi Date: Sun, 10 Oct 2021 17:00:05 +0100 Subject: [PATCH] Enable logger truncation to avoid getting DDoSd --- server/src/lib/logger/logger.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/server/src/lib/logger/logger.ts b/server/src/lib/logger/logger.ts index eb73f86c1..a37c04501 100644 --- a/server/src/lib/logger/logger.ts +++ b/server/src/lib/logger/logger.ts @@ -8,7 +8,7 @@ export type KtLogger = Logger & { severe: LeveledLogMethod }; const level = process.env.LOG_LEVEL ?? ServerConfig.LOG_LEVEL; -const formatExcessProperties = (meta: Record) => { +const formatExcessProperties = (meta: Record, limit = false) => { let i = 0; for (const key in meta) { const val = meta[key]; @@ -23,10 +23,24 @@ const formatExcessProperties = (meta: Record) => { return ""; } - return ` ${SafeJSONStringify(meta)}`; + const content = SafeJSONStringify(meta); + + return ` ${limit ? StrCap(content) : content}`; }; -const formatExcessPropertiesNoStack = (meta: Record, omitKeys: string[] = []) => { +function StrCap(string: string) { + if (string.length > 300) { + return `${string.slice(0, 297)}...`; + } + + return string; +} + +const formatExcessPropertiesNoStack = ( + meta: Record, + omitKeys: string[] = [], + limit = false +) => { let i = 0; const realMeta: Record = {}; @@ -49,21 +63,23 @@ const formatExcessPropertiesNoStack = (meta: Record, omitKeys: return ""; } - return ` ${SafeJSONStringify(realMeta)}`; + const content = SafeJSONStringify(realMeta); + + return ` ${limit ? StrCap(content) : content}`; }; const tachiPrintf = format.printf( ({ level, message, context = "tachi-root", timestamp, ...meta }) => `${timestamp} [${ Array.isArray(context) ? context.join(" | ") : context - }] ${level}: ${message}${formatExcessProperties(meta)}` + }] ${level}: ${message}${formatExcessProperties(meta, true)}` ); const tachiConsolePrintf = format.printf( ({ level, message, context = "tachi-root", timestamp, hideFromConsole, ...meta }) => `${timestamp} [${ Array.isArray(context) ? context.join(" | ") : context - }] ${level}: ${message}${formatExcessPropertiesNoStack(meta, hideFromConsole)}` + }] ${level}: ${message}${formatExcessPropertiesNoStack(meta, hideFromConsole, true)}` ); winston.addColors({