From 7b6966b9c2e02682bef97998c3a256b0ea7eb978 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Thu, 22 Jan 2026 11:29:57 +0000 Subject: [PATCH] fix: Complete JSON escaping in fingerprint_mcp_args Address coderabbitai review - implement full JSON escaping for SQL digest: - Handle backslash (\) and double quote (") - Handle control characters: newline (\n), carriage return (\r), tab (\t) - Handle other control characters (U+0000 through U+001F) with \uXXXX escapes This ensures digest_text in stats_mcp_query_digest is always valid JSON, preventing parsing errors for consumers of this data. --- lib/Discovery_Schema.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/Discovery_Schema.cpp b/lib/Discovery_Schema.cpp index 32b29ca6c..5e6ff4a99 100644 --- a/lib/Discovery_Schema.cpp +++ b/lib/Discovery_Schema.cpp @@ -3049,12 +3049,20 @@ std::string Discovery_Schema::fingerprint_mcp_args(const nlohmann::json& argumen // Escape the digest for JSON and add it to result result += "\""; if (digest) { - // Simple JSON escaping - escape backslashes and quotes + // Full JSON escaping - handle all control characters for (const char* p = digest; *p; p++) { - if (*p == '\\' || *p == '"') { - result += '\\'; + unsigned char c = (unsigned char)*p; + if (c == '\\') result += "\\\\"; + else if (c == '"') result += "\\\""; + else if (c == '\n') result += "\\n"; + else if (c == '\r') result += "\\r"; + else if (c == '\t') result += "\\t"; + else if (c < 0x20) { + char buf[8]; + snprintf(buf, sizeof(buf), "\\u%04x", c); + result += buf; } - result += *p; + else result += *p; } free(digest); }