feat: add sanitization to logger service

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-04-10 22:39:47 +02:00 committed by David Mehren
parent f1f1d8cc94
commit 2467b1250f

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -101,15 +101,30 @@ export class ConsoleLoggerService implements LoggerService {
return context; return context;
} }
static sanitize(input: string): string {
return (
input
// remove ASCII control characters
.replace(/\p{C}/gu, '')
// replace all non-zeros width spaces with one space
.replace(/\p{Zs}/gu, ' ')
);
}
private printMessage( private printMessage(
message: unknown, message: unknown,
color: (message: string) => string, color: (message: string) => string,
context = '', context = '',
isTimeDiffEnabled?: boolean, isTimeDiffEnabled?: boolean,
): void { ): void {
const output = isObject(message) let output;
? `${color('Object:')}\n${JSON.stringify(message, null, 2)}\n` if (isObject(message)) {
: color(message as string); output = ConsoleLoggerService.sanitize(
`${color('Object:')}\n${JSON.stringify(message, null, 2)}\n`,
);
} else {
output = ConsoleLoggerService.sanitize(color(message as string));
}
const localeStringOptions: DateTimeFormatOptions = { const localeStringOptions: DateTimeFormatOptions = {
year: 'numeric', year: 'numeric',