You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ra...@apache.org on 2026/10/20 12:27:25 UTC

(logging-log4j2) branch 2.x updated: Fix HTTP appender handling of invalid evaluated headers (#4360)

This is an automated email from the ASF dual-hosted git repository.

ramanathan1504 pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 96d48ec710 Fix HTTP appender handling of invalid evaluated headers (#4360)
96d48ec710 is described below

commit 96d48ec710680f387ea9049f514b98b03b3f92ff
Author: Efe <go...@gmail.com>
AuthorDate: Tue Oct 20 15:27:18 2026 +0300

    Fix HTTP appender handling of invalid evaluated headers (#4360)
    
    * Fix HTTP appender handling of invalid headers
    
    * test: verify invalid HTTP header warnings
    
    Assisted-by: Codex (GPT-5)
    Signed-off-by: Efe Gökdemir <ef...@rexcode.co.uk>
    
    * style: apply Spotless formatting
    
    * Fix handling of invalid HTTP appender header values
    
    ---------
    
    Signed-off-by: Efe Gökdemir <ef...@rexcode.co.uk>
    Co-authored-by: Ramanathan Muthu <ra...@apache.org>
---
 .../log4j/core/appender/HttpAppenderTest.java      | 30 ++++++++++++++++++++++
 .../core/appender/HttpURLConnectionManager.java    |  8 ++++--
 src/changelog/.2.x.x/4352_invalid_http_headers.xml | 10 ++++++++
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
index a4df56e507..4bb8ed72bc 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
@@ -35,6 +35,7 @@ import java.net.URL;
 import java.util.List;
 import java.util.stream.Collectors;
 import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.ThreadContext;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.Configuration;
@@ -271,6 +272,35 @@ class HttpAppenderTest {
                 .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
     }
 
+    @Test
+    @UsingStatusListener
+    void testAppendSkipsHeaderWithInvalidRuntimeValue(final ListStatusListener statusListener) throws Exception {
+        WIRE_MOCK.stubFor(post(urlEqualTo("/test/log4j/")).willReturn(SUCCESS_RESPONSE));
+        ThreadContext.put("tenant", "tenant\nwith-line-break");
+        try {
+            final Appender appender = HttpAppender.newBuilder()
+                    .setName("Http")
+                    .setLayout(JsonLayout.createDefaultLayout())
+                    .setConfiguration(CONFIGURATION)
+                    .setIgnoreExceptions(false)
+                    .setUrl(wireMockUrl("/test/log4j/", false, false))
+                    .setHeaders(new Property[] {Property.createProperty("X-Tenant", "${ctx:tenant}")})
+                    .build();
+            appender.append(createLogEvent());
+        } finally {
+            ThreadContext.remove("tenant");
+        }
+
+        WIRE_MOCK.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
+                .withoutHeader("X-Tenant")
+                .withHeader("Content-Type", containing("application/json"))
+                .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
+
+        assertThat(statusListener.getStatusData().map(StatusData::getFormattedStatus))
+                .anySatisfy(message ->
+                        assertThat(message).contains("Skipping HTTP header X-Tenant because its value is invalid"));
+    }
+
     @Test
     @UsingStatusListener
     void testAppendErrorIgnore(final ListStatusListener statusListener) throws Exception {
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
index 5cd96e5430..74752751d3 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
@@ -98,8 +98,12 @@ public class HttpURLConnectionManager extends HttpManager {
             urlConnection.setRequestProperty("Content-Type", layout.getContentType());
         }
         for (final Property header : headers) {
-            urlConnection.setRequestProperty(
-                    header.getName(), header.evaluate(getConfiguration().getStrSubstitutor()));
+            try {
+                urlConnection.setRequestProperty(
+                        header.getName(), header.evaluate(getConfiguration().getStrSubstitutor()));
+            } catch (final IllegalArgumentException e) {
+                LOGGER.warn("Skipping HTTP header {} because its value is invalid", header.getName(), e);
+            }
         }
         if (sslConfiguration != null) {
             final SSLContext sslContext = sslConfiguration.getSslContext();
diff --git a/src/changelog/.2.x.x/4352_invalid_http_headers.xml b/src/changelog/.2.x.x/4352_invalid_http_headers.xml
new file mode 100644
index 0000000000..39e1cef28c
--- /dev/null
+++ b/src/changelog/.2.x.x/4352_invalid_http_headers.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns="https://logging.apache.org/xml/ns"
+       xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+       type="fixed">
+  <issue id="4352" link="https://github.com/apache/logging-log4j2/issues/4352"/>
+  <description format="asciidoc">
+    Fix invalid evaluated HTTP appender header values instead of dropping the log event.
+  </description>
+</entry>