From 352ee818de73bd0bf1d438d63655c6fc251356cf Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Thu, 16 Nov 2023 19:45:35 +0100 Subject: [PATCH] fix regression in IsJson function (no longer accept white space in the beginning or the end) --- .../lucee/commons/io/log/log4j2/layout/JsonLayout.java | 6 +++--- .../java/lucee/runtime/functions/conversion/IsJSON.java | 6 ++++++ loader/build.xml | 2 +- loader/pom.xml | 2 +- test/functions/IsJson.cfc | 9 ++++++++- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/lucee/commons/io/log/log4j2/layout/JsonLayout.java b/core/src/main/java/lucee/commons/io/log/log4j2/layout/JsonLayout.java index 64a54a57af..676c9275f3 100644 --- a/core/src/main/java/lucee/commons/io/log/log4j2/layout/JsonLayout.java +++ b/core/src/main/java/lucee/commons/io/log/log4j2/layout/JsonLayout.java @@ -337,11 +337,11 @@ public String toSerializable(final LogEvent event) { } private static Object toJson(String strJson, Object defaultValue) { - if (StringUtil.isEmpty(strJson)) { + if (StringUtil.isEmpty(strJson, true)) { return defaultValue; } - - if (!(strJson.startsWith("{") && strJson.endsWith("}") || strJson.startsWith("[") && strJson.endsWith("]"))) { + strJson = strJson.trim(); + if ((!strJson.startsWith("{") && !strJson.startsWith("[")) || (!strJson.endsWith("}") && !strJson.endsWith("]"))) { return defaultValue; } diff --git a/core/src/main/java/lucee/runtime/functions/conversion/IsJSON.java b/core/src/main/java/lucee/runtime/functions/conversion/IsJSON.java index d54a6f726a..b45602aa15 100644 --- a/core/src/main/java/lucee/runtime/functions/conversion/IsJSON.java +++ b/core/src/main/java/lucee/runtime/functions/conversion/IsJSON.java @@ -28,6 +28,12 @@ public class IsJSON { public static boolean call(PageContext pc, Object obj) { String str = Caster.toString(obj, null); if (StringUtil.isEmpty(str, true)) return false; + + str = str.trim(); + if ((!str.startsWith("{") && !str.startsWith("[")) || (!str.endsWith("}") && !str.endsWith("]"))) { + return false; + } + try { new JSONExpressionInterpreter().interpret(pc, str); return true; diff --git a/loader/build.xml b/loader/build.xml index e6a352c8cf..ecdba6e026 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 36cb86d4be..533eae6c53 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.0.1.27-SNAPSHOT + 6.0.1.28-SNAPSHOT jar Lucee Loader Build diff --git a/test/functions/IsJson.cfc b/test/functions/IsJson.cfc index 7c08bb6ca7..44c83c17c3 100644 --- a/test/functions/IsJson.cfc +++ b/test/functions/IsJson.cfc @@ -100,5 +100,12 @@ component extends="org.lucee.cfml.test.LuceeTestCase" { assertTrue(isJson('{}')); assertEquals(deserializeJson('{}'), {}); } - + function testWhiteSpace() { + // we allow white space, even it is not really correct + assertTrue(isJson(' {a:1} ')); + assertTrue(isJson(' {a:1} ')); + assertTrue(isJson(' +{a:1} +')); + } }