From 7536cc2b21d3961d19cd9cc8bea2fd5ffebf1583 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Mon, 13 Jan 2020 13:48:34 +0100 Subject: [PATCH] Handle another double quote special case (#54474) (#54561) Co-authored-by: Elastic Machine Co-authored-by: Elastic Machine --- .../console/public/quarantined/src/utils.js | 14 +++++++++ .../tests/src/utils_string_expanding.txt | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/legacy/core_plugins/console/public/quarantined/src/utils.js b/src/legacy/core_plugins/console/public/quarantined/src/utils.js index c164c347eacc3..31a0585e190d5 100644 --- a/src/legacy/core_plugins/console/public/quarantined/src/utils.js +++ b/src/legacy/core_plugins/console/public/quarantined/src/utils.js @@ -88,6 +88,20 @@ utils.expandLiteralStrings = function(data) { // Expand to triple quotes if there are _any_ slashes if (string.match(/\\./)) { const firstDoubleQuoteIdx = string.indexOf('"'); + const lastDoubleQuoteIdx = string.lastIndexOf('"'); + + // Handle a special case where we may have a value like "\"test\"". We don't + // want to expand this to """"test"""" - so we terminate before processing the string + // further if we detect this either at the start or end of the double quote section. + + if (string[firstDoubleQuoteIdx + 1] === '\\' && string[firstDoubleQuoteIdx + 2] === '"') { + return string; + } + + if (string[lastDoubleQuoteIdx - 1] === '"' && string[lastDoubleQuoteIdx - 2] === '\\') { + return string; + } + const colonAndAnySpacing = string.slice(0, firstDoubleQuoteIdx); const rawStringifiedValue = string.slice(firstDoubleQuoteIdx, string.length); const jsonValue = JSON.parse(rawStringifiedValue) diff --git a/src/legacy/core_plugins/console/public/quarantined/tests/src/utils_string_expanding.txt b/src/legacy/core_plugins/console/public/quarantined/tests/src/utils_string_expanding.txt index 34bf0f3bc20e7..f6e29d028dfd0 100644 --- a/src/legacy/core_plugins/console/public/quarantined/tests/src/utils_string_expanding.txt +++ b/src/legacy/core_plugins/console/public/quarantined/tests/src/utils_string_expanding.txt @@ -40,3 +40,33 @@ Correctly parse with JSON embedded inside values "content\\\\": """ { "json": "inside\\" ok! 1 " } """ } +========== +Single quotes escaped special case, start and end +------------------------------------- +{ + "query": "\"test\"" +} +------------------------------------- +{ + "query": "\"test\"" +} +========== +Single quotes escaped special case, start +------------------------------------- +{ + "query": "\"test" +} +------------------------------------- +{ + "query": "\"test" +} +========== +Single quotes escaped special case, end +------------------------------------- +{ + "query": "test\"" +} +------------------------------------- +{ + "query": "test\"" +}