diff --git a/server/routers/badger/logRequestAudit.ts b/server/routers/badger/logRequestAudit.ts index 1e36bd4d5..4dd5bff99 100644 --- a/server/routers/badger/logRequestAudit.ts +++ b/server/routers/badger/logRequestAudit.ts @@ -5,6 +5,26 @@ import cache from "#dynamic/lib/cache"; import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs"; import { stripPortFromHost } from "@server/lib/ip"; +/** + * Sanitize a string field by replacing lone UTF-16 surrogates (which cannot + * be encoded as valid UTF-8) with the Unicode replacement character, and + * stripping ASCII control characters that are invalid in most text columns. + */ +function sanitizeString(value: string | undefined | null): string | undefined { + if (value == null) return undefined; + return ( + value + // Replace lone high surrogates (not followed by a low surrogate) + // and lone low surrogates (not preceded by a high surrogate) + .replace( + /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?= BATCH_SIZE) { // Fire and forget - don't block the caller