You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "fizzbeom (via GitHub)" <gi...@apache.org> on 2026/10/23 12:41:19 UTC
[PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
fizzbeom opened a new pull request, #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421
`GelfLayout` builds its JSON output by hand using `JsonUtils.quoteAsString`, but the existing tests only covered plain values.
- `JsonUtilsTest`: compare the output against Jackson's `JsonStringEncoder` for every Unicode code point, and check the common escape sequences.
- `GelfLayoutTest`: log an event whose message, logger, thread, context data, exception and map message fields contain JSON special and delimiter characters, and check that the output parses back to the original values and that the NUL and LF delimiters appear only at the end.
**INSERT HERE** a clear and concise description of what the pull request is for along with a reference to the associated issue IDs, if they exist.
## Checklist
* Base your changes on `2.x` branch if you are targeting Log4j 2; use `main` otherwise
* `./mvnw verify` succeeds ([the build instructions](https://logging.apache.org/log4j/2.x/development.html#building))
* Non-trivial changes contain an entry file in the `src/changelog/.2.x.x` directory
* Tests are provided
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "ramanathan1504 (via GitHub) via notifications" <no...@logging.apache.org>.
ramanathan1504 commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6680313552
@Shekharrajak test-only PRs don't need an issue here. Removing the escape in `JsonUtils` makes the new tests fail, so they guard a real path. @fizzbeom thanks for the tests, no issue needed. I left one inline ask on `GelfLayoutTest`.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "fizzbeom (via GitHub) via notifications" <no...@logging.apache.org>.
fizzbeom commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6680452326
> why should you add `htmllayout` test also here from #4422
Thanks! Just to make sure I understand: do you mean moving the `HtmlLayout` escaping test from #4422 into this PR, so both layout escaping tests are reviewed together? If so, I'll add it here and close #4422.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "fizzbeom (via GitHub) via notifications" <no...@logging.apache.org>.
fizzbeom commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6680527997
> why should you add `htmllayout` test also here from #4422
The reason is the same as for `GelfLayout`: `HtmlLayout` escapes every user-controlled field with `Transform.escapeHtmlTags`, but no test checked this at the layout level, only `Transform` itself. The test in #4422 puts HTML markup, attribute-breaking quotes and an invalid XML character into the thread name, logger name, source file, message, NDC, MDC and exception, and checks that each is rendered escaped, in element content and in `title` attributes. It fails if the `<` or `"` escape or the invalid-character replacement is removed, or if the escape call for the MDC field is dropped. If you think it isn't worth it, I'm fine closing #4422.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "ramanathan1504 (via GitHub) via notifications" <no...@logging.apache.org>.
ramanathan1504 commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6683641498
> > why should you add `htmllayout` test also here from #4422
>
> The reason is the same as for `GelfLayout`: `HtmlLayout` escapes every user-controlled field with `Transform.escapeHtmlTags`, but no test checked this at the layout level, only `Transform` itself. The test in #4422 puts HTML markup, attribute-breaking quotes and an invalid XML character into the thread name, logger name, source file, message, NDC, MDC and exception, and checks that each is rendered escaped, in element content and in `title` attributes. It fails if the `<` or `"` escape or the invalid-character replacement is removed, or if the escape call for the MDC field is dropped. If you think it isn't worth it, I'm fine closing #4422.
No need this `PR` looks fine. LGTM!
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "ramanathan1504 (via GitHub) via notifications" <no...@logging.apache.org>.
ramanathan1504 merged PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "ramanathan1504 (via GitHub) via notifications" <no...@logging.apache.org>.
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "fizzbeom (via GitHub) via notifications" <no...@logging.apache.org>.
fizzbeom commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6590681781
> Do we have any open issue, if these tests really required ?
There's no open issue — this PR doesn't fix a bug, it only adds coverage.
`GelfLayout` writes its JSON by hand and relies entirely on `JsonUtils.quoteAsString` for escaping, while the existing tests only check quotes and a few control characters (`JsonUtilsTest`) and don't check that a `GelfLayout` event with special characters in its fields still parses as valid JSON, or that the NUL/LF delimiter only appears at the end. A regression there would silently break GELF framing on the receiving side (e.g. Graylog), so these tests are meant as a regression guard. I verified they fail when escaping is removed.
If you'd prefer, I can open an issue to track it, or close the PR if the maintainers don't think the extra coverage is worth it.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "ramanathan1504 (via GitHub) via notifications" <no...@logging.apache.org>.
ramanathan1504 commented on PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#issuecomment-6680338326
@fizzbeom why should you add `htmllayout` test also here #4422
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add escaping tests for `GelfLayout` and `JsonUtils` [logging-log4j2]
Posted by "fizzbeom (via GitHub) via notifications" <no...@logging.apache.org>.
fizzbeom commented on code in PR #4421:
URL: https://github.com/apache/logging-log4j2/pull/4421#discussion_r4490387052
##########
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:
Added `testSpecialCharsAreEscapedInEncode` in 072eaeb. It calls `encode()` with a `SpyByteBufferDestination`, using a `ParameterizedMessage`, `setMessagePattern("%m")` and an additional field set to `${ctx:key}`. I checked it against each path separately: replacing the `quoteAsString` call with a plain `append` for the additional field, for the pattern-based `full_message`, and for the `StringBuilderFormattable` `short_message` each makes the new test fail, while the other tests still pass.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org