ramanathan1504 commented on code in PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#discussion_r4490227473
##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/layout/GelfLayoutTest.java:
##########
@@ -307,6 +314,69 @@ void testFormatTimestamp() {
GelfLayout.formatTimestamp(Long.MAX_VALUE).toString());
}
+ // Characters that must be escaped in JSON, delimiter characters, and
other unusual input
+ private static final String SPECIAL_CHARS =
"\"\\/\b\f\n\r\t\u0000\u001F\u007F\u2028\u2029</script>\uD83D\uDE00";
+
+ private static GelfLayout createLayoutForSpecialChars(final boolean
includeMapMessage) {
+ return GelfLayout.newBuilder()
+ .setHost(HOSTNAME)
+ .setCompressionType(CompressionType.OFF)
+ .setIncludeStacktrace(false)
+ .setIncludeThreadContext(true)
+ .setIncludeMapMessage(includeMapMessage)
+ .setIncludeNullDelimiter(true)
+ .setIncludeNewLineDelimiter(true)
+ .build();
+ }
+
+ private static void assertDelimitedOnlyAtTheEnd(final String output) {
+ assertThat(output.indexOf(Chars.NUL)).isEqualTo(output.length() - 2);
+ assertThat(output.indexOf(Chars.LF)).isEqualTo(output.length() - 1);
+ }
+
+ private static JsonNode parse(final String output) throws IOException {
+ return new ObjectMapper().readTree(output.substring(0, output.length()
- 2));
+ }
+
+ @Test
+ void testSpecialCharsAreEscaped() throws IOException {
+ final StringMap contextData = ContextDataFactory.createContextData();
+ contextData.putValue("mdc" + SPECIAL_CHARS, "mdc" + SPECIAL_CHARS);
+ final LogEvent event = Log4jLogEvent.newBuilder()
+ .setLoggerName("logger" + SPECIAL_CHARS)
+ .setLevel(Level.INFO)
+ .setMessage(new SimpleMessage("message" + SPECIAL_CHARS))
+ .setThreadName("thread" + SPECIAL_CHARS)
+ .setContextData(contextData)
+ .setThrown(new RuntimeException("thrown" + SPECIAL_CHARS))
+ .build();
+
+ final String output =
createLayoutForSpecialChars(false).toSerializable(event);
+
+ assertDelimitedOnlyAtTheEnd(output);
+ final JsonNode json = parse(output);
+ assertThat(json.get("short_message").asText()).isEqualTo("message" +
SPECIAL_CHARS);
+
assertThat(json.get("full_message").asText()).isEqualTo("java.lang.RuntimeException:
thrown" + SPECIAL_CHARS);
+ assertThat(json.get("_logger").asText()).isEqualTo("logger" +
SPECIAL_CHARS);
+ assertThat(json.get("_thread").asText()).isEqualTo("thread" +
SPECIAL_CHARS);
+ assertThat(json.get("_mdc" + SPECIAL_CHARS).asText()).isEqualTo("mdc"
+ SPECIAL_CHARS);
+ }
+
+ @Test
+ void testSpecialCharsInMapMessageAreEscaped() throws IOException {
Review Comment:
Three paths in `GelfLayout.toText` still pass these tests with the escape
removed: `encode()` with a `ParameterizedMessage`, `setMessagePattern("%m")`,
and an additional field set to `${ctx:key}`. `encode()` is the path appenders
use by default. Can one more test go through `encode()` with those three, using
`SpyByteBufferDestination`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]