Sam Braam created LOG4J2-1069:
---------------------------------

             Summary: Improper handling of JSON escape chars when deserializing 
JSON log events
                 Key: LOG4J2-1069
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1069
             Project: Log4j 2
          Issue Type: Bug
          Components: Core
    Affects Versions: 2.3
            Reporter: Sam Braam
            Priority: Minor
             Fix For: 2.4


There is an error in the handling of JSON escape characters while determining 
the log event boundaries in a JSON stream.  This error is causing log events 
with JSON escaped characters in the message string to be skipped.  The existing 
tests do not appear to cover this case, and other serialization types are not 
affected.  Here is a test/fix patch: 

{code}
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JsonInputStreamLogEventBridge.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JsonInputStreamLogEventBridge.java
index 1b81644..8ed2732 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JsonInputStreamLogEventBridge.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/server/JsonInputStreamLogEventBridge.java
@@ -55,8 +55,10 @@ public class JsonInputStreamLogEventBridge extends 
InputStreamLogEventBridge {
         boolean inEsc = false;
         for (int i = start; i < charArray.length; i++) {
             final char c = charArray[i];
-            if (!inEsc) {
-                inEsc = false;
+            if (inEsc) {
+               // Skip this char and continue
+               inEsc = false;
+            } else { 
                 switch (c) {
                 case EVENT_START_MARKER:
                     if (!inStr) {
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java
index 891e278..2bdb3c3 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/server/AbstractSocketServerTest.java
@@ -69,7 +69,9 @@ public abstract class AbstractSocketServerTest {
     private static final String MESSAGE = "This is test message";
 
     private static final String MESSAGE_2 = "This is test message 2";
-
+    
+    private static final String MESSAGE_WITH_SPECIAL_CHARS = 
"{This}\n[is]\"n\"a\"\r\ntrue:\n\ttest,\nmessage";
+    
     static final int PORT_NUM = AvailablePortFinder.getNextAvailable();
 
     static final String PORT = String.valueOf(PORT_NUM);
@@ -158,6 +160,13 @@ public abstract class AbstractSocketServerTest {
             testServer(m1, m2);
         }
     }
+    
+    
+    @Test
+    public void testMessagesWithSpecialChars() throws Exception {
+        testServer(MESSAGE_WITH_SPECIAL_CHARS);
+    }
+    
 
     private void testServer(final int size) throws Exception {
         final String[] messages = new String[size];
{code}

The test provided is simplistic and does not attempt to cover all possible 
special characters as the bug has to do with escaped characters in general.  
XML and java serialization handle the special chars in my test string without 
issue - I did not attempt to locate similar cases in the other serialization 
types.




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to